top-reddit-posts / crawl.js
rocca's picture
Create crawl.js
46b8862
raw
history blame
1.3 kB
import {exists} from "https://deno.land/std/fs/mod.ts";
let subreddits = JSON.parse(await Deno.writeTextFile(`./top-5k-subreddits.json`);
while(1) {
await new Promise(r => setTimeout(r, 1000));
try {
let i = -1;
for(let s of subreddits) {
i++;
if(await exists(`./post-data-by-subreddit/${s}.json`)) continue;
let after = "";
let posts = [];
while(true) {
let url = `https://www.reddit.com/r/${s}/top.json?t=all&limit=100&after=${after}`;
console.log(i, url);
let result = await fetch(url).then(r => r.json()).catch(e => (console.error(e), false));
let data = result.data;
if(!data.children) console.log(JSON.stringify(data));
for(let post of data.children) {
delete post.data.all_awardings; // awards take up a lot of space (e.g. 20MB with vs 4MB without for r/funny)
}
posts.push(...data.children);
after = data.after;
if(!after) break;
if(!data.children.find(c => c.data.score > 5)) break;
}
await Deno.writeTextFile(`./post-data-by-subreddit/${s}.json`, JSON.stringify(posts));
}
console.log("FINISHED.");
break;
} catch(e) {
console.error(e);
await new Promise(r => setTimeout(r, 1000*60*15));
}
}