1
+ #!/usr/bin/env python3
2
+ """
3
+ Example Command from root of repository:
4
+ pipenv run glam/fetch_smithsonian.py
5
+ """
6
+
7
+ # Standard library
8
+ import os
9
+ import sys
10
+ import traceback
11
+ from pprint import pprint
12
+
13
+ # Third-party
14
+ import requests
15
+ from requests .adapters import HTTPAdapter
16
+ from requests .packages .urllib3 .util .retry import Retry
17
+ import query_secrets
18
+
19
+ TIMEOUT = 10
20
+
21
+
22
+ def fetch_data (session ):
23
+ params = {"api_key" : query_secrets .S_I_O_A_API_Key }
24
+ with session .get (
25
+ "https://api.si.edu/openaccess/api/v1.0/stats" ,
26
+ params = params ,
27
+ timeout = TIMEOUT ,
28
+ ) as response :
29
+ response .raise_for_status ()
30
+ data = response .json ()
31
+ return data
32
+
33
+
34
+ def main ():
35
+ #print("API_KEY:", query_secrets.S_I_O_A_API_Key) # DEBUG
36
+
37
+ # Requests configurations
38
+ max_retries = Retry (
39
+ # try again after 5, 10, 20, 40, 80 seconds
40
+ # for specified HTTP status codes
41
+ total = 5 ,
42
+ backoff_factor = 10 ,
43
+ status_forcelist = [403 , 408 , 429 , 500 , 502 , 503 , 504 ],
44
+ )
45
+ session = requests .Session ()
46
+ session .mount ("https://" , HTTPAdapter (max_retries = max_retries ))
47
+
48
+ # Fetch and format domain data
49
+ data = fetch_data (session )
50
+
51
+ # Print data
52
+ pprint (data )
53
+
54
+
55
+ if __name__ == "__main__" :
56
+ try :
57
+ main ()
58
+ except SystemExit as e :
59
+ sys .exit (e .code )
60
+ except KeyboardInterrupt :
61
+ print ("INFO (130) Halted via KeyboardInterrupt." , file = sys .stderr )
62
+ sys .exit (130 )
63
+ except Exception :
64
+ print ("ERROR (1) Unhandled exception:" , file = sys .stderr )
65
+ print (traceback .print_exc (), file = sys .stderr )
66
+ sys .exit (1 )
67
+
68
+ # the difference between a record and an object.
0 commit comments