Skip to content

Instantly share code, notes, and snippets.

@kamilogorek
Created August 30, 2023 19:25
Show Gist options
  • Select an option

  • Save kamilogorek/2a3674e2df1d4b57d61919fd6d48143f to your computer and use it in GitHub Desktop.

Select an option

Save kamilogorek/2a3674e2df1d4b57d61919fd6d48143f to your computer and use it in GitHub Desktop.

Revisions

  1. kamilogorek created this gist Aug 30, 2023.
    83 changes: 83 additions & 0 deletions sorted-watch-later.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    // HOW TO USE:
    // 0. Get OAuth from https://developers.google.com/youtube/registering_an_application
    // 1. Go to https://www.youtube.com/feed/library
    // 2. Paste snippet below in DevTools
    // 3. Replace `VIDEOS` with copied data
    // 4. Run script

    // JSON.stringify(
    // Array.from(document.querySelectorAll("a.ytd-playlist-video-renderer"))
    // .map((v) => {
    // const time = v
    // .querySelector("span.ytd-thumbnail-overlay-time-status-renderer")
    // .getAttribute("aria-label");
    // const hoursMatch = time.match(/(\d+) hour/);
    // const hours = parseInt((hoursMatch && hoursMatch[1]) || 0, 10);
    // const minutesMatch = time.match(/(\d+) minute/);
    // const minutes = parseInt((minutesMatch && minutesMatch[1]) || 0, 10);
    // const secondsMatch = time.match(/(\d+) second/);
    // const seconds = parseInt((secondsMatch && secondsMatch[1]) || 0, 10);
    // let url = new URL(v.href);
    // return {
    // id: url.searchParams.get("v"),
    // length: seconds + minutes * 60 + hours * 60 * 60,
    // };
    // })
    // .sort((a, b) => a.length - b.length)
    // );

    const { google } = require("googleapis");
    const path = require("path");
    const { authenticate } = require("@google-cloud/local-auth");

    const VIDEOS = null;

    // initialize the Youtube API library
    const youtube = google.youtube("v3");

    // a very simple example of getting data from a playlist
    async function main() {
    const auth = await authenticate({
    keyfilePath: path.join(__dirname, "client_id.json"),
    scopes: ["https://www.googleapis.com/auth/youtube"],
    });
    google.options({ auth });

    const res = await youtube.playlists.insert({
    part: "id,snippet,status",
    requestBody: {
    snippet: {
    title: "_Sorted Watch Later",
    },
    status: {
    privacyStatus: "unlisted",
    },
    },
    });

    if (res.status === 200) {
    console.log("Playlist created.");
    let i = 0;

    for (let video of VIDEOS) {
    await youtube.playlistItems.insert({
    part: "id,snippet",
    requestBody: {
    snippet: {
    playlistId: res.data.id,
    resourceId: {
    kind: "youtube#video",
    videoId: video.id,
    },
    },
    },
    });
    i += 1;
    console.log(`Video added to playlist. ${i}/${VIDEOS.length}`);
    }
    } else {
    console.log("Failed creating playlist:", res);
    }
    }

    main().catch(console.error);