Skip to content

Commit 95533f1

Browse files
committed
refactor code and use Github GraphQL
1 parent 072b0f8 commit 95533f1

File tree

5 files changed

+378
-216
lines changed

5 files changed

+378
-216
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ access_token.txt
44
.sass-cache/
55
Gemfile.lock
66
_site/
7-
.idea/
7+
.idea/
8+
__pycache__
9+
*.pyc

auto_run.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ date
99
cd /home/li/code/github/Github-Ranking
1010
git pull
1111
source /home/li/tf36/bin/activate
12-
python save_most_stars_forks.py
12+
python source/process.py
1313
git add .
1414
today=`date +"%Y-%m-%d"`
15-
git commit -m "Auto update $today."
16-
git push origin master
15+
git commit -m "auto update $today"
16+
git push origin master

save_most_stars_forks.py

-212
This file was deleted.

source/common.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
import requests
4+
import time
5+
6+
7+
def get_access_token():
8+
with open('../access_token.txt', 'r') as f:
9+
access_token = f.read().strip()
10+
return access_token
11+
12+
13+
def write_text(file_name, method, text):
14+
"""
15+
write text to file
16+
method: 'a'-append, 'w'-overwrite
17+
"""
18+
with open(file_name, method, encoding='utf-8') as f:
19+
f.write(text)
20+
21+
22+
def write_ranking_repo(file_name, method, repos):
23+
# method: 'a'-append or 'w'-overwrite
24+
table_head = "| Ranking | Project Name | Stars | Forks | Language | Open Issues | Description | Last Commit |\n\
25+
| ------- | ------------ | ----- | ----- | -------- | ----------- | ----------- | ----------- |\n"
26+
with open(file_name, method, encoding='utf-8') as f:
27+
f.write(table_head)
28+
for idx, repo in enumerate(repos):
29+
repo_description = repo['description']
30+
if repo_description is not None:
31+
repo_description = repo_description.replace('|', '\|') # in case there is '|' in description
32+
f.write("| {} | [{}]({}) | {} | {} | {} | {} | {} | {} |\n".format(
33+
idx + 1, repo['name'], repo['html_url'], repo['stargazers_count'], repo['forks_count'],
34+
repo['language'], repo['open_issues_count'], repo_description, repo['pushed_at']
35+
))
36+
f.write('\n')
37+
38+
39+
def get_api_repos(API_URL):
40+
"""
41+
get repos of api, return repos list
42+
"""
43+
access_token = get_access_token()
44+
headers = {
45+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
46+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
47+
'Accept-Language': 'zh-CN,zh;q=0.9',
48+
'Authorization': 'token {}'.format(access_token),
49+
}
50+
s = requests.session()
51+
s.keep_alive = False # don't keep the session
52+
time.sleep(3) # not get so fast
53+
# requests.packages.urllib3.disable_warnings() # disable InsecureRequestWarning of verify=False,
54+
r = requests.get(API_URL, headers=headers)
55+
if r.status_code != 200:
56+
raise ValueError('Can not retrieve from {}'.format(API_URL))
57+
repos_dict = json.loads(r.content)
58+
repos = repos_dict['items']
59+
return repos
60+
61+
62+
def get_graphql_data(GQL):
63+
"""
64+
use graphql to get data
65+
"""
66+
access_token = get_access_token()
67+
headers = {
68+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
69+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
70+
'Accept-Language': 'zh-CN,zh;q=0.9',
71+
'Authorization': 'bearer {}'.format(access_token),
72+
}
73+
s = requests.session()
74+
s.keep_alive = False # don't keep the session
75+
graphql_api = "https://api.github.com/graphql"
76+
time.sleep(5) # not get so fast
77+
78+
# requests.packages.urllib3.disable_warnings() # disable InsecureRequestWarning of verify=False,
79+
r = requests.post(url=graphql_api, json={"query": GQL}, headers=headers)
80+
81+
if r.status_code != 200:
82+
raise ValueError('Can not retrieve from {}'.format(GQL))
83+
return r.json()

0 commit comments

Comments
 (0)