-
Notifications
You must be signed in to change notification settings - Fork 3
/
scrape.py
94 lines (82 loc) · 3.06 KB
/
scrape.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from pycricbuzz import Cricbuzz
from logger import Logger
class Scraper:
def __init__(self):
self.cricbuzz = Cricbuzz()
self.current_over = "-1"
self.logger_instance = Logger("app.log")
def _get_matches(self):
return self.cricbuzz.matches()
@staticmethod
def _get_criteria():
return {
'type': "T20",
'mchstate': "inprogress",
'srs': "Indian Premier League 2019"
}
@staticmethod
def _check_if_valid_match(match, criteria):
bools = [match[key] == value for key, value in criteria.items()]
return all(bools)
def _get_current_match_id(self, matches, criteria):
for match in matches:
current_ipl_match = self._check_if_valid_match(match, criteria)
if current_ipl_match is True:
return match['id']
return None
def _get_commentary(self, match_id):
return self.cricbuzz.commentary(match_id)['commentary']
@staticmethod
def _comm_after_new_innings(comms):
split_index = len(comms)
if len(comms) == 0:
return comms
for index, comm in enumerate(comms):
if "will open the attack" in comm['comm']:
split_index = index
return comms[:split_index]
def _new_six_scored(self, commentary, given_last_ball=None):
last_ball_commentary = [comm for comm in commentary if 'over' in comm and comm['over'] is not None]
new_innings_comm = self._comm_after_new_innings(commentary)
last_30_sixes = [
comm for comm in new_innings_comm
if "<b>SIX</b>" in comm['comm']
]
if len(last_ball_commentary) == 0:
last_ball = '0.0'
else:
last_ball = last_ball_commentary[0]['over']
if len(last_30_sixes) > 0 and float(last_30_sixes[0]['over']) > float(given_last_ball):
return {
"status": True,
"last_ball": last_ball
}
return {
"status": False,
"last_ball": last_ball
}
def scrape(self):
self.logger_instance.info("Querying Matches...")
matches = self._get_matches()
match_id = self._get_current_match_id(matches, self._get_criteria())
if match_id is not None:
self.logger_instance.info("Match is found with ID: %s" % match_id)
data = self._new_six_scored(self._get_commentary(match_id), self.current_over)
if data['last_ball'] == "19.6":
self.current_over = "-1"
else:
self.current_over = data['last_ball']
if data['status'] is True:
self.logger_instance.info("A new six was scored at ball: %s" % data["last_ball"])
return {
"status": True,
"data": data
}
self.logger_instance.warning("No match with given criteria found.")
return {
"status": False,
"data": {}
}
if __name__ == "__main__":
s = Scraper()
print(s.scrape())