Skip to content

Commit

Permalink
done with project 0x15
Browse files Browse the repository at this point in the history
  • Loading branch information
Baribor committed Jan 2, 2024
1 parent ac55fd2 commit d813e5c
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 0x15-api/0-gather_data_from_an_API.py
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)
34 changes: 34 additions & 0 deletions 0x15-api/1-export_to_CSV.py
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)
39 changes: 39 additions & 0 deletions 0x15-api/2-export_to_JSON.py
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)
41 changes: 41 additions & 0 deletions 0x15-api/3-dictionary_of_list_of_dictionaries.py
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()
1 change: 1 addition & 0 deletions 0x15-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# API

0 comments on commit d813e5c

Please sign in to comment.