-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_text.py
429 lines (387 loc) · 15.6 KB
/
add_text.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This is a Bot written by Filnik to add a text at the end of the page but above
categories, interwiki and template for the stars of the interwiki (default).
Alternatively it may also add a text at the top of the page.
These command line parameters can be used to specify which pages to work on:
¶ms;
Furthermore, the following command line parameters are supported:
-page Use a page as generator
-talkpage Put the text onto the talk page instead the generated on
-talk
-text Define which text to add. "\n" are interpreted as newlines.
-textfile Define a texfile name which contains the text to add
-summary Define the summary to use
-except Use a regex to check if the text is already in the page
-excepturl Use the html page as text where you want to see if there's
the text, not the wiki-page.
-newimages Add text in the new images
-untagged Add text in the images that don't have any license template
-always If used, the bot won't ask if it should add the text
specified
-up If used, put the text at the top of the page
-noreorder Avoid to reorder cats and interwiki
--- Example ---
1.
# This is a script to add a template to the top of the pages with
# category:catname
# Warning! Put it in one line, otherwise it won't work correctly.
python add_text.py -cat:catname -summary:"Bot: Adding a template"
-text:"{{Something}}" -except:"\{\{([Tt]emplate:|)[Ss]omething" -up
2.
# Command used on it.wikipedia to put the template in the page without any
# category.
# Warning! Put it in one line, otherwise it won't work correctly.
python add_text.py -excepturl:"class='catlinks'>" -uncat
-text:"{{Categorizzare}}" -except:"\{\{([Tt]emplate:|)[Cc]ategorizzare"
-summary:"Bot: Aggiungo template Categorizzare"
--- Credits and Help ---
This script has been written by Botwiki's staff, if you want to help us
or you need some help regarding this script, you can find us here:
* http://botwiki.sno.cc
"""
#
# (C) Filnik, 2007-2010
# (C) Pywikipedia bot team, 2007-2013
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import re, urllib2, urllib
import webbrowser
import codecs
import wikipedia as pywikibot
from pywikibot import i18n
import pagegenerators
import config
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp,
}
class NoEnoughData(pywikibot.Error):
""" Error class for when the user doesn't specified all the data needed """
class NothingFound(pywikibot.Error):
"""
An exception indicating that a regex has return [] instead of results.
"""
# Useful for the untagged function
def pageText(url):
""" Function to load HTML text of a URL """
try:
request = urllib2.Request(url)
request.add_header("User-Agent", pywikibot.useragent)
response = urllib2.urlopen(request)
text = response.read()
response.close()
# When you load to many users, urllib2 can give this error.
except urllib2.HTTPError:
pywikibot.output(u"Server error. Pausing for 10 seconds... " +
time.strftime("%d %b %Y %H:%M:%S (UTC)",
time.gmtime()))
response.close()
time.sleep(10)
return pageText(url)
return text
def untaggedGenerator(untaggedProject, limit=500):
""" Function to get the pages returned by this tool:
http://toolserver.org/~daniel/WikiSense/UntaggedImages.php
"""
lang = untaggedProject.split('.', 1)[0]
project = '.' + untaggedProject.split('.', 1)[1]
URL = 'http://toolserver.org/~daniel/WikiSense/UntaggedImages.php?'
if lang == 'commons':
link = '%swikifam=commons.wikimedia.org&since=-100d&until=&img_user_text=&order=img_timestamp&max=%d&order=img_timestamp&format=html' \
% (URL, limit)
else:
link = '%swikilang=%s&wikifam=%s&order=img_timestamp&max=%d&ofs=0&max=%d' \
% (URL, lang, project, limit, limit)
text = pageText(link)
regexp = r"<td valign='top' title='Name'><a href='http://.*?\.org/w/index\.php\?title=(.*?)'>.*?</a></td>"
results = re.findall(regexp, text)
if results == []:
print link
raise NothingFound(
'Nothing found! Try to use the tool by yourself to be sure that it '
'works!')
else:
for result in results:
yield pywikibot.Page(pywikibot.getSite(), result)
def add_text(page=None, addText=None, summary=None, regexSkip=None,
regexSkipUrl=None, always=False, up=False, putText=True,
oldTextGiven=None, reorderEnabled=True, create=False):
if not addText:
raise NoEnoughData('You have to specify what text you want to add!')
if not summary:
summary = i18n.twtranslate(pywikibot.getSite(), 'add_text-adding',
{'adding': addText[:200]})
# When a page is tagged as "really well written" it has a star in the
# interwiki links. This is a list of all the templates used (in regex
# format) to make the stars appear.
starsList = [
u'bueno',
u'bom interwiki',
u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed',
u'destacado', u'destaca[tu]',
u'enllaç[ _]ad',
u'enllaz[ _]ad',
u'leam[ _]vdc',
u'legătură[ _]a[bcf]',
u'liamm[ _]pub',
u'lien[ _]adq',
u'lien[ _]ba',
u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt',
u'liên[ _]kết[ _]chọn[ _]lọc',
u'ligam[ _]adq',
u'ligoelstara',
u'ligoleginda',
u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km',
u'link[ _]sm', u'linkfa',
u'na[ _]lotura',
u'nasc[ _]ar',
u'tengill[ _][úg]g',
u'ua',
u'yüm yg',
u'רא',
u'وصلة مقالة جيدة',
u'وصلة مقالة مختارة',
]
errorCount = 0
site = pywikibot.getSite()
pathWiki = site.family.nicepath(site.lang)
if putText:
pywikibot.output(u'Loading %s...' % page.title())
if oldTextGiven is None:
try:
text = page.get()
except pywikibot.NoPage:
if create:
pywikibot.output(u"%s doesn't exist, creating it!"
% page.title())
text = u''
else:
pywikibot.output(u"%s doesn't exist, skip!" % page.title())
return (False, False, always)
except pywikibot.IsRedirectPage:
pywikibot.output(u"%s is a redirect, skip!" % page.title())
return (False, False, always)
else:
text = oldTextGiven
# Understand if the bot has to skip the page or not
# In this way you can use both -except and -excepturl
if regexSkipUrl is not None:
url = '%s%s' % (pathWiki, page.urlname())
result = re.findall(regexSkipUrl, site.getUrl(url))
if result != []:
pywikibot.output(
u'''Exception! regex (or word) used with -exceptUrl is in the page. Skip!
Match was: %s''' % result)
return (False, False, always)
if regexSkip is not None:
result = re.findall(regexSkip, text)
if result != []:
pywikibot.output(
u'''Exception! regex (or word) used with -except is in the page. Skip!
Match was: %s''' % result)
return (False, False, always)
# If not up, text put below
if not up:
newtext = text
# Translating the \\n into binary \n
addText = addText.replace('\\n', '\n')
if (reorderEnabled):
# Getting the categories
categoriesInside = pywikibot.getCategoryLinks(newtext, site)
# Deleting the categories
newtext = pywikibot.removeCategoryLinks(newtext, site)
# Getting the interwiki
interwikiInside = pywikibot.getLanguageLinks(newtext, site)
# Removing the interwiki
newtext = pywikibot.removeLanguageLinks(newtext, site)
# Adding the text
newtext += u"\n%s" % addText
# Reputting the categories
newtext = pywikibot.replaceCategoryLinks(newtext,
categoriesInside, site,
True)
# Dealing the stars' issue
allstars = []
starstext = pywikibot.removeDisabledParts(text)
for star in starsList:
regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)'
% star, re.I)
found = regex.findall(starstext)
if found != []:
newtext = regex.sub('', newtext)
allstars += found
if allstars != []:
newtext = newtext.strip() + '\r\n\r\n'
allstars.sort()
for element in allstars:
newtext += '%s\r\n' % element.strip()
# Adding the interwiki
newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside,
site)
else:
newtext += u"\n%s" % addText
else:
newtext = addText + '\n' + text
if putText and text != newtext:
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
pywikibot.showDiff(text, newtext)
# Let's put the changes.
while True:
# If someone load it as module, maybe it's not so useful to put the
# text in the page
if putText:
if not always:
choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'All', 'open in Browser'],
['y', 'n', 'a', 'b'], 'n')
if choice == 'a':
always = True
elif choice == 'n':
return (False, False, always)
elif choice == 'b':
webbrowser.open("http://%s%s" % (
page.site().hostname(),
page.site().nice_get_address(page.title())
))
pywikibot.input("Press Enter when finished in browser.")
if always or choice == 'y':
try:
if always:
page.put(newtext, summary,
minorEdit=page.namespace() != 3)
else:
page.put_async(newtext, summary,
minorEdit=page.namespace() != 3)
except pywikibot.EditConflict:
pywikibot.output(u'Edit conflict! skip!')
return (False, False, always)
except pywikibot.ServerError:
errorCount += 1
if errorCount < 5:
pywikibot.output(u'Server Error! Wait..')
time.sleep(5)
continue
else:
raise pywikibot.ServerError(u'Fifth Server Error!')
except pywikibot.SpamfilterError, e:
pywikibot.output(
u'Cannot change %s because of blacklist entry %s'
% (page.title(), e.url))
return (False, False, always)
except pywikibot.PageNotSaved, error:
pywikibot.output(u'Error putting page: %s' % error.args)
return (False, False, always)
except pywikibot.LockedPage:
pywikibot.output(u'Skipping %s (locked page)'
% page.title())
return (False, False, always)
else:
# Break only if the errors are one after the other...
errorCount = 0
return (True, True, always)
else:
return (text, newtext, always)
def main():
# If none, the var is setted only for check purpose.
summary = None
addText = None
regexSkip = None
regexSkipUrl = None
generator = None
always = False
textfile = None
talkPage = False
reorderEnabled = True
namespaces = []
# Load a lot of default generators
genFactory = pagegenerators.GeneratorFactory()
# Put the text above or below the text?
up = False
# Loading the arguments
for arg in pywikibot.handleArgs():
if arg.startswith('-textfile'):
if len(arg) == 9:
textfile = pywikibot.input(
u'Which textfile do you want to add?')
else:
textfile = arg[10:]
elif arg.startswith('-text'):
if len(arg) == 5:
addText = pywikibot.input(u'What text do you want to add?')
else:
addText = arg[6:]
elif arg.startswith('-summary'):
if len(arg) == 8:
summary = pywikibot.input(u'What summary do you want to use?')
else:
summary = arg[9:]
elif arg.startswith('-page'):
if len(arg) == 5:
generator = [pywikibot.Page(
pywikibot.getSite(),
pywikibot.input(u'What page do you want to use?'))]
else:
generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])]
elif arg.startswith('-excepturl'):
if len(arg) == 10:
regexSkipUrl = pywikibot.input(u'What text should I skip?')
else:
regexSkipUrl = arg[11:]
elif arg.startswith('-except'):
if len(arg) == 7:
regexSkip = pywikibot.input(u'What text should I skip?')
else:
regexSkip = arg[8:]
elif arg.startswith('-untagged'):
if len(arg) == 9:
untaggedProject = pywikibot.input(
u'What project do you want to use?')
else:
untaggedProject = arg[10:]
generator = untaggedGenerator(untaggedProject)
elif arg == '-up':
up = True
elif arg == '-noreorder':
reorderEnabled = False
elif arg == '-always':
always = True
elif arg == '-talk' or arg == '-talkpage':
talkPage = True
else:
genFactory.handleArg(arg)
if textfile and not addText:
f = codecs.open(textfile, 'r', config.textfile_encoding)
addText = f.read()
f.close()
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
raise NoEnoughData(
'You have to specify the generator you want to use for the script!')
if talkPage:
generator = pagegenerators.PageWithTalkPageGenerator(generator)
site = pywikibot.getSite()
for namespace in site.namespaces():
index = site.getNamespaceIndex(namespace)
if index % 2 == 1 and index > 0:
namespaces += [index]
generator = pagegenerators.NamespaceFilterPageGenerator(
generator, namespaces)
for page in generator:
(text, newtext, always) = add_text(page, addText, summary, regexSkip,
regexSkipUrl, always, up, True,
reorderEnabled=reorderEnabled,
create=talkPage)
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()