-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1-export_to_CSV.py
More file actions
executable file
·41 lines (35 loc) · 1.36 KB
/
Copy path1-export_to_CSV.py
File metadata and controls
executable file
·41 lines (35 loc) · 1.36 KB
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
#!/usr/bin/python3
"""A simple module to make API Calls
To a mockup API server and return the
Responses. Then print them out to standard output
Usage: ./0-gather-data_from_an_API <ID>
Where <ID> is the employee ID for whom we want to list
The tasks"""
from requests import get
from sys import argv
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": "Thing Gecko/20100101 Firefox/102.0"
}
base_url = "https://jsonplaceholder.typicode.com/users/"
def save_task_status_to_csv(user_id: str) -> None:
"""
Get the task status for a certain user and save 'em
Args:
user_id (str): The user id of the user
"""
# lets first get the name of Employee
emp_name = get("{}{}".format(base_url, user_id)).json().get("username")
full_url = "{}{}/todos/".format(base_url, user_id)
response = get(full_url, headers=headers).json()
# save the tasks that belong to this user to a csv file
file_name = "{}.csv".format(user_id)
with open(file_name, "w", encoding="utf-8") as csv_file:
for resp in response:
csv_file.write('"{}","{}","{}","{}"\n'
.format(resp.get("userId"),
emp_name, resp.get("completed"),
resp.get("title")))
if __name__ == "__main__":
save_task_status_to_csv(argv[1])