This repository has been archived by the owner on Apr 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
archivebot.py
670 lines (585 loc) · 25.2 KB
/
archivebot.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
archivebot.py - discussion page archiving bot.
usage:
python archivebot.py [OPTIONS] TEMPLATE_PAGE
Bot examines backlinks (Special:Whatlinkshere) to TEMPLATE_PAGE.
Then goes through all pages (unless a specific page specified using options)
and archives old discussions. This is done by breaking a page into threads,
then scanning each thread for timestamps. Threads older than a specified
treshold are then moved to another page (the archive), which can be named
either basing on the thread's name or then name can contain a counter which
will be incremented when the archive reaches a certain size.
Trancluded template may contain the following parameters:
{{TEMPLATE_PAGE
|archive =
|algo =
|counter =
|maxarchivesize =
|minthreadsleft =
|minthreadstoarchive =
|archiveheader =
|key =
}}
Meanings of parameters are:
archive Name of the page to which archived threads will be put.
Must be a subpage of the current page. Variables are
supported.
algo specifies the maximum age of a thread. Must be in the form
old(<delay>) where <delay> specifies the age in hours or
days like 24h or 5d.
Default ist old(24h)
counter The current value of a counter which could be assigned as
variable. Will be actualized by bot. Initial value is 1.
maxarchivesize The maximum archive size before incrementing the counter.
Value can be given with appending letter like K or M which
indicates KByte or MByte. Default value is 1000M.
minthreadsleft Minimum number of threads that should be left on a page.
Default value is 5.
minthreadstoarchive The minimum number of threads to archive at once. Default
value is 2.
archiveheader Content that will be put on new archive pages as the
header. This parameter supports the use of variables.
Default value is {{talkarchive}}
key A secret key that (if valid) allows archives to not be
subpages of the page being archived.
Options:
-h, --help show this help message and exit
-f FILE, --file=FILE load list of pages from FILE
-p PAGE, --page=PAGE archive a single PAGE
-n NAMESPACE, --namespace=NAMESPACE
only archive pages from a given namespace
-s SALT, --salt=SALT specify salt
-F, --force override security options
-c PAGE, --calc=PAGE calculate key for PAGE and exit
-l LOCALE, --locale=LOCALE
switch to locale LOCALE
-L LANG, --lang=LANG set the language code to work on
"""
#
# (C) Misza13, 2006-2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import wikipedia, pagegenerators, query
Site = wikipedia.getSite()
import os, re, time, locale, traceback, string, urllib
try: #Get a constructor for the MD5 hash object
import hashlib
new_hash = hashlib.md5
except ImportError: #Old python?
import md5
new_hash = md5.md5
language = Site.language()
messages = {
'_default': {
'ArchiveFull': u'(ARCHIVE FULL)',
'InitialArchiveHeader': u'{{talkarchive}}',
'PageSummary': u'Archiving %(count)d thread(s) (%(why)s) to %(archives)s.',
'ArchiveSummary': u'Archiving %(count)d thread(s) from [[%(from)s]].',
'OlderThanSummary': u'older than',
},
'ar': {
'ArchiveFull': u'(الأرشيف ممتلئ)',
'InitialArchiveHeader': u'{{أرشيف نقاش}}',
'PageSummary': u'أرشفة %(count)d قسم(أقسام) (%(why)s) إلى %(archives)s.',
'ArchiveSummary': u'أرشفة %(count)d قسم(أقسام) من [[%(from)s]].',
'OlderThanSummary': u'أقدم من',
},
'pl': {
'ArchiveFull': u'(ARCHIWUM PEŁNE)',
'InitialArchiveHeader': u'{{archiwum}}',
'PageSummary': u'Archiwizacja %(count)d wątków (%(why)s) do %(archives)s.',
'ArchiveSummary': u'Archiwizacja %(count)d wątków z [[%(from)s]].',
'OlderThanSummary': u'starsze niż',
},
'hu': {
'ArchiveFull': u'(ARCHÍVUM BETELT)',
'InitialArchiveHeader': u'{{archív lap}}',
'PageSummary': u'%(count)d szakasz archiválása (%(why)s) a(z) %(archives)s lapra.',
'ArchiveSummary': u'%(count)d szakasz archiválása a(z) [[%(from)s]] lapról.',
'OlderThanSummary': u'régebbi, mint',
},
'ksh': {
'ArchiveFull': u'(DAT ASCHIHV ES VOLL)',
'InitialArchiveHeader': u'{{Sigg weed aschiveet}}',
'PageSummary': u'* %(count)d Schtöe) (%(why)s) en et Aschif %(archives)s jedonn.',
'ArchiveSummary': u'* %(count)d Schtö) vun [[%(from)s]] noh heh en et Aschihf jedonn.',
'OlderThanSummary': u'äer wi',
},
'no': {
'ArchiveFull': u'(ARKIV FULLT)',
'InitialArchiveHeader': u'{{arkiv}}',
'PageSummary': u'Arkiverer %(count)d tråder (%(why)s) til %(archives)s.',
'ArchiveSummary': u'Arkiverer %(count)d tråder fra [[%(from)s]].',
'OlderThanSummary': u'eldre enn',
},
'nn': {
'ArchiveFull': u'(ARKIV FULLT)',
'InitialArchiveHeader': u'{{arkiv}}',
'PageSummary': u'Arkiverer %(count)d trådar (%(why)s) til %(archives)s.',
'ArchiveSummary': u'Arkiverer %(count)d trådar frå [[%(from)s]].',
'OlderThanSummary': u'eldre enn',
},
# fix below
'pt': {
'ArchiveFull': u'(ARQUIVO COMPLETO)',
'InitialArchiveHeader': u'{{talkarchive}}',
'PageSummary': u'Arquivando %(count)d thread(s) (%(why)s) to %(archives)s.',
'ArchiveSummary': u'Archiving %(count)d thread(s) from [[%(from)s]].',
'OlderThanSummary': u'older than',
},
'sv': {
'ArchiveFull': u'(ARKIV FULLT)',
'InitialArchiveHeader': u'{{arkiv}}',
'PageSummary': u'Arkiverar %(count)d trådar (%(why)s) till %(archives)s.',
'ArchiveSummary': u'Arkiverar %(count)d trådar från [[%(from)s]].',
'OlderThanSummary': u'äldre än',
},
'fi': {
'ArchiveFull' : u'(ARCHIVE FULL)',
'InitialArchiveHeader': u'{{arkisto}}',
'PageSummary': u'Arkistoidaan %(count)d keskustelua (%(why)s) %(archives)s arkistoon.',
'ArchiveSummary': u'Arkistoidaan %(count)d keskutelua sivulta [[%(from)s]].',
'OlderThanSummary': u'vanhempi kuin',
},
}
def message(key, lang=Site.language()):
if not lang in messages:
lang = '_default'
return messages[lang][key]
class MalformedConfigError(wikipedia.Error):
"""There is an error in the configuration template."""
class MissingConfigError(wikipedia.Error):
"""The config is missing in the header (either it's in one of the threads
or transcluded from another page)."""
class AlgorithmError(MalformedConfigError):
"""Invalid specification of archiving algorithm."""
class ArchiveSecurityError(wikipedia.Error):
"""Archive is not a subpage of page being archived and key not specified
(or incorrect)."""
def str2time(str):
"""Accepts a string defining a time period:
7d - 7 days
36h - 36 hours
Returns the corresponding time, measured in seconds."""
if str[-1] == 'd':
return int(str[:-1])*24*3600
elif str[-1] == 'h':
return int(str[:-1])*3600
else:
return int(str)
def str2size(str):
"""Accepts a string defining a size:
1337 - 1337 bytes
150K - 150 kilobytes
2M - 2 megabytes
Returns a tuple (size,unit), where size is an integer and unit is
'B' (bytes) or 'T' (threads)."""
if str[-1] in string.digits: #TODO: de-uglify
return (int(str),'B')
elif str[-1] in ['K', 'k']:
return (int(str[:-1])*1024,'B')
elif str[-1] == 'M':
return (int(str[:-1])*1024*1024,'B')
elif str[-1] == 'T':
return (int(str[:-1]),'T')
else:
return (int(str[:-1])*1024,'B')
def int2month(num):
"""Returns the locale's full name of month 'num' (1-12)."""
if hasattr(locale, 'nl_langinfo'):
return locale.nl_langinfo(locale.MON_1+num-1).decode('utf-8')
Months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
return Site.mediawiki_message(Months[num-1])
def int2month_short(num):
"""Returns the locale's abbreviated name of month 'num' (1-12)."""
if hasattr(locale, 'nl_langinfo'):
return locale.nl_langinfo(locale.ABMON_1+num-1).replace('\xa0', '').decode('utf-8')
Months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
return Site.mediawiki_message(Months[num-1])
def txt2timestamp(txt, format):
"""Attempts to convert the timestamp 'txt' according to given 'format'.
On success, returns the time tuple; on failure, returns None."""
#print txt, format
try:
return time.strptime(txt,format)
except ValueError:
try:
return time.strptime(txt.encode('utf8'),format)
except:
pass
return None
def generateTransclusions(Site, template, namespaces=[], eicontinue=''):
qdata = {
'action' : 'query',
'list' : 'embeddedin',
'eititle' : template,
'einamespace' : '|'.join(namespaces),
'eilimit' : '100',
'format' : 'json',
}
if eicontinue:
qdata['eicontinue'] = eicontinue
wikipedia.output(u'Fetching template transclusions...')
response, result = query.GetData(qdata, Site, back_response = True)
for page_d in result['query']['embeddedin']:
yield wikipedia.Page(Site, page_d['title'])
if 'query-continue' in result:
eicontinue = result['query-continue']['embeddedin']['eicontinue']
for page in generateTransclusions(Site, template, namespaces, eicontinue):
yield page
class DiscussionThread(object):
"""An object representing a discussion thread on a page, that is something of the form:
== Title of thread ==
Thread content here. ~~~~
:Reply, etc. ~~~~
"""
def __init__(self, title):
self.title = title
self.content = ""
self.timestamp = None
def __repr__(self):
return '%s("%s",%d bytes)' % (self.__class__.__name__,self.title,len(self.content))
def feedLine(self, line):
if not self.content and not line:
return
self.content += line + '\n'
#Update timestamp
# nnwiki:
# 19:42, 25 mars 2008 (CET)
# enwiki
# 16:36, 30 March 2008 (UTC)
# huwiki
# 2007. december 8., 13:42 (CET)
TM = re.search(r'(\d\d):(\d\d), (\d\d?) (\S+) (\d\d\d\d) \(.*?\)', line)
if not TM:
TM = re.search(r'(\d\d):(\d\d), (\S+) (\d\d?), (\d\d\d\d) \(.*?\)', line)
if not TM:
TM = re.search(r'(\d{4})\. (\S+) (\d\d?)\., (\d\d:\d\d) \(.*?\)', line)
# 18. apr 2006 kl.18:39 (UTC)
# 4. nov 2006 kl. 20:46 (CET)
if not TM:
TM = re.search(r'(\d\d?)\. (\S+) (\d\d\d\d) kl\.\W*(\d\d):(\d\d) \(.*?\)', line)
#3. joulukuuta 2008 kello 16.26 (EET)
if not TM:
TM = re.search(r'(\d\d?)\. (\S+) (\d\d\d\d) kello \W*(\d\d).(\d\d) \(.*?\)', line)
if not TM:
# 14:23, 12. Jan. 2009 (UTC)
pat = re.compile(r'(\d\d):(\d\d), (\d\d?)\. (\S+)\.? (\d\d\d\d) \(UTC\)')
TM = pat.search(line)
if TM:
# wikipedia.output(TM)
TIME = txt2timestamp(TM.group(0),"%d. %b %Y kl. %H:%M (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0), "%Y. %B %d., %H:%M (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%d. %b %Y kl.%H:%M (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %d %B %Y (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %d %b %Y (%Z)")
if not TIME:
TIME = txt2timestamp(re.sub(' *\([^ ]+\) *','',TM.group(0)),"%H:%M, %d %b %Y")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %b %d %Y (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %B %d %Y (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %b %d, %Y (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %B %d, %Y (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%d. %Bta %Y kello %H.%M (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0),"%H:%M, %d. %b. %Y (%Z)")
if TIME:
self.timestamp = max(self.timestamp,time.mktime(TIME))
# wikipedia.output(u'Time to be parsed: %s' % TM.group(0))
# wikipedia.output(u'Parsed time: %s' % TIME)
# wikipedia.output(u'Newest timestamp in thread: %s' % TIME)
def size(self):
return len(self.title) + len(self.content) + 12
def toText(self):
return "== " + self.title + ' ==\n\n' + self.content
def shouldBeArchived(self,Archiver):
algo = Archiver.get('algo')
reT = re.search(r'^old\((.*)\)$',algo)
if reT:
if not self.timestamp:
return ''
#TODO: handle this:
#return 'unsigned'
maxage = str2time(reT.group(1))
if self.timestamp + maxage < time.time():
return message('OlderThanSummary') + ' ' + reT.group(1)
return ''
class DiscussionPage(object):
"""A class that represents a single discussion page as well as an archive page.
Feed threads to it and run an update() afterwards."""
#TODO: Make it a subclass of wikipedia.Page
def __init__(self, title, archiver, vars=None):
self.title = title
self.threads = []
self.Page = wikipedia.Page(Site,self.title)
self.full = False
self.archiver = archiver
self.vars = vars
try:
self.loadPage()
except wikipedia.NoPage:
self.header = archiver.get('archiveheader',message('InitialArchiveHeader'))
if self.vars:
self.header = self.header % self.vars
def loadPage(self):
"""Loads the page to be archived and breaks it up into threads."""
self.header = ''
self.threads = []
self.archives = {}
self.archivedThreads = 0
lines = self.Page.get().split('\n')
state = 0 #Reading header
curThread = None
for line in lines:
threadHeader = re.search('^== *([^=].*?) *== *$',line)
if threadHeader:
state = 1 #Reading threads now
if curThread:
self.threads.append(curThread)
curThread = DiscussionThread(threadHeader.group(1))
else:
if state == 1:
curThread.feedLine(line)
else:
self.header += line + '\n'
if curThread:
self.threads.append(curThread)
def feedThread(self, thread, maxArchiveSize=(250*1024,'B')):
self.threads.append(thread)
self.archivedThreads += 1
if maxArchiveSize[1] == 'B':
if self.size() >= maxArchiveSize[0]:
self.full = True
elif maxArchiveSize[1] == 'T':
if len(self.threads) >= maxArchiveSize[0]:
self.full = True
return self.full
def size(self):
return len(self.header) + sum([t.size() for t in self.threads])
def update(self, summary, sortThreads = False):
if sortThreads:
wikipedia.output(u'Sorting threads...')
self.threads.sort(key = lambda t: t.timestamp)
newtext = re.sub('\n*$','\n\n',self.header) #Fix trailing newlines
for t in self.threads:
newtext += t.toText()
if self.full:
summary += ' ' + message('ArchiveFull')
self.Page.put(newtext, minorEdit=True, comment=summary)
class PageArchiver(object):
"""A class that encapsulates all archiving methods.
__init__ expects a wikipedia.Page object.
Execute by running the .run() method."""
algo = 'none'
pageSummary = message('PageSummary')
archiveSummary = message('ArchiveSummary')
def __init__(self, Page, tpl, salt, force=False):
self.attributes = {
'algo' : ['old(24h)',False],
'archive' : ['',False],
'maxarchivesize' : ['1000M',False],
'counter' : ['1',False],
'key' : ['',False],
}
self.tpl = tpl
self.salt = salt
self.force = force
self.Page = DiscussionPage(Page.title(),self)
self.loadConfig()
self.commentParams = {
'from' : self.Page.title,
}
self.archives = {}
self.archivedThreads = 0
def get(self, attr, default=''):
return self.attributes.get(attr,[default])[0]
def set(self, attr, value, out=True):
self.attributes[attr] = [value, out]
def saveables(self):
return [a for a in self.attributes if self.attributes[a][1] and a != 'maxage']
def attr2text(self):
return '{{%s\n%s\n}}' % (self.tpl,'\n'.join(['|%s = %s'%(a,self.get(a)) for a in self.saveables()]))
def key_ok(self):
s = new_hash()
s.update(self.salt+'\n')
s.update(self.Page.title.encode('utf8')+'\n')
return self.get('key') == s.hexdigest()
def loadConfig(self):
hdrlines = self.Page.header.split('\n')
# wikipedia.output(u'Looking for: %s' % self.tpl)
mode = 0
for line in hdrlines:
if mode == 0 and re.search('{{'+self.tpl,line):
mode = 1
continue
if mode == 1 and re.match('}}',line):
break
attRE = re.search(r'^\| *(\w+) *= *(.*?) *$',line)
if mode == 1 and attRE:
self.set(attRE.group(1),attRE.group(2))
continue
if mode == 0 or not self.get('algo',''):
raise MissingConfigError
#Last minute fix:
self.set('archive', self.get('archive').replace('_',' '), True)
def feedArchive(self, archive, thread, maxArchiveSize, vars=None):
"""Feed the thread to one of the archives.
If it doesn't exist yet, create it.
If archive name is an empty string (or None), discard the thread (/dev/null).
Also checks for security violations."""
if not archive:
return
if not self.force and not self.Page.title+'/' == archive[:len(self.Page.title)+1] and not self.key_ok():
raise ArchiveSecurityError
if not archive in self.archives:
self.archives[archive] = DiscussionPage(archive,self,vars)
return self.archives[archive].feedThread(thread,maxArchiveSize)
def analyzePage(self):
maxArchSize = str2size(self.get('maxarchivesize'))
archCounter = int(self.get('counter','1'))
oldthreads = self.Page.threads
self.Page.threads = []
T = time.mktime(time.gmtime())
whys = []
for t in oldthreads:
if len(oldthreads) - self.archivedThreads <= int(self.get('minthreadsleft',5)):
self.Page.threads.append(t)
continue #Because there's too little threads left.
#TODO: Make an option so that unstamped (unsigned) posts get archived.
why = t.shouldBeArchived(self)
if why:
archive = self.get('archive')
TStuple = time.gmtime(t.timestamp)
vars = {
'counter' : archCounter,
'year' : TStuple[0],
'month' : TStuple[1],
'monthname' : int2month(TStuple[1]),
'monthnameshort' : int2month_short(TStuple[1]),
'week' : int(time.strftime('%W',TStuple)),
}
archive = archive % vars
if self.feedArchive(archive,t,maxArchSize,vars):
archCounter += 1
self.set('counter',str(archCounter))
whys.append(why)
self.archivedThreads += 1
else:
self.Page.threads.append(t)
return set(whys)
def run(self):
if not self.Page.Page.botMayEdit(Site.username):
return
whys = self.analyzePage()
if self.archivedThreads < int(self.get('minthreadstoarchive',2)):
return #We might not want to archive a measly few threads (lowers edit frequency)
if whys:
#Save the archives first (so that bugs don't cause a loss of data)
for a in sorted(self.archives.keys()):
self.commentParams['count'] = self.archives[a].archivedThreads
comment = self.archiveSummary % self.commentParams
self.archives[a].update(comment)
#Save the page itself
rx = re.compile('{{'+self.tpl+'\n.*?\n}}',re.DOTALL)
self.Page.header = rx.sub(self.attr2text(),self.Page.header)
self.commentParams['count'] = self.archivedThreads
self.commentParams['archives'] = ', '.join(['[['+a.title+']]' for a in self.archives.values()])
if not self.commentParams['archives']:
self.commentParams['archives'] = '/dev/null'
self.commentParams['why'] = ', '.join(whys)
comment = self.pageSummary % self.commentParams
self.Page.update(comment)
def main():
global Site, language
from optparse import OptionParser
parser = OptionParser(usage='usage: %prog [options] [LINKPAGE(s)]')
parser.add_option('-f', '--file', dest='filename',
help='load list of pages from FILE', metavar='FILE')
parser.add_option('-p', '--page', dest='pagename',
help='archive a single PAGE', metavar='PAGE')
parser.add_option('-n', '--namespace', dest='namespace', type='int',
help='only archive pages from a given namespace')
parser.add_option('-s', '--salt', dest='salt',
help='specify salt')
parser.add_option('-F', '--force', action='store_true', dest='force',
help='override security options')
parser.add_option('-c', '--calc', dest='calc',
help='calculate key for PAGE and exit', metavar='PAGE')
parser.add_option('-l', '--locale', dest='locale',
help='switch to locale LOCALE', metavar='LOCALE')
parser.add_option('-L', '--lang', dest='lang',
help='current language code', metavar='lang')
parser.add_option('-T', '--timezone', dest='timezone',
help='switch timezone to TIMEZONE', metavar='TIMEZONE')
(options, args) = parser.parse_args()
if options.locale:
locale.setlocale(locale.LC_TIME,options.locale) #Required for english month names
if options.timezone:
os.environ['TZ'] = options.timezone
#Or use the preset value
if hasattr(time, 'tzset'):
time.tzset()
if options.calc:
if not options.salt:
parser.error('you must specify a salt to calculate a key')
s = new_hash()
s.update(options.salt+'\n')
s.update(options.calc+'\n')
wikipedia.output(u'key = ' + s.hexdigest())
return
if options.salt:
salt = options.salt
else:
salt = ''
if options.force:
force = True
else:
force = False
if options.lang:
Site = wikipedia.getSite(options.lang)
language = Site.language()
if wikipedia.debug: print Site
for a in args:
pagelist = []
if not options.filename and not options.pagename:
#for pg in wikipedia.Page(Site,a).getReferences(follow_redirects=False,onlyTemplateInclusion=True):
if not options.namespace == None:
ns = [str(options.namespace)]
else:
ns = []
for pg in generateTransclusions(Site, a, ns):
pagelist.append(pg)
if options.filename:
for pg in file(options.filename,'r').readlines():
pagelist.append(wikipedia.Page(Site,pg))
if options.pagename:
pagelist.append(wikipedia.Page(Site,options.pagename))
pagelist = sorted(pagelist)
#if not options.namespace == None:
# pagelist = [pg for pg in pagelist if pg.namespace()==options.namespace]
for pg in pagelist:
try: #Catching exceptions, so that errors in one page do not bail out the entire process
Archiver = PageArchiver(pg, a, salt, force)
Archiver.run()
time.sleep(10)
except:
wikipedia.output(u'Error occured while processing page %s' % pg.aslink(True))
traceback.print_exc()
if __name__ == '__main__':
try:
main()
finally:
wikipedia.stopme()