forked from fenyx-it-academy/Class7-Python-Module-Week3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (37 loc) · 1.23 KB
/
utils.py
File metadata and controls
54 lines (37 loc) · 1.23 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
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
import sys
import requests
from datetime import datetime
def make_api_call():
"""make api call to get user information
return: list - user info
"""
try:
res = requests.get('http://jsonplaceholder.typicode.com/users')
if res.status_code >= 400:
raise requests.exceptions.HTTPError("There is a connection error")
except requests.exceptions.HTTPError as exc:
print(exc)
sys.exit()
else:
result_obj = res.json()
return result_obj
############################################################################3
def clean_result(lst):
"""clean up a result obj
return: list- keeps only (id, name, username, email)
"""
new_lst = []
for i in range (0,10):
new_lst.append(lst[i]["id"])
new_lst.append(lst[i]["name"])
new_lst.append(lst[i]["username"])
new_lst.append(lst[i]["email"])
return new_lst
##########################################################################
def create_file(new_lst):
"""creates a file with the users data
"""
dt_string = datetime.now().strftime("%d%m%Y%H%M%S")
with open(f"users_{dt_string}.json", "w") as f:
json.dump(new_lst, f)