From db3449fcea1e0edfe2688919cbaf73d15f839db1 Mon Sep 17 00:00:00 2001 From: cube Date: Sat, 30 May 2026 19:46:32 +0100 Subject: [PATCH] can now get the situation diagram from the race stats page --- README.md | 8 ++++++++ example2.py | 12 ++++++++++++ pcslive.py | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 example2.py diff --git a/README.md b/README.md index 457b1c5..e57612a 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,11 @@ stats = LiveStats() stats.print_races() ``` +# examples + +`example.py` posts the latest timeline of one current race (checking every 10 seconds for updates) + +`example2.py` organises the situation diagram in a dictionary of timegap keys to a list of riders in that group. it could probably be organised better but it works + + + diff --git a/example2.py b/example2.py new file mode 100644 index 0000000..ba53c3a --- /dev/null +++ b/example2.py @@ -0,0 +1,12 @@ +from pcslive import LiveStats +import time + +stats = LiveStats() + +if len(stats.races) > 0: + race = stats.races[0] + + race.get_situation_long() + + for x in race.situation_long: + print(x, race.situation_long[x]) \ No newline at end of file diff --git a/pcslive.py b/pcslive.py index dd4a184..784f205 100644 --- a/pcslive.py +++ b/pcslive.py @@ -6,6 +6,9 @@ class LiveStats: self.refresh_live() self.get_races() + self.timeline = [] + self.situation_long = [] + def refresh_live(self): req = requests.get("https://www.procyclingstats.com/") html = req.text @@ -60,15 +63,20 @@ class Race: print("") print(self.url) - def get_timeline(self): + def get_race_page(self): if self.url != "None": full_url = "https://www.procyclingstats.com/" + self.url req = requests.get(full_url) html = req.text soup = BeautifulSoup(html, "html.parser") + return soup + return None - all = soup.find_all(attrs={"class":"timeline3cont"}) + def get_timeline(self): + page = self.get_race_page() + if page: + all = page.find_all(attrs={"class":"timeline3cont"}) live = all[0] timeline = live.find_all("li") self.timeline = [] @@ -78,6 +86,30 @@ class Race: stat_content = stat.find(attrs={"class":"textCont"}) self.timeline.append(self.remove_tags(stat_content)) + def get_situation_long(self): + page = self.get_race_page() + if page: + all = page.find_all(attrs={"class":"situCont"}) + live = all[0] + situation_long = live.find_all("li") + self.situation_long = {} + last_timegap = None + for item in situation_long: + #print(item) + time_gap = item.find(attrs={"class":"time"}) + group_name = item.find(attrs={"class":"groupname"}) + rider = item.find(attrs={"class":"maxw200"}) + + if time_gap: + tg = self.remove_tags(time_gap) + self.situation_long[tg] = [] + last_timegap = tg + rider_name = self.remove_tags(rider) + if rider_name not in self.situation_long[last_timegap]: + self.situation_long[last_timegap].append(rider_name) + + #print(self.remove_tags(time_gap), self.remove_tags(group_name), self.remove_tags(riders)) + def remove_tags(self, text): text = str(text)