-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (71 loc) · 2.2 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const jsonToCsvLib = require('json-2-csv')
const jtoc = jsonToCsvLib.json2csvAsync
const { promisify } = require('util')
const fs = require('fs')
const writeFile = promisify(fs.writeFile)
const qs = require('querystring')
const bent = require('bent')('json', { 'User-Agent': 'Best-Programing-lang-' })
const OAUTH_TOKEN = process.env.TOKEN
const FROM = 1000
const TO = 1003
const PAGE = `${FROM}..${TO}`
console.log(PAGE)
const FILENAME = `data-${PAGE.replace('..', '-')}.csv` // Constant
// Clean FILENAME csv content
writeFile(FILENAME, '').catch(e => console.error(e))
// String -> Promise
const w = data => writeFile(FILENAME, data, { flag: 'a' })
// _ -> Promise
const main = async () => {
try {
const url = `https://api.github.com/search/repositories?${qs.stringify({
q: `stars:${PAGE} is:public`,
sort: 'stars',
order: 'desc',
per_page: 100,
// page: PAGE,
// page: 1, -> 10
access_token: OAUTH_TOKEN
})}`
// run 10 times and push it to file.
const res = await bent(url)
const json = res.items.map(
({
name,
created_at,
size,
stargazers_count,
language,
forks_count,
open_issues_count,
license,
languages_url,
description,
html_url
}) => {
return {
name,
language,
stars: stargazers_count,
forks: forks_count,
license: license ? license.key : '',
open_issues: open_issues_count,
created: created_at,
size,
description,
url: html_url,
languages: languages_url
}
}
)
console.log(json.length)
const csv = await jtoc(json, {
prependHeader: false,
expandArrayObjects: true
})
await w(csv + '\n')
} catch (e) {
console.error(e)
}
}
main().catch(e => console.error(e))