6
6
and each library.
7
7
"""
8
8
9
+ import os
9
10
import datetime
10
11
import sys
11
12
import argparse
12
13
import traceback
13
14
import operator
14
15
import requests
15
16
17
+ import github as pygithub
18
+ from google .cloud import bigquery
19
+ import google .oauth2 .service_account
20
+
16
21
from adabot import github_requests as gh_reqs
17
22
from adabot .lib import common_funcs
18
23
24
+ GH_INTERFACE = pygithub .Github (os .environ .get ("ADABOT_GITHUB_ACCESS_TOKEN" ))
25
+
19
26
# Setup ArgumentParser
20
27
cmd_line_parser = argparse .ArgumentParser (
21
28
description = "Adabot utility for CircuitPython Library download stats."
53
60
PIWHEELS_PACKAGES_URL = "https://www.piwheels.org/packages.json"
54
61
55
62
56
- def piwheels_stats ():
63
+ def retrieve_piwheels_stats ():
57
64
"""Get data dump of piwheels download stats"""
58
65
stats = {}
59
66
response = requests .get (PIWHEELS_PACKAGES_URL )
@@ -68,12 +75,12 @@ def piwheels_stats():
68
75
return stats
69
76
70
77
71
- def get_pypi_stats ():
78
+ def parse_piwheels_stats ():
72
79
"""Map piwheels download stats for each repo"""
73
80
successful_stats = {}
74
81
failed_stats = []
75
82
repos = common_funcs .list_repos ()
76
- dl_stats = piwheels_stats ()
83
+ dl_stats = retrieve_piwheels_stats ()
77
84
for repo in repos :
78
85
if repo ["owner" ]["login" ] == "adafruit" and repo ["name" ].startswith (
79
86
"Adafruit_CircuitPython"
@@ -101,6 +108,55 @@ def get_pypi_stats():
101
108
return successful_stats , failed_stats
102
109
103
110
111
+ def retrieve_pypi_stats (repos ):
112
+ """Get data dump of PyPI download stats (for the last 7 days)"""
113
+ # Create access info dictionary
114
+ access_info = {
115
+ "private_key" : os .environ ["BIGQUERY_PRIVATE_KEY" ],
116
+ "client_email" : os .environ ["BIGQUERY_CLIENT_EMAIL" ],
117
+ "token_uri" : "https://oauth2.googleapis.com/token" ,
118
+ }
119
+
120
+ # Use credentials to create a BigQuery client object
121
+ credentials = google .oauth2 .service_account .Credentials .from_service_account_info (
122
+ access_info
123
+ )
124
+ client = bigquery .Client ("circuitpython-stats" , credentials = credentials )
125
+
126
+ # Get the list of PyPI package names
127
+ packages = [repo ["name" ].replace ("_" , "-" ).lower () for repo in repos ]
128
+
129
+ # Construct the query to use
130
+ query = """
131
+ SELECT
132
+ file.project as name, COUNT(*) AS num_downloads,
133
+ FROM
134
+ `bigquery-public-data.pypi.file_downloads`
135
+ WHERE DATE(timestamp)
136
+ BETWEEN DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY), DAY)
137
+ AND DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY), DAY)
138
+ AND file.project in (
139
+ """
140
+ packages_query = ["?" for _ in packages ]
141
+ query_parameters = [
142
+ bigquery .ScalarQueryParameter (None , "STRING" , package ) for package in packages
143
+ ]
144
+ query += "," .join (packages_query )
145
+ query += """
146
+ )
147
+ GROUP BY file.project
148
+ ORDER BY num_downloads DESC
149
+ """
150
+
151
+ # Configure and run the query
152
+ job_config = bigquery .QueryJobConfig (query_parameters = query_parameters )
153
+ query_job = client .query (
154
+ query ,
155
+ job_config = job_config ,
156
+ )
157
+ return query_job .result ()
158
+
159
+
104
160
def get_bundle_stats (bundle ):
105
161
"""Returns the download stats for 'bundle'. Uses release tag names to compile download
106
162
stats for the last 7 days. This assumes an Adabot release within that time frame, and
@@ -172,7 +228,7 @@ def run_stat_check():
172
228
]
173
229
output_handler ("Adafruit CircuitPython Library Piwheels downloads:" )
174
230
output_handler ()
175
- pypi_downloads , pypi_failures = get_pypi_stats ()
231
+ pypi_downloads , pypi_failures = parse_piwheels_stats ()
176
232
for stat in sorted (
177
233
pypi_downloads .items (), key = operator .itemgetter (1 , 1 ), reverse = True
178
234
):
0 commit comments