Advertisement
Guest User

Untitled

a guest
Nov 18th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8  -*-
  3. """
  4. This bot creates a table of templates linking to disambig pages: Wikiprojekt:Strony ujednoznaczniające z linkami/Linki z szablonów
  5. Call:
  6. python basictemplatedisamblist.py -catr:"Szablony nawigacyjne" -ns:10 -outpage:"Wikiprojekt:Strony ujednoznaczniające z linkami/Linki z szablonów" -summary:"Bot uaktualnia stronę"
  7. python basicarticledisamblist.py -cat:"Artykuły na medal" -ns:0 -outpage:"Wikiprojekt:Strony ujednoznaczniające z linkami/Artykuły na medal" -summary:"Bot uaktualnia stronę"
  8. python basicarticledisamblist.py -cat:"Dobre artykuły" -ns:0 -outpage:"Wikiprojekt:Strony ujednoznaczniające z linkami/Dobre artykuły" -summary:"Bot uaktualnia stronę"
  9.  
  10.  
  11. The following parameters are supported:
  12.  
  13. &params;
  14.  
  15. -summary:XYZ      Set the summary message text for the edit to XYZ, bypassing
  16.                  the predefined message texts with original and replacements
  17.                  inserted.
  18.  
  19. All other parameters will be regarded as part of the title of a single page,
  20. and the bot will only work on that single page.
  21.  
  22.  
  23.  
  24. """
  25. #
  26. # (C) Pywikipedia bot team, 2006-2011
  27. #
  28. # Distributed under the terms of the MIT license.
  29. #
  30. __version__ = '$Id: basic.py 10358 2012-06-13 12:29:02Z drtrigon $'
  31. #
  32.  
  33. import re
  34.  
  35. import wikipedia as pywikibot
  36. import pagegenerators
  37. import re
  38. import httplib, socket, urllib, urllib2, cookielib
  39. from pywikibot import i18n
  40.  
  41. # This is required for the text that is shown when you run this script
  42. # with the parameter -help.
  43. docuReplacements = {
  44.     '&params;': pagegenerators.parameterHelp
  45. }
  46.  
  47. class BasicBot:
  48.     # Edit summary message that should be used is placed on /i18n subdirectory.
  49.     # The file containing these messages should have the same name as the caller
  50.     # script (i.e. basic.py in this case)
  51.  
  52.     def __init__(self, generator, summary, outputpage, maxlines):
  53.         """
  54.        Constructor. Parameters:
  55.            @param generator: The page generator that determines on which pages
  56.                              to work.
  57.            @type generator: generator.
  58.            @param summary: Set the summary message text for the edit.
  59.            @type summary: (unicode) string.
  60.            @param outputpage: title of the output page
  61.            @type outputpage: (unicode) string
  62.            @param maxlines: maximum number of lines in output table
  63.            @type maxlines: integer
  64.        """
  65.         self.generator = generator
  66.         # init constants
  67.         self.site = pywikibot.getSite(code=pywikibot.default_code)
  68.         # Set the edit summary message
  69.         if summary:
  70.             self.summary = summary
  71.         else:
  72.             self.summary = i18n.twtranslate(self.site, 'basic-changing')
  73.         self.outputpage = outputpage
  74.         self.maxlines = maxlines
  75.  
  76.     def run(self):
  77.         #prepare new page with table
  78.         header = u'{{Wikiprojekt:Strony ujednoznaczniające z linkami/Nagłówek}}\n\n'
  79.     header += u":<small>Pominięto strony z szablonem {{s|Inne znaczenia}}</small>\n\n"
  80.     header += u"Ta strona jest okresowo uaktualniana przez [[Wikipedysta:MastiBot|bota]]. Ostatnia aktualizacja ~~~~~. \nWszelkie uwagi proszę zgłaszać w [[Dyskusja_Wikipedysty:Masti|dyskusji operatora]]."
  81.     header += u"\n<small>"
  82.     header += u"\n*Legenda:"
  83.     header += u"\n*:'''Hasło''' - Tytuł hasła"
  84.     header += u"\n*:'''Ujednoznacznienia''' - Lista stron ujednoznaczniających"
  85.     header += u"\n</small>\n"
  86.     header += u'{| class="wikitable" style="font-size:85%;"\n|-\n!Lp.\n!Hasło\n!Ujednoznacznienia'
  87.  
  88.         footer = u'\n|}\n'
  89.  
  90.         finalpage = header
  91.         licznik = 0
  92.         wiersz = 0
  93.         for page in self.generator:
  94.             licznik += 1
  95.             #finalpage = finalpage + self.treat(page)
  96.             pywikibot.output(u'Processing page #%s (%s marked): %s' % (str(licznik), str(wiersz), page.title(asLink=True)) )
  97.             result = self.treat(page)
  98.             if not result == u'':
  99.                wiersz += 1
  100.                finalpage += u'\n|-\n| ' + str(wiersz) + u' || ' + result
  101.                pywikibot.output(u'Added line #%i: %s' % ( wiersz, u'\n|-\n| ' + str(wiersz) + u' || ' + result))
  102.             #pywikibot.output(finalpage)
  103.         finalpage += footer
  104.  
  105.         #Save page
  106.         pywikibot.output(finalpage)
  107.         outpage = pywikibot.Page(pywikibot.getSite(), self.outputpage)
  108.         if not self.save(finalpage, outpage, self.summary):
  109.            pywikibot.output(u'Page %s not saved.' % outpage.title(asLink=True))
  110.      
  111.  
  112.     def treat(self, page):
  113.         """
  114.        Loads the given page, looks for linked disambigs
  115.        """
  116.         found = False
  117.         rowtext = u''
  118.         text = self.load(page)
  119.         if not text:
  120.             return(0)
  121.  
  122.         pageiw = u''
  123.     firstline = True
  124.         for link in page.linkedPages():
  125.        pywikibot.output(u'processing page %s->%s' % (page.title(asLink=True),link.title(asLink=True)))
  126.            if u'{{{' in link.title():
  127.               pywikibot.output(u"Page %s is a parameter call. Skipping." % page.title(asLink=True))
  128.               continue
  129.        try:
  130.               isdisamb = link.isDisambig()
  131.               if isdisamb and not (u'{{Inne znaczenia' in text):
  132.                  found = True
  133.              if not firstline:
  134.                 pageiw = pageiw + u'<br />' + link.title(asLink=True)
  135.              else:
  136.                 firstline = False
  137.             pageiw = u'{{s|' + page.title(withNamespace=False) + u'}} || ' + link.title(asLink=True)
  138.                  pywikibot.output(u'adding %s' % link.title(asLink=True))
  139.        except pywikibot.SectionError:
  140.               pywikibot.output(u"Page %s has missing section. skipping" % link.title(asLink=True))
  141.            except RuntimeError:
  142.               pywikibot.output(u"Page %s is weird; RuntimeError encountered.skipping." % link.title(asLink=True))
  143.            except urllib2.HTTPError:
  144.               pywikibot.output(u"Page %s is weird; HTTPError encountered.skipping." % link.title(asLink=True))
  145.            except:
  146.               pywikibot.output(u"Page %s is weird; Uknownkn Error encountered.skipping." % link.title(asLink=True))
  147.    
  148.     #write result
  149.         if found:
  150.        pywikibot.output(u"%s" % pageiw)
  151.  
  152.     return(pageiw)
  153.  
  154.         ################################################################
  155.         # NOTE: Here you can modify the text in whatever way you want. #
  156.         ################################################################
  157.  
  158.        
  159.     def load(self, page):
  160.         """
  161.        Loads the given page, does some changes, and saves it.
  162.        """
  163.         try:
  164.             # Load the page
  165.             text = page.get()
  166.         except pywikibot.NoPage:
  167.             pywikibot.output(u"Page %s does not exist; skipping."
  168.                              % page.title(asLink=True))
  169.         except pywikibot.IsRedirectPage:
  170.             pywikibot.output(u"Page %s is a redirect; skipping."
  171.                              % page.title(asLink=True))
  172.         else:
  173.             return text
  174.         return None
  175.  
  176.     def save(self, text, page, comment=None, minorEdit=True,
  177.              botflag=True):
  178.         # only save if something was changed
  179.         try:        
  180.        pagetext = page.get()
  181.         except:
  182.            pagetext = u''
  183.         if text != pagetext:
  184.             # Show the title of the page we're working on.
  185.             # Highlight the title in purple.
  186.             pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
  187.                              % page.title())
  188.             # show what was changed
  189.             pywikibot.showDiff(pagetext, text)
  190.             pywikibot.output(u'Comment: %s' %comment)
  191.             #choice = pywikibot.inputChoice(
  192.             #    u'Do you want to accept these changes?',
  193.             #    ['Yes', 'No'], ['y', 'N'], 'N')
  194.             try:
  195.                     # Save the page
  196.                     page.put(text, comment=comment or self.comment,
  197.                              minorEdit=minorEdit, botflag=botflag)
  198.             except pywikibot.LockedPage:
  199.                     pywikibot.output(u"Page %s is locked; skipping."
  200.                                      % page.title(asLink=True))
  201.             except pywikibot.EditConflict:
  202.                     pywikibot.output(
  203.                         u'Skipping %s because of edit conflict'
  204.                         % (page.title()))
  205.             except pywikibot.SpamfilterError, error:
  206.                     pywikibot.output(
  207. u'Cannot change %s because of spam blacklist entry %s'
  208.                         % (page.title(), error.url))
  209.             else:
  210.                     return True
  211.         return False
  212.  
  213. class AutoBasicBot(BasicBot):
  214.     # Intended for usage e.g. as cronjob without prompting the user.
  215.  
  216.     _REGEX_eol = re.compile(u'\n')
  217.  
  218.     def __init__(self):
  219.         BasicBot.__init__(self, None, None)
  220.  
  221.     ## @since   10326
  222.     #  @remarks needed by various bots
  223.     def save(self, page, text, comment=None, minorEdit=True, botflag=True):
  224.         pywikibot.output(u'\03{lightblue}Writing to wiki on %s...\03{default}' % page.title(asLink=True))
  225.  
  226.         comment_output = comment or pywikibot.action
  227.         pywikibot.output(u'\03{lightblue}Comment: %s\03{default}' % comment_output)
  228.  
  229.         #pywikibot.showDiff(page.get(), text)
  230.  
  231.         for i in range(3): # try max. 3 times
  232.             try:
  233.                 # Save the page
  234.                 page.put(text, comment=comment, minorEdit=minorEdit, botflag=botflag)
  235.             except pywikibot.LockedPage:
  236.                 pywikibot.output(u"\03{lightblue}Page %s is locked; skipping.\03{default}" % page.title(asLink=True))
  237.             except pywikibot.EditConflict:
  238.                 pywikibot.output(u'\03{lightblue}Skipping %s because of edit conflict\03{default}' % (page.title()))
  239.             except pywikibot.SpamfilterError, error:
  240.                 pywikibot.output(u'\03{lightblue}Cannot change %s because of spam blacklist entry %s\03{default}' % (page.title(), error.url))
  241.             else:
  242.                 return True
  243.         return False
  244.  
  245.     ## @since   10326
  246.     #  @remarks needed by various bots
  247.     def append(self, page, text, comment=None, minorEdit=True, section=None):
  248.         if section:
  249.             pywikibot.output(u'\03{lightblue}Appending to wiki on %s in section %s...\03{default}' % (page.title(asLink=True), section))
  250.  
  251.             for i in range(3): # try max. 3 times
  252.                 try:
  253.                     # Append to page section
  254.                     page.append(text, comment=comment, minorEdit=minorEdit, section=section)
  255.                 except pywikibot.PageNotSaved, error:
  256.                     pywikibot.output(u'\03{lightblue}Cannot change %s because of %s\03{default}' % (page.title(), error))
  257.                 else:
  258.                     return True
  259.         else:
  260.             content = self.load( page )     # 'None' if not existing page
  261.             if not content:                 # (create new page)
  262.                 content = u''
  263.  
  264.             content += u'\n\n'
  265.             content += text
  266.  
  267.             return self.save(page, content, comment=comment, minorEdit=minorEdit)
  268.  
  269.     ## @since   10326
  270.     #  @remarks needed by various bots
  271.     def loadTemplates(self, page, template, default={}):
  272.         """Get operating mode from page with template by searching the template.
  273.  
  274.           @param page: The user (page) for which the data should be retrieved.
  275.  
  276.           Returns a list of dict with the templates parameters found.
  277.        """
  278.  
  279.         self._content = self.load(page) # 'None' if not existing page
  280.  
  281.         templates = []
  282.         if not self._content:
  283.             return templates  # catch empty or not existing page
  284.  
  285.         for tmpl in pywikibot.extract_templates_and_params(self._content):
  286.             if tmpl[0] == template:
  287.                 param_default = {}
  288.                 param_default.update(default)
  289.                 param_default.update(tmpl[1])
  290.                 templates.append( param_default )
  291.         return templates
  292.  
  293.     ## @since   10326
  294.     #  @remarks common interface to bot job queue on wiki
  295.     def loadJobQueue(self, page, queue_security, reset=True):
  296.         """Check if the data queue security is ok to execute the jobs,
  297.           if so read the jobs and reset the queue.
  298.  
  299.           @param page: Wiki page containing job queue.
  300.           @type  page: page
  301.           @param queue_security: This string must match the last edit
  302.                              comment, or else nothing is done.
  303.           @type  queue_security: string
  304.  
  305.           Returns a list of jobs. This list may be empty.
  306.        """
  307.  
  308.         try:    actual = page.getVersionHistory(revCount=1)[0]
  309.         except:    pass
  310.  
  311.         secure = False
  312.         for item in queue_security[0]:
  313.             secure = secure or (actual[2] == item)
  314.  
  315.         secure = secure and (actual[3] == queue_security[1])
  316.  
  317.         if not secure: return []
  318.  
  319.         data = self._REGEX_eol.split(page.get())
  320.         if reset:
  321.             pywikibot.output(u'\03{lightblue}Job queue reset...\03{default}')
  322.            
  323.             pywikibot.setAction(u'reset job queue')
  324.             page.put(u'', minorEdit = True)
  325.  
  326.         queue = []
  327.         for line in data:
  328.             queue.append( line[1:].strip() )
  329.         return queue
  330.  
  331. def main():
  332.     # This factory is responsible for processing command line arguments
  333.     # that are also used by other scripts and that determine on which pages
  334.     # to work on.
  335.     genFactory = pagegenerators.GeneratorFactory()
  336.     # The generator gives the pages that should be worked upon.
  337.     gen = None
  338.     # This temporary array is used to read the page title if one single
  339.     # page to work on is specified by the arguments.
  340.     pageTitleParts = []
  341.     # summary message
  342.     editSummary = ''
  343.     outPage = 'User:MastiBot/test'
  344.     maxLines = 1000
  345.  
  346.     # Parse command line arguments
  347.     for arg in pywikibot.handleArgs():
  348.         if arg.startswith('-summary:'):
  349.             editSummary = arg[9:]
  350.         elif arg.startswith('-outpage:'):
  351.             outPage = arg[9:]
  352.         elif arg.startswith('-maxlines:'):
  353.             maxLines = int(arg[10:])
  354.         else:
  355.             # check if a standard argument like
  356.             # -start:XYZ or -ref:Asdf was given.
  357.             if not genFactory.handleArg(arg):
  358.                 pageTitleParts.append(arg)
  359.  
  360.     if pageTitleParts != []:
  361.         # We will only work on a single page.
  362.         pageTitle = ' '.join(pageTitleParts)
  363.         page = pywikibot.Page(pywikibot.getSite(), pageTitle)
  364.         gen = iter([page])
  365.  
  366.     if not gen:
  367.         gen = genFactory.getCombinedGenerator()
  368.     if gen:
  369.         # The preloading generator is responsible for downloading multiple
  370.         # pages from the wiki simultaneously.
  371.         gen = pagegenerators.PreloadingGenerator(gen)
  372.         bot = BasicBot(gen, editSummary, outPage, maxLines)
  373.         bot.run()
  374.     else:
  375.         pywikibot.showHelp()
  376.  
  377. if __name__ == "__main__":
  378.     try:
  379.         main()
  380.     finally:
  381.         pywikibot.stopme()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement