added vods to description

main
Benjamin Kraft 11 months ago
parent 60ffb11d01
commit 840dc029f6
  1. 1
      init_api.py
  2. 64
      main.py

@ -18,6 +18,7 @@ def get_credentials():
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print("Credentials refreshed!")
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)

@ -5,22 +5,25 @@ from datetime import datetime, timedelta
import requests
import bs4
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup, Tag
from init_api import get_service
import dotenv
import asyncio
class Match:
def __init__(self, team1: str, team2: str, score1: int, score2: int, raw_time: str, stage: str, roundName: str):
def __init__(self, team1: str, team2: str, score1: int, score2: int,
raw_time: str, stage: str, roundName: str, vods: list[str]):
self.team1 = team1
self.team2 = team2
self.score1 = score1
self.score2 = score2
self.stage = stage
self.roundName = roundName
self.vods = vods
self.time = datetime(*list(map(lambda v: int(v), raw_time.split(","))))
self.eventId = None
@ -39,6 +42,8 @@ class Match:
return 5
def event_data(self):
vods = "\n".join([f'<a href="{link}">Game {i + 1}</a>' for i, link in enumerate(self.vods)])
return {
"start": {
"dateTime": self.time.isoformat() + "Z"
@ -47,23 +52,29 @@ class Match:
"dateTime": (self.time + timedelta(hours=self.get_format())).isoformat() + "Z"
},
"summary": f"{self.team1} - {self.team2}",
"description": f"{self.stage} {self.roundName} BO{self.get_format()}\n{self.score1} - {self.score2}"
"description": f"{self.stage} {self.roundName} BO{self.get_format()}\n{self.score1} - {self.score2}\n{vods}"
}
def update_event(self):
async def update_event(self):
api = get_service()
api.events().update(
calendarId=os.environ["CALENDAR_ID"],
eventId=self.eventId,
body=self.event_data()
).execute()
print(self)
print(f"Event updated: {self.eventId}")
print("")
def create_event(self):
async def create_event(self):
api = get_service()
api.events().insert(
calendarId=os.environ["CALENDAR_ID"],
body=self.event_data()
).execute()
print(self)
print(f"Event created")
print("")
def is_in_calendar(self, calendar_events):
for event in calendar_events:
@ -90,11 +101,12 @@ def fetch_matches():
fetched_data = requests.get(src).text
soup = BeautifulSoup(fetched_data, "html.parser")
match_lists: list[bs4.Tag] = soup.find_all(attrs={"class": "matchlist"})
match_lists: list[Tag] = soup.find_all(attrs={"class": "matchlist"})
vodsTable: Tag = soup.find(attrs={"id", "md-table"}).find("tbody")
for match_list in match_lists:
roundName = match_list.find("tr").find("th").find(recursive=False, string=True).text
raw_matches: list[bs4.Tag] = match_list.find_all(attrs={"class", "ml-row"})
raw_matches: list[Tag] = match_list.find_all(attrs={"class", "ml-row"})
# somehow identify stage...
stage = stages[0] if (src == sources[0] or src == sources[1]) else \
@ -108,7 +120,27 @@ def fetch_matches():
score2 = scores[1]
team1 = raw_match.find(attrs={"class", "matchlist-team1"}).find(attrs={"class", "teamname"}).text
team2 = raw_match.find(attrs={"class", "matchlist-team2"}).find(attrs={"class", "teamname"}).text
matches.append(Match(team1, team2, score1, score2, raw_time, stage, roundName))
vods = []
if "TBD" not in [team1, team2]:
def is_current_match_row(tr):
both = tr.find_all(attrs={"class", "teamname"})
if len(both) == 2:
return both[0].text == team1 and both[1].text == team2
return False
roundStartRow: Tag = vodsTable.find(string=roundName).parent.parent
currentRow: Tag = roundStartRow.find_next_sibling(is_current_match_row)
for i in range(5):
fullLink = currentRow.find("a", string="PB")
if fullLink is not None:
vods.append(fullLink["href"])
currentRow = currentRow.find_next_sibling("tr")
if currentRow is None or len(currentRow.contents) > 5:
break
else:
break
matches.append(Match(team1, team2, score1, score2, raw_time, stage, roundName, vods))
return matches
@ -135,22 +167,20 @@ def delete_calendar_events():
print("Deleted all calendar events")
def update():
async def update():
dotenv.load_dotenv()
events = get_calendar_events()
tasks = []
for match in fetch_matches():
print(f"Processing match: {match}")
if match.is_in_calendar(events):
print(f"Event found: {match.eventId}")
match.update_event()
print("Event updated")
tasks.append(match.update_event())
else:
match.create_event()
print("Event created")
print("")
tasks.append(match.create_event())
await asyncio.gather(*tasks)
if __name__ == "__main__":
update()
asyncio.run(update())

Loading…
Cancel
Save