You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

25 lines
920 B

import requests
from termcolor import colored
def filtr_podle_uzivatele(user_id):
url = "https://jsonplaceholder.typicode.com/posts"
print(f"--Vyhledávám posty uživatele: {user_id}")
try:
response = requests.get(url)
response.raise_for_status()
vsechny_prispevky = response.json()
nalezene_prispevky = []
#for cyklus a if podmínka pro filtraci příspěvků
for p in vsechny_prispevky:
if p["userId"] == user_id:
nalezene_prispevky.append(p)
print(f"Nalezeno {len(nalezene_prispevky)} příspěvků")
print("-"*30)
for p in nalezene_prispevky:
print(f"- {colored(p['title'],'green')} | ID:{colored(p['id'],'red')}")
except Exception as e:
print(f"chyba: {e}")
if __name__ == "__main__":
uzivatel = int(input("Zadej ID uživatele(1-10):"))
filtr_podle_uzivatele(uzivatel)