-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive.py
76 lines (60 loc) · 2.14 KB
/
live.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
""" Parses http://www.live-footballontv.com for info about live matches """
import re
from datetime import datetime, timedelta
import requests
from bs4 import BeautifulSoup
url = 'https://www.live-footballontv.com'
headers = {'User-Agent': 'Football Push Notifications'}
def convert_date(date):
"""
Returns datetime object from date string e.g. Friday 6th October 2025
"""
# Remove nd, th, rd
date = date.split(' ')
date[1] = date[1][:-2]
date = ' '.join(date)
return datetime.strptime(date, '%A %d %B %Y')
def search_matches(match_list, search_list, ignore_list=None):
"""
Return list of football matches that match search
"""
if ignore_list is None:
ignore_list = []
search = re.compile('|'.join(search_list))
my_matches = [m for m in match_list if search.search(m['fixture'])]
if ignore_list:
ignore = re.compile('|'.join(ignore_list))
my_matches = [m for m in my_matches if not ignore.search(m["fixture"])]
return my_matches
def gather_data():
"""
Returns the list of matches
"""
soup = BeautifulSoup(requests.get(url, headers=headers).text,
'html.parser')
data = soup.find_all(True, {'class': ['matchdate',
'matchfixture',
'competition',
'kickofftime',
'channels']})
dates = []
matches = []
i = 0
while i < len(data):
if 'matchdate' in data[i].attrs.values()[0]:
dates.append(convert_date(data[i].text))
i += 1
else:
d = {}
d['fixture'] = data[i].text
d['competition'] = data[i + 1].text
d['kotime'] = data[i + 2].text
d['channels'] = data[i + 3].text
kotime = d['kotime']
if kotime != 'TBC':
hours, minutes = kotime.split(':')
date = dates[-1] + timedelta(hours=int(hours), minutes=int(minutes))
d['date'] = date
matches.append(d)
i += 4
return matches