Advertisement
Guest User

20170109-preklad.py

a guest
Jan 9th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.72 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. An incomplete sample script.
  5.  
  6. This is not a complete bot; rather, it is a template from which simple
  7. bots can be made. You can rename it to mybot.py, then edit it in
  8. whatever way you want.
  9.  
  10. Use global -simulate option for test purposes. No changes to live wiki
  11. will be done.
  12.  
  13. The following parameters are supported:
  14.  
  15. &params;
  16.  
  17. -always           If used, the bot won't ask if it should file the message
  18.                  onto user talk page.
  19.  
  20. -text:            Use this text to be added; otherwise 'Test' is used
  21.  
  22. -replace:         Dont add text but replace it
  23.  
  24. -top              Place additional text on top of the page
  25.  
  26. -summary:         Set the action summary message for the edit.
  27. """
  28. #
  29. # (C) Pywikibot team, 2006-2016
  30. #
  31. # Distributed under the terms of the MIT license.
  32. #
  33. from __future__ import absolute_import, unicode_literals
  34.  
  35. __version__ = '$Id: c1795dd2fb2de670c0b4bddb289ea9d13b1e9b3f $'
  36. #
  37.  
  38. import pywikibot, re, urllib
  39. from pywikibot import pagegenerators, textlib
  40.  
  41. from pywikibot.textlib import _MultiTemplateMatchBuilder
  42. from pywikibot.bot import (
  43.     SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot)
  44. from pywikibot.tools import issue_deprecation_warning
  45.  
  46. # This is required for the text that is shown when you run this script
  47. # with the parameter -help.
  48. docuReplacements = {
  49.     '&params;': pagegenerators.parameterHelp
  50. }
  51.  
  52.  
  53. class BasicBot(
  54.     # Refer pywikobot.bot for generic bot classes
  55.     SingleSiteBot,  # A bot only working on one site
  56.     # CurrentPageBot,  # Sets 'current_page'. Process it in treat_page method.
  57.     #                  # Not needed here because we have subclasses
  58.     ExistingPageBot,  # CurrentPageBot which only treats existing pages
  59.     NoRedirectPageBot,  # CurrentPageBot which only treats non-redirects
  60.     AutomaticTWSummaryBot,  # Automatically defines summary; needs summary_key
  61. ):
  62.  
  63.     """
  64.    An incomplete sample bot.
  65.  
  66.    @ivar summary_key: Edit summary message key. The message that should be used
  67.        is placed on /i18n subdirectory. The file containing these messages
  68.        should have the same name as the caller script (i.e. basic.py in this
  69.        case). Use summary_key to set a default edit summary message.
  70.    @type summary_key: str
  71.    """
  72.  
  73.     summary_key = 'basic-changing'
  74.  
  75.     def __init__(self, generator, **kwargs):
  76.         """
  77.        Constructor.
  78.  
  79.        @param generator: the page generator that determines on which pages
  80.            to work
  81.        @type generator: generator
  82.        """
  83.         # Add your own options to the bot and set their defaults
  84.         # -always option is predefined by BaseBot class
  85.         self.availableOptions.update({
  86.             'replace': False,  # delete old text and write the new text
  87.             'summary': None,  # your own bot summary
  88.             'text': 'Test',  # add this text from option. 'Test' is default
  89.             'top': False,  # append text on top of the page
  90.         })
  91.  
  92.         # call constructor of the super class
  93.         super(BasicBot, self).__init__(site=True, **kwargs)
  94.  
  95.         # handle old -dry paramter
  96.         self._handle_dry_param(**kwargs)
  97.  
  98.         # assign the generator to the bot
  99.         self.generator = generator
  100.  
  101.     def _handle_dry_param(self, **kwargs):
  102.         """
  103.        Read the dry parameter and set the simulate variable instead.
  104.  
  105.        This is a private method. It prints a deprecation warning for old
  106.        -dry paramter and sets the global simulate variable and informs
  107.        the user about this setting.
  108.  
  109.        The constuctor of the super class ignores it because it is not
  110.        part of self.availableOptions.
  111.  
  112.        @note: You should ommit this method in your own application.
  113.  
  114.        @keyword dry: deprecated option to prevent changes on live wiki.
  115.            Use -simulate instead.
  116.        @type dry: bool
  117.        """
  118.         if 'dry' in kwargs:
  119.             issue_deprecation_warning('dry argument',
  120.                                       'pywikibot.config.simulate', 1)
  121.             # use simulate variable instead
  122.             pywikibot.config.simulate = True
  123.             pywikibot.output('config.simulate was set to True')
  124.  
  125.     def treat_page(self):
  126.         """Load the given page, do some changes, and save it."""
  127.         text = self.current_page.text
  128.  
  129.         ################################################################
  130.         #                           výjimky                            #
  131.         ################################################################
  132.  
  133.         #exceptions = ['comment', 'math', 'nowiki', 'pre', 'startspace', 'table', 'template']
  134.         exceptions = ['comment','nowiki','pre']
  135.  
  136.         ################################################################
  137.         #                           shrnutí                            #
  138.         ################################################################
  139.  
  140.         #shrnuti = self.getOption('summary')
  141.         shrnuti = 'doplnění chybějící šablony překlad'
  142.  
  143.         ################################################################
  144.         #                            regexy                            #
  145.         ################################################################
  146.  
  147.         soubor = urllib.request.urlopen('https://tools.wmflabs.org/urbanecmbot/test/preklads.txt')
  148.         radky = soubor.readlines()
  149.         nahrady = {}
  150.         for radek in radky:
  151.             nahrady[str(radek.split(b'\t')[0],'utf-8')] = str(radek.split(b'\t')[1].rstrip(),'utf-8')
  152.        
  153.         preklad = nahrady[self.current_page.title(underscore=True)]
  154.  
  155.         if re.search(r'(?i)== *Reference *==', text):
  156.             text = textlib.replaceExcept(text, r'(?i)==(=?) *Reference *===?\s*', r'==\1 Reference ==\1\n' + preklad + r'\n', exceptions)
  157.         elif re.search(r'(?i)== *Poznámky *==', text):
  158.             text = textlib.replaceExcept(text, r'(?i)==(=?) *Poznámky *===?\s+(.*)\s*', r'==\1 Poznámky ==\1\n\2\n\n==\1 Reference ==\1\n' + preklad + r'\n\n', exceptions)
  159.         elif re.search(r'(?i)== *(Literatura|Související články|Externí odkazy) *==', text):
  160.             text = textlib.replaceExcept(text, r'(?i)\s*==(=?) *(Literatura|Související články|Externí odkazy) *===?', r'\n\n==\1 Reference ==\1\n' + preklad + r'\n\n==\1 \2 ==\1', exceptions, count=1)
  161.         elif re.search(r'(?i)\{\{(Autoritní data|Portály)[^\}]*\}\}', text):
  162.             text = textlib.replaceExcept(text, r'(?i)\s*\{\{((Autoritní data|Portály)[^\}]*)\}\}', r'\n\n== Reference ==\n' + preklad + r'\n\n{{\2}}', exceptions, count=1)
  163.         elif re.search(r'(?i)\[\[Kategorie:[^\]]*\]\]', text):
  164.             text = textlib.replaceExcept(text, r'(?i)\s*\[\[Kategorie:([^\]]*)\]\]', r'\n\n== Reference ==\n' + preklad + r'\n\n[[Kategorie:\2]]', exceptions, count=1)
  165.         else:
  166.             text = text + '\n\n== Reference ==\n' + preklad
  167.  
  168.         ################################################################
  169.         self.put_current(text, summary='Robot: ' + shrnuti)
  170.  
  171.  
  172. def main(*args):
  173.     """
  174.    Process command line arguments and invoke bot.
  175.  
  176.    If args is an empty list, sys.argv is used.
  177.  
  178.    @param args: command line arguments
  179.    @type args: list of unicode
  180.    """
  181.     options = {}
  182.     # Process global arguments to determine desired site
  183.     local_args = pywikibot.handle_args(args)
  184.  
  185.     # This factory is responsible for processing command line arguments
  186.     # that are also used by other scripts and that determine on which pages
  187.     # to work on.
  188.     genFactory = pagegenerators.GeneratorFactory()
  189.  
  190.     # Parse command line arguments
  191.     for arg in local_args:
  192.  
  193.         # Catch the pagegenerators options
  194.         if genFactory.handleArg(arg):
  195.             continue  # nothing to do here
  196.  
  197.         # Now pick up your own options
  198.         arg, sep, value = arg.partition(':')
  199.         option = arg[1:]
  200.         if option in ('summary', 'text'):
  201.             if not value:
  202.                 pywikibot.input('Please enter a value for ' + arg)
  203.             options[option] = value
  204.         # take the remaining options as booleans.
  205.         # You will get a hint if they aren't pre-definded in your bot class
  206.         else:
  207.             options[option] = True
  208.  
  209.     gen = genFactory.getCombinedGenerator()
  210.     if gen:
  211.         # The preloading generator is responsible for downloading multiple
  212.         # pages from the wiki simultaneously.
  213.         gen = pagegenerators.PreloadingGenerator(gen)
  214.         # pass generator and private options to the bot
  215.         bot = BasicBot(gen, **options)
  216.         bot.run()  # guess what it does
  217.         return True
  218.     else:
  219.         pywikibot.bot.suggest_help(missing_generator=True)
  220.         return False
  221.  
  222. if __name__ == '__main__':
  223.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement