|
12 | 12 | import requests
|
13 | 13 | from requests.auth import HTTPBasicAuth
|
14 | 14 | from datetime import date
|
| 15 | +from dateutil.relativedelta import relativedelta |
15 | 16 | import pandas as pd
|
16 | 17 | import os
|
17 | 18 | import json
|
@@ -40,29 +41,27 @@ def schedule_report(year, month):
|
40 | 41 |
|
41 | 42 |
|
42 | 43 | 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() |
45 | 46 | data = {}
|
46 | 47 | for header in HEADERS:
|
47 | 48 | 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 | + |
66 | 65 | df = pd.DataFrame(data,columns=HEADERS)
|
67 | 66 | return df
|
68 | 67 |
|
|
0 commit comments