|
| 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...") |
0 commit comments