Outils personnels
User menu

Create page

De Dbpedia.fr.

Bot permettant d'ajouter du texte à une page. Contrairement à add_text.py (en ligne de commande), il crée la page si celle-ci n'existe pas.

En fait, il se sert de la méthode add_text() du bot add_text.py avec le paramètre create=True. Bizarrement l'option n'est pas disponible à l'exécution de add_text.py ...

Le code de create_page.py:

#!/usr/bin/python
# -*- coding: utf-8  -*-


import add_text
import re, pagegenerators, urllib2, urllib
import wikipedia as pywikibot


def main():
    # If none, the var is setted only for check purpose.
    summary = None; addText = None; regexSkip = None; regexSkipUrl = None;
    generator = None; always = False
    textfile=None
    talkPage=False
    reorderEnabled = True
    namespaces=[]
    # Load a lot of default generators
    genFactory = pagegenerators.GeneratorFactory()
    # Put the text above or below the text?
    up = False
    # Loading the arguments
    for arg in pywikibot.handleArgs():
        if arg.startswith('-textfile'):
            if len(arg) == 9:
                textfile = pywikibot.input(
                    u'Which textfile do you want to add?')
            else:
                textfile = arg[10:]
        elif arg.startswith('-text'):
            if len(arg) == 5:
                addText = pywikibot.input(u'What text do you want to add?')
            else:
                addText = arg[6:]
        elif arg.startswith('-summary'):
            if len(arg) == 8:
                summary = pywikibot.input(u'What summary do you want to use?')
            else:
                summary = arg[9:]
        elif arg.startswith('-page'):
            if len(arg) == 5:
                generator = [pywikibot.Page(
                    pywikibot.getSite(),
                    pywikibot.input(u'What page do you want to use?')
                    )]
            else:
                generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])]
        elif arg.startswith('-excepturl'):
            if len(arg) == 10:
                regexSkipUrl = pywikibot.input(u'What text should I skip?')
            else:
                regexSkipUrl = arg[11:]
        elif arg.startswith('-except'):
            if len(arg) == 7:
                regexSkip = pywikibot.input(u'What text should I skip?')
            else:
                regexSkip = arg[8:]
        elif arg.startswith('-untagged'):
            if len(arg) == 9:
                untaggedProject = pywikibot.input(
                    u'What project do you want to use?')
            else:
                untaggedProject = arg[10:]
            generator = untaggedGenerator(untaggedProject)
        elif arg == '-up':
            up = True
        elif arg == '-noreorder':
            reorderEnabled = False
        elif arg == '-always':
            always = True
        elif arg == '-talk' or arg == '-talkpage':
            talkPage = True
        else:
            genFactory.handleArg(arg)
    if textfile and not addText:
        f = codecs.open(textfile, 'r', config.textfile_encoding)
        addText = f.read()
        f.close()
    if not generator:
        generator = genFactory.getCombinedGenerator()
    # Check if there are the minimal settings
    if not generator:
        raise NoEnoughData(
            'You have to specify the generator you want to use for the script!')
    if talkPage:
        generator = pagegenerators.PageWithTalkPageGenerator(generator)
        site = pywikibot.getSite()
        for namespace in site.namespaces():
            index = site.getNamespaceIndex(namespace)
            if index%2==1 and index>0:
                namespaces += [index]
        generator = pagegenerators.NamespaceFilterPageGenerator(
            generator, namespaces)
    # Main Loop
    for page in generator:
        (text, newtext, always) = add_text.add_text(page, addText, summary, regexSkip,
                                           regexSkipUrl, always, up, True, 
                                           reorderEnabled=reorderEnabled, create=True)

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()
  • Dernière modification de cette page le 29 novembre 2012 à 08:39.
  • Cette page a été consultée 618 fois.