-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_examples.py
112 lines (73 loc) · 2.85 KB
/
api_examples.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
BOOTCAMP_FRED_API_KEY = "4b208f3c49c58b57081514bb13a81577"
def download_data_from_url():
import requests
DOWNLOAD_URL = f"https://api.stlouisfed.org/fred/series/observations?series_id=UNRATE&api_key={BOOTCAMP_FRED_API_KEY}"
response = requests.get(DOWNLOAD_URL)
print(response.text)
def download_data_from_url_as_json():
import requests
DOWNLOAD_URL = f"https://api.stlouisfed.org/fred/series/observations?series_id=UNRATE&api_key={BOOTCAMP_FRED_API_KEY}" + "&file_type=json"
response = requests.get(DOWNLOAD_URL)
print(response.text)
print(type(response.text))
print(response.json())
print(type(response.json()))
def download_data_from_url_as_json():
import requests
DOWNLOAD_URL = f"https://api.stlouisfed.org/fred/series/observations?series_id=UNRATE&api_key={BOOTCAMP_FRED_API_KEY}" + "&file_type=json"
print(DOWNLOAD_URL) # take a look in Firefox
print()
response = requests.get(DOWNLOAD_URL)
print(response.text)
print(type(response.text))
print(response.json())
print(type(response.json()))
def download_and_save_json_data_from_url():
import requests
DOWNLOAD_URL = f"https://api.stlouisfed.org/fred/series/observations?series_id=UNRATE&api_key={BOOTCAMP_FRED_API_KEY}" + "&file_type=json"
response = requests.get(DOWNLOAD_URL)
content = response.text
with open("fred_data.json", "w") as f:
f.write(content)
def download_json_data_into_dataframe_and_process():
import requests
import pandas as pd
from datetime import datetime as dt
DOWNLOAD_URL = f"https://api.stlouisfed.org/fred/series/observations?series_id=UNRATE&api_key={BOOTCAMP_FRED_API_KEY}" + "&file_type=json"
response = requests.get(DOWNLOAD_URL)
json_res = response.json()
df = pd.DataFrame(json_res['observations'])
print(df.head())
print("""
-------------------------------------------
""")
df.index = [dt.strptime(s, '%Y-%m-%d') for s in df.date]
del df['date']
del df['realtime_end']
del df['realtime_start']
df.columns = ['UNEMPLOYMENT']
print(df.head())
return df
def pull_from_fred_api():
# https://github.com/mortada/fredapi
from fredapi import Fred
import pandas as pd
fred = Fred(api_key=BOOTCAMP_FRED_API_KEY)
fred_series_id_list = ["CSUSHPINSA", "CPIAUCSL"]
fred_raw = {}
for i in fred_series_id_list:
fred_raw[i] = fred.get_series(series_id=i)
fred_list = []
for i in fred_series_id_list:
raw = fred_raw[i]
processed = pd.Series(raw, name=i)
fred_list.append(processed)
print(fred_list)
return fred_list
if __name__ == "__main__":
# download_data_from_url()
# download_data_from_url_as_json()
# download_and_save_json_data_from_url()
# download_json_data_into_dataframe_and_process()
# pull_from_fred_api()
pass