Files
it_school/project2/famousquotes.py
Ionel Andrei Cataon b58666ead4 p2
2026-02-12 16:50:19 +02:00

37 lines
1.2 KiB
Python

import requests
import os
import sys
# API stabil pentru a evita erorile de rețea anterioare
API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice")
def get_quote():
try:
# Timeout de 20s pentru a preveni blocarea pipeline-ului
response = requests.get(API_URL, timeout=20)
response.raise_for_status()
data = response.json()
# Extragem mesajul (format AdviceSlip)
if 'slip' in data:
quote = data['slip'].get('advice')
author = "AdviceSlip"
else:
quote = data.get('content') or data.get('q') or "No message found."
author = data.get('author') or data.get('a') or "Unknown"
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**"
# Salvăm fișierul local în container (/app)
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(message)
print(f"Succes! Mesaj pregătit.")
except Exception as e:
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(f"⚠️ Eroare la preluare: {str(e)[:80]}")
sys.exit(0) # Nu oprim pipeline-ul forțat
if __name__ == "__main__":
get_quote()