File size: 1,300 Bytes
46b8862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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));
  }

}