Skip to content

Commit f64c0f1

Browse files
committed
Enhance Telegram message handling and logging; improve error reporting and database saving logic
1 parent 894c48c commit f64c0f1

4 files changed

Lines changed: 61 additions & 59 deletions

File tree

scripts/real_madrid/as.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ def getUrlData(url):
120120
for url in urls:
121121
print(url)
122122
if url_exists(collection=realMadridArticlesCollection, url=url):
123-
print("Url in database - Skipping")
123+
print("☑️ Url in database - Skipping")
124124
continue
125-
print("\nUrl not in database - Working")
125+
print("\n⌛ Url not in database - Working")
126126
data = getUrlData(url)
127127
if not data:
128128
print("Faild to get url page - Skipping\n")
129129
continue
130130
caption, imageUrl, authorName = data
131131
# Send to telegram:
132132
print("Send message to telegram - Sending...")
133-
isSuccessSend = asyncio.run(
133+
status = asyncio.run(
134134
send_photo_message(
135135
token=TELEGRAM_TOKEN_REAL_MADRID,
136136
chat_id=TELEGRAM_CHAT_ID,
@@ -140,20 +140,19 @@ def getUrlData(url):
140140
buttonText=f"{authorName} عبر صحيفة ٱس",
141141
)
142142
)
143-
if not isSuccessSend:
144-
print("Message not send to telegram - Skipping\n")
145-
continue
146-
print("Message sended to telegram successfully")
147143

148-
# Save to database:
149-
print("Save url to database - Saving...")
150-
save_to_database(
151-
collection=realMadridArticlesCollection,
152-
data={"article_url": url, "source": SOURCE_NAME},
153-
)
154-
print("Url saved to database successfully")
144+
if status == True or status == "TIMEOUT":
145+
# Save to database:
146+
print("Save url to database - Saving...")
147+
save_to_database(
148+
collection=realMadridArticlesCollection,
149+
data={"article_url": url, "source": SOURCE_NAME},
150+
)
151+
print("✅ Url saved to database successfully\n")
152+
else:
153+
print("Message failed strictly. Not saving to DB - Skipping\n")
155154

156-
print("✅ All Done - Exiting")
155+
print("\n✅ All Done - Exiting")
157156
except Exception as e:
158157
print(e)
159158
else:

scripts/real_madrid/marca.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def getUrlData(url):
4444

4545
titleEle = article.find("h1", class_="ue-c-article__headline")
4646
if not titleEle:
47-
print("Missing elements - Skipping\n")
47+
print("❗ Missing elements - Skipping")
48+
print(f"🔗 URL for checking: {url}\n")
4849
return None
4950

5051
title = translator.translate(titleEle.get_text(strip=True))
@@ -73,12 +74,7 @@ def getUrlData(url):
7374
subTitle = ("\n" + subTitle + "\n") if subTitle else ""
7475
desc = "\n" + desc + "\n" if desc else ""
7576

76-
caption = (
77-
f"<b>{title}</b>\n"
78-
f"{subTitle}"
79-
f"{desc}"
80-
f"\n\n{publishedAt}"
81-
)
77+
caption = f"<b>{title}</b>\n" f"{subTitle}" f"{desc}" f"\n\n{publishedAt}"
8278
return caption, authorName
8379
except Exception as e:
8480
print(f"Exception ERR: {e}")
@@ -138,11 +134,16 @@ def getUrlData(url):
138134

139135
for url in urls:
140136
if url_exists(collection=realMadridArticlesCollection, url=url):
141-
print("Url in database - Continue")
137+
print("☑️ Url in database - Continue")
142138
continue
143-
print("Url not in database - Working")
144-
caption, authorName = getUrlData(url)
145-
if not caption:
139+
print("\n⌛ Url not in database - Working")
140+
data = getUrlData(url)
141+
if not data:
142+
print("❗ No data avaliable - Skipping")
143+
print(f"🔗 URL for checking: {url}\n")
144+
continue
145+
caption, authorName = data
146+
if not all([caption, authorName]):
146147
continue
147148
imageUrl = articlesImages.get(url)
148149
imageUrl = re.sub(r"(?<!:)//", "/", imageUrl)
@@ -153,28 +154,27 @@ def getUrlData(url):
153154
photo = BytesIO(imageResponse.content)
154155
# Send to telegram:
155156
print("Send message to telegram - Sending...")
156-
isSuccessSend = asyncio.run(
157+
status = asyncio.run(
157158
send_photo_message(
158159
token=TELEGRAM_TOKEN_REAL_MADRID,
159160
chat_id=TELEGRAM_CHAT_ID,
160161
caption=caption,
161162
photo_url=photo,
162163
source_url=url,
163-
buttonText=f"{authorName} عبر صحيفة ماركا"
164+
buttonText=f"{authorName} عبر صحيفة ماركا",
164165
)
165166
)
166-
if not isSuccessSend:
167-
print("Message not send to telegram - Skipping\n")
168-
continue
169-
print("Message sended to telegram successfully")
170167

171-
# Save to database:
172-
print("Save url to database - Saving...")
173-
save_to_database(
174-
collection=realMadridArticlesCollection,
175-
data={"article_url": url, "source": SOURCE_NAME},
176-
)
177-
print("Url saved to database successfully")
168+
if status == True or status == "TIMEOUT":
169+
# Save to database:
170+
print("Save url to database - Saving...")
171+
save_to_database(
172+
collection=realMadridArticlesCollection,
173+
data={"article_url": url, "source": SOURCE_NAME},
174+
)
175+
print("✅ Url saved to database successfully\n")
176+
else:
177+
print("Message failed strictly. Not saving to DB - Skipping\n")
178178
print("✅ All Done - Exiting")
179179
except Exception as e:
180180
print(e)

scripts/real_madrid/official_news.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ def getArticleData(url):
114114
realMadridArticlesCollection = get_collection(
115115
uri=MONGO_URI, collection_name=COLLECTION_NAME, db_name="my_db"
116116
)
117-
print(f"✅ Get articles from database successfully\n")
117+
print(f"✅ Get articles from database successfully")
118118

119119
for url in urls:
120120
if url_exists(collection=realMadridArticlesCollection, url=url):
121121
print(f"\n🔗 Url: {url}")
122-
print(" Url in database - Skipping")
122+
print("☑️ Url in database - Skipping")
123123
continue
124124

125125
print("\n⌛ Url not in database - Working")
@@ -132,7 +132,7 @@ def getArticleData(url):
132132

133133
# Send to telegram:
134134
print("- Send message to telegram - Sending...")
135-
isSuccessSend = asyncio.run(
135+
status = asyncio.run(
136136
send_photo_message(
137137
token=TELEGRAM_TOKEN_REAL_MADRID,
138138
chat_id=TELEGRAM_CHAT_ID,
@@ -142,20 +142,19 @@ def getArticleData(url):
142142
buttonText="الموقع الرسمي لريال مدريد",
143143
)
144144
)
145-
if not isSuccessSend:
146-
print("- Message not send to telegram - Skipping")
147-
continue
148-
print("✅ Message sended to telegram successfully")
149145

150-
# Save to database:
151-
print("Save url to database - Saving...")
152-
save_to_database(
153-
collection=realMadridArticlesCollection,
154-
data={"article_url": url, "source": SOURCE_NAME},
155-
)
156-
print("✅ Url saved to database successfully")
146+
if status == True or status == "TIMEOUT":
147+
# Save to database:
148+
print("Save url to database - Saving...")
149+
save_to_database(
150+
collection=realMadridArticlesCollection,
151+
data={"article_url": url, "source": SOURCE_NAME},
152+
)
153+
print("✅ Url saved to database successfully\n")
154+
else:
155+
print("Message failed strictly. Not saving to DB - Skipping\n")
157156

158-
print("\nScript End - Exitting...")
157+
print("\nAll Done - Exiting")
159158
except Exception as e:
160159
print(e)
161160
else:

shared/telegram_service.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ async def send_photo_message(
88
token, chat_id, caption, photo_url, source_url, buttonText
99
):
1010
bot = telegram.Bot(token)
11-
1211
keyboard = [[InlineKeyboardButton(buttonText, url=source_url)]]
1312
reply_markup = InlineKeyboardMarkup(keyboard)
1413

@@ -20,16 +19,21 @@ async def send_photo_message(
2019
caption=caption,
2120
reply_markup=reply_markup,
2221
parse_mode=ParseMode.HTML,
22+
read_timeout=30,
23+
write_timeout=30,
2324
)
24-
2525
return True
2626

27-
except TelegramError as e:
28-
print(f"SEND TELEGRAM ERR: {e}")
27+
except telegram.error.TimedOut:
28+
print("SEND TELEGRAM ERR: Timeout (Message might be sent)")
29+
return "TIMEOUT"
30+
31+
except telegram.error.BadRequest as e:
32+
print(f"SEND TELEGRAM ERR: Bad Request (Won't reach): {e}")
2933
return False
3034

3135
except Exception as e:
32-
print(f"SEND TELEGRAM ERR: {e}")
36+
print(f"SEND TELEGRAM ERR: General Error: {e}")
3337
return False
3438

3539

0 commit comments

Comments
 (0)