ویکی‌پدیا:درخواست‌های ربات/ربات نظرسنجی

از ویکی‌پدیا، دانشنامهٔ آزاد
{{{نام ربات}}}
این کاربر یک ربات است
وظیفه(ها)خط‌زدن رأی‌های
موافق محرومیت؟خیر
موافق خاموش‌شدن اضطراری؟بله
اجازه‌نامهCC-BY-SA 3.0
تست شده؟نه

<syntaxhighlight lang="python">

  1. !/usr/bin/python
  2. -*- coding: utf-8 -*-
  3. ZxxZxxZ (User:ZxxZxxZ), 2011
  4. Reza(User:reza1615), 2011
  5. Based on basic.py by xqt
  6. 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('fa') 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('fawiki-p.db.toolserver.org', 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

نتایج بررسی‌های رباتیکی[ویرایش]

از هم‌اکنون که این پیام گذاشته می‌شود فرایند مانیتورینگ رباتیک صفحهٔ نظرخواهی آغاز شده‌است. چنانچه کاربری که بنا بر زاپاس‌بودن یا به حد نصاب‌نرسیدن ویرایش‌هایش در این نظرخواهی رأی دهد نام وی در اینجا ثبت خواهد شد. با قفل‌شدن صفحهٔ نظرخواهی فرایند مانیتورینگ متوقف خواهد شد.Xqbot (بحث) ‏۲۰ آوریل ۲۰۲۰، ساعت ۱۲:۰۱ (UTC)[پاسخ]

       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'رأی %s (در %s) پذیرفته نیست چون حساب کاربری حسابی زاپاس است.' % (pageTitle, revision, username, timedate)
             else: # == 2
                message = u'تعداد ویرایش‌های کاربر:%s به حد نصاب رأی‌دادن نرسیده‌است — رأی پذیرفته نیست.' % (username)
                messageInTalk = u'رأی %s (در %s) پذیرفته نیست چون تعداد ویرایش‌های وی به حد نصاب نرسیده‌است.' % (pageTitle, revision, username, timedate)
             text = self.load(page)
             textTmp = text
             textTmp = re.sub(u'({{ *(?:موافق|مخالف|ممتنع)[^{]*?[Uu]ser) *: *%s *(?:\|.*?)?.*?)\n' % username, ur'\1 %sXqbot (بحث) ‏۲۰ آوریل ۲۰۲۰، ساعت ۱۲:۰۱ (UTC)\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%sXqbot (بحث) ‏۲۰ آوریل ۲۰۲۰، ساعت ۱۲:۰۱ (UTC)\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توجه:\ اگر صفحهٔ نظرخواهی به دلیلی غیر از پایان نظرخواهی قفل شده‌است لطفاً هرچه زودتر به صاحبم اطلاع دهید.Xqbot (بحث) ‏۲۰ آوریل ۲۰۲۰، ساعت ۱۲:۰۱ (UTC)\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()