Skip to content

Commit bf03aa8

Browse files
authored
Merge pull request #205 from bennuttall/patch-1
Add piwheels stats
2 parents 671d282 + b3a86a4 commit bf03aa8

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

adabot/circuitpython_libraries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args, er
293293
print_issue_overview(blinka_insights)
294294
output_handler("* {} open issues".format(len(blinka_insights["open_issues"])))
295295
output_handler(" * https://github.com/adafruit/Adafruit_Blinka/issues")
296-
blinka_dl, _ = dl_stats.pypistats_get('adafruit-blinka')
297-
output_handler("* PyPI Downloads in the last week: {}".format(blinka_dl))
296+
blinka_dl = dl_stats.piwheels_stats().get('adafruit-blinka', {}).get("month", "N/A")
297+
output_handler("* Piwheels Downloads in the last month: {}".format(blinka_dl))
298298
output_handler(
299299
"Number of supported boards: {}".format(blinka_funcs.board_count())
300300
)

adabot/circuitpython_library_download_stats.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,42 @@
4949
# i.e. are not named 'Adafruit_CircuitPython_'.
5050
PYPI_FORCE_NON_CIRCUITPYTHON = ["Adafruit-Blinka"]
5151

52+
# https://www.piwheels.org/json.html
53+
PIWHEELS_PACKAGES_URL = "https://www.piwheels.org/packages.json"
5254

53-
def pypistats_get(repo_name):
54-
# get download stats for the last week
55-
api_url = "https://pypistats.org/api/packages/" + repo_name + "/recent"
56-
pypi_stats_last_week = requests.get(api_url, timeout=30)
57-
if not pypi_stats_last_week.ok:
58-
# return "Failed to retrieve data ({})".format(pypi_stats.text)
59-
return None, None
60-
pypi_dl_last_week = pypi_stats_last_week.json()["data"]["last_week"]
61-
62-
# get total download stats
63-
pypi_dl_total = 0
64-
api_url = "https://pypistats.org/api/packages/" + repo_name + "/overall?mirrors=false"
65-
pypi_stats_total = requests.get(api_url, timeout=30)
66-
if pypi_stats_total.ok:
67-
for data in pypi_stats_total.json()["data"]:
68-
pypi_dl_total += data["downloads"]
69-
70-
return pypi_dl_last_week, pypi_dl_total
55+
56+
def piwheels_stats():
57+
stats = {}
58+
response = requests.get(PIWHEELS_PACKAGES_URL)
59+
if response.ok:
60+
packages = response.json()
61+
stats = {
62+
pkg: {"total": dl_all, "month": dl_month}
63+
for pkg, dl_month, dl_all, *_ in packages if pkg.startswith("adafruit")
64+
}
65+
66+
return stats
7167

7268
def get_pypi_stats():
7369
successful_stats = {}
7470
failed_stats = []
7571
repos = common_funcs.list_repos()
72+
dl_stats = piwheels_stats()
7673
for repo in repos:
7774
if (repo["owner"]["login"] == "adafruit" and repo["name"].startswith("Adafruit_CircuitPython")):
7875
if common_funcs.repo_is_on_pypi(repo):
79-
pypi_dl_last_week, pypi_dl_total = pypistats_get(repo["name"].replace("_", "-").lower())
80-
if pypi_dl_last_week is None:
76+
pkg_name = repo["name"].replace("_", "-").lower()
77+
if pkg_name in dl_stats:
78+
successful_stats[repo["name"]] = (dl_stats[pkg_name]["month"], dl_stats[pkg_name]["total"])
79+
else:
8180
failed_stats.append(repo["name"])
82-
continue
83-
successful_stats[repo["name"]] = (pypi_dl_last_week, pypi_dl_total)
8481

8582
for lib in PYPI_FORCE_NON_CIRCUITPYTHON:
86-
pypi_dl_last_week, pypi_dl_total = pypistats_get(lib.lower())
87-
if pypi_dl_last_week is None:
88-
failed_stats.append(lib)
89-
continue
90-
successful_stats[lib] = (pypi_dl_last_week, pypi_dl_total)
83+
pkg_name = lib.lower()
84+
if pkg_name in dl_stats:
85+
successful_stats[lib] = (dl_stats[pkg_name]["month"], dl_stats[pkg_name]["total"])
86+
else:
87+
failed_stats.append(repo["name"])
9188

9289
return successful_stats, failed_stats
9390

@@ -142,9 +139,9 @@ def run_stat_check():
142139

143140
pypi_downloads = {}
144141
pypi_failures = []
145-
downloads_list = [["| Library", "| Last Week", "| Total |"],
142+
downloads_list = [["| Library", "| Last Month", "| Total |"],
146143
["|:-------", "|:--------:", "|:-----:|"]]
147-
output_handler("Adafruit CircuitPython Library PyPi downloads:")
144+
output_handler("Adafruit CircuitPython Library Piwheels downloads:")
148145
output_handler()
149146
pypi_downloads, pypi_failures = get_pypi_stats()
150147
for stat in sorted(pypi_downloads.items(), key=operator.itemgetter(1,1), reverse=True):

0 commit comments

Comments
 (0)