Skip to content

Commit f2ee33b

Browse files
committed
GitHub stats
1 parent f3718f3 commit f2ee33b

File tree

6 files changed

+127
-6
lines changed

6 files changed

+127
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ dmypy.json
132132

133133
pypi_data.json
134134
awesome.json
135+
pypi_gh_data.json
135136
.envrc
136137

137138
secrets/*

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
'featured',
125125
'color',
126126
'deprecated',
127+
'stars',
127128
]
128129

129130

docs/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Use the export buttons to get the current view as a PDF or Excel file.
5454
The export takes filter and sorting into account.
5555

5656
.. needtable::
57-
:columns: id, title as "Name", content as "Description", sphinx_type as "Type", license, points, monthly as "Monthly downloads", release_days as "Days since last release"
58-
:colwidths: 10, 20, 35, 10, 5, 5, 5, 10
57+
:columns: id, title as "Name", content as "Description", sphinx_type as "Type", license, points, monthly as "Monthly downloads", release_days as "Days since last release", stars as "Stars"
58+
:colwidths: 10, 20, 35,10, 5, 5, 5, 10, 5
5959
:style_row: awesome_[[copy('color')]]
6060

6161

scripts/awesome_config.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@
4242
##################################################
4343
# NEEDS_JSON.PY configs
4444
##################################################
45-
#PYPI_FILE = 'pypi_data.json'
46-
PYPI_FILE = 'data/20221024_pypi_data.json'
45+
PYPI_FILE = 'pypi_gh_data.json'
46+
#PYPI_FILE = 'data/20221024_pypi_data.json'
4747

4848
NEED_FILE = 'awesome.json'
4949

5050
NEED_TYPE = 'sw'
5151

5252
MAX_NEEDS = 5000 # User for development for faster tests
53+
54+
55+
##################################################
56+
# GITHUB_STATS.PY configs
57+
##################################################
58+
GH_JSON_FILE = 'pypi_gh_data.json'

scripts/github_stats.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Collects GitHub stats from project in pypi_data.json
3+
"""
4+
import json
5+
import sys
6+
import os
7+
from threading import Thread
8+
from github import Github, UnknownObjectException
9+
10+
# Make Python aware of the awesom_config file
11+
sys.path.append(os.path.dirname(__file__))
12+
from awesome_config import *
13+
14+
15+
KEY = "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"
16+
17+
18+
def get_gh_topics(name, gh_project, results, counter):
19+
awesome_tags = []
20+
awesome_stars = -1
21+
try:
22+
repo = g.get_repo(gh_project)
23+
awesome_tags = repo.get_topics()
24+
awesome_stars = repo.stargazers_count
25+
print(f'{counter}/{len(pypi_data)} {name}: Tags retrieved: {awesome_tags}')
26+
except UnknownObjectException:
27+
pass
28+
29+
results[name] = {
30+
'tags': awesome_tags,
31+
'stars': awesome_stars
32+
}
33+
34+
35+
36+
print(f'Reading pypi data from {PYPI_FILE}')
37+
with open(PYPI_FILE, 'r') as f:
38+
pypi_data = json.load(f)
39+
40+
gh_projects={}
41+
42+
for name, project in pypi_data.items():
43+
# Collect possible github repository URLS
44+
from contextlib import suppress
45+
46+
gh_urls = []
47+
with suppress(KeyError,TypeError):
48+
gh_urls.append(project['info']['project_urls']['Github'])
49+
with suppress(KeyError,TypeError):
50+
gh_urls.append(project['info']['project_urls']['Code'])
51+
with suppress(KeyError,TypeError):
52+
gh_urls.append(project['info']['project_urls']['Source Code'])
53+
with suppress(KeyError,TypeError):
54+
gh_urls.append(project['info']['project_urls']['Source'])
55+
with suppress(KeyError,TypeError):
56+
gh_urls.append(project['info']['project_urls']['Homepage'])
57+
with suppress(KeyError,TypeError):
58+
gh_urls.append(project['info']['home_page'])
59+
60+
61+
gh_urls_clean = []
62+
for url in gh_urls:
63+
if url is not None and 'github.com' in url and '/issues' not in url:
64+
url_clean = url.replace('https://github.com/', '').strip('/')
65+
gh_urls_clean.append(url_clean)
66+
67+
if gh_urls_clean:
68+
gh_projects[name] = gh_urls_clean[0]
69+
70+
print(f'Github projects found: {len(gh_projects)}/{len(pypi_data)}')
71+
72+
g = Github(KEY)
73+
counter = 0
74+
results = {}
75+
threads = {}
76+
77+
for name, project in pypi_data.items():
78+
79+
awesome_tags = []
80+
if name in gh_projects:
81+
gh_project = gh_projects[name]
82+
threads[name] = Thread(target=get_gh_topics, args=(name, gh_project, results, counter))
83+
threads[name].start()
84+
counter += 1
85+
86+
print(f'Received {len(results)} tag-results from GitHub')
87+
88+
for name, project in pypi_data.items():
89+
project['awesome_stats']['tags'] = []
90+
project['awesome_stats']['stars'] = -1
91+
if name in results:
92+
project['awesome_stats']['tags'] = results[name]['tags']
93+
project['awesome_stats']['stars'] = results[name]['stars']
94+
95+
print(f'Storing data into {GH_JSON_FILE}')
96+
with open(GH_JSON_FILE, 'w') as f:
97+
json.dump(pypi_data, f, sort_keys=True, indent=4)
98+
99+
print('Done. Exit now!')
100+
101+
# for repo in g.get_user().get_repos():
102+
# print(repo.name)

scripts/needs_json.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ def classifiers_check(value, classifiers):
7070
except KeyError:
7171
overall = 0
7272

73+
try:
74+
tags = data['awesome_stats']['tags']
75+
except KeyError:
76+
tags = []
77+
78+
try:
79+
stars = data['awesome_stats']['stars']
80+
except KeyError:
81+
stars = 0
82+
7383
# urls
7484
code = ""
7585
if data['info']['project_urls']:
@@ -98,8 +108,9 @@ def classifiers_check(value, classifiers):
98108
"sphinx_type": sphinx_type,
99109
"license": license,
100110
"monthly": monthly,
101-
"overall": overall,
102-
"tags": [],
111+
# "overall": overall, # currently not collected
112+
"tags": tags,
113+
"stars": stars,
103114
"pypi": data['info']['package_url'],
104115
"code": code,
105116
"website": data['info']['home_page'],

0 commit comments

Comments
 (0)