-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.py
191 lines (160 loc) · 6.72 KB
/
function.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
from audioop import mul
import os
import constant
import shutil
import urllib.request
import json
import constant
import unicodedata
import requests
from bs4 import BeautifulSoup
######### DOWNLOAD DATA FUNCTION ##########
def getActualVersion():
versionJSON = getJSONData("https://ddragon.leagueoflegends.com/api/versions.json")
constant.version = versionJSON[0]
return versionJSON[0]
def getChampionsData():
championsJSON = getJSONData("https://ddragon.leagueoflegends.com/cdn/"+constant.version+"/data/en_US/champion.json")
return championsJSON["data"]
def getChampionsSpellsSpecific():
otherAbilitesJSON = getJSONData("https://raw.githubusercontent.com/Kla35/lol-dl-abilities-images-py/master/othersabilities.json")
return otherAbilitesJSON
def getChampionDataById(id):
req = urllib.request.Request("https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champions/"+id+".json", headers={'User-Agent': 'Mozilla/5.0'})
data = json.loads(urllib.request.urlopen(req).read())
return data
def getSummonersData():
summoners = getJSONData("https://ddragon.leagueoflegends.com/cdn/"+constant.version+"/data/fr_FR/summoner.json")
return summoners["data"]
def getItemsData():
items = getJSONData("https://ddragon.leagueoflegends.com/cdn/"+constant.version+"/data/fr_FR/item.json")
return items["data"]
######### TREAT FUNCTION ############
def countOthersAbilities(data):
nbAbilities = 0
for champ in data:
nbAbilities+= len(champ["spells"])
return nbAbilities
def countNbImagesToDownload(champions, championsPerso, summoners, items, mainGUI):
if mainGUI.cb_spells.get() == 1:
nbSpells = countOthersAbilities(championsPerso)+len(champions)*5
else:
nbSpells = 0
if mainGUI.cb_summoners.get() == 1:
nbSummoners = len(summoners)
else:
nbSummoners = 0
if mainGUI.cb_items.get() == 1:
nbItems = len(items)
else:
nbItems = 0
if mainGUI.cb_items.get() == 1:
nbItems = len(items)
else:
nbItems = 0
nbChampion = len(champions)
multiplicator = mainGUI.cb_splash.get() + mainGUI.cb_icon.get() + mainGUI.cb_vertical.get()
champImage = nbChampion * multiplicator
return champImage + nbSpells + nbSummoners + nbItems
def trimChampionName(str):
returnStr = str.replace("'","")
returnStr = returnStr.replace(".","")
returnStr = returnStr.replace("&","")
returnStr = returnStr.replace(" ","")
returnStr = returnStr.replace(" ","")
return returnStr
def defineSpellKey(str):
match str:
case "P2":
return "Passif_2"
case "P3":
return "Passif_3"
case "Q2":
return "A_2"
case "Q3":
return "A_3"
case "Q4":
return "A_4"
case "W2":
return "Z_2"
case "W3":
return "Z_3"
case "E2":
return "E_2"
case "E3":
return "E_3"
case "R2":
return "R_2"
case "R3":
return "R_3"
case "R4":
return "R_4"
case "R5":
return "R_5"
case "Q":
return "A"
case "W":
return "Z"
case _:
return str
######## DOWNLOAD IMAGES FUNCTION ########
def download_image(url, file_path, file_name):
full_path = file_path + '/' + file_name
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
try:
response = urllib.request.urlopen( req )
except urllib.error.URLError as e:
if hasattr( e, 'reason' ):
print( 'Fail in reaching the server -> ', e.reason )
print(url)
return False
elif hasattr( e, 'code' ):
print( 'The server couldn\'t fulfill the request -> ', e.code )
print(url)
return False
else:
with open( full_path, 'wb' ) as fo:
fo.write( response.read() )
#print( 'Url saved as %s' % full_path )
return True
def downloadSpellImage(spellPath, fileName):
imageURL = ""
if spellPath.startswith('/lol-game-data/assets/ASSETS/'):
trim = spellPath.split('/lol-game-data/assets/ASSETS/')
imageURL = 'https://raw.communitydragon.org/latest/game/assets/'+trim[1].lower()
else:
trim = spellPath.split('/lol-game-data/assets/v1/champion-ability-icons/')
imageURL = 'https://raw.communitydragon.org/latest/game/data/characters/qiyana/hud/icons2d/'+trim[1].lower()
download_image(imageURL, constant.folders['cb_spells'], fileName)
def downloadIconImage(champName,version):
imageURL = 'http://ddragon.leagueoflegends.com/cdn/'+version+'/img/champion/'+champName+'.png';
download_image(imageURL, constant.folders['cb_icon'], champName+'.jpg')
def downloadSplashImage(champName):
imageURL = 'https://ddragon.leagueoflegends.com/cdn/img/champion/splash/'+champName+'_0.jpg';
download_image(imageURL, constant.folders['cb_splash'], champName+'.jpg')
def downloadVerticalImage(champName):
imageURL = 'https://ddragon.leagueoflegends.com/cdn/img/champion/loading/'+champName+'_0.jpg';
download_image(imageURL, constant.folders['cb_vertical'], champName+'.jpg')
def downloadSummonerImage(summonerPath, summonerName):
imageURL = 'https://ddragon.leagueoflegends.com/cdn/'+constant.version+'/img/spell/'+summonerPath
download_image(imageURL, constant.folders['cb_summoners'], summonerName+'.jpg')
def downloadItemImage(itemPath, itemName):
imageURL = 'https://ddragon.leagueoflegends.com/cdn/'+constant.version+'/img/item/'+itemPath
download_image(imageURL, constant.folders['cb_items'], itemName+'.png')
######## GENERAL FUNCTION ###########
def getJSONData(url):
json_url = urllib.request.urlopen(url)
data = json.loads(json_url.read())
return data
def recreate_folders(mainGUI):
foldersCb = ['cb_icon','cb_spells','cb_splash','cb_vertical','cb_summoners','cb_items']
base_folder = mainGUI.folder_path.get()
constant.folders = {'cb_icon': os.path.join(base_folder, 'championIcon'), 'cb_spells': os.path.join(base_folder, 'championSpell'), 'cb_splash': os.path.join(base_folder, 'championSplash'), 'cb_vertical': os.path.join(base_folder, 'championVertical'), 'cb_summoners': os.path.join(base_folder, 'summoners'), 'cb_items': os.path.join(base_folder, 'items')}
for folderCb in foldersCb:
if mainGUI.getCheckbox(folderCb).get() == 1:
folder = constant.folders[folderCb]
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])