-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms-psb.py
640 lines (541 loc) · 23 KB
/
ms-psb.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
An incomplete sample script by masti for creating statistics/listings pages
This is not a complete bot; rather, it is a template from which simple
bots can be made. You can rename it to mybot.py, then edit it in
whatever way you want.
Use global -simulate option for test purposes. No changes to live wiki
will be done.
This bot creates a pages with links to tennis players.
Call:
python pwb.py masti/ms-psb.py -page:"Wikipedysta:PuchaczTrado/PSB" -maxlines:10000 -summary:"Bot uaktualnia tabelę"
python pwb.py masti/ms-psb.py -page:"Wikipedysta:mastiBot/PSB/Tabela" -maxlines:10000 -renew -summary:"Bot uaktualnia tabelę"
The following parameters are supported:
¶ms;
-always If used, the bot won't ask if it should file the message
onto user talk page.
-outpage Results page; otherwise "Wikipedysta:mastiBot/test" is used
-maxlines Max number of entries before new subpage is created; default 1000
-text: Use this text to be added; otherwise 'Test' is used
-replace: Dont add text but replace it
-top Place additional text on top of the page
-summary: Set the action summary message for the edit.
"""
#
# (C) Pywikibot team, 2006-2016
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals
__version__ = '$Id: c1795dd2fb2de670c0b4bddb289ea9d13b1e9b3f $'
#
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot)
from pywikibot.tools import issue_deprecation_warning
import re
import urllib3
import datetime
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
actionCounters = {
'total':0,
'blue':0,
'red':0,
'redirs':0,
'disambigs':0
}
class Person(object):
#Class to describe person from list
def __init__(self):
self.title = None
self.dob = '' #Date of birth
self.dod = '' #Date of death
self.dobprecision = 11 #date precision for birth
self.dodprecision = 11 #date precision for death
self.occupation = [] #list of occupations
self.instanceof = None
self.wditem = ''
self.disambig = False
self.description = ''
self.sex = None
self.wdexists = False
self.link = None
self.comment = None
def personPrint(self):
pywikibot.output('Link:%s' % self.link)
pywikibot.output('Title:%s' % self.title)
pywikibot.output('Wikidata exists:%s' % self.wdexists)
if self.wdexists:
pywikibot.output('Wikidata item:%s' % self.wditem)
pywikibot.output('Is disambig:%s' % self.disambig)
pywikibot.output('Is instance of:%s' % self.instanceof)
pywikibot.output('Date of birth:%s' % self.dob)
pywikibot.output('Date of birth precision:%s' % self.precision(self.dobprecision))
pywikibot.output('Date of death:%s' % self.dod)
pywikibot.output('Date of death precision:%s' % self.precision(self.dodprecision))
pywikibot.output('Sex:%s' % self.sex)
try:
pywikibot.output('Occupation:%s' % ",".join(self.occupation))
except:
pywikibot.output('Occupation: ERROR')
pywikibot.output('Description:%s' % self.description)
pywikibot.output('Comment:%s' % self.comment)
def WDexists(self):
return(self.wdexists)
def isDisambig(self):
return(self.disambig)
def whatIs(self):
if self.sex:
return(self.sex)
elif self.disambig:
return('<strona ujednoznaczniająca>')
elif self.instanceof:
return(self.instanceof)
else:
return('<brak danych>')
def DoB(self):
return(self.dob)
def DoD(self):
return(self.dod)
def Occupation(self):
return(", ".join(self.occupation))
def centuryDecade(self,date):
#return decade of century i.e. 'lata 20. 20 wieku'
if date > 0:
return(pywikibot.date.formats['DecadeAD']['pl'](date))
else:
return(pywikibot.date.formats['DecadeBC']['pl'](date))
def centuryFromDate(self,date):
#return century from give wbTime
year = date.year
if year % 100:
return(int(1+year/100))
else:
return(int(year/100))
def millenniumFromDate(self,date):
#return century from give wbTime
year = date.year
if year % 1000:
return(int(1+year/1000))
else:
return(int(year/1000))
def millennium(self,date):
#return millennium i.e. '20 wieku'
mill = self.millenniumFromDate(date)
if mill > 0:
return('%i tysiąclecie' % mill)
else:
return('%i tysiąclecie p.n.e.' % -1*mill)
def century(self,date):
#return century i.e. '20 wieku'
if date > 0:
return(pywikibot.date.formats['CenturyAD']['pl'](self.centuryFromDate(date)))
else:
return(pywikibot.date.formats['CenturyBC']['pl'](self.centuryFromDate(date)))
def formatDate(self,date,maxPrecision=None):
#return date formatted according to date.precision
#pywikibot.output('Format Date:%i' % date.precision)
if maxPrecision:
prec = min(maxPrecision,date.precision)
else:
prec = date.precision
if prec == 11: #days
return('%d-%02d-%02d' % (date.year,date.month,date.day))
elif prec == 10: #months
return('%d-%02d' % (date.year,date.month))
elif prec == 9: #years
return('{{L|%d}}' % date.year)
elif prec == 8: #10 years
return(self.centuryDecade(date))
elif prec == 7: #100 years
return(self.century(date))
elif prec == 6: #1000 years
return(self.millennium(date))
return(date)
def setDoB(self,date):
try:
self.dob = self.formatDate(date,maxPrecision=9)
self.dobprecision = date.precision
except:
self.dob = ''
def setDoD(self,date):
try:
self.dod = self.formatDate(date,maxPrecision=9)
self.dodprecision = date.precision
except:
self.dod = ''
def precision(self,prec):
#return precision text from numeric value prec
datePrecision = {
0:'miliard lat',
1:'100 milionów lat',
2:'10 milionów lat',
3:'milion lat',
4:'100.000 lat',
5:'10.000 lat',
6:'1000 lat',
7:'100 lat',
8:'10 lat',
9:'rok',
10:'miesiąc',
11:'dzień',
12:'godzina',
13:'minuta',
14:'sekunda',
}
return(datePrecision[prec])
class BasicBot(
# Refer pywikobot.bot for generic bot classes
SingleSiteBot, # A bot only working on one site
# CurrentPageBot, # Sets 'current_page'. Process it in treat_page method.
# # Not needed here because we have subclasses
ExistingPageBot, # CurrentPageBot which only treats existing pages
NoRedirectPageBot, # CurrentPageBot which only treats non-redirects
AutomaticTWSummaryBot, # Automatically defines summary; needs summary_key
):
"""
An incomplete sample bot.
@ivar summary_key: Edit summary message key. The message that should be used
is placed on /i18n subdirectory. The file containing these messages
should have the same name as the caller script (i.e. basic.py in this
case). Use summary_key to set a default edit summary message.
@type summary_key: str
"""
summary_key = 'basic-changing'
def __init__(self, generator, **kwargs):
"""
Constructor.
@param generator: the page generator that determines on which pages
to work
@type generator: generator
"""
# Add your own options to the bot and set their defaults
# -always option is predefined by BaseBot class
self.availableOptions.update({
'replace': False, # delete old text and write the new text
'summary': None, # your own bot summary
'text': 'Test', # add this text from option. 'Test' is default
'top': False, # append text on top of the page
'outpage': u'Wikipedysta:mastiBot/test', #default output page
'maxlines': 1000, #default number of entries per page
'test': False, #test options
'listscount': 1000, #max number of processed lists
'labels': False, #show labels in WD Item
'object': False, #show object content
'testcount': 10000, #process only testcount items from input page
'renew': False, #use original or generated pages as input
'skip': 0, #skip that many first pages
})
# call constructor of the super class
super(BasicBot, self).__init__(site=True, **kwargs)
#super(SingleSiteBot, self).__init__(site=True, **kwargs)
# handle old -dry paramter
self._handle_dry_param(**kwargs)
# assign the generator to the bot
self.generator = generator
def _handle_dry_param(self, **kwargs):
"""
Read the dry parameter and set the simulate variable instead.
This is a private method. It prints a deprecation warning for old
-dry paramter and sets the global simulate variable and informs
the user about this setting.
The constuctor of the super class ignores it because it is not
part of self.availableOptions.
@note: You should ommit this method in your own application.
@keyword dry: deprecated option to prevent changes on live wiki.
Use -simulate instead.
@type dry: bool
"""
if 'dry' in kwargs:
issue_deprecation_warning('dry argument',
'pywikibot.config.simulate', 1)
# use simulate variable instead
pywikibot.config.simulate = True
pywikibot.output('config.simulate was set to True')
def getLabel(self,ent,lang):
#get entity label in lang language with fallback
entcontent = ent.get()
for l in lang:
try:
#pywikibot.output('get label for:%s' % l)
return(entcontent['labels'][l])
except:
continue
return(None)
def extractsection(self,page,section,level):
# extract section of page returning it's content
sectionR = re.compile(r'(?s)={'+str(level)+'}\s*?'+section+'\s*?={'+str(level)+'}(?P<text>.*?)\n={'+str(level)+'} ')
if self.getOption('test'):
pywikibot.output(u'(?s)={'+str(level)+'}\s*?'+section+'\s*?={'+str(level)+'}(?P<text>.*?)\n={'+str(level)+'} ')
return(sectionR.search(page.text).group('text'))
def genpages(self,text,ns=0, rgx=r'\[\[(?P<title>[^\|\]]*?)[\|\]]'):
#generate pages based on wikilinks in text
#titleR = re.compile(r'\[\[(?P<title>[^\|\]]*?)[\|\]]')
titleR = re.compile(rgx)
for t in titleR.finditer(text):
title = re.sub(r' ',' ',t.group('title'))
page = pywikibot.Page(pywikibot.Site(),title)
if page.namespace() == ns:
yield page,t
def header(self):
header = u"Ta strona jest okresowo uaktualniana przez [[Wikipedysta:MastiBot|MastiBota]]. Ostatnia aktualizacja '''~~~~~'''."
header += u"\n\nWszelkie uwagi proszę zgłaszać w [[Dyskusja wikipedysty:Masti|dyskusji operatora]]."
header += u"\n:<small>Znalezione artykuły proszę wpisywać w kolumnie ''Link''</small>"
header += u"\n:<small>Uwagi i komentarze kolumnie ''Uwagi''</small>"
header += u"\n:<small>Pozostałę kolumny są aktualizowane automatycznie i zmiany w nich nie zostaną zachowane</small>"
header +=u'\n\n{| class="wikitable sortable" style="font-size:85%;"'
header +=u'\n|-'
header +=u'\n!Nr'
header +=u'\n!Link'
header +=u'\n!Artykuł'
header +=u'\n!Wikidane'
header +=u'\n!Jest to'
header +=u'\n!Data urodzenia'
header +=u'\n!Data śmierci'
header +=u'\n!Zajęcie'
header +=u'\n!Opis'
header +=u'\n!Uwagi'
return(header)
def footer(self):
footer = u'\n|}'
return(footer)
def resetCounters(self):
actionCounters['blue'] = 0
actionCounters['red'] = 0
actionCounters['redirs'] = 0
actionCounters['disambigs'] = 0
def run(self):
reflinks = [] #initiate list
count = 0
for tpage in self.generator:
count += 1
if self.getOption('test'):
pywikibot.output(u'[%s]Treating #%i: %s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), count, tpage.title()))
text = tpage.text
if count <= int(self.getOption('skip')):
continue
if count > int(self.getOption('listscount')):
break
if self.getOption('test'):
pywikibot.output(u'[%s][%i]L:%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), count, tpage.title() ))
try:
refs = self.treat(tpage)
except pywikibot.exceptions.MaxlagTimeoutError:
refs = None
pywikibot.output('[%s] MaxlagTimeoutError in [[%s]]' % \
(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), tpage.title()))
#if self.getOption('test'):
# pywikibot.output(refs)
if refs:
reflinks.append(refs)
self.resetCounters()
"""
count = 0
for p,t in self.genpages(text,ns=2):
count += 1
if count <= int(self.getOption('skip')):
continue
if count > int(self.getOption('listscount')):
break
if self.getOption('test'):
pywikibot.output(u'[%s][%i]L:%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), count, p.title() ))
refs = self.treat(p)
#if self.getOption('test'):
# pywikibot.output(refs)
reflinks.append(refs)
self.resetCounters()
"""
#footer += u'\n\nPrzetworzono ' + str(counter) + u' stron'
outputpage = self.getOption('outpage')
#result = self.generateresultspage(reflinks,outputpage,header,footer)
def getData(self,page):
#get data from page & WD
#return Person object
sex = { 'Q6581097':'mężczyzna', 'Q6581072':'kobieta','Q1097630':'obojnactwo', 'Q2449503':'transmężczyzna', 'Q1052281':'transkobieta', 'Q48270':'genderqueer'}
obj = Person()
obj.title = page.title()
try:
wd = pywikibot.ItemPage.fromPage(page)
wdcontent = wd.get()
obj.wditem = '[[:d:%s]]' % wd.title()
obj.wdexists = True
if self.getOption('labels'):
pywikibot.output(wdcontent['claims'].keys())
except:
pywikibot.output('WikiData page do not exists for: [[%s]]' % page.title())
obj.wdexists = False
return(obj)
wbs = pywikibot.Site('wikidata','wikidata')
for pid, claims in wdcontent['claims'].items():
for claim in claims:
trg = claim.getTarget()
prp = pywikibot.PropertyPage(wbs,title=pid)
prpcontent = prp.get()
#pywikibot.output('Property content:%s' % self.getLabel(prp,['xhs','pl','sco','en']))
#pywikibot.output('Property:%s Value:%s' % (pid,trg))
if pid in ['P21','P569','P570','P106']:
if self.getOption('labels'):
pywikibot.output('P:%s, V:%s' % (self.getLabel(prp,['pl','en']),trg))
if pid == 'P21':
try:
obj.sex = sex[trg.title()]
except:
obj.sex = 'inna'
if pid == 'P31':
obj.instanceof = self.getLabel(trg,['pl','en'])
if pid == 'P569':
obj.setDoB(trg)
if pid == 'P570':
obj.setDoD(trg)
if pid == 'P106':
lbl = self.getLabel(trg,['pl','en'])
if lbl:
obj.occupation.append(lbl)
return(obj)
def treat(self, page):
#treat all links on page
if self.getOption('renew'):
linkR = r'\|[ \d]*?\|\| *?\[\[(?P<title>[^\]]*)\]\]( *?\|\|.*?)*\|\| *(?P<description>.*)\|\|[ \t]*(?P<comment>.*)'
else:
linkR = r'# \[\[(?P<title>[^|\]]*?)(\|.*?)?\]\]\s*?(?P<description>.*)'
result = []
text = page.text
count = 0
for p,t in self.genpages(text,rgx=linkR):
count += 1
if count > int(self.getOption('testcount')):
break
actionCounters['total'] += 1
obj = Person()
if self.getOption('test'):
pywikibot.output(u'Treat:[%s][%i][%i]:%s' % \
(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), actionCounters['total'], count, p.title() ))
if not p.exists():
actionCounters['red'] += 1
else:
actionCounters['blue'] += 1
pp = p
while pp.isRedirectPage():
actionCounters['redirs'] += 1
pp = pp.getRedirectTarget()
if self.getOption('test'):
pywikibot.output(u'Redirect:[%s]:%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), pp.title() ))
if pp.isDisambig():
actionCounters['disambigs'] += 1
obj = self.getData(pp)
obj.instanceof = '<strona ujednoznaczniająca>'
obj.disambig = True
obj.title = pp.title()
if self.getOption('test'):
pywikibot.output(u'Disambig:[%s][#%i]:%s' % \
(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),actionCounters['disambigs'], pp.title() ))
else:
obj = self.getData(pp)
obj.link = p.title()
obj.description = t.group('description').strip()
if self.getOption('renew'):
obj.comment = t.group('comment').strip()
if self.getOption('object'):
obj.personPrint()
result.append(obj)
if self.getOption('renew'):
self.generateresultspage(result,page.title(),self.header(),self.footer())
else:
self.generateresultspage(result,page.title()+'/Tabela',self.header(),self.footer())
def generateresultspage(self, redirlist, pagename, header, footer):
"""
Generates results page from redirlist
Starting with header, ending with footer
Output page is pagename
"""
maxlines = int(self.getOption('maxlines'))
finalpage = header
#res = sorted(redirlist, key=redirlist.__getitem__, reverse=False)
#res = sorted(redirlist)
res = redirlist
itemcount = 0
if self.getOption('test'):
pywikibot.output(u'GENERATING RESULTS')
for i in res:
if self.getOption('test'):
#pywikibot.output(i)
i.personPrint()
itemcount += 1
if i.WDexists():
if i.isDisambig():
finalpage += u'\n|-\n| %i || [[%s]] || [[%s]] || %s || colspan=4 style="background-color:NavajoWhite; text-align:center;" | %s || %s || %s' % \
( itemcount, i.link, i.title, i.wditem, i.whatIs(), i.description, i.comment )
else:
finalpage += u'\n|-\n| %i || [[%s]] || [[%s]] || %s || %s || %s || %s || %s || %s || %s' % \
( itemcount, i.link, i.title, i.wditem, i.whatIs(), i.DoB(), i.DoD(), i.Occupation(), i.description, i.comment )
else:
finalpage += u'\n|-\n| %i || [[%s]] || [[%s]] || %s || colspan=4 style="background-color:LightSteelBlue; text-align:center;" | <brak danych> || %s || %s' % \
( itemcount, i.link, i.title, i.wditem, i.description, i.comment )
finalpage += footer
finalpage += '\n\n=== Statystyka ==='
finalpage += '\n* Istniejące: %i' % actionCounters['blue']
finalpage += '\n* Nieistniejące: %i' % actionCounters['red']
finalpage += '\n* Przekierowania: %i' % actionCounters['redirs']
finalpage += '\n* Ujednoznacznienia: %i' % actionCounters['disambigs']
if self.getOption('test'):
pywikibot.output(finalpage)
success = True
outpage = pywikibot.Page(pywikibot.Site(), pagename)
outpage.text = finalpage
if self.getOption('test'):
pywikibot.output(outpage.title())
outpage.save(summary=self.getOption('summary'))
#if not outpage.save(finalpage, outpage, self.summary):
# pywikibot.output(u'Page %s not saved.' % outpage.title(asLink=True))
# success = False
return(success)
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
options = {}
# Process global arguments to determine desired site
local_args = pywikibot.handle_args(args)
# This factory is responsible for processing command line arguments
# that are also used by other scripts and that determine on which pages
# to work on.
genFactory = pagegenerators.GeneratorFactory()
# Parse command line arguments
for arg in local_args:
# Catch the pagegenerators options
if genFactory.handleArg(arg):
continue # nothing to do here
# Now pick up your own options
arg, sep, value = arg.partition(':')
option = arg[1:]
if option in ('summary', 'text', 'outpage', 'maxlines', 'listscount', 'testcount', 'skip'):
if not value:
pywikibot.input('Please enter a value for ' + arg)
options[option] = value
# take the remaining options as booleans.
# You will get a hint if they aren't pre-definded in your bot class
else:
options[option] = True
gen = genFactory.getCombinedGenerator()
if gen:
# The preloading generator is responsible for downloading multiple
# pages from the wiki simultaneously.
gen = pagegenerators.PreloadingGenerator(gen)
# pass generator and private options to the bot
bot = BasicBot(gen, **options)
bot.run() # guess what it does
return True
else:
pywikibot.bot.suggest_help(missing_generator=True)
return False
if __name__ == '__main__':
main()