-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·99 lines (84 loc) · 2.28 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
'use strict'
const pMap = require('p-map')
const cheerio = require('cheerio')
const got = require('got')
const { resolve } = require('url')
const baseUrl = 'https://github.com'
exports.main = async () => {
const { body } = await got(`https://github.com/trending`)
const $ = cheerio.load(body)
const repos = $('.repo-list li').get().map((li) => {
try {
const $li = $(li)
const $link = $li.find('h3 a')
const url = resolve(baseUrl, $link.attr('href'))
const linkText = $link.text()
const repoParts = linkText.split('/').map((p) => p.trim())
const desc = $li.find('p').text().trim()
return {
url,
userName: repoParts[0],
repoName: repoParts[1],
desc
}
} catch (err) {
console.error('parse error', err)
}
}).filter(Boolean)
return (await pMap(repos, processDetailPage, {
concurrency: 3
})).filter(Boolean)
}
async function processDetailPage (repo) {
console.warn('processing repo', repo.url)
try {
const { body } = await got(repo.url)
const $ = cheerio.load(body)
const numCommits = parseInt($('.commits span').text().trim().replace(/,/g, ''))
const [
numIssues,
numPRs,
numProjects
] = $('.Counter').map((i, el) => parseInt($(el).text().trim())).get()
const [
numWatchers,
numStars,
numStarsRedundant, // eslint-disable-line
numForks
] = $('.social-count').map((i, el) => parseInt($(el).text().trim().replace(/,/g, ''))).get()
const languages = $('.repository-lang-stats-numbers li').map((i, li) => {
const $li = $(li)
const lang = $li.find('.lang').text().trim()
const percentStr = $li.find('.percent').text().trim().replace('%', '')
const percent = parseFloat(percentStr)
return {
language: lang,
percent
}
}).get()
return {
...repo,
numCommits,
numIssues,
numPRs,
numProjects,
numWatchers,
numStars,
numForks,
languages
}
} catch (err) {
console.error(err.message)
}
}
if (!module.parent) {
exports.main()
.then((repos) => {
console.log(JSON.stringify(repos, null, 2))
process.exit(0)
}).catch((err) => {
console.error(err)
process.exit(1)
})
}