-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1-export_to_CSV.py
executable file
·33 lines (29 loc) · 1.02 KB
/
1-export_to_CSV.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
#!/usr/bin/python3
"""
Using https://jsonplaceholder.typicode.com
gathers data from API and exports it to CSV file
Implemented using recursion
"""
import re
import requests
import sys
API = "https://jsonplaceholder.typicode.com"
"""REST API url"""
if __name__ == '__main__':
if len(sys.argv) > 1:
if re.fullmatch(r'\d+', sys.argv[1]):
id = int(sys.argv[1])
user_res = requests.get('{}/users/{}'.format(API, id)).json()
todos_res = requests.get('{}/todos'.format(API)).json()
user_name = user_res.get('username')
todos = list(filter(lambda x: x.get('userId') == id, todos_res))
with open('{}.csv'.format(id), 'w') as file:
for todo in todos:
file.write(
'"{}","{}","{}","{}"\n'.format(
id,
user_name,
todo.get('completed'),
todo.get('title')
)
)