mirror of
https://github.com/SocksPls/hltv-api
synced 2025-04-29 23:23:08 +00:00
Merge pull request #34 from SocksPls/get-countdown-func
feat(main): get_match_countdown
This commit is contained in:
commit
68ed4126be
2 changed files with 41 additions and 6 deletions
|
@ -293,3 +293,10 @@ Provides an API for HLTV
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `get_match_countdown`
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> hltv.get_match_countdown(2357395)
|
||||||
|
'1:29:00'
|
||||||
|
```
|
||||||
|
|
40
main.py
40
main.py
|
@ -201,7 +201,18 @@ def _get_historical_lineup(player_anchors):
|
||||||
players.append(player)
|
players.append(player)
|
||||||
return players
|
return players
|
||||||
|
|
||||||
|
def _generate_countdown(date: str, time: str):
|
||||||
|
timenow = datetime.datetime.now().astimezone(LOCAL_ZONEINFO).strftime('%Y-%m-%d %H:%M')
|
||||||
|
deadline = date + " " + time
|
||||||
|
currentTime = datetime.datetime.strptime(timenow,'%Y-%m-%d %H:%M')
|
||||||
|
ends = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M')
|
||||||
|
if currentTime < ends:
|
||||||
|
return str(ends - currentTime)
|
||||||
|
return None
|
||||||
|
|
||||||
|
MATCH_WITH_COUNTDOWN = None
|
||||||
def get_matches():
|
def get_matches():
|
||||||
|
global MATCH_WITH_COUNTDOWN
|
||||||
matches = get_parsed_page("https://www.hltv.org/matches/")
|
matches = get_parsed_page("https://www.hltv.org/matches/")
|
||||||
matches_list = []
|
matches_list = []
|
||||||
|
|
||||||
|
@ -221,13 +232,11 @@ def get_matches():
|
||||||
timeFromHLTV = timeFromHLTV.astimezone(LOCAL_ZONEINFO)
|
timeFromHLTV = timeFromHLTV.astimezone(LOCAL_ZONEINFO)
|
||||||
matchObj['date'] = timeFromHLTV.strftime('%Y-%m-%d')
|
matchObj['date'] = timeFromHLTV.strftime('%Y-%m-%d')
|
||||||
matchObj['time'] = timeFromHLTV.strftime('%H:%M')
|
matchObj['time'] = timeFromHLTV.strftime('%H:%M')
|
||||||
timenow = datetime.datetime.now().astimezone(LOCAL_ZONEINFO).strftime('%Y-%m-%d %H:%M')
|
|
||||||
deadline = date + " " + getMatch.find("div", {"class": "matchTime"}).text
|
|
||||||
currentTime = datetime.datetime.strptime(timenow,'%Y-%m-%d %H:%M')
|
|
||||||
ends = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M')
|
|
||||||
|
|
||||||
if currentTime < ends:
|
matchObj['countdown'] = _generate_countdown(date, getMatch.find("div", {"class": "matchTime"}).text)
|
||||||
matchObj['countdown'] = str(ends - currentTime)
|
|
||||||
|
if not MATCH_WITH_COUNTDOWN and matchObj['countdown']:
|
||||||
|
MATCH_WITH_COUNTDOWN = converters.to_int(getMatch.find("a", {"class": "match a-reset"}).get("href").split("/")[-2])
|
||||||
|
|
||||||
if getMatch.find("div", {"class": "matchEvent"}):
|
if getMatch.find("div", {"class": "matchEvent"}):
|
||||||
matchObj['event'] = getMatch.find("div", {"class": "matchEvent"}).text.strip()
|
matchObj['event'] = getMatch.find("div", {"class": "matchEvent"}).text.strip()
|
||||||
|
@ -360,6 +369,22 @@ def get_results_by_date(start_date, end_date):
|
||||||
|
|
||||||
return results_list
|
return results_list
|
||||||
|
|
||||||
|
def get_match_countdown(match_id):
|
||||||
|
url = "https://www.hltv.org/matches/" + str(match_id) + "/page"
|
||||||
|
match_page = get_parsed_page(url)
|
||||||
|
timeAndEvent = match_page.find("div", {"class": "timeAndEvent"})
|
||||||
|
date = timeAndEvent.find("div", {"class": "date"}).text
|
||||||
|
time = timeAndEvent.find("div", {"class": "time"}).text
|
||||||
|
dateArr = date.replace("th of","").split()
|
||||||
|
dateTextFromArrPadded = _padIfNeeded(dateArr[2]) + "-" + _padIfNeeded(_monthNameToNumber(dateArr[1])) + "-" + _padIfNeeded(dateArr[0])
|
||||||
|
|
||||||
|
dateFromHLTV = datetime.datetime.strptime(dateTextFromArrPadded,'%Y-%m-%d').replace(tzinfo=HLTV_ZONEINFO)
|
||||||
|
dateFromHLTV = dateFromHLTV.astimezone(LOCAL_ZONEINFO)
|
||||||
|
|
||||||
|
date = dateFromHLTV.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
return _generate_countdown(date, time)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import pprint
|
import pprint
|
||||||
pp = pprint.PrettyPrinter()
|
pp = pprint.PrettyPrinter()
|
||||||
|
@ -388,3 +413,6 @@ if __name__ == "__main__":
|
||||||
pp.pprint('get_results_by_date')
|
pp.pprint('get_results_by_date')
|
||||||
today_iso = datetime.datetime.today().isoformat().split('T')[0]
|
today_iso = datetime.datetime.today().isoformat().split('T')[0]
|
||||||
pp.pprint(get_results_by_date(today_iso, today_iso))
|
pp.pprint(get_results_by_date(today_iso, today_iso))
|
||||||
|
|
||||||
|
pp.pprint('get_match_countdown')
|
||||||
|
pp.pprint(get_match_countdown(MATCH_WITH_COUNTDOWN))
|
||||||
|
|
Loading…
Add table
Reference in a new issue