Skip to content

Instantly share code, notes, and snippets.

@towerofnix
Last active March 27, 2016 20:51
Show Gist options
  • Save towerofnix/2ae2c8558393d75877ce to your computer and use it in GitHub Desktop.
Save towerofnix/2ae2c8558393d75877ce to your computer and use it in GitHub Desktop.
don't increase limit in doTheThing over like 5 because youtube will probs ban your IP
var urls = {
proxy: (u) => `//crossorigin.me/${u}`,
proxy: (u) => u,
annotations: (id) => urls.proxy(`https://www.youtube.com/annotations_invideo?video_id=${id}`),
isYouTube: (u) => !!u.match('https://www.youtube.com/'),
getYouTubeID(u) {
var res = u.match(/\?v=([^$&]+)/);
return res ? res[1] : '';
}
};
var parseAnnotations = function(data) {
var doc = (new DOMParser).parseFromString(data, 'text/html').querySelector('document');
var annotations = doc.querySelectorAll('annotation');
return annotations;
};
var doTheThing = function(limit) {
var data = {meta: {requests: 0, ids: []}};
var recurse = (id, depth) => {
data.meta.requests++;
return fetch(urls.annotations(id))
.then(res => res.text())
.then(parseAnnotations)
.then(annotations => {
var res = [];
annotations.forEach(a => {
var actions = Array.from(a.querySelectorAll('action'));
actions.forEach(act => {
if (act.getAttribute('type') === 'openUrl') {
var url = act.querySelector('url').getAttribute('value');
if (urls.isYouTube(url)) {
var id = urls.getYouTubeID(url);
if (id) res.push(id);
}
}
});
});
return res;
})
.then(ids => {
var res = [];
ids.forEach(aId => {
if (aId in data) {
data[aId].push(id);
} else {
data[aId] = [id];
}
if (depth < limit && !data.meta.ids.includes(aId)) {
data.meta.ids.push(aId);
res.push(recurse(aId, depth + 1));
}
console.log(`%c${aId}: %c${res.length}`, res.length ? 'color: blue' : 'color: #AAF', 'color: auto');
});
return Promise.all(res);
});
};
return recurse('4gRK5f9o_yk', 0)
.then(() => data);
};
doTheThing(2)
.then(d => console.log('Did ' + d.meta.requests + ' requests to get', d));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment