Skip to content

Commit a9a04c8

Browse files
committed
Refactor message formatting and add button text to Telegram messages; create matches_time script for Real Madrid match updates
1 parent ba47dfb commit a9a04c8

4 files changed

Lines changed: 186 additions & 4 deletions

File tree

scripts/cve/nvd_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def generate_report(vector_string):
149149
publishedDate = datetime.strptime(
150150
cve.get("published"), "%Y-%m-%dT%H:%M:%S.%f"
151151
)
152-
messageTitle = ("🔴 CRITICAL" if baseScore >= 9 else "🟠 HIGH") + " ALERT"
152+
messageTitle = (
153+
"🔴 CRITICAL" if baseScore >= 9 else "🟠 HIGH"
154+
) + " ALERT"
153155
reportDict = generate_report(cvssDataDict.get("vectorString"))
154156
authRequired = reportDict.get("authRequired", "UNKNOWN")
155157
attackVector = reportDict.get("attackVector", "UNKNOWN")
@@ -184,6 +186,7 @@ def generate_report(vector_string):
184186
chat_id=TELEGRAM_CHAT_ID,
185187
text=message,
186188
source_url=f"https://www.cvedetails.com/cve/{cveId}",
189+
buttonText="CVE Details",
187190
)
188191
)
189192
if not isSuccessSend:

scripts/real_madrid/configs/official_news_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BASE_URL = "https://www.realmadrid.com"
22
NEWS_URL = f"{BASE_URL}/ar-AE/news/football/first-team?page=1"
3+
MATCHES_URL = f"{BASE_URL}/ar-AE/schedule"
34

45
HEADERS = {"User-Agent": "Mozilla/5.0"}
56

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import os
2+
import asyncio
3+
import requests
4+
from datetime import datetime
5+
from bs4 import BeautifulSoup
6+
from shared.telegram_service import send_text_message
7+
from dotenv import load_dotenv
8+
from scripts.real_madrid.configs.official_news_config import (
9+
MATCHES_URL,
10+
HEADERS,
11+
)
12+
13+
load_dotenv()
14+
15+
# Secret Keys:
16+
TELEGRAM_TOKEN_REAL_MADRID = os.getenv("TELEGRAM_TOKEN_REAL_MADRID")
17+
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
18+
if not all([TELEGRAM_TOKEN_REAL_MADRID, TELEGRAM_CHAT_ID]):
19+
raise Exception("Missing environment variables")
20+
21+
# Start Print:
22+
print("Run Real Madrid.Official News Script")
23+
# Start Request:
24+
response = requests.get(
25+
url=MATCHES_URL,
26+
headers=HEADERS,
27+
timeout=10,
28+
)
29+
if response.status_code == 200:
30+
print(f"✅ Request Success: {response.status_code}")
31+
soup = BeautifulSoup(response.text, "html.parser")
32+
cards = soup.find_all("app-all-event-card", class_="calendar-list__card")
33+
filteredCards = []
34+
matchDates = {}
35+
36+
# If cards:
37+
if cards:
38+
for card in cards:
39+
cardCategory = card.find("p", class_="event-card__category")
40+
if not cardCategory:
41+
continue
42+
if cardCategory.get_text(strip=True) == "Fútbol · Primer Equipo":
43+
filteredCards.append(card)
44+
else:
45+
print("❗ No data avalibale - Existting")
46+
# If filter cards:
47+
if filteredCards:
48+
print(f"Matches This Month: {len(filteredCards)} Matches\n")
49+
# Extract Matche Dates:
50+
for card in filteredCards:
51+
date = ""
52+
title = ""
53+
subtitle = ""
54+
team1 = ""
55+
team2 = ""
56+
location = ""
57+
# Get Info ELements:
58+
cardInfoElements = card.find_all("li", class_="event-card__info")
59+
if not cardInfoElements:
60+
print("Card info elements not avaliable - Skipping\n")
61+
continue
62+
63+
# Date and location
64+
dateEle = cardInfoElements[0].find("p")
65+
locationEle = cardInfoElements[1].find("p")
66+
if not all([dateEle, locationEle]):
67+
print(
68+
"Card info elements avaliable but date or location element(s) not avalaible - Skipping\n"
69+
)
70+
continue
71+
date = dateEle.get_text(strip=True)
72+
location = locationEle.get_text(strip=True)
73+
if not all([date, location]):
74+
print("Date or Location text(s) not avaliable - Skipping\n")
75+
continue
76+
date = date.replace(" h", "")
77+
location = (
78+
location.replace("ملعب ", "ال")
79+
if location == "ملعب سانتياغو برنابيو"
80+
else location
81+
)
82+
# Date and location
83+
84+
# Title
85+
titleEle = card.find("h3", class_="event-card__title")
86+
if not titleEle:
87+
print(
88+
"Card info elements avaliable but title element not avalaible - Skipping\n"
89+
)
90+
continue
91+
title = titleEle.get_text(strip=True)
92+
# Title
93+
94+
# Subtitle
95+
subtitleEle = card.find("p", class_="event-card__subtitle")
96+
if subtitleEle:
97+
subtitle = subtitleEle.get_text(strip=True)
98+
else:
99+
print(f"At: {date}")
100+
print(
101+
"Card info elements avaliable but subtitle element not avalaible - Ignore\n"
102+
)
103+
# Subtitle
104+
105+
# Team names
106+
teamNameElements = card.find_all("p", class_="rm-game__name")
107+
if not teamNameElements:
108+
print(
109+
"Card info elements avaliable but team name elements not avalaible - Skipping\n"
110+
)
111+
continue
112+
team1 = teamNameElements[0].get_text(strip=True)
113+
team2 = teamNameElements[1].get_text(strip=True)
114+
if not all([team1, team2]):
115+
print(
116+
"Card info elements avaliable but team 1 or team 2 not avalaible - Skipping\n"
117+
)
118+
continue
119+
# Team names
120+
121+
if date not in matchDates:
122+
matchDates[date] = {}
123+
if title:
124+
matchDates[date] = {**matchDates[date], "title": title}
125+
if subtitle:
126+
matchDates[date] = {**matchDates[date], "subtitle": subtitle}
127+
if team1:
128+
matchDates[date] = {**matchDates[date], "team1": team1}
129+
if team2:
130+
matchDates[date] = {**matchDates[date], "team2": team2}
131+
if location:
132+
matchDates[date] = {**matchDates[date], "location": location}
133+
else:
134+
print("❗ No data avalibale - Existting")
135+
136+
# If match dates:
137+
if matchDates:
138+
for date in matchDates:
139+
dateStr = str(date)
140+
dateParts = dateStr.split("، ")
141+
dateMonth = dateParts[1]
142+
dateDay = int(dateMonth.split(" ")[0])
143+
if datetime.now().date().day == dateDay:
144+
print(f"Founded match today: {date}")
145+
146+
matchDec = matchDates[date]
147+
team1 = matchDec["team1"]
148+
team2 = matchDec["team2"]
149+
location = matchDec["location"]
150+
if not team1 == "ريال مدريد":
151+
team1 = "ريال مدريد"
152+
team2 = team1
153+
154+
messageText = (
155+
f"<b>مباراة اليوم - الساعة {dateParts[2]}</b>\n\n"
156+
f"<b>{team1}</b> و <b>{team2}</b> - علي ملعب {location}"
157+
)
158+
159+
print("Send to telegram - Sending")
160+
isSuccessSend = asyncio.run(
161+
send_text_message(
162+
token=TELEGRAM_TOKEN_REAL_MADRID,
163+
chat_id=TELEGRAM_CHAT_ID,
164+
text=messageText,
165+
source_url=MATCHES_URL,
166+
buttonText="الموقع الرسمي لريال مدريد",
167+
)
168+
)
169+
if not isSuccessSend:
170+
print("Faild to send telegram message - Continue")
171+
continue
172+
print("Telegram message sended successfully")
173+
else:
174+
print("❗ No data avalibale - Existting")
175+
else:
176+
print(f"🚫 Request Fail: {response.status_code} - Exitting...")

shared/telegram_service.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from telegram.error import TelegramError
55

66

7-
async def send_photo_message(token, chat_id, caption, photo_url, source_url, buttonText):
7+
async def send_photo_message(
8+
token, chat_id, caption, photo_url, source_url, buttonText
9+
):
810
bot = telegram.Bot(token)
911

1012
keyboard = [[InlineKeyboardButton(buttonText, url=source_url)]]
@@ -31,10 +33,10 @@ async def send_photo_message(token, chat_id, caption, photo_url, source_url, but
3133
return False
3234

3335

34-
async def send_text_message(token, chat_id, text, source_url):
36+
async def send_text_message(token, chat_id, text, buttonText, source_url):
3537
bot = telegram.Bot(token)
3638

37-
keyboard = [[InlineKeyboardButton("CVE Detail", url=source_url)]]
39+
keyboard = [[InlineKeyboardButton(buttonText, url=source_url)]]
3840
reply_markup = InlineKeyboardMarkup(keyboard)
3941

4042
try:

0 commit comments

Comments
 (0)