diff --git a/project2/Dockerfile b/project2/Dockerfile index b8b1735..ee28d4b 100644 --- a/project2/Dockerfile +++ b/project2/Dockerfile @@ -8,5 +8,4 @@ COPY famousquotes.py . RUN touch quote.txt && chmod 666 quote.txt -# Comanda de rulare CMD ["python", "famousquotes.py"] \ No newline at end of file diff --git a/project2/famousquotes.py b/project2/famousquotes.py index 47e8606..21cda87 100644 --- a/project2/famousquotes.py +++ b/project2/famousquotes.py @@ -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() \ No newline at end of file diff --git a/project2/jenkinsfile b/project2/jenkinsfile index 855f3ff..080841d 100644 --- a/project2/jenkinsfile +++ b/project2/jenkinsfile @@ -20,10 +20,12 @@ pipeline { dir('project2') { sh "docker rm -f quotes-worker || true" try { - sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app" + // Rulăm containerul pentru a genera fișierul + sh "docker run --name quotes-worker quotes-app" + // Copiem rezultatul din container în workspace-ul Jenkins sh "docker cp quotes-worker:/app/quote.txt ." } catch (e) { - echo "⚠️ Eroare la rulare, dar încercăm notificarea." + echo "⚠️ Eroare la execuție." } finally { sh "docker rm -f quotes-worker || true" } @@ -38,31 +40,25 @@ pipeline { script { def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" - def quoteText = "Nu am putut citi fișierul." + def quoteContent = "Fișierul cu citate lipsește." if (fileExists('project2/quote.txt')) { - quoteText = readFile('project2/quote.txt').trim() - - // CURĂȚARE TEXT: Prevenim stricarea JSON-ului manual - quoteText = quoteText.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') + quoteContent = readFile('project2/quote.txt').trim() + // Escapăm caracterele speciale pentru a nu strica JSON-ul + quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') } def resultStatus = currentBuild.result ?: 'SUCCESS' def statusEmoji = (resultStatus == 'SUCCESS') ? "✅" : "⚠️" - def buildHeader = "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**" + def header = "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**" - echo "🚀 Generăm payload-ul și trimitem către Discord..." - - // MODIFICARE CRITICĂ: Înlocuim writeJSON cu 'cat' (Linux native) sh """ cat << 'EOF' > discord_payload.json { - "content": "${buildHeader}\\n\\n${quoteText}" + "content": "${header}\\n\\n${quoteContent}" } EOF """ - // Trimitem fișierul JSON brut către Discord - // Folosim --data-binary pentru a păstra formatarea corectă sh "curl -X POST -H 'Content-Type: application/json' --data-binary @discord_payload.json '${discordUrl}'" } }