Skip to content

Instantly share code, notes, and snippets.

@ljaviertovar
Created July 25, 2021 14:42
Show Gist options
  • Save ljaviertovar/6c5bb2660f165ecde5a45ea24ab14ebc to your computer and use it in GitHub Desktop.
Save ljaviertovar/6c5bb2660f165ecde5a45ea24ab14ebc to your computer and use it in GitHub Desktop.
Scraper to get songs of spotify and create playlist on youtube
// go to youtube
await page.goto(YT_BASE_URL, { waitUntil: "networkidle2" });
// search and add track to playlist
for (const listTrack of listTracks) {
// text to search
let track = `${listTrack.artist} live ${listTrack.song}`;
// wait for the input and clean it
await page.waitForSelector("input[name=search_query]");
await page.evaluate(
() => (document.querySelector("input[name=search_query]").value = "")
);
await sleep(3000);
// type song and search it
await page.type("input[name=search_query]", track, { delay: 50 });
await sleep(1000);
await page.click("#search-icon-legacy");
await page.waitForSelector("#contents");
await sleep(3000);
// find the menu to add song to list
await page.mouse.move(500, 300);
await sleep(2000);
await page.evaluate(() => {
let labels = Array.from(document.querySelectorAll("#label"));
labels[1].click();
});
// add song
await page.click(".ytp-miniplayer-expand-watch-page-button");
await sleep(2000);
}
// function to wait in ms
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment