From f8261ffcf2794896501f8f6b67e6f64065463522 Mon Sep 17 00:00:00 2001 From: Ionel Andrei Cataon Date: Thu, 12 Feb 2026 17:19:25 +0200 Subject: [PATCH] p2 --- project2/famousquotes.py | 30 +++++++++++++++--------------- project2/jenkinsfile | 21 ++++++++------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/project2/famousquotes.py b/project2/famousquotes.py index 5d9c4ef..65598d0 100644 --- a/project2/famousquotes.py +++ b/project2/famousquotes.py @@ -1,28 +1,28 @@ import requests def get_quote(): - # Folosim un endpoint de fallback care e foarte stabil - url = "https://api.adviceslip.com/advice" + # API alternativ foarte stabil + url = "http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en" + try: - print(f"Încercăm conectarea la {url}...") + print(f"Obținem citat real de la {url}...") response = requests.get(url, timeout=10) response.raise_for_status() data = response.json() - advice = data.get('slip', {}).get('advice', 'Fii bun cu cei din jur.') - - # Deoarece AdviceSlip e anonim, punem noi un autor simbolic - output = f"📜 *\"{advice}\"*\n\n✍️ **Autor:** AdviceSlip Bot" - - with open("quote.txt", "w", encoding="utf-8") as f: - f.write(output) - print("Succes: Sfatul a fost salvat.") + text = data.get('quoteText', 'Keep pushing forward.') + author = data.get('quoteAuthor', 'Unknown') + if not author.strip(): author = "Anonim" + + output = f"📜 *\"{text}\"*\n\n✍️ **Autor:** {author}" except Exception as e: - fallback = "📜 *\"Eroarea de astăzi este lecția de mâine.\"*\n\n✍️ **Autor:** Jenkins Debugger" - with open("quote.txt", "w", encoding="utf-8") as f: - f.write(fallback) - print(f"Fallback activat din cauza: {str(e)}") + print(f"API indisponibil ({e}). Folosim rezerva locală...") + # Rezervă de calitate în caz de eroare rețea + output = "📜 *\"Success is not final, failure is not fatal: it is the courage to continue that counts.\"*\n\n✍️ **Autor:** Winston Churchill" + + with open("quote.txt", "w", encoding="utf-8") as f: + f.write(output) if __name__ == "__main__": get_quote() \ No newline at end of file diff --git a/project2/jenkinsfile b/project2/jenkinsfile index e52a7d3..d3b1113 100644 --- a/project2/jenkinsfile +++ b/project2/jenkinsfile @@ -5,7 +5,6 @@ pipeline { steps { script { dir('project2') { - // Folosim --no-cache ca să fim siguri că ia noul Python sh "docker build --no-cache -t quotes-app ." } } @@ -16,11 +15,8 @@ pipeline { script { dir('project2') { sh "docker rm -f quotes-worker || true" - // Testăm rețeaua înainte de rulare - sh "docker run --rm --dns 8.8.8.8 alpine ping -c 2 google.com || echo 'Internet indisponibil'" - sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app" - sh "docker cp quotes-worker:/app/quote.txt ." + sh "docker cp quotes-worker:/app/quote.txt . " } } } @@ -30,20 +26,19 @@ pipeline { always { script { def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" - def quoteContent = "Eroare la citirea fișierului." - if (fileExists('project2/quote.txt')) { - quoteContent = readFile('project2/quote.txt').trim() - quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') - } + def quote = fileExists('project2/quote.txt') ? readFile('project2/quote.txt').trim() : "Nu am putut genera citatul." + + // Escapăm citatul pentru JSON + def safeQuote = quote.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') sh """ - cat << 'EOF' > discord_payload.json + cat << 'EOF' > payload.json { - "content": "✅ **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteContent}" + "content": "✅ **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${safeQuote}" } EOF """ - sh "curl -X POST -H 'Content-Type: application/json' --data-binary @discord_payload.json '${discordUrl}'" + sh "curl -X POST -H 'Content-Type: application/json' --data-binary @payload.json '${discordUrl}'" } } }