File size: 7,559 Bytes
56f6887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import urljoin
import numpy as np
from sklearn.preprocessing import LabelEncoder
import traceback
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

import chromedriver_autoinstaller
from selenium.common import exceptions


chromedriver_autoinstaller.install()


options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")


def selnium(url):
    try:
        driver = webdriver.Chrome(options=options)
        driver.get(url)
        with open("temp/temp.html", "w+") as f:
            f.write(driver.page_source)
        driver.quit()
        return True
    except exceptions.InvalidSessionIdException as e:
        print(traceback.format_exc())
        print(e.message)
        return False
    except BaseException as e:
        print(traceback.format_exc())
        print(e.message)
        return False


def get_batting_team(soup, status, inning, teams_this_match):
    # teams_this_match = sorted(
    #     np.load("team.npy", allow_pickle=True),
    #     key=lambda x: soup.text.lower().count(x.lower()),
    # )[-2:]
    # print(f"{teams_this_match=}")
    batting_team = ""
    if inning == 2:
        batting_team = status.split("need")[0].strip()
        for idx, team in enumerate(teams_this_match):
            if team.lower() in batting_team.lower():
                batting_team = team
    else:
        for idx, team in enumerate(teams_this_match):
            if team.lower() in status.lower():
                if "opt to bowl" in status.lower():
                    batting_team = teams_this_match[int(~idx)]
                elif "opt to bat" in status.lower():
                    batting_team = team
                else:
                    print("Could not get batting team)")
    bowling_team = list(set(teams_this_match).difference([batting_team]))[0]
    print(f"{batting_team=}, {bowling_team=}")
    batting_team_enc, bowling_team_enc = None, None
    le = LabelEncoder()
    le.classes_ = np.load("model/team.npy", allow_pickle=True)
    if batting_team in le.classes_:
        batting_team_enc = le.transform([batting_team])[0]
    if bowling_team in le.classes_:
        bowling_team_enc = le.transform([bowling_team])[0]
    return batting_team, bowling_team, batting_team_enc, bowling_team_enc


def scrape(url):
    try:
        if selnium(url) is False:
            return ("Selenium scrape error",)
        soup = BeautifulSoup(open("temp/temp.html", "r").read(), "html.parser")
        # print("Debug>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.", soup.text)
        matchState = re.findall(
            'var matchState ="([\da-zA-Z]*)"',
            "\n".join(map(lambda x: x.text, soup.find_all("script"))),
        )[0].lower()
        print(f"{matchState=}")
        title = soup.find_all("title")[0].text
        format = re.findall(
            'var matchFormat = "([\da-zA-Z]*)"',
            "\n".join(map(lambda x: x.text, soup.find_all("script"))),
        )[0]
        print(f"{format=}")
        if format not in {"ODI", "T20"}:
            raise BaseException("Not ODI or T20")
        status = (
            soup.find_all("div", {"class": "cb-text-inprogress"})[0].text
            if matchState == "inprogress"
            else soup.find_all("div", {"class": "cb-text-complete"})[0].text
            if matchState == "complete"
            else soup.find_all("div", {"class": "cb-text-inningsbreak"})[0].text
            if matchState == "inningsbreak"
            else ""
        )
        score = (
            soup.find_all("div", {"class": "cb-min-bat-rw"})[0].text
            if matchState in ["complete", "inprogress", "inningbreak"]
            else ""
        )
        if matchState != "inprogress":
            return (
                matchState,
                score,
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                format,
                title,
                status,
                None,
                None,
                None,
                None,
                None,
            )
        teams_this_match = re.match(
            r"(.*) vs (.*)",
            soup.find_all("a", {"class": "cb-nav-tab"})[0]["title"].split(",")[0],
        ).groups()
        print(f"{teams_this_match=}")

        data = re.findall("(\d+)/(\d+) \(([\.\d]+)\)", soup.text)
        runs, wkts, overs = map(float, data[-1])
        print(f"{runs=}, {wkts=}, {overs=}")

        if overs >= 5:
            last_5_ovs = (
                soup.find_all("span", string="Last 5 overs")[0].findNext("span").text
            )
            run_last_5_overs, wkt_last_5_overs = map(
                float, re.match("(\d+) runs, (\d+) wkts", last_5_ovs).groups()
            )
        else:
            run_last_5_overs, wkt_last_5_overs = runs, wkts
        print(f"{run_last_5_overs=}, {wkt_last_5_overs=}")

        req_rr = -9999
        if soup.find_all("span", string="\xa0\xa0REQ:\xa0"):
            reqdata = (
                soup.find_all("span", string="\xa0\xa0REQ:\xa0")[0]
                .findNext("span")
                .text
            )
            if reqdata.strip() != "":
                req_rr = list(map(float, re.match("([\d\.]+)", reqdata).groups()))[0]
        else:
            print("REQ_RR not parsed")

        crr = -9999
        if soup.find_all("span", string="\xa0\xa0CRR:\xa0"):
            crrdata = (
                soup.find_all("span", string="\xa0\xa0CRR:\xa0")[0]
                .findNext("span")
                .text
            )
            if crrdata.strip() != "":
                crr = list(map(float, re.match("([\d\.]+)", crrdata).groups()))[0]
        else:
            print("CRR not parsed")

        print(f"{crr=}, {req_rr=}")

        inning = 2 if req_rr > 0 else 1
        (
            batting_team,
            bowling_team,
            batting_team_enc,
            bowling_team_enc,
        ) = get_batting_team(soup, status, inning, teams_this_match)

        req = -9999
        if inning == 2:
            req = int(re.match(r".*need (\d+) runs", status).groups()[0])
            print(f"{req=}")
        else:
            print("Not chasing so target not set")

        return (
            matchState,
            score,
            run_last_5_overs,
            wkt_last_5_overs,
            runs,
            wkts,
            overs,
            req_rr,
            req,
            crr,
            format,
            title,
            status,
            batting_team,
            bowling_team,
            batting_team_enc,
            bowling_team_enc,
            inning,
        )
    except BaseException as e:
        print(traceback.format_exc())
        return (str(e),)


def get_live_matches(url):
    if selnium(url) is False:
        return None
    soup = BeautifulSoup(open("temp/temp.html", "r").read(), "html.parser")
    matches = soup.find_all("a", {"class": "cb-mat-mnu-itm cb-ovr-flo"})
    return {
        m.text: urljoin(url, m.get("href"))
        for m in matches
        if m not in soup.find_all("a", {"id": "live-scores-link"})
    }


if __name__ == "__main__":
    url = "https://cricbuzz.com/live-cricket-scores/79055/wa-vs-saus-3rd-match-australia-domestic-one-day-cup-2023-24"
    print(scrape(url))
    # print(get_live_matches("https://cricbuzz.com"))