Skip to content

Instantly share code, notes, and snippets.

@gngeorgiev
Created March 27, 2017 15:53
Show Gist options
  • Save gngeorgiev/8b3523229eb042d7f06d0716a39ffc9a to your computer and use it in GitHub Desktop.
Save gngeorgiev/8b3523229eb042d7f06d0716a39ffc9a to your computer and use it in GitHub Desktop.
async search(substring) {
const url = `${SEARCH_URL}${encodeURIComponent(substring)}`;
const response = await fetch(url);
const $ = cheerio.load(await response.text());
const videoElements = $(VIDEO_CLASS);
const getIdSelector = id => `${VIDEO_CLASS}[data-context-item-id=${id}]`;
return Array.from(videoElements).map((videoEl) => {
const id = videoEl.attribs['data-context-item-id'];
const idSelector = getIdSelector(id);
return {
id: id,
thumbnail: `https://i.ytimg.com/vi/${id}/hqdefault.jpg`,
title: $(idSelector).find('.yt-lockup-title > a').attr('title')
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment