Advertisement
Guest User

JoRedirectBot

a guest
Aug 20th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. import pywikibot
  5. import time
  6.  
  7. def joReplacer(word, startIndex = 0, replaced=False):
  8.     result = []
  9.     count = word.count(u'ё', startIndex)
  10.     if count > 1 or (count == 1 and replaced):
  11.         newStartIndex = word.find(u'ё', startIndex) + 1
  12.         result.extend(joReplacer(word, newStartIndex, replaced))
  13.         result.extend(joReplacer(word[:startIndex] + word[startIndex:].replace(u'ё', u'е', 1), newStartIndex, True))
  14.     elif count == 1:
  15.         result.append(word[:startIndex] + word[startIndex:].replace(u'ё', u'е'))
  16.     else:
  17.         result.append(word)
  18.     return result
  19.  
  20. def processLine(line):
  21.     masterWord = unicode(line[:-1], 'utf8')
  22.     masterPage = pywikibot.Page(site, masterWord)
  23.     if site.page_isredirect(masterPage):
  24.         masterPage = masterPage.getRedirectTarget()
  25.  
  26.     for word in joReplacer(masterWord):
  27.         page = pywikibot.Page(site, word)
  28.         if (page.text == ''):
  29.             page.text = u"#REDIRECT [[" + masterPage.title() + u"]]"
  30.             #page.set_redirect_target(masterPage, create=True)
  31.             site.editpage(page, u'Тестовая проверка ё-перенаправлятора')
  32.  
  33.  
  34. filename = "no_jo.txt"
  35. site = pywikibot.Site()
  36. site.login()
  37. lines = open(filename).readlines()
  38.  
  39. for i, line in enumerate(lines[:]):
  40.     processLine(line)
  41.     del lines[0]
  42.     time.sleep(10)
  43.  
  44. open(filename, "w").writelines(lines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement