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

PartioWikistä
< Käyttäjä:KonttiBot
Versio hetkellä 5. lokakuuta 2015 kello 12.38 – tehnyt Egel (keskustelu | muokkaukset) (Tekstin korvaus – ”<br>” muotoon ”<br />”)
(ero) ← Vanhempi versio | Nykyinen versio (ero) | Uudempi versio → (ero)
Siirry navigaatioon Siirry hakuun
#!/usr/bin/python
# -*- coding: utf-8  -*-

"""
This bot goes over multiple pages of the home wiki, and edits them without
changing. This is for example used to get category links in templates
working.

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 -start:page_name, specifies that the robot should
                   touch all pages referring to the named page.

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

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

    -redir         specifies that the robot should touch redirect pages;
                   otherwise, they will be skipped.

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

__version__='$Id: touch.py,v 1.13 2006/03/01 14:07:06 russblau Exp $'

import wikipedia, pagegenerators, catlib
import sys
import codecs

class TouchBot:
    def __init__(self, generator, touch_redirects):
        self.generator = generator
        self.touch_redirects = touch_redirects

    def run(self):
        #Starttexts
        alkutekstit=u"'''Eth\u00e4n muokkaa t\u00e4t\u00e4 sivua. T\u00e4m\u00e4n sivun tekee automaattisesti [[K\u00e4ytt\u00e4j\u00e4:KonttiBot|KonttiBot]].'''\n"
        alkutekstit+=u"'''<br />Do not modify this page. This page is automatically created by [[K\u00e4ytt\u00e4j\u00e4:KonttiBot|KonttiBot]].'''\n\n"
        #All the languagelinks
        tekstit=u''
        #Endtexts
        lopputekstit=u"[[Luokka: PartioWiki]]"
        for page in self.generator:
            try:
                text = page.get(get_redirect=self.touch_redirects)
                scoutwikis=['partiowiki:','scoutpedia:','scoutpedia.com:','scout-o-wiki:','gondi-online:','pfadiwiki:','wikiroca:','schlauesbuch:','scoutwiki.nl:','scoutaid:','roexploratori:','scoutopedia:']
                for llink in scoutwikis:
                  lstart=text.find("[["+llink)
                  if lstart>=0:
                    lend=text.find("]]",lstart)+2
                    ltext=text[lstart+2:lend-2]
                    tekstit+=u'* '+page.aslink()+' -> [[:'+ltext+']]\n'
            except wikipedia.NoPage:
                print "Page %s does not exist?!" % page.aslink()
            except wikipedia.IsRedirectPage:
                print "Page %s is a redirect; skipping." % page.aslink()
            except wikipedia.LockedPage:
                print "Page %s is locked?!" % page.aslink()
        #Page to write Langualinks
        writePage='PartioWiki:Kielilinkit'
        page = wikipedia.Page(wikipedia.getSite(), writePage)
        page.put(alkutekstit+tekstit+lopputekstit)
def main():
    #page generator
    gen = None
    redirs = False
    pageTitle = []
    for arg in wikipedia.handleArgs():
        if arg.startswith('-start:'):
            page = wikipedia.Page(wikipedia.getSite(),arg[7:])
            gen = pagegenerators.AllpagesPageGenerator(page.titleWithoutNamespace(),namespace=page.namespace())
        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)
        elif arg == '-redir':
            redirs = True
        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 = TouchBot(preloadingGen, redirs)
        bot.run()

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