-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTopFollowers.py
114 lines (102 loc) · 3.69 KB
/
getTopFollowers.py
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
"""
Copyright 2020-2022 Yufan You <https://github.com/ouuan>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import requests
import json
import sys
import re
if __name__ == "__main__":
assert(len(sys.argv) == 4)
handle = sys.argv[1]
token = sys.argv[2]
readmePath = sys.argv[3]
headers = {
"Authorization": f"token {token}"
}
followers = []
cursor = None
while True:
query = f'''
query {{
user(login: "{handle}") {{
followers(first: 100{f', after: "{cursor}"' if cursor else ''}) {{
pageInfo{{
endCursor
hasNextPage
}}
nodes {{
login
name
databaseId
following {{
totalCount
}}
repositories(first: 3, isFork: false, orderBy: {{
field: STARGAZERS,
direction: DESC
}}) {{
totalCount
nodes {{
stargazerCount
}}
}}
followers {{
totalCount
}}
}}
}}
}}
}}
'''
response = requests.post(f"https://api.github.com/graphql", json.dumps({ "query": query }), headers = headers)
res = response.json()["data"]["user"]["followers"]
for follower in res["nodes"]:
following = follower["following"]["totalCount"]
repoCount = follower["repositories"]["totalCount"]
login = follower["login"]
name = follower["name"]
id = follower["databaseId"]
followerNumber = follower["followers"]["totalCount"]
thirdStars = follower["repositories"]["nodes"][2]["stargazerCount"] if repoCount >= 3 else 0
if following > thirdStars * 50 + repoCount * 5 + followerNumber:
# 为什么要跳过?
print(f"Skipped: https://github.com/{login} with {followerNumber} followers and {following} following")
# continue
followers.append((followerNumber, login, id, name if name else login))
print(followers[-1])
if not res["pageInfo"]["hasNextPage"]:
break
cursor = res["pageInfo"]["endCursor"]
followers.sort(reverse = True)
html = "<table>\n"
for i in range(min(len(followers), 21)):
login = followers[i][1]
id = followers[i][2]
name = followers[i][3]
if i % 7 == 0:
if i != 0:
html += " </tr>\n"
html += " <tr>\n"
html += f''' <td align="center">
<a href="https://github.com/{login}">
<img src="https://avatars2.githubusercontent.com/u/{id}" width="100px;" alt="{login}"/>
</a>
<br />
<a href="https://github.com/{login}">{name}</a>
</td>
'''
html += " </tr>\n</table>"
with open(readmePath, "r") as readme:
content = readme.read()
newContent = re.sub(r"(?<=<!\-\-START_SECTION:top\-followers\-\->)[\s\S]*(?=<!\-\-END_SECTION:top\-followers\-\->)", f"\n{html}\n", content)
with open(readmePath, "w") as readme:
readme.write(newContent)