forked from egoist/gh-pinned-repos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (197 loc) · 5.01 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const url = require('url')
const qs = require('querystring')
const aimer = require('aimer')
const { send } = require('micro')
const cache = require('lru-cache')({
max: 1024 * 1024,
maxAge: 86400 * 30,
})
function ghPinnedRepos(username) {
return aimer(`https://github.com/${username}`).then(async ($) => {
const pinned = $('.pinned-item-list-item.public')
// if empty
if (!pinned || pinned.length === 0) return []
const result = []
for (const [index, item] of Object.entries(pinned)) {
if (!isNaN(index)) {
const owner = getOwner($, item)
const repo = getRepo($, item)
const link = 'https://github.com/' + (owner || username) + '/' + repo
const description = getDescription($, item)
const image = await getImage(link)
const website = await getWebsite(link)
const language = getLanguage($, item)
const languageColor = getLanguageColor($, item)
const stars = getStars($, item)
const forks = getForks($, item)
result[index] = {
owner: owner || username,
repo,
link,
description: description || undefined,
image: image || undefined,
website: website || undefined,
language: language || undefined,
languageColor: languageColor || undefined,
stars: stars || 0,
forks: forks || 0,
}
}
}
// });
return result
})
}
function getOwner($, item) {
try {
return $(item).find('.owner').text()
} catch (error) {
return undefined
}
}
function getRepo($, item) {
try {
return $(item).find('.repo').text()
} catch (error) {
return undefined
}
}
function getDescription($, item) {
try {
return $(item).find('.pinned-item-desc').text().trim()
} catch (error) {
return undefined
}
}
function getWebsite(repo) {
return aimer(repo)
.then(($) => {
try {
const site = $('.BorderGrid-cell')
if (!site || site.length === 0) return []
let href
site.each((index, item) => {
if (index == 0) {
href = getHREF($, item)
}
})
return href
} catch (error) {
console.error(error)
return undefined
}
})
.catch((error) => {
console.error(error)
return undefined
})
}
function getHREF($, item) {
try {
return $(item).find('a[href^="https"]').attr('href').trim()
} catch (error) {
return undefined
}
}
function getImage(repo) {
return aimer(repo)
.then(($) => {
try {
const site = $('meta')
if (!site || site.length === 0) return []
let href
site.each((index, item) => {
const attr = $(item).attr('property')
if (attr == 'og:image') {
href = getSRC($, item)
}
})
return href
} catch (error) {
console.error(error)
return undefined
}
})
.catch((error) => {
console.error(error)
return undefined
})
}
function getSRC($, item) {
try {
return $(item).attr('content').trim()
} catch (error) {
return undefined
}
}
function getStars($, item) {
try {
return $(item).find('a[href$="/stargazers"]').text().trim()
} catch (error) {
return 0
}
}
function getForks($, item) {
try {
return $(item).find('a[href$="/network/members"]').text().trim()
} catch (error) {
return 0
}
}
function getLanguage($, item) {
try {
return $(item).find('[itemprop="programmingLanguage"]').text()
} catch (error) {
return undefined
}
}
function getLanguageColor($, item) {
try {
return $(item).find('.repo-language-color').css('background-color')
} catch (error) {
return undefined
}
}
module.exports = async function (req, res) {
/* allow cors from any origin */
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Request-Method', '*')
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
res.setHeader('Access-Control-Allow-Headers', '*')
if (req.method === 'OPTIONS') {
return send(res, 200)
}
const { query } = url.parse(req.url)
const { username, refresh } = qs.parse(query)
if (!username) {
res.setHeader('Content-Type', 'text/html')
return send(
res,
200,
`
<head>
<meta charset="utf-8" />
<title>GitHub pinned repos API</title>
</head>
<style>body {font-family: Helvetica, serif;margin: 30px;}</style>
<p>GET /?username=GITHUB_USERNAME</p>
<p>
<form action="/">
<input type="text" name="username" placeholder="username" />
<button type="submit">Go!</button>
</form>
</p>
<p>made by <a href="https://github.com/egoist">@egoist</a> · <a href="https://github.com/egoist/gh-pinned-repos">source code</a></p>
`,
)
}
let result
const cachedResult = cache.get(username)
if (cachedResult && !refresh) {
result = cachedResult
} else {
result = await ghPinnedRepos(username)
cache.set(username, JSON.stringify(result))
}
send(res, 200, result)
}