2 changed files with 44 additions and 0 deletions
@ -0,0 +1,19 @@ |
|||
import requests |
|||
from termcolor import colored |
|||
|
|||
def vypis_komentare_prispevku(post_id): |
|||
url = f"https://jsonplaceholder.typicode.com/posts/{post_id}/comments" |
|||
print(f"--Načítám komentáře příspěvku s ID: {post_id}") |
|||
try: |
|||
response = requests.get(url) |
|||
response.raise_for_status |
|||
komentare = response.json() |
|||
for k in komentare: |
|||
print(f"Autor: {colored(k['email'],'blue')}") |
|||
print(f"Komentář: {k['body']}") |
|||
print("-"*20) |
|||
except Exception as e: |
|||
print(f"chyba:{e}") |
|||
if __name__ == "__main__": |
|||
id_prisevku = int(input("Zadej ID příspěvku pro komentáře(1-100):")) |
|||
vypis_komentare_prispevku(id_prisevku) |
|||
@ -0,0 +1,25 @@ |
|||
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) |
|||
Loading…
Reference in new issue