Skip to content

Instantly share code, notes, and snippets.

@kolya182
Created March 25, 2019 03:26
Show Gist options
  • Save kolya182/dc4383ced74185be75b27f59071fb0c7 to your computer and use it in GitHub Desktop.
Save kolya182/dc4383ced74185be75b27f59071fb0c7 to your computer and use it in GitHub Desktop.
Node.js script to be run daily for achiving YouTube videos in a specified channel list
const fs = require('fs');
const execSync = require('child_process').execSync;
const CHANNELS_PER_RUN = 8;
function appendLog(string) {
fs.appendFileSync('log.txt', `${new Date().toUTCString()} - ${string}\n`, () => {});
}
let channels = fs.readFileSync('channels.txt', 'utf8');
channels = channels.split('\n').map(line => line.split(' ')[0]).filter(line => line != '').filter(line => line[0] != '#');
for (let i = 0; i < CHANNELS_PER_RUN; i++) {
if (channels.length === 0) {
appendLog('Stopping as all channels are exhausted');
}
let channel = channels.splice(Math.floor(Math.random() * channels.length), 1);
appendLog(`Archiving channel ${channel}`);
console.log('Working');
let command = `youtube-dl -i --write-thumbnail --write-info-json --download-archive ARCHIVED.txt -f 'bestvideo[ext=mp4][height<=1080]+bestaudio[ext=m4a]/best[ext=mp4]' --merge-output-format mp4 -o "%(uploader)s/%(upload_date)s %(title)s" --sleep-interval 10 ${channel} > last_output.txt`;
try {
execSync(command);
} catch (error) {
appendLog(`Terminating due to error ` + error);
break;
}
}
appendLog('Finished run!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment