Files
it_school/project2/famousquotes.py
Ionel Andrei Cataon 8713c62e82 p2
2026-02-12 16:38:57 +02:00

34 lines
1.1 KiB
Python

import requests
import os
import sys
# Luăm URL-ul din variabila de mediu sau folosim unul default
API_URL = os.getenv("QUOTE_API_URL", "https://api.quotable.io/random")
def get_quote():
try:
response = requests.get(API_URL, timeout=10, verify=True)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
data = data[0]
quote = data.get('content') or data.get('advice') or data.get('q') or "No quote found."
author = data.get('author') or data.get('a') or "Unknown Author"
# Afișăm în consola Jenkins pentru debug
print(f"Citat extras: {quote} - {author}")
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(f"📜 *\"{quote}\"* \n\n✍️ **{author}**")
except Exception as e:
print(f"Eroare la apelarea API-ului: {e}")
# În caz de eroare, scriem un mesaj de fallback în fișier
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(f"⚠️ Nu am putut recupera citatul, dar build-ul a reușit.")
sys.exit(1)
if __name__ == "__main__":
get_quote()