Skip to content

Commit 01754a0

Browse files
committed
remove pypistats retrieval; finalize piwheels stats retrieval/reporting
1 parent 9a10bec commit 01754a0

File tree

1 file changed

+22
-42
lines changed

1 file changed

+22
-42
lines changed

adabot/circuitpython_library_download_stats.py

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,55 +53,41 @@
5353
PIWHEELS_PACKAGES_URL = "https://www.piwheels.org/packages.json"
5454

5555

56-
def pypistats_get(repo_name):
57-
# get download stats for the last week
58-
api_url = "https://pypistats.org/api/packages/" + repo_name + "/recent"
59-
pypi_stats_last_week = requests.get(api_url, timeout=30)
60-
if not pypi_stats_last_week.ok:
61-
# return "Failed to retrieve data ({})".format(pypi_stats.text)
62-
return None, None
63-
pypi_dl_last_week = pypi_stats_last_week.json()["data"]["last_week"]
64-
65-
# get total download stats
66-
pypi_dl_total = 0
67-
api_url = "https://pypistats.org/api/packages/" + repo_name + "/overall?mirrors=false"
68-
pypi_stats_total = requests.get(api_url, timeout=30)
69-
if pypi_stats_total.ok:
70-
for data in pypi_stats_total.json()["data"]:
71-
pypi_dl_total += data["downloads"]
72-
73-
return pypi_dl_last_week, pypi_dl_total
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
7467

7568
def get_pypi_stats():
7669
successful_stats = {}
7770
failed_stats = []
7871
repos = common_funcs.list_repos()
72+
dl_stats = piwheels_stats()
7973
for repo in repos:
8074
if (repo["owner"]["login"] == "adafruit" and repo["name"].startswith("Adafruit_CircuitPython")):
8175
if common_funcs.repo_is_on_pypi(repo):
82-
pypi_dl_last_week, pypi_dl_total = pypistats_get(repo["name"].replace("_", "-").lower())
83-
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:
8480
failed_stats.append(repo["name"])
85-
continue
86-
successful_stats[repo["name"]] = (pypi_dl_last_week, pypi_dl_total)
8781

8882
for lib in PYPI_FORCE_NON_CIRCUITPYTHON:
89-
pypi_dl_last_week, pypi_dl_total = pypistats_get(lib.lower())
90-
if pypi_dl_last_week is None:
91-
failed_stats.append(lib)
92-
continue
93-
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"])
9488

9589
return successful_stats, failed_stats
9690

97-
def get_piwheels_stats():
98-
r = requests.get(PIWHEELS_PACKAGES_URL)
99-
if r.ok:
100-
packages = r.json()
101-
for pkg, d_month, d_all, *_ in packages:
102-
if pkg.startswith('adafruit'):
103-
yield (pkg, d_month, d_all)
104-
10591
def get_bundle_stats(bundle):
10692
""" Returns the download stats for 'bundle'. Uses release tag names to compile download
10793
stats for the last 7 days. This assumes an Adabot release within that time frame, and
@@ -153,7 +139,7 @@ def run_stat_check():
153139

154140
pypi_downloads = {}
155141
pypi_failures = []
156-
downloads_list = [["| Library", "| Last Week", "| Total |"],
142+
downloads_list = [["| Library", "| Last Month", "| Total |"],
157143
["|:-------", "|:--------:", "|:-----:|"]]
158144
output_handler("Adafruit CircuitPython Library PyPi downloads:")
159145
output_handler()
@@ -172,12 +158,6 @@ def run_stat_check():
172158
output_handler(" * Failed to retrieve stats for the following libraries:")
173159
for fail in pypi_failures:
174160
output_handler(" * {}".format(fail))
175-
176-
output_handler("Piwheels downloads:")
177-
output_handler()
178-
piwheels_stats = reversed(sorted(list(get_piwheels_stats()), key=operator.itemgetter(2)))
179-
for pkg, d_month, d_all in piwheels_stats:
180-
output_handler("{}: {:,} downloads (last month) {:,} downloads (total)".format(pkg, d_month, d_all))
181161

182162
if __name__ == "__main__":
183163
cmd_line_args = cmd_line_parser.parse_args()

0 commit comments

Comments
 (0)