Created
November 24, 2021 20:20
-
-
Save Frontesque/9530e092e018bcd7ec6072b4e318a65f to your computer and use it in GitHub Desktop.
Scrape the video IDs off of the youtube homepage with one click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--- Config ---// | |
const config = { | |
videoIdFinderStart: `"videoId":"`, | |
videoIdFinderEnd: '"', | |
timesToCheck: 180, | |
videoIdLength: 11, | |
}; | |
//--- Module Imports ---// | |
const axios = require('axios'); | |
const fs = require('fs'); | |
//--- Parse Function ---// | |
function parse(text, start, end) { | |
var find = text.search(start); | |
let result = ''; | |
let complete; | |
let inc = find+start.length-1; | |
while (complete == null) { | |
inc++ | |
if (text[inc] == end) { | |
complete = true; | |
} else { | |
result += text[inc]; | |
} | |
} | |
return {result: result, startLocation: find, endLocation: inc}; | |
} | |
//--- Request Youtube Home ---// | |
console.log("Getting IDs") | |
axios.get('https://youtube.com') | |
.then(function(res) { | |
const body = res.data | |
//--- Set up stuff for loops ---// | |
let videoIds = new Array(); | |
let strippedBody = body; | |
let iterations = 0; | |
//--- Iterate ---// | |
while (iterations < config.timesToCheck) { | |
const parsed = parse(strippedBody, config.videoIdFinderStart,config.videoIdFinderEnd); | |
//console.log(parsed) | |
strippedBody = strippedBody.slice(parsed.endLocation); | |
//--- Extra Filtering ---// | |
if (!videoIds.includes(parsed.result)) { | |
if (parsed.result.length == config.videoIdLength) { | |
videoIds.push(parsed.result) | |
} | |
} | |
//--- Count it as a found/checked ID ---// | |
iterations++ | |
} | |
//--- Generate Result File ---// | |
console.log("IDs Found: ",videoIds.length) | |
fs.writeFileSync('results.txt',JSON.stringify(videoIds)) | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment