-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-fetcher.ts
56 lines (49 loc) · 1.92 KB
/
rss-fetcher.ts
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
46
47
48
49
50
51
52
53
54
55
56
import {DOMParser} from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
import {LANGUAGES, TIME_PERIODS} from "./src/app.constants.ts";
type Repository = {
name: string,
description: string,
}
const REPOSITORIES: any = {};
LANGUAGES.forEach((language: {id: string, label: string}) => {
REPOSITORIES[language.id] = {}
TIME_PERIODS.forEach((timePeriod: {id: string, label: string}) => {
REPOSITORIES[language.id][timePeriod.id] = []
})
})
const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const fetchRepositories = async (language: string, timePeriod: string) => {
console.log(`Fetching ${language} repositories for ${timePeriod}...`);
const repositories: Repository[] = [];
const url = `https://github.com/trending${language ? '/' + language : ''}${`?since=${timePeriod}`}`;
const body = await fetch(url).then(res => res.text());
const document = new DOMParser().parseFromString(body, "text/html");
if (!document.querySelector('.Box')) {
console.error('GitHub Trending is not available');
Deno.exit(1);
}
if (document.querySelector(".Box-row")) {
document.querySelectorAll(".Box-row").forEach((row) => {
repositories.push({
name: row.querySelector('h2 > a').innerText.replace(/\s/g, ''),
description: row.querySelector('p')?.innerText.trim() || '',
});
});
REPOSITORIES[language][timePeriod] = repositories;
await sleep(1000);
} else {
REPOSITORIES[language][timePeriod] = [];
await sleep(300);
}
}
const fetchAll = async () => {
for (let i = 0; i < LANGUAGES.length; i++) {
for (let j = 0; j < TIME_PERIODS.length; j++) {
await fetchRepositories(LANGUAGES[i].id, TIME_PERIODS[j].id);
}
}
}
await fetchAll();
await Deno.writeTextFile("./feeds.json", JSON.stringify(REPOSITORIES, null, 2));