-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
180 lines (132 loc) · 5.64 KB
/
main.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
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
#!/bin/python3
import requests
import re
from bs4 import BeautifulSoup
from collections import Counter
import collections, functools, operator
pl_teams = "https://www.primeleague.gg/de/leagues/teams/"
pl_matches = "https://www.primeleague.gg/de/leagues/matches/"
def get_soup(uri: str):
'Get a soup from uri'
response = requests.get(uri)
soup = BeautifulSoup(response.text, "html.parser")
return soup
def get_summoners(team_id: str):
summoners = []
team_soup = get_soup(pl_teams + team_id)
for span in team_soup.find_all("span", title="League of Legends » LoL Summoner Name (EU West)"):
summoner = span.get_text().lower()
summoners.append(summoner)
return summoners
def generate_opgg(team_id: str):
'Generate a op.gg link with all summoners from the team'
op_link = "https://euw.op.gg/multi/query="
for summoner in get_summoners(team_id):
op_link += ("%2C" + summoner.replace(" ",""))
return op_link
def get_matches(team_id: str):
'Generate an array with IDs to all played games'
team_soup = get_soup(pl_teams + team_id)
match_history = []
table = team_soup.find_all("td", "col-3 col-text-right")
for row in table:
match = row.find("a","table-cell-container")
link = match.get("href")
'cut ID from whole link'
regex = r"https\:\/\/www\.primeleague\.gg\/de\/leagues\/matches\/(\d+).+"
match_id = re.search(regex, link)
match_history.append(match_id.group(1))
return match_history
def get_champions(match_id: str):
'returns a dictionary with summoners and champions'
match_soup = get_soup(pl_matches + match_id)
'extracts the summoner names'
summoners = []
for i in match_soup.find_all("div", "submatch-lol-player-name"):
summoners.append(i.text.lower())
'extracts the played champions'
champions = []
for i in match_soup.find_all("img", "img-player-hero"):
champions.append(i.get("title"))
if summoners or champions:
'from start to half length of champions'
game1_champions = champions[:len(champions)//2]
'from half length to end of champions'
game2_champions = champions[len(champions)//2:]
game1_summoners = summoners[:len(summoners)//2]
game2_summoners = summoners[len(summoners)//2:]
game1 = dict(zip(game1_summoners, game1_champions))
game2 = dict(zip(game2_summoners, game2_champions))
match = {
"game1" : game1,
"game2" : game2
}
return match
def get_champions_of_team(team_id: str):
'returns a dictionary of played champs and their play count'
played_champs = []
for match in get_matches(team_id):
if get_champions(match):
for summoner in list(get_champions(match)["game1"]):
if summoner in get_summoners(team_id):
'only appends the summoners of one team'
played_champs.append(get_champions(match)["game1"][summoner])
for summoner in list(get_champions(match)["game2"]):
if summoner in get_summoners(team_id):
played_champs.append(get_champions(match)["game2"][summoner])
return(played_champs)
def count_to_dict(array: list):
counted_array = Counter(array)
sorted_counted_array = dict(sorted(counted_array.items(), key=lambda x: x[1], reverse=True))
return sorted_counted_array
def get_all_bans(match_id: str):
'returns a dictionary with champions and their bancount'
match_soup = get_soup(pl_matches + match_id)
'extracts the banned champions'
banned_champs = []
for imagebox in match_soup.find_all("div", "submatch-lol-bans"):
for image in imagebox.findAll("img"):
banned_champs.append(image.get("title"))
counted_bans = Counter(banned_champs)
sorted_counted_bans = dict(sorted(counted_bans.items(), key=lambda x: x[1], reverse=True))
return sorted_counted_bans
def get_bans_against(match_id: str, team_id: str):
'returns a dictionary with champions and their bancount'
match_soup = get_soup(pl_matches + match_id)
'extracts the banned champions'
banned_champs = []
for imagebox in match_soup.find_all("div", "submatch-lol-bans"):
for image in imagebox.findAll("img"):
banned_champs.append(image.get("title"))
banned_champions = []
blueside = started_blueside(match_id,team_id)
for counter,ban in enumerate(banned_champs):
if not(blueside ^ int(counter/5)%2):
banned_champions.append(ban)
return banned_champions
def get_all_bans_against(team_id: str):
banned_champs = []
if get_matches(team_id):
for match in get_matches(team_id):
if get_bans_against(match, team_id):
for ban in get_bans_against(match, team_id):
banned_champs.append(ban)
return banned_champs
def started_blueside(match_id: str, team_id: str):
match_soup = get_soup(pl_matches + match_id)
team = get_summoners(team_id)
if match_soup.find("div", "submatch-lol-player-name"):
if match_soup.find("div", "submatch-lol-player-name").text.lower() in team:
return True
return False
def get_champion_presence(team_id: str):
champion_presence = []
for champion in get_champions_of_team(team_id):
champion_presence.append(champion)
for champion in get_all_bans_against(team_id):
champion_presence.append(champion)
return champion_presence
def get_name(team_id: str):
team_soup = get_soup(pl_teams + team_id)
name = team_soup.find("div","page-title")
print(name.text)