-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatu.py
46 lines (41 loc) · 1.62 KB
/
atu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import requests
import xmltodict
import pandas as pd
# TODO: Determine issue with direction parameter
def request(request: str,
form: str,
order_by: str = "",
# direction : str = "",
limit: int = "0") -> pd.DataFrame:
'''Return the response from a request to the ATU database.
The ATU (All Things Umphrey's) database contains the history of
setlists from Umphrey's McGee. This function takes a valid
request to the API (v1). The form of valid requests can be found at
https://allthings.umphreys.com/api/docs.
Attributes:
request (str): A valid request to the API (v1)
form (str): {'json', 'xml'}.
order_by (str): Column to order by. Defaults to None
direction (str): {'asc', 'desc'}. Defaults to 'asc'
limit (int): Maximum number of results to return
Returns:
pd.DataFrame: The pandas dataframe representing the response
'''
# &direction=%s" % (direction)
query = "order_by=%s&limit=%s" % (order_by, str(limit))
base = "https://allthings.umphreys.com/api/v1/"
url = base + "%s.%s?%s" % (request, form, query)
txt = requests.get(url).text
print('Request: ' + "%s.%s?%s" % (request, form, query))
if form == 'json':
return pd.DataFrame(json.loads(txt)['data'])
elif form == 'xml':
try:
df_dict = json.loads(json.dumps(xmltodict.parse(txt)))
df_dict = df_dict['results']['result']
return pd.DataFrame(df_dict)
except Exception:
return pd.DataFrame()
elif form == 'html':
return pd.read_html(txt)[0]