forked from xbmc/xbmc.wiki.bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addons.py
263 lines (234 loc) · 8.86 KB
/
addons.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
# -*- coding: utf-8 -*-
"""
Reads addons.xml and creates/updates addon pages.
Usage: python pwb.py addons.py [repo]
where repo (optional) can be one of these:
* Gotham
* Helix
* Isengard
* Jarvis
* Krypton
* Leia (default)
"""
#
# Copyright (C) 2005-2015 Team Kodi
# http://kodi.tv
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XBMC; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
import pywikibot
import sys, urllib2, re, zlib, os
from BeautifulSoup import BeautifulStoneSoup # For processing XML
repoUrls={'Gotham':u'http://mirrors.kodi.tv/addons/gotham/',
'Helix':u'http://mirrors.kodi.tv/addons/helix/',
'Isengard':u'http://mirrors.kodi.tv/addons/isengard/',
'Jarvis':u'http://mirrors.kodi.tv/addons/jarvis/',
'Krypton':u'http://mirrors.kodi.tv/addons/krypton/',
'Leia':u'http://mirrors.kodi.tv/addons/leia/',
}
def UpdateAddons(*args):
site = pywikibot.getSite()
try:
repoUrl = repoUrls[pywikibot.handleArgs(*args)[0]]
except:
repoUrl = repoUrls['Leia']
pywikibot.output(u'Repo URL: ' + repoUrl)
try:
soup = importAddonXML(repoUrl + 'addons.xml.gz')
except urllib2.HTTPError:
soup = importAddonXML(repoUrl + 'addons.xml')
for addon in soup.addons:
newtext = None
addontext = None
oldtext = None
iconUrl = None
# Extract Add-on details from xml
addon_data = extractAddonData(addon)
# Which Wiki page are we looking at?
pagename = 'Add-on:' + addon_data['name']
#pagename = 'Sandbox'
# Get content of wiki page
page = pywikibot.Page(site, pagename)
try:
oldtext = page.get(force = False, get_redirect = True, throttle = True, sysop = False, change_edit_time = True)
except pywikibot.NoPage:
oldtext = ''
pywikibot.output(u'%s not found' % pagename)
except pywikibot.IsRedirectPage:
pywikibot.output(u'%s is a redirect!' % pagename)
except pywikibot.Error: # third exception, take the problem and print
pywikibot.output(u"Some Error, skipping..")
continue
if addon_data['icon url'] != u"":
iconUrl = repoUrl + addon_data['icon url']
else:
iconUrl = u""
# Create Addon template
try:
addontext = ("{{Addon \n|Name=" + addon_data['name'] +
"\n|provider-name="+ addon_data['provider-name'] +
"\n|ID=" + addon_data['id'] +
"\n|latest-version=" + addon_data['version']+
"\n|extension point=" + addon_data['extension point'] +
"\n|provides="+ addon_data['provides'] +
"\n|Summary=" + addon_data['summary'] +
"\n|Description=" + addon_data['description'] +
"\n|Platform=" + addon_data['platform'] +
"\n|Language=" + addon_data['language'] +
"\n|License=" + addon_data['license'] +
"\n|Forum=" + addon_data['forum'] +
"\n|Website=" + addon_data['website'] +
"\n|Source=" + addon_data['source'] +
"\n|Email=" + addon_data['email'] +
"\n|broken=" + addon_data['broken'] +
"\n|icon url=" + iconUrl + "}}")
except:
pywikibot.output(u"Some Error creating Addons String, skipping..")
continue
# Replace existing Addon template
templateRegex = re.compile(r'\{\{ *(' + ':|'+ \
r':|[mM][sS][gG]:)?Addon' + \
r'(?P<parameters>\s*\|.+?|) *}}',
re.DOTALL)
replacedText = re.subn(templateRegex, addontext, oldtext)
if replacedText[1] > 0:
newtext = replacedText[0]
else:
newtext = addontext + "\n" + oldtext
# print newtext (debug)
# pywikibot.output(newtext)
# Push new page to wiki
try:
page.put(newtext, comment='Addon-Bot Update', watchArticle = None, minorEdit = True)
except pywikibot.LockedPage:
pywikibot.output(u"Page %s is locked; skipping." % page.aslink())
except pywikibot.EditConflict:
pywikibot.output(u'Skipping %s because of edit conflict' % (page.title()))
except pywikibot.SpamfilterError, error:
pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
except:
pywikibot.output(u"Some Error writing to wiki page, skipping..")
continue
# break here for testing purposes
# break
# Converts soup element into a tuple
# Gets english summaries/descriptions (if language specified)
# data: Soup addon element from repo addons.xml
def extractAddonData(data):
addon = {'name' : data['name']}
addon['id'] = data['id']
addon['version'] = data['version']
try:
addon['extension point'] = data.extension['point']
except:
addon['extension point'] = ""
try:
addon['provider-name'] = u""+data['provider-name'].replace('|',' & ')
except:
addon['provider-name'] = u""
if addon['extension point'] == 'xbmc.python.pluginsource' or addon['extension point'] == 'xbmc.python.script':
try:
addon['provides'] = u""+data.find('extension',library=True).provides.string
except:
addon['provides'] = u""
else:
addon['provides'] = u""
try:
addon['summary'] = u""+data('summary', lang="en_GB")[0].string
except:
try:
addon['summary'] = u""+data('summary', lang="en")[0].string
except:
try:
addon['summary'] = u""+data.summary.string
except:
addon['summary'] = u""
try:
addon['description'] = u""+data('description', lang="en_GB")[0].string
except:
try:
addon['description'] = u""+data('description', lang="en")[0].string
except:
try:
addon['description'] = u""+data.description.string
except:
addon['description'] = u""
try:
addon['platform'] = u""+data.platform.string
except:
addon['platform'] = u""
try:
addon['language'] = u""+data.language.string
except:
addon['language'] = u""
try:
addon['license'] = u""+data.license.string
except:
addon['license'] = u""
try:
addon['forum'] = u""+data.forum.string
except:
addon['forum'] = u""
try:
addon['website'] = u""+data.website.string
except:
addon['website'] = u""
try:
addon['source'] = u""+data.source.string
except:
addon['source'] = u""
try:
addon['email'] = u""+data.email.string
except:
addon['email'] = u""
try:
addon['broken'] = u""+data.broken.string
except:
addon['broken'] = u""
try:
addon['path'] = u""+os.path.split(data.path.string)[0]
except:
addon['path'] = u""
try:
if data.noicon.string == u"true":
addon['noicon'] = True
else:
addon['noicon'] = False
except:
addon['noicon'] = False
if addon['noicon']:
addon['icon url'] = u""
else:
try:
addon['icon url'] = u""+addon['path']+'/'+data.assets.icon.string
except:
addon['icon url'] = u''+addon['path']+'/icon.png'
addon['summary'] = re.sub("\[CR\]","\\n",addon['summary'])
addon['description'] = re.sub("\[CR\]","\\n",addon['description'])
return addon
# Download addons.xml and return Soup xml class
def importAddonXML(url):
headers = {'User-Agent':'Kodi-AddonBot'}
req = urllib2.Request(url, None, headers)
page = urllib2.urlopen(req)
if page.headers.get('Content-Type').find('gzip') >= 0 or page.headers.get('Content-Type').find('application/octet-stream') >= 0:
d = zlib.decompressobj(16+zlib.MAX_WBITS)
page = d.decompress(page.read())
return BeautifulStoneSoup(page)
if __name__ == '__main__':
try:
UpdateAddons()
finally:
pywikibot.stopme()