saper has uploaded a new change for review. https://gerrit.wikimedia.org/r/98457
Change subject: Sample bot adding content from text file ...................................................................... Sample bot adding content from text file An example bot roughly based on basic.py to solve a problem posted on the mailing list: http://thread.gmane.org/gmane.science.linguistics.wikipedia.technical/74126 Change-Id: I325e10ae0d1ef42e9b999030404be99bf032791b --- A onelinecontent.py 1 file changed, 90 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/compat refs/changes/57/98457/1 diff --git a/onelinecontent.py b/onelinecontent.py new file mode 100644 index 0000000..9b9d657 --- /dev/null +++ b/onelinecontent.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +An example bot to fix the issue described in the + +http://thread.gmane.org/gmane.science.linguistics.wikipedia.technical/74126 + +The following parameters are supported: + +-summary:XYZ Set the summary message text for the edit to XYZ, bypassing + the predefined message texts with original and replacements + inserted. + +-contentfile:XYZ A name of a file containing "pagename:content" lines + +All other parameters will be regarded as part of the title of a single page, +and the bot will only work on that single page. +""" +# +# (C) Pywikipedia bot team, 2006-2011 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id$' +# + +import re + +import wikipedia as pywikibot +import pagegenerators +from pywikibot import i18n, Page +from basic import BasicBot + + +def FileContentGenerator(fp, sep=":", site=None): + """Yield (pagename, text) pairs read from the file""" + if site is None: + site = pywikibot.getSite() + for line in fp.readlines(): + (pagename, text) = line.split(sep) + yield (Page(site, pagename), text) + + +class BasicContentBot(BasicBot): + def run(self): + for (page, text) in self.generator: + self.treatcontent(page, text) + + def treatcontent(self, page, text): + """ + Loads the given page, does some changes, and saves it. + """ + if not text: + return + + ################################################################ + # NOTE: Here you can modify the text in whatever way you want. # + ################################################################ + + # If you find out that you do not want to edit this page, just return. + # Example: This puts the text 'Test' at the beginning of the page. + + if not self.save(text, page, self.summary): + pywikibot.output(u'Page %s not saved.' % page.title(asLink=True)) + + +def main(): + editSummary = '' + contentfile = None + + # Parse command line arguments + for arg in pywikibot.handleArgs(): + if arg.startswith('-summary:'): + editSummary = arg[9:] + else: + if arg.startswith('-contentfile:'): + contentfile = arg[13:] + + if contentfile: + gen = FileContentGenerator(open(contentfile, "r")) + bot = BasicContentBot(gen, editSummary) + bot.run() + else: + pywikibot.showHelp() + +if __name__ == "__main__": + try: + main() + finally: + pywikibot.stopme() -- To view, visit https://gerrit.wikimedia.org/r/98457 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I325e10ae0d1ef42e9b999030404be99bf032791b Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/compat Gerrit-Branch: master Gerrit-Owner: saper <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
