youtube-maze/explore/index.mjs

64 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs';
import util from 'util';
import * as api from './api.mjs';
import {graphToDOT} from './graph.mjs';
// Fetch the output path from command line
if (process.argv.length !== 4)
{
console.error(`Usage: ${process.argv[1]} ROOT DEST
Explore videos linked from ROOT and write the resulting graph to DEST.`);
process.exit(1);
}
const root = process.argv[2];
const dest = process.argv[3];
// Store metadata about each visited video
const videosNodes = Object.create(null);
// List of videos linked from each video either through a card or an
// endscreen item
const nextVideos = Object.create(null);
/**
* Recursively explore a video and the video linked from it to fill
* the video graph.
*
* @param videoId Source video identifier.
*/
const exploreVideo = async videoId =>
{
// Make sure we dont explore the same video twice
if (videoId in videosNodes)
{
return Promise.resolve();
}
videosNodes[videoId] = {};
const playerConfig = await api.getPlayerConfig(videoId);
videosNodes[videoId] = api.getVideoMeta(playerConfig);
nextVideos[videoId] = new Set();
// Add links between this video and the linked ones
api.getEndScreenVideos(playerConfig)
.forEach(nextId => nextVideos[videoId].add(nextId));
api.getCardVideos(playerConfig)
.forEach(nextId => nextVideos[videoId].add(nextId));
// Recurse on linked videos
return Promise.all(
Array.from(nextVideos[videoId])
.map(id => exploreVideo(id))
);
};
console.log('Starting to explore!');
exploreVideo(root).then(() =>
{
fs.writeFileSync(dest, graphToDOT(videosNodes, nextVideos));
console.log(`Finished. Result in ${dest}`);
}).catch(console.error);