Käyttäjä:KonttiBot/tynkä.py

PartioWikistä
< Käyttäjä:KonttiBot
Versio hetkellä 5. huhtikuuta 2006 kello 00.05 – tehnyt Kontti (keskustelu | muokkaukset)
(ero) ← Vanhempi versio | Nykyinen versio (ero) | Uudempi versio → (ero)
Siirry navigaatioon Siirry hakuun
#!/usr/bin/python
# -*- coding: utf-8  -*-

"""
Tama botti on interaktiivinen, ja kysyy kayttajalta onko artikkeli tynka, minitynka vai epatynka.
Vastauksen jalkeen botti lisaa oikean tynka merkinnan loppuun.
Tynka tiedoston pituus on saadettavissa.

Tama tiedosto on muokattu selflink.py tiedostosta.

This script understands various command-line arguments:

    -start:        used as -start:page_name, specifies that the robot should
                   go alphabetically through all pages on the home wiki,
                   starting at the named page.

    -file:         used as -file:file_name, read a list of pages to treat
                   from the named textfile. Page titles should be enclosed
                   in [[double-squared brackets]].

    -ref:          used as -ref:page_name, specifies that the robot should
                   work on all pages referring to the named page.

    -links:        used as -links:page_name, specifies that the robot should
                   work on all pages referred to from the named page.

    -cat:          used as -cat:category_name, specifies that the robot should
                   work on all pages in the named category.

All other parameters will be regarded as a page title; in this case, the bot
will only work on a single page.
"""

__version__='$Id: tynka.py,v 1.0 2006/04/4 19:26:01 wikipedian Exp $'

import wikipedia, pagegenerators, catlib
import re, sys

# Summary messages in different languages
# NOTE: Predefined replacement tasks might use their own dictionary, see 'fixes'
# below.
msg = {
         'en':u'Robot: Stub marks',
         'fi':u'Bot: Merkitty jonkinlaiseksi tyng\u00e4ksi'
       }

class ThumbBot:
    def __init__(self, generator):
        self.generator = generator

    def run(self):
        # how many bytes should be displayed around the current link
        context = 4000
        # Montako tavua pitaa olla, ettei artikkeli ole tynka
        stubsize = 1000
        comment = wikipedia.translate(wikipedia.getSite(), msg)
        wikipedia.setAction(comment)

        for page in self.generator:
            wikipedia.output(u"\n\n>>> %s <<<" % page.title())
            try:
                oldText = page.get()
                text = oldText
                curpos = 0
                if len(text)< stubsize:
                    if text.find('{{tynk')<0 and text.find('{{minitynk')<0 and text.find(u'{{ep\u00e4tynk\u00e4}}')<0:
                        pos=0
                        colors = [None for c in text[max(0, pos - context) : pos]]
                        wikipedia.output(text[max(0, pos - context) : pos + context], colors = colors)
                        choice = wikipedia.inputChoice(u'Mit\u00e4s tehd\u00e4\u00e4n?',  [u'tynk\u00e4', u'minitynk\u00e4', u'ep\u00e4tynk\u00e4', 'skip'], ['t', 'm','e', 's'], 'u')
                        print
                        if choice != 's':
                             if choice == 't':
                                 text = text + u'\n{{tynk\u00e4}}\n'
                             elif choice == 'e':
                                 text = text + u'\n{{ep\u00e4tynk\u00e4}}\n'
                             else:
                                 text = text + u'\n{{minitynk\u00e4}}\n'
                if oldText == text:
                    wikipedia.output(u'No changes necessary.')
                else:
                    wikipedia.showDiff(oldText, text)
                    page.put(text)
            except wikipedia.NoPage:
                wikipedia.output(u"Page %s does not exist?!" % page.aslink())
            except wikipedia.IsRedirectPage:
                wikipedia.output(u"Page %s is a redirect; skipping." % page.aslink())
            except wikipedia.LockedPage:
                wikipedia.output(u"Page %s is locked?!" % page.aslink())

def main():
    #page generator
    gen = None
    pageTitle = []
    for arg in wikipedia.handleArgs():
        if arg.startswith('-start:'):
            gen = pagegenerators.AllpagesPageGenerator(arg[7:])
        elif arg.startswith('-ref:'):
            referredPage = wikipedia.Page(wikipedia.getSite(), arg[5:])
            gen = pagegenerators.ReferringPageGenerator(referredPage)
        elif arg.startswith('-links:'):
            linkingPage = wikipedia.Page(wikipedia.getSite(), arg[7:])
            gen = pagegenerators.LinkedPageGenerator(linkingPage)
        elif 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('selflink')
    else:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        bot = ThumbBot(preloadingGen)
        bot.run()

if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()