p2
This commit is contained in:
@@ -1,37 +1,22 @@
|
||||
import requests
|
||||
import os
|
||||
import sys
|
||||
import random
|
||||
|
||||
API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice")
|
||||
|
||||
def get_quote():
|
||||
# Citate de rezervă în caz că rețeaua pică
|
||||
fallback_quotes = [
|
||||
{"q": "The only way to do great work is to love what you do.", "a": "Steve Jobs"},
|
||||
{"q": "Innovation distinguishes between a leader and a follower.", "a": "Steve Jobs"},
|
||||
{"q": "Stay hungry, stay foolish.", "a": "Steve Jobs"}
|
||||
]
|
||||
|
||||
try:
|
||||
# Încercăm să luăm de pe net (timeout mic să nu așteptăm mult)
|
||||
response = requests.get(API_URL, timeout=5)
|
||||
# Folosim API-ul Quotable pentru a primi și autorul
|
||||
response = requests.get("https://api.quotable.io/random", timeout=10)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
if 'slip' in data:
|
||||
quote, author = data['slip'].get('advice'), "AdviceSlip"
|
||||
else:
|
||||
quote = data.get('content') or data.get('q')
|
||||
author = data.get('author') or data.get('a') or "Unknown"
|
||||
except Exception:
|
||||
# Dacă rețeaua eșuează, alegem unul din listă
|
||||
choice = random.choice(fallback_quotes)
|
||||
quote, author = choice['q'], choice['a']
|
||||
|
||||
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**"
|
||||
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||
f.write(message)
|
||||
quote = data.get('content', 'No quote found')
|
||||
author = data.get('author', 'Unknown')
|
||||
|
||||
# Salvăm formatat pentru Discord
|
||||
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||
f.write(f"📜 *\"{quote}\"*\n\n✍️ **Autor:** {author}")
|
||||
|
||||
except Exception as e:
|
||||
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||
f.write(f"⚠️ Nu am putut prelua citatul. Eroare: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
get_quote()
|
||||
Reference in New Issue
Block a user