p2
This commit is contained in:
@@ -1,28 +1,28 @@
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
def get_quote():
|
def get_quote():
|
||||||
# Folosim un endpoint de fallback care e foarte stabil
|
# API alternativ foarte stabil
|
||||||
url = "https://api.adviceslip.com/advice"
|
url = "http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(f"Încercăm conectarea la {url}...")
|
print(f"Obținem citat real de la {url}...")
|
||||||
response = requests.get(url, timeout=10)
|
response = requests.get(url, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
advice = data.get('slip', {}).get('advice', 'Fii bun cu cei din jur.')
|
text = data.get('quoteText', 'Keep pushing forward.')
|
||||||
|
author = data.get('quoteAuthor', 'Unknown')
|
||||||
|
if not author.strip(): author = "Anonim"
|
||||||
|
|
||||||
# Deoarece AdviceSlip e anonim, punem noi un autor simbolic
|
output = f"📜 *\"{text}\"*\n\n✍️ **Autor:** {author}"
|
||||||
output = f"📜 *\"{advice}\"*\n\n✍️ **Autor:** AdviceSlip Bot"
|
|
||||||
|
except Exception as 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:
|
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||||
f.write(output)
|
f.write(output)
|
||||||
print("Succes: Sfatul a fost salvat.")
|
|
||||||
|
|
||||||
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)}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
get_quote()
|
get_quote()
|
||||||
@@ -5,7 +5,6 @@ pipeline {
|
|||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
dir('project2') {
|
dir('project2') {
|
||||||
// Folosim --no-cache ca să fim siguri că ia noul Python
|
|
||||||
sh "docker build --no-cache -t quotes-app ."
|
sh "docker build --no-cache -t quotes-app ."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,11 +15,8 @@ pipeline {
|
|||||||
script {
|
script {
|
||||||
dir('project2') {
|
dir('project2') {
|
||||||
sh "docker rm -f quotes-worker || true"
|
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 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 {
|
always {
|
||||||
script {
|
script {
|
||||||
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
|
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
|
||||||
def quoteContent = "Eroare la citirea fișierului."
|
def quote = fileExists('project2/quote.txt') ? readFile('project2/quote.txt').trim() : "Nu am putut genera citatul."
|
||||||
if (fileExists('project2/quote.txt')) {
|
|
||||||
quoteContent = readFile('project2/quote.txt').trim()
|
// Escapăm citatul pentru JSON
|
||||||
quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
|
def safeQuote = quote.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
|
||||||
}
|
|
||||||
|
|
||||||
sh """
|
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
|
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}'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user