Skip to content

Instantly share code, notes, and snippets.

@sparcut
Created December 22, 2017 09:33
Show Gist options
  • Save sparcut/71ab603329c419792098b4d814904b06 to your computer and use it in GitHub Desktop.
Save sparcut/71ab603329c419792098b4d814904b06 to your computer and use it in GitHub Desktop.
Difference in API responses
const _ = require('lodash');
const fs = require('fs');
const images = require('./images.json'); // https://twitchemotes.com/api_cache/v3/images.json
const subJSON = require('./subscriber.json'); // https://twitchemotes.com/api_cache/v3/subscriber.json
const subIds = Object.keys(subJSON);
const sub = {}
for(let i = 0, len = subIds.length; i < len; i++) {
const channel = subJSON[subIds[i]];
const emotes = channel.emotes.length && channel.emotes;
for(let i2 = 0, len2 = emotes.length; i2 < len2; i2++) {
const emote = emotes[i2];
if(typeof emote === 'undefined') {
console.log(emote);
}
sub[emote.id] = {
id: emote.id,
code: emote.code,
emoticon_set: emote.emoticon_set,
channel_id: channel.channel_id,
channel_name: channel.channel_name
}
}
}
const result = difference(sub, images);
fs.writeFileSync('differences.json', JSON.stringify(result, null, 2));
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
* https://gist.github.com/Yimiprod/7ee176597fef230d1451
*/
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {
result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
}
});
}
return changes(object, base);
}
{
"147178": {
"id": 147178,
"code": "mapuuGood",
"emoticon_set": 16199,
"channel_id": "36183562",
"channel_name": "mapuu"
},
"164438": {
"id": 164438,
"code": "khaljiitCoin",
"emoticon_set": 22147,
"channel_id": "93733614",
"channel_name": "khaljiit"
},
"167434": {
"id": 167434,
"code": "mapuuTilt",
"emoticon_set": 16199,
"channel_id": "36183562",
"channel_name": "mapuu"
},
"208151": {
"channel_name": "tiobravewars"
},
"208153": {
"channel_name": "tiobravewars"
},
"208155": {
"channel_name": "tiobravewars"
},
"209961": {
"channel_name": "tiobravewars"
},
"216886": {
"id": 216886,
"code": "averag4Rocko",
"emoticon_set": 100534,
"channel_id": "55665911",
"channel_name": "averagesnipe"
},
"230306": {
"id": 230306,
"code": "tripodGold",
"emoticon_set": 125622,
"channel_id": "66765497",
"channel_name": "tripodgg"
},
"296938": {
"id": 296938,
"code": "phoeni19PhoenixZFFeathers",
"emoticon_set": 230221,
"channel_id": "140216893",
"channel_name": "phoenixb2"
},
"307139": {
"id": 307139,
"code": "datnoiFISHFOOD",
"emoticon_set": 255487,
"channel_id": "121067874",
"channel_name": "datnoidfarmer"
},
"353860": {
"id": 353860,
"code": "shieemGH",
"emoticon_set": 137966,
"channel_id": "55772347",
"channel_name": "shimoichitv"
},
"471516": {
"channel_name": "kissabunny"
},
"479445": {
"channel_name": "kissabunny"
},
"480631": {
"id": 480631,
"code": "fourthgenSHOOK",
"emoticon_set": 107807,
"channel_id": "48163973",
"channel_name": "the4thgengamer"
},
"485671": {
"id": 485671,
"code": "aeloisSwiftRage",
"emoticon_set": 31523,
"channel_id": "76396807",
"channel_name": "aelois"
},
"505801": {
"channel_name": "kissabunny"
},
"516238": {
"channel_name": "tiobravewars"
},
"544927": {
"channel_name": "nekthecat"
},
"548834": {
"id": 548834,
"code": "baundiScrub",
"emoticon_set": 373800,
"channel_id": "148751010",
"channel_name": "baundiesel"
},
"556855": {
"channel_name": "tntensetv"
},
"565186": {
"channel_name": "tiobravewars"
},
"565192": {
"channel_name": "tiobravewars"
},
"582256": {
"id": 582256,
"code": "peteryUmmy",
"emoticon_set": 444629,
"channel_id": "75845019",
"channel_name": "peteryum"
},
"587063": {
"channel_name": "tntensetv"
},
"596459": {
"channel_name": "tiobravewars"
},
"596477": {
"channel_name": "tiobravewars"
},
"608750": {
"channel_name": "hyp_ow"
},
"608752": {
"channel_name": "hyp_ow"
},
"609430": {
"channel_name": "hyp_ow"
},
"617249": {
"id": 617249,
"code": "pewzKing",
"emoticon_set": 403438,
"channel_id": "42379032",
"channel_name": "mariapewz"
},
"627677": {
"id": 627677,
"code": "jankkeKyrvansyyla",
"emoticon_set": 343278,
"channel_id": "153393078",
"channel_name": "jankkestream"
},
"632854": {
"id": 632854,
"code": "imkoSad",
"emoticon_set": 19994,
"channel_id": "95740885",
"channel_name": "imsko"
},
"633434": {
"id": 633434,
"code": "horror6Bat",
"emoticon_set": 492153,
"channel_id": "47563456",
"channel_name": "horror_scream_"
},
"637593": {
"channel_name": "tntensetv"
},
"640907": {
"channel_name": "tiobravewars"
},
"641211": {
"id": 641211,
"code": "peteryQQ",
"emoticon_set": 444630,
"channel_id": "75845019",
"channel_name": "peteryum"
},
"641322": {
"id": 641322,
"code": "blehaLucio",
"emoticon_set": 52301,
"channel_id": "101557275",
"channel_name": "aoskyao"
},
"645357": {
"channel_name": "tiobravewars"
},
"645365": {
"channel_name": "tiobravewars"
},
"645369": {
"channel_name": "tiobravewars"
},
"645374": {
"channel_name": "tiobravewars"
},
"646481": {
"id": 646481,
"code": "fourthgenSAD",
"emoticon_set": 107807,
"channel_id": "48163973",
"channel_name": "the4thgengamer"
},
"648214": {
"id": 648214,
"code": "sollfgChicken",
"emoticon_set": 21666,
"channel_id": "36424761",
"channel_name": "soltek1h"
},
"648222": {
"id": 648222,
"code": "sollfgHuh",
"emoticon_set": 21666,
"channel_id": "36424761",
"channel_name": "soltek1h"
},
"649106": {
"id": 649106,
"code": "peteryEZ",
"emoticon_set": 444631,
"channel_id": "75845019",
"channel_name": "peteryum"
},
"651520": {
"id": 651520,
"code": "snycmaHI",
"emoticon_set": 374051,
"channel_id": "45543526",
"channel_name": "snycmaster"
},
"651521": {
"id": 651521,
"code": "snycmaLOV",
"emoticon_set": 374052,
"channel_id": "45543526",
"channel_name": "snycmaster"
},
"651523": {
"id": 651523,
"code": "snycmaRAGE",
"emoticon_set": 374053,
"channel_id": "45543526",
"channel_name": "snycmaster"
},
"652801": {
"id": 652801,
"code": "imgfleFlexTilt",
"emoticon_set": 432949,
"channel_id": "148210785",
"channel_name": "imgflexin"
},
"654063": {
"id": 654063,
"code": "tozIsh",
"emoticon_set": 56745,
"channel_id": "96143990",
"channel_name": "princetoz"
},
"656058": {
"id": 656058,
"code": "scottc1LOCADELCONIO",
"emoticon_set": 445546,
"channel_id": "91235986",
"channel_name": "scottcoresixx"
},
"657729": {
"id": 657729,
"code": "vampybitmeLit",
"emoticon_set": 321900,
"channel_id": "23014595",
"channel_name": "vampybitme"
},
"657880": {
"id": 657880,
"code": "buttersDrink",
"emoticon_set": 342066,
"channel_id": "36043808",
"channel_name": "noisybutters"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment