can now get the situation diagram from the race stats page

This commit is contained in:
cube
2026-05-30 19:46:32 +01:00
parent 452a2572fa
commit db3449fcea
3 changed files with 54 additions and 2 deletions

View File

@@ -37,3 +37,11 @@ stats = LiveStats()
stats.print_races() 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

12
example2.py Normal file
View File

@@ -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])

View File

@@ -6,6 +6,9 @@ class LiveStats:
self.refresh_live() self.refresh_live()
self.get_races() self.get_races()
self.timeline = []
self.situation_long = []
def refresh_live(self): def refresh_live(self):
req = requests.get("https://www.procyclingstats.com/") req = requests.get("https://www.procyclingstats.com/")
html = req.text html = req.text
@@ -60,15 +63,20 @@ class Race:
print("") print("")
print(self.url) print(self.url)
def get_timeline(self): def get_race_page(self):
if self.url != "None": if self.url != "None":
full_url = "https://www.procyclingstats.com/" + self.url full_url = "https://www.procyclingstats.com/" + self.url
req = requests.get(full_url) req = requests.get(full_url)
html = req.text html = req.text
soup = BeautifulSoup(html, "html.parser") 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] live = all[0]
timeline = live.find_all("li") timeline = live.find_all("li")
self.timeline = [] self.timeline = []
@@ -78,6 +86,30 @@ class Race:
stat_content = stat.find(attrs={"class":"textCont"}) stat_content = stat.find(attrs={"class":"textCont"})
self.timeline.append(self.remove_tags(stat_content)) 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): def remove_tags(self, text):
text = str(text) text = str(text)