User:Flubot/cedilla2Bot

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Run with:

python cedilla2Bot.py acer (to edit only this page) ... or
python cedilla2Bot.py -cat:"English nouns" (to edit all pages in the given category) ... or
python cedilla2Bot.py -file:listOfWords (to retrieve page names from a list)

cedilla2bot.py[edit]

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import wikipedia, pagegenerators, catlib
import re
 
class cedillabot:
 
  def __init__(self, generator, site, debug_bul = True):
    self.generator = generator
    self.debug_bul = debug_bul
    self.site = site
 
  def run(self):
    romanian = re.compile('\*( *)Romanian( *):')
    ttbc = re.compile('\*( *)\{\{ttbc\|ro\}\}')
    l_templ = re.compile('\{\{l\|ro\|([^\}]+)\}\}')
    t_templ = re.compile('\{\{t([+-]*)\|ro\|([^\}]+)\}\}')
    ro_term = re.compile('\{\{term\|(.*)\|lang=ro(\}\|)')
 
    s1 = re.compile(u'ş')
    s2 = re.compile(u'Ş')
    t1 = re.compile(u'ţ')
    t2 = re.compile(u'Ţ')
 
    # Here is defined the entry to modificate
 
    for p in self.generator:
          titlos = p.title()
          print(titlos)
          page = wikipedia.Page(wikipedia.getSite(), titlos)
          arxiko = page.get(get_redirect=True)
 
	  marker = '@@'
          while marker in arxiko:
            marker += '@'
 
          site = self.site 
          interwiki = wikipedia.getLanguageLinks(arxiko, insite = site)
          textnoiws = wikipedia.removeLanguageLinks(arxiko.replace(marker,'').strip(), site = self.site) + site.family.category_text_separator
 
        # Replace
          linesOftext = textnoiws.split("\n")
          my_new_text = []  
          for lineOfMyText in linesOftext:
		if romanian.search(lineOfMyText) or ttbc.search(lineOfMyText):
		     lineOfMyText = t1.sub(u'ț', lineOfMyText)
		     lineOfMyText = t2.sub(u'Ț', lineOfMyText)	
		     lineOfMyText = s1.sub(u'ș', lineOfMyText)
		     lineOfMyText = s2.sub(u'Ș', lineOfMyText)
		if ro_term.search(lineOfMyText):
		     ro_word = ro_term.search(lineOfMyText).group(1)
		     ro_word2 = t1.sub(u'ț', ro_word)
		     ro_word2 = t2.sub(u'Ț', ro_word2)
		     ro_word2 = s1.sub(u'ş', ro_word2)
		     ro_word2 = s2.sub(u'Ş', ro_word2)
		     lineOfMyText = lineOfMyText.replace(ro_word, ro_word2)
		if l_templ.search(lineOfMyText):
		     ro_word = l_templ.search(lineOfMyText).group(1)
		     ro_word2 = t1.sub(u'ț', ro_word)
		     ro_word2 = t2.sub(u'Ț', ro_word2)
		     ro_word2 = s1.sub(u'ş', ro_word2)
		     ro_word2 = s2.sub(u'Ş', ro_word2)
		     lineOfMyText = lineOfMyText.replace(ro_word, ro_word2)
		if t_templ.search(lineOfMyText):
		     ro_word = t_templ.search(lineOfMyText).group(2)
		     ro_word2 = t1.sub(u'ț', ro_word)
		     ro_word2 = t2.sub(u'Ț', ro_word2)
		     ro_word2 = s1.sub(u'ş', ro_word2)
		     ro_word2 = s2.sub(u'Ş', ro_word2)
		     lineOfMyText = lineOfMyText.replace(ro_word, ro_word2)
		my_new_text.append(lineOfMyText)
          keimeno1 = "\n".join(my_new_text)
          keimeno = wikipedia.replaceLanguageLinks(keimeno1, interwiki, site = self.site)
 
          # Save page
          wikipedia.setAction(u'bot:changing cedillas with commas in Romanian words')
 
          if keimeno1 != textnoiws:
            # Show the title of the page we're working on.
            # Highlight the title in purple.
              wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
            ## show what was changed
              wikipedia.showDiff(page.get(), keimeno)
              #choice= 'y'
              if self.debug_bul:
                choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['yes', 'No', 'all'], ['y', 'N', 'a'], 'N')
                if choice == 'a':
                  choice = 'y'
                  self.debug_bul = False
                elif choice != 'y':
                  choice='n'
              if choice == 'y':
                try:
                        # Save the page
                        page.put(keimeno)
                except wikipedia.IsRedirectPage:
                        wikipedia.output(u'Skipping %s because it is a redirect' % (page.title()))
                except wikipedia.EditConflict:
                        wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
                except wikipedia.SpamfilterError, error:
                        wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
    return 0
 
 
def main():
    gen = None
    pageTitle = []
    for arg in wikipedia.handleArgs():
        if arg:
            if arg.startswith('-file:'):
                gen = pagegenerators.TextfilePageGenerator(arg[6:])
            elif arg.startswith('-cat:'):
                cat = catlib.Category(wikipedia.getSite(), arg[5:])
                gen = pagegenerators.CategorizedPageGenerator(cat)
            else:
                pageTitle.append(arg)
 
    if pageTitle:
        page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle))
        gen = iter([page])
    if not gen:
        wikipedia.showHelp('touch')
    else:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        bot = cedillabot(preloadingGen, wikipedia.getSite())
        bot.run()
 
if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()