-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/python3 | ||
""" Gathers data from an API | ||
""" | ||
import requests | ||
import sys | ||
|
||
BASE = 'https://jsonplaceholder.typicode.com' | ||
|
||
|
||
def getEmployeeData(id): | ||
"""Displays an employee's detail | ||
Args: | ||
id (integer): Id of the employee | ||
""" | ||
emp = requests.get(f'{BASE}/users/{id}').json() | ||
todos = requests.get(f'{BASE}/todos?userId={id}').json() | ||
completed_todos = list(filter(lambda t: t['completed'], todos)) | ||
print( | ||
'Employeee {} is done with tasks({}/{}):'.format( | ||
emp.get('name'), | ||
len(completed_todos), | ||
len(todos) | ||
)) | ||
for todo in completed_todos: | ||
print(f"\t {todo['title']}") | ||
|
||
|
||
if __name__ == '__main__': | ||
id = int(sys.argv[1]) | ||
getEmployeeData(id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/python3 | ||
""" Gathers data from an API | ||
""" | ||
import requests | ||
import sys | ||
import os | ||
|
||
BASE = 'https://jsonplaceholder.typicode.com' | ||
|
||
|
||
def getEmployeeData(id): | ||
"""Saves an employee's detail to csv file | ||
Args: | ||
id (integer): Id of the employee | ||
""" | ||
filename = f'{id}.csv' | ||
if os.path.exists(filename): | ||
os.remove(filename) | ||
|
||
emp = requests.get(f'{BASE}/users/{id}').json() | ||
todos = requests.get(f'{BASE}/todos?userId={id}').json() | ||
for todo in todos: | ||
print('"{}","{}","{}","{}"'.format( | ||
id, | ||
emp.get('username'), | ||
todo.get('completed'), | ||
todo.get('title') | ||
), file=open(filename, 'a')) | ||
|
||
|
||
if __name__ == '__main__': | ||
id = int(sys.argv[1]) | ||
getEmployeeData(id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/python3 | ||
""" Gathers data from an API | ||
""" | ||
import requests | ||
import sys | ||
import os | ||
import json | ||
|
||
BASE = 'https://jsonplaceholder.typicode.com' | ||
|
||
|
||
def getEmployeeData(id): | ||
"""Saves an employee's detail to json file | ||
Args: | ||
id (integer): Id of the employee | ||
""" | ||
filename = f'{id}.json' | ||
if os.path.exists(filename): | ||
os.remove(filename) | ||
|
||
emp = requests.get(f'{BASE}/users/{id}').json() | ||
todos = requests.get(f'{BASE}/todos?userId={id}').json() | ||
formatted_todos = [] | ||
for todo in todos: | ||
formatted_todos.append( | ||
{ | ||
"task": todo.get('title'), | ||
"completed": todo.get('completed'), | ||
"username": emp.get('username') | ||
} | ||
) | ||
data = {f'{id}': formatted_todos} | ||
print(json.dumps(data), file=open(filename, 'a')) | ||
|
||
|
||
if __name__ == '__main__': | ||
id = int(sys.argv[1]) | ||
getEmployeeData(id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/python3 | ||
""" Gathers data from an API | ||
""" | ||
import requests | ||
import os | ||
import json | ||
|
||
BASE = 'https://jsonplaceholder.typicode.com' | ||
|
||
|
||
def getEmployeeData(): | ||
"""Saves an employees detail to json file | ||
Args: | ||
id (integer): Id of the employee | ||
""" | ||
filename = 'todo_all_employees.json' | ||
if os.path.exists(filename): | ||
os.remove(filename) | ||
|
||
emps = requests.get(f'{BASE}/users').json() | ||
data = {} | ||
|
||
for emp in emps: | ||
formatted_todos = [] | ||
todos = requests.get(f'{BASE}/todos?userId={emp.get("id")}').json() | ||
for todo in todos: | ||
formatted_todos.append( | ||
{ | ||
"task": todo.get('title'), | ||
"completed": todo.get('completed'), | ||
"username": emp.get('username') | ||
} | ||
) | ||
data[f'{emp.get("id")}'] = formatted_todos | ||
|
||
print(json.dumps(data), file=open(filename, 'a')) | ||
|
||
|
||
if __name__ == '__main__': | ||
getEmployeeData() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# API |