Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # Based on basic.py by xqt
- # Distributed under the terms of CC-BY-SA 3.0.
- __version__ = '$Id: BOT 9359 2011-07-10 12:22:39Z$'
- import wikipedia as pywikibot
- import pagegenerators, catlib, urllib, gzip, codecs
- from pywikibot import i18n
- import MySQLdb as mysqldb
- import config
- import time
- import category
- pageTitle = raw_input(u'Page> ')
- site = pywikibot.getSite('ur')
- cat = pywikibot.Page(site, u'زمرہ:جرابی کٹھ پتلیاں')
- page = pywikibot.Page(site, pageTitle)
- talkPage = pywikibot.Page(site, u'تبادلۂ خیال:'+pageTitle)
- global checked
- global checkedOld
- checked, checkedOld = 0, 0
- uListPerm = []
- pywikibot.config.put_throttle = 0
- pywikibot.put_throttle.setDelay()
- def checkUser(username, yesrs, months, days):
- count = 0
- query='SELECT DATE(CONCAT(YEAR(rev_timestamp),"-",MONTH(rev_timestamp),"-",DAY(rev_timestamp))) AS DAY,COUNT(rev_timestamp) AS revisions FROM `revision` JOIN `page` ON `page_id` = `rev_page` JOIN `user` ON `user_id` = `rev_user` WHERE `user_name` = "'+username+'" AND `page_namespace` = 0 GROUP BY DAY;'
- conn = mysqldb.connect('urwiki-p.labsdb', db = site.dbName(),
- user = config.db_username,
- passwd = config.db_password)
- cursor = conn.cursor()
- query = query.encode(site.encoding())
- cursor.execute(query)
- results = cursor.fetchall()
- #------------------------------sql finsh------------------
- for row in results:
- dates = str(row[0])
- if int(dates[0:4]) < years:
- count += int(row[1])
- continue
- if int(dates[0:4]) == years and int(dates[5:7]) < months:
- count += int(row[1])
- continue
- if int(dates[0:4]) == years and int(dates[5:7]) == months and int(dates[8:10]) < days:
- count += int(row[1])
- return count
- class BasicBot:
- def __init__(self, generator, dry):
- self.generator = generator
- self.dry = dry
- def run(self):
- for page in self.generator:
- self.treat(page)
- def checkUserRight(self, user):
- zapasha = CategoryDatabase.getArticles(cat)
- for zapas in zapasha:
- if user == zapas.title()
- return 3
- del zapasha
- zapashaCat = CategoryDatabase.getSubcats(cat)
- for zapasCat in zapashaCat:
- zapasha = CategoryDatabase.getArticles(zapasCat)
- for zapas in zapasha:
- if user == zapas.title().replace(u'زمرہ:جرابی کٹھ پتلیاں/', '')
- return 3
- if checkUser(user, yesrs, months, days) >= 100:
- return 2 # احازه دارد
- else:
- return 1 # اجازه ندارد
- return None # اگر هیچیک نبود
- def checkPage(self, page):
- hist = page.fullVersionHistory()
- uList = []
- for i in range(checked, len(hist)):
- rightStatus = checkUserRight(hist[i][2])
- if rightStatus is not None:
- uList.append((hist[i][2], hist[i][1], hist[i][0], rightStatus)) # hist[i][2] is user name & hist[i][1] is edit time/date & hist[i][0] is revision ID
- uListPerm += uList
- checkedOld = checked
- checked = len(hist)
- return uList
- def addToLog(self, txt, repText):
- return re.sub(u'(= *نتایج بررسیهای رباتیکی *=+\n.*?)(?=\n=|$)', repText, txt, re.S)
- def run(self, page):
- talkText = self.load(talkPage)
- talkText += u'''
- == نتایج بررسیهای رباتیکی ==
- <!--
- در این زیربخش ***ویرایشی مکنید.***
- اگر میخواهید مطلب جدیدی به این صفحه بیفزایید، زیر همین زیربخش، زیربخش جدیدی ایجاد کنید.
- -->
- از هماکنون که این پیام گذاشته میشود فرایند مانیتورینگ رباتیک صفحهٔ نظرخواهی آغاز شدهاست.
- چنانچه کاربری که بنا بر [[وپ:زاپاس|زاپاسبودن]] یا به حد نصابنرسیدن ویرایشهایش در این نظرخواهی رأی دهد نام وی در اینجا ثبت خواهد شد.
- با قفلشدن صفحهٔ نظرخواهی فرایند مانیتورینگ متوقف خواهد شد.~~~~
- '''
- if self.saveTalk(talkText, talkPage, u'ربات: آغاز فرایند مانیتورینگ — گزارش کار اینجا ثبت خواهد شد. ([[وپ:درخواستهای ربات/ربات نظرسنجی|کد]])')
- pywikibot.output(u'INFO: Talk page %s editted' % page.title(asLink=True))
- while True:
- newOnes = self.checkPage(page)
- if newOnes == []:
- time.sleep(3600) # Sleeping for an hour
- else:
- for i in range(0, len(newOnes)):
- username = newOnes[i][0]
- timedate = newOnes[i][1]
- revision = newOnes[i][2]
- if newOnes[i][3] == 1:
- message = u'حساب کاربری رأیدهنده ([[کاربر:%s]]) حساب [[وپ:زاپاس|زاپاس]] است — رأی پذیرفته نیست.' % username
- messageInTalk = u'[http://fa.wikipedia.org/w/index.php?title=%s&diff=prev&oldid=%s رأی %s] (در %s) پذیرفته نیست چون حساب کاربری حسابی [[وپ:زاپاس|زاپاس]] است.' % (pageTitle, revision, username, timedate)
- else: # == 2
- message = u'تعداد ویرایشهای [[کاربر:%s]] به حد نصاب رأیدادن نرسیدهاست — رأی پذیرفته نیست.' % (username)
- messageInTalk = u'[http://fa.wikipedia.org/w/index.php?title=%s&diff=prev&oldid=%s رأی %s] (در %s) پذیرفته نیست چون تعداد ویرایشهای وی به حد نصاب نرسیدهاست.' % (pageTitle, revision, username, timedate)
- text = self.load(page)
- textTmp = text
- textTmp = re.sub(u'({{ *(?:موافق|مخالف|ممتنع)[^{]*?[[* (?:کاربر|[Uu]ser) *: *%s *(?:\|.*?)?]].*?)\n' % username, ur'<s>\1</s> %s~~~~\n\n' % message, textTmp, re.S)
- if textTmp != text:
- altMessage = u' رأی کاربر به طور خودکار خط زده شد.'
- text = textTmp
- if self.save(text, page):
- pywikibot.output(u'INFO: Page %s editted.' % page.title(asLink=True))
- talkText = addToLog(talkText, ur'\1\n%s%s~~~~\n' % (messageInTalk, altMessage))
- if self.saveTalk(talkText, talkPage)
- pywikibot.output(u'INFO: Talk page %s editted.' % page.title(asLink=True))
- if done is True:
- pywikibot.stopme()
- def load(self, page):
- try:
- text = page.get()
- except pywikibot.NoPage:
- pywikibot.output(u'ERROR: Page %s does not exist.' % page.title(asLink=True))
- pywikibot.stopme()
- except pywikibot.IsRedirectPage:
- pywikibot.output(u'INFO: Page %s is a redirect.' % page.title(asLink=True))
- page = page.getRedirectTarget()
- text = page.get()
- else:
- return text
- return None
- def save(self, text, page, comment=u'ربات: خطزدن رأی ([[وپ:درخواستهای ربات/ربات نظرسنجی|کد]])', minorEdit=True, botflag=True):
- if text != page.get():
- pywikibot.output(u'INFO: Working on %s' % page.title())
- if not self.dry:
- try:
- page.put(text, comment=comment, minorEdit=minorEdit, botflag=botflag)
- except pywikibot.LockedPage:
- pywikibot.output(u"INFO: Page %s is locked. It seems the work is done. I'm trying to report it on the talk page. Monitoring process is no longer active." % page.title(asLink=True))
- talkText = addToLog(talkText, ur"\1\nصفحهٔ نظرخواهی قفل شدهاست. فرایند مانیتورینگ متوقف شد.\n\n'''توجه:\''' اگر صفحهٔ نظرخواهی به دلیلی غیر از پایان نظرخواهی قفل شدهاست لطفاً هرچه زودتر به صاحبم اطلاع دهید.~~~~\n" % (messagem, altMessage))
- done = True
- except pywikibot.EditConflict:
- pywikibot.output(u'INFO: Edit conflict on %s. Trying again...' % page.title())
- checked = checkedOld
- except pywikibot.SpamfilterError, error:
- pywikibot.output(u'ERROR: Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
- pywikibot.stopme()
- else:
- return True
- return False
- def saveTalk(self, text, page, comment=u'ربات: گزارش کار ([[وپ:درخواستهای ربات/ربات نظرسنجی|کد]])', minorEdit=True, botflag=True):
- if text != page.get():
- pywikibot.output(u'INFO: Working on %s' % page.title())
- if not self.dry:
- try:
- page.put(text, comment=comment, minorEdit=minorEdit, botflag=botflag)
- except pywikibot.LockedPage:
- pywikibot.output(u'INFO: Talk Page %s is locked so it seems the work is done; but the log is NOT complete -- you shold check the talk page later. Stopping the process...' % page.title(asLink=True))
- pywikibot.stopme()
- except pywikibot.EditConflict:
- pywikibot.output(u'INFO: Edit conflict on %s. Trying again...' % page.title())
- checked = checkedOld
- except pywikibot.SpamfilterError, error:
- pywikibot.output(u'ERROR: Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
- pywikibot.stopme()
- else:
- return True
- return False
- def main():
- dry = False
- bot = BasicBot(gen, dry)
- bot.run()
- if __name__ == "__main__":
- try:
- main()
- finally:
- pywikibot.stopme()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement