Skip to content

Commit 61724df

Browse files
author
amvanbaren
committed
reduce complexity get_open_vsx_data.py
1 parent 44ac954 commit 61724df

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

reports/get_open_vsx_data.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import requests
1313
from requests.auth import HTTPBasicAuth
1414
from datetime import date
15+
from dateutil.relativedelta import relativedelta
1516
import pandas as pd
1617
import os
1718
import json
@@ -40,29 +41,27 @@ def schedule_report(year, month):
4041

4142

4243
def get_publishing_data(starting_year, starting_month):
43-
current_year = date.today().year
44-
current_month = date.today().month
44+
start_date = date(starting_year, starting_month, 1)
45+
today = date.today()
4546
data = {}
4647
for header in HEADERS:
4748
data[header] = []
48-
for year in range(starting_year, current_year + 1):
49-
for month in range(1, 13):
50-
if year == starting_year and month >= starting_month or \
51-
starting_year < year < current_year or year == current_year and month <= current_month:
52-
url = '%sadmin/report?year=%s&month=%s&token=%s' % (API_ENDPOINT, year, month, ACCESS_TOKEN)
53-
response = requests.get(url)
54-
if response.status_code == 200:
55-
try:
56-
json_results = response.json()
57-
for col in HEADERS:
58-
data[col].append(json_results[col])
59-
year_month = '%s-%s' % (year, month)
60-
print("processed results for %s" % year_month)
61-
except JSONDecodeError:
62-
json_results = None
63-
print("Error decoding JSON results for %s" % url)
64-
else:
65-
print("%s error processing results for %s" % (response.status_code, url))
49+
while start_date.year < today.year or (start_date.year == today.year and start_date.month < today.month):
50+
url = '%sadmin/report?year=%s&month=%s&token=%s' % (API_ENDPOINT, start_date.year, start_date.month, ACCESS_TOKEN)
51+
response = requests.get(url)
52+
if response.status_code == 200:
53+
try:
54+
json_results = response.json()
55+
for col in HEADERS:
56+
data[col].append(json_results[col])
57+
print("processed results for %s-%s" % (start_date.year, start_date.month))
58+
except JSONDecodeError:
59+
json_results = None
60+
print("Error decoding JSON results for %s" % url)
61+
else:
62+
print("%s error processing results for %s" % (response.status_code, url))
63+
start_date = start_date + relativedelta(months=1)
64+
6665
df = pd.DataFrame(data,columns=HEADERS)
6766
return df
6867

0 commit comments

Comments
 (0)