-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
370 lines (317 loc) · 14.5 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import json
import os
import requests
from requests.auth import HTTPBasicAuth
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
opts = Options()
opts.headless = True
with open("rune_data.json", "r") as f:
RUNE_DICT = json.load(f)
class runechanger:
def __init__(self):
if os.name == 'nt':
self.out = "C:\Riot Games\League of Legends/lockfile"
else:
self.out = "/Applications/League of Legends.app/Contents/LoL/lockfile"
try:
with open(self.out, 'r') as f:
data = f.readline().strip().split(":")
except:
print("Please make sure your LoL client is open.")
exit()
self.port = data[2]
self.password = data[3]
self.scheme = data[4]
self.url = f"wss://riot:{self.password}@127.0.0.1:{str(self.port)}"
self.base_url = f"{self.scheme}://127.0.0.1:{self.port}"
self.auth_header = HTTPBasicAuth('riot', self.password)
self.items_res = requests.get("https://ddragon.leagueoflegends.com/cdn/11.16.1/data/en_US/item.json").json()
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:86.0) Gecko/20100101 Firefox/86.0'}
self.new_champ = (None, None)
self.runes = {"primary_runes": None,
"secondary_runes": None,
"fragments": None}
def listener(self):
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
print("------------------------------------------")
print(" https://github.com/RAV64/LoL-RuneChanger ")
print("------------------------------------------")
print("LoL RuneChanger started up successfully.")
print("CTRL + C to close the program.\n")
while True:
# Client open, not in champselect
page = requests.get(f"{self.base_url}/lol-champ-select/v1/session",
verify=False, auth=self.auth_header).json()
if "errorCode" not in page:
# Entered champselect
self.__parse_response(page)
if self.champ_id != 0:
# Champ chosen
champ_name = self.__get_champ_name()[1]
if self.new_champ != (champ_name, self.new_champ[1]):
self.new_champ = (champ_name, self.new_champ[1])
self.__prepare_driver(champ_name.lower())
new_champ_or_role = True
if self.phase == "FINALIZATION" and new_champ_or_role:
self.__scrapeUGG()
self.__set_runes()
self.__set_items()
self.__update_cli()
self.phase == None
new_champ_or_role = False
def __item_names_to_ids(self):
starting_item_ids = []
for starting_item_lists in self.starting_items:
x = []
for sil in starting_item_lists:
x += [item for item in self.items_res["data"]
if self.items_res["data"][item]["name"] == sil]
starting_item_ids.append(x)
core_item_ids = []
for core_item_lists in self.core_items:
x = []
for sil in core_item_lists:
x += [item for item in self.items_res["data"]
if self.items_res["data"][item]["name"] == sil]
core_item_ids.append(x)
item_ids = []
for xitem in self.items:
item_ids.append([item for item in self.items_res["data"]
if self.items_res["data"][item]["name"] == xitem][0])
self.blocks = []
for i, slist in enumerate(starting_item_ids):
self.blocks.append({
"type": f"Starting items {i+1}",
"hideIfSummonerSpell": "",
"showIfSummonerSpell": "",
"items": [{"id": list_item, "count": 1} for list_item in slist]
})
for i, slist in enumerate(core_item_ids):
self.blocks.append({
"type": f"Core items {i+1}",
"hideIfSummonerSpell": "",
"showIfSummonerSpell": "",
"items": [{"id": list_item, "count": 1} for list_item in slist]
})
self.blocks.append({
"type": f"Popular items on {self.new_champ[0]} in popularity order",
"hideIfSummonerSpell": "",
"showIfSummonerSpell": "",
"items": [{"id": itemid, "count": 1} for itemid in item_ids]
})
def __set_items(self):
self.__get_item_page_id()
self.__item_names_to_ids()
data = {
"timestamp": 0,
"accountId": self.summoner_id,
"itemSets": [{
"uid": str(self.uid),
"title": f"{self.new_champ[0]} / {self.role_or_aram} / {' -> '.join(self.skill_order)}",
"mode": "any",
"map": "any",
"type": "custom",
"sortrank": 0,
"startedFrom": "blank",
"associatedChampions": [self.champ_id],
"associatedMaps": [0, 11, 12],
"blocks": self.blocks,
"preferredItemSlots": [{
"id": "string",
"preferredItemSlot": 0}]}]}
url = self.base_url + \
f"/lol-item-sets/v1/item-sets/{self.summoner_id}/sets"
r = requests.put(url, verify=False, auth=self.auth_header, json=data)
def __set_runes(self):
page_id = self.__get_rune_page_id()["id"]
url = f"{self.base_url}/lol-perks/v1/pages/{str(page_id)}"
data = {"name": f"{self.new_champ[0]} @ {self.role_or_aram}",
"primaryStyleId": self.runes["primary_runes"][0],
"subStyleId": self.runes["secondary_runes"][0],
"selectedPerkIds": self.runes["primary_runes"][1:] +
self.runes["secondary_runes"][1:] +
self.runes["fragments"],
"current": True,
"isActive": True}
r = requests.put(url, verify=False, auth=self.auth_header, json=data)
def __get_rune_page_id(self):
url = self.base_url + "/lol-perks/v1/currentpage"
r = requests.get(url, verify=False, auth=self.auth_header)
return r.json()
def __get_item_page_id(self):
url = self.base_url + \
f"/lol-item-sets/v1/item-sets/{self.summoner_id}/sets"
r = requests.get(url, verify=False, auth=self.auth_header)
with open("test.json", 'w') as f:
json.dump(r.json(), f, indent=4)
self.uid = r.json()["itemSets"][0]["uid"]
def __clean_role(self, role):
if role.startswith("t"):
self.assigned_role = "top"
elif role.startswith("j"):
self.assigned_role = "jungle"
elif role.startswith("m"):
self.assigned_role = "mid"
elif role.startswith("b"):
self.assigned_role = "bot"
else:
self.assigned_role = "support"
@staticmethod
def __clean_role_items(role):
if role == "mid":
role = "middle"
elif role == "bot":
role = "adc"
return role
def __prepare_driver(self, champ_name):
if self.assigned_role:
self.__clean_role(self.assigned_role.lower())
role = self.__clean_role_items(self.assigned_role)
url = f"https://u.gg/lol/champions/{champ_name}/build?rank=diamond_2_plus&role={self.assigned_role}"
items_url = f"https://www.leagueofgraphs.com/champions/items/{champ_name}/{role}"
self.role_or_aram = self.assigned_role
else:
url = f"https://u.gg/lol/champions/aram/{champ_name}-aram"
items_url = f"https://www.leagueofgraphs.com/champions/items/{champ_name}/diamond/aram"
self.role_or_aram = "Aram"
driver.get(url)
WebDriverWait(driver, 20).until(EC.visibility_of_element_located(
(By.XPATH, "//*[@id='content']/div/div[1]/div/div/div[5]/div/div[2]/div[1]/div[2]/div[1]/div[1]/div")))
if "https://static.u.gg/assets/ugg/icons/alert-yellow.svg" in driver.page_source:
url = f"https://u.gg/lol/champions/{champ_name}/build?role={self.assigned_role}"
driver.get(url)
WebDriverWait(driver, 20).until(EC.visibility_of_element_located(
(By.XPATH, "//*[@id='content']/div/div[1]/div/div/div[5]/div/div[2]/div[1]/div[2]/div[1]/div[1]/div")))
self.items_response = requests.get(items_url, headers=self.headers).content.decode()
def __scrape_items(self):
self.items = []
self.starting_items = []
self.core_items = []
soup = bs(self.items_response, 'html.parser')
table = soup.find_all("table", {"class": "data_table sortable_table"})
trs = table[3].find_all("tr")
for i, tr in enumerate(trs):
if i == 0:
continue
elif i == 11:
break
self.items.append(tr.find("img")["alt"])
trs = table[0].find_all("tr")
for i, tr in enumerate(trs):
if i == 0:
continue
elif i == 4:
break
self.core_items.append([x["alt"] for x in tr.find_all("img")])
table = soup.find_all(
"table", {"class": "data_table itemStarters sortable_table"})
trs = table[0].find_all("tr")
for i, tr in enumerate(trs):
if i == 0:
continue
elif i == 4:
break
self.starting_items.append([x["alt"] for x in tr.find_all("img")])
def __scrapeUGG(self):
driver.switch_to.window(driver.window_handles[0])
page = driver.page_source
soup = bs(page, 'html.parser')
primary_tree = soup.find_all(
"div", {"class": "rune-tree_v2 primary-tree"})[0]
secondary_tree = soup.find_all("div", {"class": "secondary-tree"})[0]
skill_list = soup.find_all("div", {"class": "skill-priority-path"})[0]
primary_runes = self.__get_primary_runes(primary_tree)
secondary_runes = self.__get_secondary_runes(secondary_tree)
fragments = self.__get_fragments(secondary_tree)
self.__scrape_items()
self.__get_skill_order(skill_list)
self.runes = {"primary_runes": primary_runes,
"secondary_runes": secondary_runes,
"fragments": fragments}
def __get_primary_runes(self, primary_tree):
keystone = primary_tree.find_all(
"div", {"class": "perk keystone perk-active"})[0]
sub_runes = primary_tree.find_all("div", {"class": "perk perk-active"})
style = primary_tree.find_all(
"div", {"class": "perk-style-title"})[0].text.lower()
runes = [RUNE_DICT['name_to_id'][style]]
runes += [self.__get_rune_id(keystone, prefix="The Keystone")]
runes += [self.__get_rune_id(sub_rune, prefix="The Rune")
for sub_rune in sub_runes]
return runes
def __get_secondary_runes(self, secondary_tree):
sub_runes = secondary_tree.find_all(
"div", {"class": "perk perk-active"})
style = secondary_tree.find_all(
"div", {"class": "perk-style-title"})[0].text.lower()
runes = [RUNE_DICT['name_to_id'][style]]
runes += [self.__get_rune_id(sub_rune, prefix="The Rune")
for sub_rune in sub_runes]
return runes
def __get_fragments(self, secondary_tree):
fragments = secondary_tree.find_all(
"div", {"class": "shard shard-active"})
fragments = [self.__get_rune_id(
fragment, prefix="The", suffix="Shard") for fragment in fragments]
return fragments
@staticmethod
def __get_rune_id(rune, prefix="", suffix=""):
rune_name = rune.find("img")["alt"] \
.replace(f"{prefix}", "") \
.replace(" ", "") \
.replace(f"{suffix}", "") \
.replace(":", "") \
.lower()
if rune_name == "adaptiveforce":
rune_name = "adaptive"
elif rune_name == "magicresist":
rune_name = "magicres"
elif rune_name == "scalingcdr":
rune_name = "cdrscaling"
elif rune_name == "scalingbonushealth":
rune_name = "healthscaling"
rune_id = RUNE_DICT["name_to_id"][rune_name]
return rune_id
def __parse_response(self, response):
cellid = response["localPlayerCellId"] % 5
self.phase = response["timer"]["phase"]
self.champ_id = response["myTeam"][cellid]["championId"]
self.assigned_role = response["myTeam"][cellid]["assignedPosition"]
self.summoner_id = response["myTeam"][cellid]["summonerId"]
def __get_champ_name(self):
url_champs = f"https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champions/{self.champ_id}.json"
r = requests.get(url_champs).json()
if r["alias"] == "MonkeyKing":
r["alias"] = "wukong"
return r["name"], r["alias"]
def __get_skill_order(self, skill_list):
skills = skill_list.find_all(
"div", {"class": "skill-label bottom-center"})
self.skill_order = [skill.text for skill in skills]
def __update_cli(self):
print("-------------------------------------------")
print("Lol RuneChanger")
print(f"Champ: {self.new_champ[0]}")
print(f"Role: {self.role_or_aram}")
print(f"levelup: {' -> '.join(self.skill_order)}")
print(f"\nStarting items: \n")
for i, item in enumerate(self.starting_items):
print(f"\t{i + 1}. {', '.join(item)}")
print(f"\nMost common items: \n")
for i, item in enumerate(self.items):
print(f"\t{i+1}. {item}")
print("\nCTRL + C to close the program.")
try:
with webdriver.Firefox(options=opts) as driver:
driver.get("https://u.gg/lol/champions/neeko/build")
rc = runechanger()
rc.listener()
except KeyboardInterrupt as e:
print("Shutting down.")