In [ ]:
import json
import requests
from reportOptions import *
from resources import *
import pandas as pd
from pandas.io.json import json_normalize
import openpyxl
resources={
"ajr": {
"host":'https://www.ajronline.org/',
"api_key":"",
"requestor_id":"LS730876",
"customer_id":'728125',
},
"oxford": {
'host':'https://sushi5.scholarlyiq.com/counter/r5/',
'api_key':"",
'requestor_id':"7068e173-0984-42b4-8e2b-c449811c4228",
'customer_id':'4026508'
},
'highwire':{
"host":'https://hwdpapi.highwire.org/sushi/',
"customer_id":"patrick.duff2@va.gov",
"requestor_id":"patrick.duff2@va.gov",
"api_key":"patrick.duff2@va.gov|Pd05091981",
'report':"reports/"
}
}
report=[
'Platform',
'Title',
'Database',
'Database Report 1',
"Database Report 2",
'Book Report 1',
'Book Report 2',
'Book Report 3',
'Journal Report 1',
'Journal Report 2',
'Journal Report 3',
'Journal Report 4'
]
#begin_year,begin_month,begin_day,end_year,end_month,end_day,resources
def getData(begin_year,begin_month,begin_day,end_year,end_month,end_day,resources,report):
#build url using format method
urls="{}{}customer_id={}&requestor_id={}&api_key={}&begin_date={}-{}-{}&end_date={}-{}-{}"
if report == 0:
resources['report']='reports/PR_P1?'
elif report == 1:
resources['report']='reports/tr?'
elif report == 2:
resources['report']='reports/dr?'
elif report == 3:
resources['report']='reports/DR_d1?'
elif report == 4:
resources['report']='reports/dr_d2?'
elif report == 5:
resources['report']='reports/tr_b1?'
elif report == 6:
resources['report']='reports/tr_b2?'
elif report == 7:
resources['report']='reports/tr_b3?'
elif report == 8:
report ='reports/tr_j1?'
elif report == 9:
resources['report']='reports/tr_j2?'
elif report == 10:
resources['report']='reports/tr_j3?'
elif report == 11:
resources['report']='reports/tr_j4?'
else:
print('Please select another choice')
#populate 'urls' values
for y in resources:
print(y['host'])
data=urls.format(
y['host'],
y['report'],
y['customer_id'],
y['requestor_id'],
y['api_key'],
begin_year,
begin_month,
begin_day,
end_year,
end_month,
end_day)
print (data)# make connections to vendors
try:
r = requests.get(data)
print("Connection Status: ", r.status_code) # print http response code
# get data from vendors
resp=r.json()
items=resp
print(items)
print("Processing Data....")
table2=json_normalize(items['Report_Items'],record_path=["Item_ID"])
table=json_normalize(items['Report_Items'],record_path=['Performance','Instance'],meta=['Title',
['Performance','Period','Begin_Date'],
['Performance','Period','End_Date']
], errors='ignore')
table=table.reindex(columns=['Title','Performance.Period.Begin_Date', 'Performance.Period.End_Date', 'Metric_Type', "Count"])
table=table.groupby(['Title','Metric_Type'])['Count'].sum()
table=table.reset_index()
except json.decoder.JSONDecodeError:
print(data, "This is not a JSON format..") # catch vendor JSON errors
table.to_html('output.html')
writer = pd.ExcelWriter('output.xlsx',index=False, engine='xlsxwriter')
table.to_excel(writer,sheet_name='reports')#output to a file
workbook = writer.book
worksheet = writer.sheets['report']
header_fmt = workbook.add_format({'bold': True})
worksheet.set_row(0, None, header_fmt)
writer.save()
print(table)
print('Report')
for x, y in enumerate(report):
print(x, y)
report = int(input("----->"))
print("In order to get your report I am going to need the report date range. ")
begin_year = str(input("What is the report's 4 digit start year. "))
begin_month = str(input("Now, tell me what is the 2 digit start month. "))
begin_day = str(input("Next, I am going to need 2 digit start day. "))
print("Now you are going to tell me the report's end date. ")
end_year = str(input("Please tell me the report's 4 digit end year. "))
end_month = str(input("Enter report 2 digit end month "))
end_day = str(input("Finally I need to know the report's 2 digit end day. "))
print("Now that I have everything I need, I'm going go fetch your usage statistics.")
getData(begin_year,begin_month,begin_day,end_year,end_month,end_day,resources,report)