-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·72 lines (56 loc) · 1.71 KB
/
app.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
import json
import sys
import requests
def main():
if check_required_version() is False:
print("This script requires Python 3")
exit()
while True:
try:
try:
choice = int(input("Enter an album id to view. > "))
if not isinstance(choice, int):
raise ValueError()
except ValueError:
print ("That was not a number! Please provide an Album ID which is a number. E.g. 1, 2, 3, 4, ...")
else:
photo_list = get_the_json(choice)
output_photos(photo_list)
except (KeyboardInterrupt, SystemExit):
pass
def get_the_json(album_id):
"""
Load the json data
:param album_id:int
:return:
"""
url = "https://jsonplaceholder.typicode.com/photos?albumId=" + str(album_id)
try:
response = requests.get(url)
except requests.exceptions.RequestException as e:
print("WHOOOOOOOOOOOO - Response error, lets abort our mission!", e)
sys.exit()
else:
# for this simple example let's assume the json response is aOK
json_data = json.loads(response.text)
return json_data
def output_photos(photo_list):
"""
Print out the list of photos in the album
:param photo_list:
"""
if len(photo_list) is 0:
print("We did not find an album with the ID provided")
for photos in photo_list:
print('[', photos['id'], '] ', photos['title'])
def check_required_version():
"""
Test for the required python version
:return: boolean
"""
if sys.version_info[0] != 3:
return False
else:
return True
if __name__ == '__main__':
main()