forked from kuhnertdm/wow-addon-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteHandler.py
232 lines (197 loc) · 7.88 KB
/
SiteHandler.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
import packages.requests as requests
import re
import zipfile
import os
import tempfile
import shutil
from io import *
# Site splitter
def findZiploc(addonpage):
# Curse
if addonpage.startswith('https://mods.curse.com/addons/wow/'):
return curse(convertOldCurseURL(addonpage))
elif addonpage.startswith('https://www.curseforge.com/wow/addons/'):
return curse(addonpage)
# Curse Project
elif addonpage.startswith('https://wow.curseforge.com/projects/'):
return curseProject(addonpage)
# Tukui
elif addonpage.startswith('https://git.tukui.org/'):
return tukui(addonpage)
# Wowinterface
elif addonpage.startswith('http://www.wowinterface.com/'):
return wowinterface(addonpage)
# Invalid page
else:
print('Invalid addon page.')
def getCurrentVersion(addonpage):
# Curse
if addonpage.startswith('https://mods.curse.com/addons/wow/'):
return getCurseVersion(convertOldCurseURL(addonpage))
elif addonpage.startswith('https://www.curseforge.com/wow/addons/'):
return getCurseVersion(addonpage)
# Curse Project
elif addonpage.startswith('https://wow.curseforge.com/projects/'):
return getCurseProjectVersion(addonpage)
# Tukui
elif addonpage.startswith('https://git.tukui.org/'):
return getTukuiVersion(addonpage)
# Wowinterface
elif addonpage.startswith('http://www.wowinterface.com/'):
return getWowinterfaceVersion(addonpage)
# Invalid page
else:
print('Invalid addon page.')
def installAddon(addonUrl, installPath):
try:
if addonUrl.lower().startswith(('https://mods.curse.com/',
'https://www.curseforge.com/', 'http://www.wowinterface.com/')):
installSingleLevelZip(addonUrl, installPath)
elif addonUrl.startswith('https://git.tukui.org/'):
installZippedGitRepo(addonUrl, installPath)
else:
print('Invalid addon URL.')
except Exception as err:
print('Failed to install ' + addonUrl)
print(err)
def installSingleLevelZip(addonUrl, installPath):
try:
r = requests.get(addonUrl, stream=True)
z = zipfile.ZipFile(BytesIO(r.content))
z.extractall(installPath)
except Exception as err:
print('Failed to install ' + addonUrl)
print(err)
def installZippedGitRepo(addonUrl, installPath):
try:
r = requests.get(addonUrl, stream=True)
z = zipfile.ZipFile(BytesIO(r.content))
with tempfile.TemporaryDirectory() as tempDirPath:
z.extractall(tempDirPath)
directories = list(filter(gitZipFilter, z.infolist()))
for info in directories:
parts = splitall(info.filename)
src = os.path.join(tempDirPath, parts[0], parts[1])
dst = os.path.join(installPath, parts[1])
shutil.rmtree(dst, True)
shutil.copytree(src, dst)
shutil.rmtree(os.path.join(tempDirPath, z.infolist()[0].filename), True)
except Exception as err:
print('Failed to install ' + addonUrl)
print(err)
# This function returns true if the ZipInfo objects references a directory
# and that directory is one level below the root of the archive and it
# does not begin with '.'
def gitZipFilter(zipInfo):
if zipInfo.is_dir() == False:
return False
parts = splitall(zipInfo.filename)
if len(parts) != 3:
return False
if parts[1].startswith('.'):
return False
return True
def splitall(path):
allparts = []
while 1:
parts = os.path.split(path)
if parts[0] == path: # sentinel for absolute paths
allparts.insert(0, parts[0])
break
elif parts[1] == path: # sentinel for relative paths
allparts.insert(0, parts[1])
break
else:
path = parts[0]
allparts.insert(0, parts[1])
return allparts
# Curse
def curse(addonpage):
try:
page = requests.get(addonpage + '/download')
contentString = str(page.content)
indexOfZiploc = contentString.find('download__link') + 22 # Will be the index of the first char of the url
endQuote = contentString.find('"', indexOfZiploc) # Will be the index of the ending quote after the url
return 'https://www.curseforge.com' + contentString[indexOfZiploc:endQuote]
except Exception:
print('Failed to find downloadable zip file for addon. Skipping...\n')
return ''
def convertOldCurseURL(addonpage):
try:
# Curse has renamed some addons, removing the numbers from the URL. Rather than guess at what the new
# name and URL is, just try to load the old URL and see where Curse redirects us to. We can guess at
# the new URL, but they should know their own renaming scheme better than we do.
page = requests.get(addonpage)
return page.url
except Exception:
print('Failed to find the current page for old URL "' + addonpage + '". Skipping...\n')
return ''
def getCurseVersion(addonpage):
try:
page = requests.get(addonpage + '/files')
contentString = str(page.content)
indexOfVer = contentString.find('file__name full') + 17 # first char of the version string
endTag = contentString.find('</span>', indexOfVer) # ending tag after the version string
return contentString[indexOfVer:endTag].strip()
except Exception:
print('Failed to find version number for: ' + addonpage)
return ''
# Curse Project
def curseProject(addonpage):
try:
return addonpage + '/files/latest'
except Exception:
print('Failed to find downloadable zip file for addon. Skipping...\n')
return ''
def getCurseProjectVersion(addonpage):
try:
page = requests.get(addonpage + '/files')
contentString = str(page.content)
indexOfVer = contentString.find('data-name') + 11 # first char of the version string
endTag = contentString.find('">', indexOfVer) # ending tag after the version string
return contentString[indexOfVer:endTag].strip()
except Exception:
print('Failed to find version number for: ' + addonpage)
return ''
# Tukui
def tukui(addonpage):
try:
return addonpage + '/repository/development/archive.zip'
except Exception:
print('Failed to find downloadable zip file for addon. Skipping...\n')
return ''
def getTukuiVersion(addonpage):
try:
response = requests.get(addonpage)
content = str(response.content)
match = re.search('<a\sclass="commit-sha\s[^>]*>(?P<hash>[^<]*)<\/a>', content)
result = ''
if match:
result = match.group('hash')
return result
except Exception as err:
print('Failed to find version number for: ' + addonpage)
print(err)
return ''
# Wowinterface
def wowinterface(addonpage):
downloadpage = addonpage.replace('info', 'download')
try:
page = requests.get(downloadpage + '/download')
contentString = str(page.content)
indexOfZiploc = contentString.find('Problems with the download? <a href="') + 37 # first char of the url
endQuote = contentString.find('"', indexOfZiploc) # ending quote after the url
return contentString[indexOfZiploc:endQuote]
except Exception:
print('Failed to find downloadable zip file for addon. Skipping...\n')
return ''
def getWowinterfaceVersion(addonpage):
try:
page = requests.get(addonpage)
contentString = str(page.content)
indexOfVer = contentString.find('id="version"') + 22 # first char of the version string
endTag = contentString.find('</div>', indexOfVer) # ending tag after the version string
return contentString[indexOfVer:endTag].strip()
except Exception:
print('Failed to find version number for: ' + addonpage)
return ''