-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathgithubFunc.js
99 lines (83 loc) · 2.46 KB
/
githubFunc.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
const fetch = require("node-fetch")
const githubToken = require("./token.js")
const parser = require("node-html-parser")
const db = require("./DB")
const makeRequest = async (url) => {
let res = await fetch(url,
{
headers: {
authorization: `token ${githubToken.token}`
}
}
)
return res.json()
}
const fetchHtml = async () => {
let res = await fetch("https://github.com/trending")
let body = await res.text()
return body
}
const getRepoFromTrending = async () => {
let htmlData = await fetchHtml()
htmlData = parser.parse(htmlData);
let repoList = []
let articles = htmlData.querySelectorAll("article")
articles.forEach(element => {
let h1 = element.querySelectorAll("h1")[0]
let a = h1.querySelectorAll("a")[0]
let repoStr = a.innerText.trim();
repoStr = repoStr.split("/")
let owner = repoStr[0].trim()
let name = repoStr[1].replace("\n", "").trim()
let data = {
"owner": owner,
"name": name,
"url": `https://github.com/${owner}/${name}`
}
repoList.push(data)
});
return repoList
}
const getRepoFromStarred = async (username) => {
let repoList = await makeRequest(`https://api.github.com/users/${username}/starred`)
repoList = repoList.map(elem => {
return {
"owner": elem["owner"]["login"],
"name": elem["name"],
"url": elem["svn_url"]
}
})
return await repoList
}
const getRepoViaMethod = async (method, username) => {
method = Number(method)
if (method == 1) {
return db
} else if (method == 2) {
return getRepoFromTrending()
} else if (method == 3) {
return await getRepoFromStarred(username)
}
return []
}
const getIssues = async (arr) => {
let list = []
for (let i = 0; i < arr.length; i++) {
let url = `https://api.github.com/repos/${arr[i]["owner"]}/${arr[i]["name"]}/issues?labels=good first issue`
let issues = await makeRequest(url)
issues = issues.map(issue => {
return {
"title": issue["title"],
"url": issue["html_url"]
}
})
let data = {
"repoOwner": arr[i]["owner"],
"repoName": arr[i]["name"],
"issues": issues
}
list.push(data)
}
return list
}
module.exports = { getRepoViaMethod, getIssues }