-
Notifications
You must be signed in to change notification settings - Fork 0
/
0-gather_data_from_an_API.py
executable file
·42 lines (28 loc) · 1.09 KB
/
0-gather_data_from_an_API.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
#!/usr/bin/python3
# print employee and there completed task name
def print_for_0(user):
""" Special output for assignment 0 """
name = user.get('name')
tasks = user.get('tasks')
done = [task.get('title') for task in tasks if
task.get('completed')]
print("Employee {} is done with tasks({}/{}):".format(name, len(done),
len(tasks)))
for title in done:
print('\t {}'.format(title))
if __name__ == "__main__":
import requests
from sys import argv
if len(argv) != 2:
raise Exception("Need to pass in the User id")
url = "https://jsonplaceholder.typicode.com/users"
param = {'id': argv[1]}
response = requests.get(url, params=param).json()
if len(response) != 1:
raise Exception("Invalid User Id")
user = response[0]
url = "https://jsonplaceholder.typicode.com/todos"
param = {'userId': user.get('id')}
tasks = requests.get(url, params=param).json()
user['tasks'] = tasks # link list of tasks with respective user
print_for_0(user)