some updates from todays racing and functions added for easier timeline use
This commit is contained in:
85
pcslive.py
85
pcslive.py
@@ -42,6 +42,13 @@ class LiveStats:
|
||||
for race in self.races:
|
||||
race.print_stats()
|
||||
|
||||
# finds a race by its title
|
||||
def find_race(self, query):
|
||||
for race in self.races:
|
||||
if query.lower() in race.title.lower():
|
||||
return race
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -62,6 +69,10 @@ class Race:
|
||||
self.timeline = []
|
||||
self.situation_long = []
|
||||
|
||||
# when using timeline_latest() put already returned updates
|
||||
# inside here so that they dont get repeated
|
||||
self.timeline_latest_store = []
|
||||
|
||||
# to parse the raw data given by LiveStats
|
||||
def refresh_info(self):
|
||||
title_r = self.raw.find(attrs={"class":"title"})
|
||||
@@ -100,8 +111,8 @@ class Race:
|
||||
if self.url != "None":
|
||||
full_url = "https://www.procyclingstats.com/" + self.url
|
||||
|
||||
req = requests.get(full_url)
|
||||
html = req.text
|
||||
self.req = requests.get(full_url)
|
||||
html = self.req.text
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
return soup
|
||||
return None
|
||||
@@ -112,15 +123,77 @@ class Race:
|
||||
page = self.get_race_page()
|
||||
if page:
|
||||
all = page.find_all(attrs={"class":"timeline3cont"})
|
||||
live = all[0]
|
||||
timeline = live.find_all("li")
|
||||
self.timeline_live = all[0]
|
||||
timeline = self.timeline_live.find_all("li")
|
||||
self.timeline = []
|
||||
for item in timeline:
|
||||
stat = item.find(attrs={"class":"stat"})
|
||||
if stat:
|
||||
stat_content = stat.find(attrs={"class":"textCont"})
|
||||
self.timeline.append(self.remove_tags(stat_content))
|
||||
stat_content, is_data, has_info_number = self.timeline_stats(stat)
|
||||
if is_data:
|
||||
pass
|
||||
elif has_info_number:
|
||||
# same as in the timeline_latest function
|
||||
number = self.remove_tags(has_info_number)
|
||||
text = self.remove_tags(stat_content)
|
||||
update = number + " " + text
|
||||
self.timeline.append(update)
|
||||
else:
|
||||
self.timeline.append(self.remove_tags(stat_content))
|
||||
|
||||
# a function for getting only the latest timeline updates!
|
||||
# useful for making an async timeline feed
|
||||
def timeline_latest(self):
|
||||
# its a bit weird if it doesnt display Anything at first run
|
||||
# so if the seen list is empty, just show the latest update
|
||||
# from the full timeline (then add that to seen)
|
||||
# self.timeline_latest_store is the list for storing seen updates
|
||||
if len(self.timeline_latest_store) == 0:
|
||||
update = self.timeline[0]
|
||||
self.timeline_latest_store.append(update)
|
||||
return update
|
||||
|
||||
# now most of this code is identical to the full timeline
|
||||
# except that it just uses find instead of find_all
|
||||
# assuming it finds the first one... which it does
|
||||
latest = self.timeline_live.find("li")
|
||||
stat = latest.find(attrs={"class":"stat"})
|
||||
if stat:
|
||||
stat_content, is_data, has_info_number = self.timeline_stats(stat)
|
||||
if is_data:
|
||||
pass
|
||||
elif has_info_number:
|
||||
# some timeline updates use a big number
|
||||
# like 150 kilometers to the finish
|
||||
# and scraper only finds "kilometers to the finish"
|
||||
# so if there is a big number, get it and add it
|
||||
# there is a drawback of it doing this for every Big Number
|
||||
# but i sorta dont care rn haha
|
||||
number = self.remove_tags(has_info_number)
|
||||
text = self.remove_tags(stat_content)
|
||||
update = number + " " + text
|
||||
|
||||
# only show the update if it hasnt been seen before
|
||||
if update not in self.timeline_latest_store:
|
||||
# then store it so we know we've already seen it
|
||||
self.timeline_latest_store.append(update)
|
||||
return update
|
||||
else:
|
||||
# same as above but without the big number
|
||||
update = self.remove_tags(stat_content)
|
||||
if update not in self.timeline_latest_store:
|
||||
self.timeline_latest_store.append(update)
|
||||
return update
|
||||
return None
|
||||
|
||||
# function for getting specific things from the timeline
|
||||
# it seemed like a good idea to put it here at the time
|
||||
# shrug
|
||||
def timeline_stats(self, stat):
|
||||
stat_content = stat.find(attrs={"class":"textCont"})
|
||||
is_data = stat.find(attrs={"class":"chartCont"})
|
||||
has_info_number = stat.find(attrs={"class":"number"})
|
||||
return stat_content, is_data, has_info_number
|
||||
|
||||
# creates a dictionary containing time gaps as keys and each
|
||||
# timegap points to a list of riders in that group
|
||||
|
||||
Reference in New Issue
Block a user