-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms-CEESpringpl2.py
810 lines (696 loc) · 32.8 KB
/
ms-CEESpringpl2.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Call:
python pwb.py masti/ms-CEESpringpl2.py -page:"Wikipedia:CEE Spring 2020/nazwa tygodnia" -outpage:"Wikipedia:CEE Spring 2020/nazwa tygodnia/wyniki" -pt:0 -summary:"Bot uaktualnia statystyki"
for test:
python pwb.py masti/ms-CEESpringpl.py -page:"Wikipedia:CEE Spring 2020" -outpage:"Wikipedysta:mastiBot/CCES2020" -pt:0 -summary:"Bot uaktualnia statystyki" -reset -test -simulate -testdays -testinc -testweeks -testincdays -testiw -testchange
Use global -simulate option for test purposes. No changes to live wiki
will be done.
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.
-negative: mark if text not in page
"""
#
# (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
import re
from pywikibot import textlib
import datetime
from datetime import ( datetime, timedelta )
import pickle
from pywikibot import (
config, config2,
)
import operator
from pywikibot.bot import (
SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot)
from pywikibot.tools import issue_deprecation_warning
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
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'
wikichallenge = {
'articles':{},
'users':{},
'weeks':{},
'days':{},
'incdays':{},
'positions':{},
}
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'User:mastiBot/test', #default output page
'maxlines': 1000, #default number of entries per page
'test': False, # print testoutput
'negative': False, #if True negate behavior i.e. mark pages that DO NOT contain search string
'reset': False, # rebuild database from scratch
'testpickle': False, # make verbose output for article list load/save
'testdays': False, # make verbose output for days results
'testinc': False, # make verbose output for incdays results
'testweeks': False, # make verbose output for weekly results
'testincdays': False, # make verbose output for incremental days results
'testiw': False, # make verbose output for interwikis
'testchange': False, # make verbose output for position changes
'testbody': False, # test for section body
'testnick': False, # test for user,nick
'testclean': False, # test for number of cleaned infoboxes
})
# call constructor of the super class
super(BasicBot, self).__init__(site=True, **kwargs)
# handle old -dry paramter
self._handle_dry_param(**kwargs)
# assign the generator to the bot
self.generator = generator
self.calendar = {} # calendar dict
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 run(self):
header = '{{Wikipedia:CEE Spring 2020/Nawigacja}}\n\n'
header += "Ostatnia aktualizacja przez bota: '''~~~~~'''.\n"
header += "*<small>'''Uwaga:''' aktualizacja po zakończeniu tygodnia.</small>\n\n"
header += "*<small>'''Długość artykułu''': ilość znaków po usunięciu infoboksów, i sekcji ''Linki zewnętrzne'' i ''Zobacz też''</small>\n\n"
#header += "{{Spis treści}}\n\n"
footer = '\n\n== Zobacz też ==\n'
footer += '* [[%s/lista|Szczegółowa lista haseł i punktacji]]' % self.getOption('outpage')
#load articleList from previous run
if not self.getOption('reset'):
self.loadArticleList('masti/ceespringpl2020.dat')
for p in self.generator:
if self.getOption('test'):
pywikibot.output(u'Treating: %s' % p.title())
self.treatWeek(p)
self.saveArticleList(self.wikichallenge,self.getOption('outpage'),header,'')
return
def getSubpage(self,page,subpage):
#get a subpage for a give page
pagename = page.title() + subpage
return(pywikibot.Page(pywikibot.Site(), pagename))
def CEEweek(self,date):
#return week number
for w in self.calendar.keys():
if date >= self.calendar[w]['startdate'] and date <= self.calendar[w]['enddate']:
return(w)
return(None)
def todate(self,datestr):
#convert declinated date to datetime.obj
months = { 'marca':'03','kwietnia':'04','maja':'05'}
elems = datestr.split()
if self.getOption('test'):
pywikibot.output('elems:%s' % elems)
dt = '2020-%s-%s' % (months[elems[1]], elems[0])
return(datetime.strptime(dt, '%Y-%m-%d'))
def getCalendar(self,page):
#analyze calendar page
calR = re.compile(r'(?i)\|\s*?tydzień\s*?(?P<week>\d+)\s*?\|\|\s*?\[\[[^\/]*(?P<subpage>[^\|]*)\s*?\|\s*?(?P<start>\d+ .*?)\s*?–\s*(?P<end>\d+ .*?)\]\]\s*?\|\|(?P<countries>.*?)\|')
calpage = self.getSubpage(page,'/Kalendarz')
for w in calR.finditer(calpage.text):
week = int(w.group('week'))
self.calendar[week] = {}
self.calendar[week]['subpageTitle'] = w.group('subpage')
self.calendar[week]['subpage'] = self.getSubpage(page,self.calendar[week]['subpageTitle'])
self.calendar[week]['start'] = w.group('start')
self.calendar[week]['end'] = w.group('end')
self.calendar[week]['startdate'] = self.todate(w.group('start'))
self.calendar[week]['enddate'] = self.todate(w.group('end')) + timedelta(days=1)
self.calendar[week]['countries'] = w.group('countries').strip().split(', ')
#if self.getOption('test'):
# pywikibot.output('WEEK %s:%s' % (week,self.calendar[week]))
def treatWeek(self,page):
"""Load the given page, do some changes, and save it."""
pywikibot.output('Treat Week: %s' % page.title())
text = page.text
#if self.getOption('test'):
# pywikibot.output('Page text:%s' % text)
artlist = self.getWeekBody('Nowe artykuły',text)
#if self.getOption('testbody'):
# pywikibot.output('ARTLIST:%s' % artlist)
if artlist:
self.wikichallenge['articles'] = self.getArtsUsers(artlist)
for a in self.wikichallenge['articles'].keys():
self.getArtInfo(a)
return
"""
self.resetCounters()
self.countDayResults()
self.countWeekResults()
self.printSortedDays()
self.countIncDayResults()
self.printSortedIncDays()
self.countPositions()
"""
def getUserNick(self,text):
# get username and dispaly nick from signature
unR1 = re.compile(r'(?i)\[\[wikipedystk?a:(?P<user>[^\|]*)\|(?P<nick>[^\]]*)')
unR2 = re.compile(r'(?i)\[\[wikipedystk?a:(?P<user>[^\]]*)')
if self.getOption('testnick'):
pywikibot.output('geUserNick: %s' % text)
if '|' in text:
#pywikibot.output('testnick1:%s' % text)
#pywikibot.output('geUserNick "|": %s' % text)
usr = unR1.match(text)
user = usr.group('user')
nick = usr.group('nick')
if self.getOption('testnick'):
pywikibot.output('testnick ([[user:nick|nick]]):[[Wikipedysta:%s|%s]]' % (user, nick))
return(user,nick)
elif ']' in text:
pywikibot.output('geUserNick "]": %s' % text)
usr = unR2.match(text)
user = usr.group('user')
nick = user
if self.getOption('testnick'):
pywikibot.output('testnick2 ([[user:nick]]):[[Wikipedysta:%s|%s]]' % (user, nick))
return(user,nick)
else:
if self.getOption('testnick'):
pywikibot.output('testnick3 (only nick): [[Wikipedysta:%s|%s]]' % (text,text))
return(text.strip(),text.strip())
def getArtsUsers(self,text):
#return list of (art,user)
#pywikibot.output('getArtUsers:%s' % text)
auR = re.compile(r'(?im)#\s*?(\(\d*\))?[^[]*?\[\[(?P<title>[^\|\]]*)[^\]]*\]\][^–-]*?[−–-]\s*(?P<user>.*)')
count = 0
result = {}
for au in auR.finditer(text):
count += 1
#if self.getOption('testnick'):
# pywikibot.output('Art:[[%s]], User:%s' % (au.group('title'),au.group('user')))
title = au.group('title')
user, nick = self.getUserNick(au.group('user'))
if self.getOption('testnick'):
pywikibot.output('#%i:testnick:[[%s]] - [[Wikipedysta:%s|%s]]' % (count,title,user,nick))
result[title] = {'user':user, 'nick':nick}
result[title]['length'],result[title]['clean'] = self.getArtInfo(title)
pywikibot.output(result)
return(result)
def getArtInfo(self,title):
# get length of full and cleaned article
page = pywikibot.Page( pywikibot.Site(), title )
text = page.text
fullLength = len(text)
cleanLength = len(self.cleanText(text))
return(fullLength,cleanLength)
def cleanText(self,text):
#remove infoboxes, "linki zewnętrzne", "zobacz też"
infoboxR = re.compile('\{\{.*?infobox[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*(?:(\{[^\{\}]*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\})+?[^\{\}]*)*?\}\}')
toRemove = []
#clean disabled parts
text = textlib.removeDisabledParts(text)
#clean infoboxes
text,count = infoboxR.subn('',text)
if self.getOption('testclean') and count:
pywikibot.output('CLEANED %i infoboxes' % count)
#clean sections
head,body,tail = textlib.extract_sections(text)
for t,s in body:
if "linki zewnętrzne" in t.lower() or "zobacz też" in t.lower():
if self.getOption('testbody'):
pywikibot.output('Title:%s' % t)
pywikibot.output('Body:%s' % s)
toRemove.append(t + '\n' + s)
# remove unnecessary sections
for tr in toRemove:
text = text.replace(tr,'')
return(text)
def getWeekBody(self,title,text):
#return weekly section with articles list
pywikibot.output('GETWEEKBODY:%s' % title)
head,body,tail = textlib.extract_sections(text)
for t,s in body:
if title in t:
if self.getOption('testbody'):
pywikibot.output('Title:%s' % t)
pywikibot.output('Body:%s' % s)
return(s)
return(None)
def saveArticleList(self, res, pagename, header, footer):
# save article list at self.getOption('outpage')/list
# generate table header
finalpage = header
finalpage += u'\n{| class="wikitable sortable" style="text-align: center;"'
finalpage += u'\n|-'
finalpage += u'\n! #'
finalpage += u'\n! Artykuł'
finalpage += u'\n! Autor'
finalpage += u'\n! Długość artykułu'
finalpage += u'\n! Długość oryginalna'
count = 0
srt = sorted(res['articles'], key=lambda x: (res['articles'][x]['clean']), reverse=True)
for a in srt:
count += 1
finalpage += u'\n|-\n|'
if res['articles'][a]['clean'] > 2000 :
finalpage += "%i. || '''[[%s]]''' || '''[[Wikipedysta:%s|%s]]''' || {{L|%i}} || {{L|%i}}" % (count, a,
res['articles'][a]['user'],
res['articles'][a]['nick'],
res['articles'][a]['clean'],
res['articles'][a]['length'] )
else:
finalpage += "%i. || [[%s]] || [[Wikipedysta:%s|%s]] || {{L|%i}} || {{L|%i}}" % (count, a,
res['articles'][a]['user'],
res['articles'][a]['nick'],
res['articles'][a]['clean'],
res['articles'][a]['length'] )
finalpage += u'\n|}'
finalpage += footer
outpage = pywikibot.Page(pywikibot.Site(), pagename)
if self.getOption('test'):
pywikibot.output(u'ArticleList:%s' % outpage.title())
outpage.text = finalpage
outpage.save(summary=self.getOption('summary'))
return
def resetCounters(self):
#reset all counters after getting info on articles
self.wikichallenge['users'] = {}
self.wikichallenge['weeks'] = {}
self.wikichallenge['days'] = {}
self.wikichallenge['incdays'] = {}
self.wikichallenge['positions'] = {}
def printSortedDays(self):
for i in sorted(self.wikichallenge['days'].keys()):
sorted_days = sorted(self.wikichallenge['days'][i].items(), key=operator.itemgetter(1), reverse=True)
pywikibot.output('DAY %i:%s' % (i,sorted_days))
def printSortedIncDays(self):
for i in sorted(self.wikichallenge['incdays'].keys()):
sorted_days = sorted(self.wikichallenge['incdays'][i].items(), key=operator.itemgetter(1), reverse=True)
pywikibot.output('INCDAY %i:%s' % (i,sorted_days))
def points(self,interwiki):
# count point as min(iw^2,400)
res = interwiki*interwiki
if res>400:
return(400)
else:
return(res)
def countInterwiki(self,t):
#count interwikis
siteR = re.compile(ur'(?m)^(?P<site>.*)wiki$')
if t in self.wikichallenge['articles'].keys():
if self.getOption('test'):
pywikibot.output('INTERWIKI EXISTING [[%s]]:%i' % (t,self.wikichallenge['articles'][t]['interwiki']))
return(self.wikichallenge['articles'][t]['interwiki'])
iw = []
page = pywikibot.Page( pywikibot.Site(), t )
while page.isRedirectPage():
page = page.getRedirectTarget()
try:
d = page.data_item()
pywikibot.output(u'WD: %s WIKI:%s' % (d.title(),page.title()) )
dataItem = d.get()
#pywikibot.output(u'DataItem:%s' % dataItem.keys() )
sitelinks = dataItem['sitelinks']
if self.getOption('testiw'):
pywikibot.output('SITELINKS:%s' % sitelinks)
for s in sitelinks:
site = siteR.match(s)
if site:
if not site.group('site') in ['pl','commons']:
iw.append( s )
if self.getOption('testiw'):
pywikibot.output('SITELINKS adding site:%s' % s)
#print( iw)
except:
pass
return(len(iw))
l = len(iw)
if l:
return(l-1)
else:
return(0)
def getDays(self,text):
#return list of day sections from text
dayR = re.compile(ur'(?P<day>\n=+\s*?(?P<date>\d+\s*?(czerwca))\s*?=+(\n+#\s*?[^\n]*)*)')
count = 0
result = {}
for d in dayR.finditer(text):
count += 1
dayNumber = self.dayToNumber(d.group('date'))
result[dayNumber] = {}
result[dayNumber]['day'] = d.group('date')
result[dayNumber]['week'] = self.wikiweek(dayNumber)
result[dayNumber]['body'] = d.group('day')
if self.getOption('test'):
pywikibot.output('======================================')
pywikibot.output('= Day %i = %s =' % (count,d.group('date')))
pywikibot.output('= Day calc %i =' % self.dayToNumber(d.group('date')))
pywikibot.output('= Week calc %i =' % self.wikiweek(self.dayToNumber(d.group('date'))))
pywikibot.output('======================================')
pywikibot.output(d.group('day'))
return(result)
def wikiweek(self,day):
return(int((day-1)/6)+1)
def dayToNumber(self,day):
dtn = { '1 czerwca':1, '2 czerwca':2, '3 czerwca':3, '4 czerwca':4, '5 czerwca':5, '6 czerwca':6, '7 czerwca':7, '8 czerwca':8, '9 czerwca':9, '10 czerwca':10, '11 czerwca':11, '12 czerwca':12, '13 czerwca':13, '14 czerwca':14, '15 czerwca':15, '16 czerwca':16, '17 czerwca':17, '18 czerwca':18, '19 czerwca':19, '20 czerwca':20, '21 czerwca':21, '22 czerwca':22, '23 czerwca':23, '24 czerwca':24, '25 czerwca':25, '26 czerwca':26, '27 czerwca':27, '28 czerwca':28, '29 czerwca':29, '30 czerwca':30 }
return(dtn[day])
def numberToDay(self,num):
ntd = { 1:'1 czerwca', 2:'2 czerwca', 3:'3 czerwca', 4:'4 czerwca', 5:'5 czerwca', 6:'6 czerwca', 7:'7 czerwca', 8:'8 czerwca', 9:'9 czerwca', 10:'10 czerwca', 11:'11 czerwca', 12:'12 czerwca', 13:'13 czerwca', 14:'14 czerwca', 15:'15 czerwca', 16:'16 czerwca', 17:'17 czerwca', 18:'18 czerwca', 19:'19 czerwca', 20:'20 czerwca', 21:'21 czerwca', 22:'22 czerwca', 23:'23 czerwca', 24:'24 czerwca', 25:'25 czerwca', 26:'26 czerwca', 27:'27 czerwca', 28:'28 czerwca', 29:'29 czerwca', 30:'30 czerwca' }
return(ntd[num])
def getInterwiki(self,page):
# yield interwiki sites generator
iw = []
iwlinks = 0
try:
d = page.data_item()
pywikibot.output(u'WD: %s' % d.title() )
dataItem = d.get()
#pywikibot.output(u'DataItem:%s' % dataItem.keys() )
sitelinks = dataItem['sitelinks']
iwlinks = len(sitelinks)
'''
for s in sitelinks:
#if self.getOption('test'):
# pywikibot.output(u'SL iw: %s' % d)
site = re.sub(ur'(.*)wiki$', ur'\1',s)
if site == u'be_x_old':
site = u'be-tarask'
ssite = pywikibot.Site(site,fam='wikipedia')
spage = pywikibot.Page( ssite, title=sitelinks[s] )
#pywikibot.output(u'gI Page: %s' % spage.title(asLink=True,forceInterwiki=True) )
iw.append( spage )
#print( iw)
'''
except:
pass
#print(iw)
return(iwlinks)
def loadArticleList(self,picklefile):
#load article list form pickled dictionary
#result = {}
if self.getOption('reset'):
if self.getOption('testpickle'):
pywikibot.output('PICKLING SKIPPED at %s' % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
else:
if self.getOption('testpickle'):
pywikibot.output('PICKLING LOAD at %s' % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
try:
with open(picklefile, 'rb') as datfile:
self.wikichallenge = pickle.load(datfile)
except (IOError, EOFError):
# no saved history exists yet, or history dump broken
if self.getOption('testpickle'):
pywikibot.output('PICKLING FILE NOT FOUND')
#result = {}
if self.getOption('testpickle'):
pywikibot.output('PICKLING RESULT:%s' % self.wikichallenge)
def pickleArticleList(self,artList,picklefile):
#save list as pickle file
if self.getOption('testpickle'):
pywikibot.output('PICKLING SAVE at %s' % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
with open(picklefile, 'wb') as f:
pickle.dump(artList, f, protocol=config.pickle_protocol)
def generateResultTable(self, res, pagename, header, footer):
#generate main results page sorted daily
finalpage = header
incDays = res['incdays']
if self.getOption('test'):
pywikibot.output('RESULT:%s' % incDays)
finalpage += '\n== Wyniki dzienne =='
for i in sorted(incDays.keys(),reverse=True):
finalpage += '\n=== %s (dzień %i.) ===' % (self.numberToDay(i), i)
count = 0
finalpage += u'\n{| class="wikitable sortable" style="text-align: center;"'
finalpage += u'\n|-'
finalpage += u'\n! #'
finalpage += u'\n! Zmiana'
finalpage += u'\n! Autor'
finalpage += u'\n! Punkty'
for u,p in sorted(incDays[i].items(), key=operator.itemgetter(1), reverse=True):
count += 1
finalpage += u'\n|-\n| '
finalpage += '%i. || %s || [[Wikipedysta:%s|%s]] || %i' % (res['positions'][i][u], self.posDiff(u,i), u, u, p)
finalpage += u'\n|}'
weeks = res['weeks']
if self.getOption('test'):
pywikibot.output('RESULT:%s' % weeks)
finalpage += '\n== Wyniki konkursów 6-dniowych =='
for i in sorted(weeks.keys(),reverse=True):
finalpage += '\n=== Konkurs %i. (%s - %s) ===' % (i,self.numberToDay(i*6-5),self.numberToDay(i*6))
count = 0
finalpage += u'\n{| class="wikitable sortable" style="text-align: center;"'
finalpage += u'\n|-'
finalpage += u'\n! #'
finalpage += u'\n! Autor'
finalpage += u'\n! Punkty'
for u,p in sorted(weeks[i].items(), key=operator.itemgetter(1), reverse=True):
count += 1
finalpage += u'\n|-\n| '
finalpage += '%i. || [[Wikipedysta:%s|%s]] || %i' % (count, u, u, p)
finalpage += u'\n|}'
finalpage += footer
outpage = pywikibot.Page(pywikibot.Site(), pagename)
if self.getOption('test'):
pywikibot.output(u'ArticleList:%s' % outpage.title())
outpage.text = finalpage
outpage.save(summary=self.getOption('summary'))
def countIncDayResults(self):
#count result incrementaly
if self.getOption('testinc'):
pywikibot.output('countIncDayResults')
for i in sorted(self.wikichallenge['days'].keys()):
if self.getOption('testinc'):
pywikibot.output('incDays: i:%s' % i)
self.wikichallenge['incdays'][i] = {}
if i > 1:
currDay = sorted(self.wikichallenge['days'][i].items(), key=operator.itemgetter(1))
prevDay = sorted(self.wikichallenge['incdays'][i-1].items(), key=operator.itemgetter(1))
if self.getOption('testinc'):
pywikibot.output('incDays CURR day:%s' % currDay)
pywikibot.output('incDays PREV day:%s' % prevDay)
for u,p in prevDay:
if self.getOption('testinc'):
pywikibot.output('incDays prevday: i:%s<>%s' % (u,p))
self.wikichallenge['incdays'][i][u] = p
for u,p in currDay:
if u in self.wikichallenge['incdays'][i].keys():
if self.getOption('testinc'):
pywikibot.output('incDays adding: %s=%i+%i' % (u,self.wikichallenge['incdays'][i][u],p))
self.wikichallenge['incdays'][i][u] += p
else:
if self.getOption('testinc'):
pywikibot.output('incDays setting: %s=%i' % (u,p))
self.wikichallenge['incdays'][i][u] = p
else:
currDay = sorted(self.wikichallenge['days'][i].items(), key=operator.itemgetter(1))
if self.getOption('testinc'):
pywikibot.output('incDays1 CURR day:%s' % currDay)
for u,p in currDay:
if self.getOption('testinc'):
pywikibot.output('incDays setting: %s=%i' % (u,p))
self.wikichallenge['incdays'][i][u] = p
if self.getOption('testincdays'):
pywikibot.output('CHALLENGE INCDAYS:%s' % self.wikichallenge['incdays'])
def countDayResults(self):
#count result per each day
for a in self.wikichallenge['articles'].keys():
currArt = self.wikichallenge['articles'][a]
currArtN = currArt['dayN']
points = currArt['points']
if not currArt['dayN'] in self.wikichallenge['days'].keys():
self.wikichallenge['days'][currArtN] = {}
if currArt['author'] in self.wikichallenge['days'][currArtN].keys():
self.wikichallenge['days'][currArtN][currArt['author']] += points
else:
self.wikichallenge['days'][currArtN][currArt['author']] = points
if self.getOption('testdays'):
pywikibot.output('CHALLENGE DAYS:%s' % self.wikichallenge['days'])
def countWeekResults(self):
#count result per each day
for a in self.wikichallenge['articles'].keys():
currArt = self.wikichallenge['articles'][a]
currArtN = currArt['week']
if not currArt['week'] in self.wikichallenge['weeks'].keys():
self.wikichallenge['weeks'][currArtN] = {}
if currArt['author'] in self.wikichallenge['weeks'][currArtN].keys():
self.wikichallenge['weeks'][currArtN][currArt['author']] += currArt['points']
else:
self.wikichallenge['weeks'][currArtN][currArt['author']] = currArt['points']
if self.getOption('testweeks'):
pywikibot.output('CHALLENGE WEEKS:%s' % self.wikichallenge['weeks'])
def countPositions(self):
#count user positions in contest
days = self.wikichallenge['incdays']
for d in days.keys():
results = days[d]
if self.getOption('testchange'):
pywikibot.output('POS RESULTS:%s' % results)
positions = sorted(results.items(), key=operator.itemgetter(1), reverse=True)
if self.getOption('testchange'):
pywikibot.output('POSITIONS:%s' % positions)
count = 0
self.wikichallenge['positions'][d] = {}
pos = self.wikichallenge['positions'][d]
for u,p in positions:
if not count:
count += 1
pos[u] = count
oldpoints = p
oldcount = count
else:
count += 1
if p == oldpoints:
pos[u] = oldcount
else:
pos[u] = count
oldpoints = p
oldcount = count
if self.getOption('testchange'):
pywikibot.output('POSITIONS CALCULATED:%s' % sorted(pos.items(), key=operator.itemgetter(1)))
def posDiff(self,user,currday):
#calculate change position in user position
# return {{zmiana|wzrost|2}} or '''N'''
prevday = currday - 1
if not prevday:
return("'''N'''")
pday = self.wikichallenge['positions'][prevday]
cday = self.wikichallenge['positions'][currday]
if user in pday.keys():
diff = pday[user] - cday[user]
if diff < 0:
return('{{zmiana|spadek}} %i' % diff)
elif diff > 0:
return('{{zmiana|wzrost}} +%i' % diff)
else:
return('{{zmiana|stagnacja}}')
else:
return("'''N'''")
def templateWithNamedParams(self):
"""
Iterate template as returned by templatesWithNames()
@return: a generator that yields a tuple for each param of a template
type: named, int
name: name of param
value: value of param
@rtype: generator
"""
# TODO
def templateArg(self, param):
"""
return name,value for each template param
input text in form "name = value"
@return: a tuple for each param of a template
named: named (True) or int
name: name of param or None if numbered
value: value of param
@rtype: tuple
"""
# TODO
paramR = re.compile(ur'(?P<name>.*)=(?P<value>.*)')
if '=' in param:
match = paramR.search(param)
named = True
name = match.group("name").strip()
value = match.group("value").strip()
else:
named = False
name = None
value = param
#test
pywikibot.output(u'name:%s:value:%s' % (name, value))
return named, name, value
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'):
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()