-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDBRunner_Country.py
78 lines (65 loc) · 2.51 KB
/
DBRunner_Country.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
import urllib.request
from bs4 import BeautifulSoup
import re
import numpy as np
from DBFinder import DBCalculator
import json
# Define game formats/countries
NationNumsAll = {'england': 1,
'australia': 2,
'south africa': 3,
'west indies': 4,
'new zealand': 5,
'india': 6,
'pakistan': 7,
'sri lanka': 8,
'zimbabwe': 9,
'bermuda': 12,
'netherlands': 15,
'canada': 17,
'hong kong': 19,
'papua new guinea': 20,
'bangladesh': 25,
'kenya': 26,
'united arab emirates': 27,
'ireland': 29,
'afghanistan': 40}
PlayerNameAll = {}
BatsmanText = np.empty(0)
# Select a format & teams
while True:
try:
PlayerTeamName = input('Enter a test playing nation: ').lower()
PlayerTeamID = NationNumsAll[PlayerTeamName]
break
except (NameError, KeyError):
print("Team not found! ", end="", flush=True)
# Parse and extract list of all players for nation
PlayerListURL = f"http://www.espncricinfo.com/england/content/player/caps.json?country={PlayerTeamID!s};class=1"
PlayerListJSON = urllib.request.urlopen(PlayerListURL).read()
PlayerListSoup = BeautifulSoup(PlayerListJSON, "lxml")
PlayerListSoupLi = PlayerListSoup.find_all("li", {"class": "ciPlayername"})
for PConstructor in PlayerListSoupLi:
PConstructString = str(PConstructor)
PConstructURL = (PConstructor.find('a', href=True)['href'])
PConstructNamePos = PConstructString.find("middle;") + 9
PConstructName = PConstructString[PConstructNamePos:len(PConstructString) - 9]
PConstructID = int(re.findall("\\d+", PConstructURL)[0])
PlayerNameAll.update({PConstructName: PConstructID})
# Pick player
print(f"Found {len(PlayerNameAll)!s} players for {PlayerTeamName.title()!s}")
# Open DB database
with open('Database.json', 'r') as fp:
Database = json.load(fp)
for PlayerName, PlayerNum in PlayerNameAll.items():
# Find DB index
if PlayerName not in Database.keys():
DB = DBCalculator(PlayerName, PlayerTeamName, PlayerNum)
with open("Database.json", 'r+') as fp:
Database = json.load(fp)
# Save DB database
Database[PlayerName] = DB
with open("Database.json", 'r+') as fp:
json.dump(Database, fp)
else:
print(f" Already Calculated {PlayerName}. Skipping")