Revision: 8240 Author: xqt Date: 2010-06-03 07:19:33 +0000 (Thu, 03 Jun 2010)
Log Message: ----------- import wikipedia as pywikibot for easier merging to rewrite
Modified Paths: -------------- trunk/pywikipedia/add_text.py trunk/pywikipedia/archivebot.py trunk/pywikipedia/articlenos.py trunk/pywikipedia/basic.py trunk/pywikipedia/casechecker.py trunk/pywikipedia/catall.py trunk/pywikipedia/commonscat.py
Modified: trunk/pywikipedia/add_text.py =================================================================== --- trunk/pywikipedia/add_text.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/add_text.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -63,7 +63,8 @@ """
# -# (C) Filnik, 2007-2008 +# (C) Filnik, 2007-2009 +# (C) Pywikipedia bot team, 2007-2010 # # Distributed under the terms of the MIT license. # @@ -71,7 +72,7 @@ #
import re, pagegenerators, urllib2, urllib -import wikipedia +import wikipedia as pywikibot import codecs, config
# This is required for the text that is shown when you run this script @@ -104,10 +105,10 @@
nn_iw_msg = u'<!--interwiki (no, sv, da first; then other languages alphabetically by name)-->'
-class NoEnoughData(wikipedia.Error): +class NoEnoughData(pywikibot.Error): """ Error class for when the user doesn't specified all the data needed """
-class NothingFound(wikipedia.Error): +class NothingFound(pywikibot.Error): """ An exception indicating that a regex has return [] instead of results."""
# Useful for the untagged function @@ -115,13 +116,13 @@ """ Function to load HTML text of a URL """ try: request = urllib2.Request(url) - request.add_header("User-Agent", wikipedia.useragent) + request.add_header("User-Agent", pywikibot.useragent) response = urllib2.urlopen(request) text = response.read() response.close() # When you load to many users, urllib2 can give this error. except urllib2.HTTPError: - wikipedia.output(u"Server error. Pausing for 10 seconds... " + time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) ) + pywikibot.output(u"Server error. Pausing for 10 seconds... " + time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) ) response.close() time.sleep(10) return pageText(url) @@ -146,7 +147,7 @@ 'Nothing found! Try to use the tool by yourself to be sure that it works!') else: for result in results: - yield wikipedia.Page(wikipedia.getSite(), result) + yield pywikibot.Page(pywikibot.getSite(), result)
def add_text(page = None, addText = None, summary = None, regexSkip = None, regexSkipUrl = None, always = False, up = False, putText = True, @@ -154,7 +155,7 @@ if not addText: raise NoEnoughData('You have to specify what text you want to add!') if not summary: - summary = wikipedia.translate(wikipedia.getSite(), msg) % addText[:200] + summary = pywikibot.translate(pywikibot.getSite(), msg) % addText[:200]
# When a page is tagged as "really well written" it has a star in the # interwiki links. This is a list of all the templates used (in regex @@ -188,25 +189,25 @@ ]
errorCount = 0 - site = wikipedia.getSite() + site = pywikibot.getSite() # /wiki/ is not always the right path in non-wiki projects pathWiki = site.family.nicepath(site.lang)
if putText: - wikipedia.output(u'Loading %s...' % page.title()) + pywikibot.output(u'Loading %s...' % page.title()) if oldTextGiven == None: try: text = page.get() - except wikipedia.NoPage: + except pywikibot.NoPage: if create: - wikipedia.output(u"%s doesn't exist, creating it!" + pywikibot.output(u"%s doesn't exist, creating it!" % page.title()) text = u'' else: - wikipedia.output(u"%s doesn't exist, skip!" % page.title()) + pywikibot.output(u"%s doesn't exist, skip!" % page.title()) return (False, False, always) # continue - except wikipedia.IsRedirectPage: - wikipedia.output(u"%s is a redirect, skip!" % page.title()) + except pywikibot.IsRedirectPage: + pywikibot.output(u"%s is a redirect, skip!" % page.title()) return (False, False, always) # continue else: text = oldTextGiven @@ -216,27 +217,28 @@ url = '%s%s' % (pathWiki, page.urlname()) result = re.findall(regexSkipUrl, site.getUrl(url)) if result != []: - wikipedia.output( + pywikibot.output( u'Exception! regex (or word) used with -exceptUrl is in the page. Skip!') return (False, False, always) # continue if regexSkip != None: result = re.findall(regexSkip, text) if result != []: - wikipedia.output( + pywikibot.output( u'Exception! regex (or word) used with -except is in the page. Skip!') return (False, False, always) # continue # If not up, text put below if not up: newtext = text # Getting the categories - categoriesInside = wikipedia.getCategoryLinks(newtext, site) + categoriesInside = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories - newtext = wikipedia.removeCategoryLinks(newtext, site) + newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki - interwikiInside = wikipedia.getLanguageLinks(newtext, site) + interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki - newtext = wikipedia.removeLanguageLinks(newtext, site) - #nn got a message between the categories and the iw's and they want to keep it there, first remove it + newtext = pywikibot.removeLanguageLinks(newtext, site) + # nn got a message between the categories and the iw's and they want to + # keep it there, first remove it if (site.language()==u'nn'): newtext = newtext.replace(nn_iw_msg, '') # Translating the \n into binary \n @@ -244,13 +246,14 @@ # Adding the text newtext += u"\n%s" % addText # Reputting the categories - newtext = wikipedia.replaceCategoryLinks(newtext, categoriesInside, site, True) + newtext = pywikibot.replaceCategoryLinks(newtext, + categoriesInside, site, True) #Put the nn iw message back if (site.language()==u'nn'): newtext = newtext + u'\n' + nn_iw_msg # Dealing the stars' issue allstars = [] - starstext = wikipedia.removeDisabledParts(text) + starstext = pywikibot.removeDisabledParts(text) for star in starsList: regex = re.compile('({{(?:template:|)%s|.*?}}[\s]*)' % star, re.I) @@ -264,21 +267,21 @@ for element in allstars: newtext += '%s\r\n' % element.strip() # Adding the interwiki - newtext = wikipedia.replaceLanguageLinks(newtext, interwikiInside, site) + newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside, site) # If instead the text must be added above... else: newtext = addText + '\n' + text if putText and text != newtext: - wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" + pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) - wikipedia.showDiff(text, newtext) + pywikibot.showDiff(text, newtext) # Let's put the changes. while True: # If someone load it as module, maybe it's not so useful to put the # text in the page if putText: if not always: - choice = wikipedia.inputChoice( + choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': @@ -291,27 +294,27 @@ page.put(newtext, summary) else: page.put_async(newtext, summary) - except wikipedia.EditConflict: - wikipedia.output(u'Edit conflict! skip!') + except pywikibot.EditConflict: + pywikibot.output(u'Edit conflict! skip!') return (False, False, always) - except wikipedia.ServerError: + except pywikibot.ServerError: errorCount += 1 if errorCount < 5: - wikipedia.output(u'Server Error! Wait..') + pywikibot.output(u'Server Error! Wait..') time.sleep(5) continue else: - raise wikipedia.ServerError(u'Fifth Server Error!') - except wikipedia.SpamfilterError, e: - wikipedia.output( + raise pywikibot.ServerError(u'Fifth Server Error!') + except pywikibot.SpamfilterError, e: + pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) return (False, False, always) - except wikipedia.PageNotSaved, error: - wikipedia.output(u'Error putting page: %s' % error.args) + except pywikibot.PageNotSaved, error: + pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) - except wikipedia.LockedPage: - wikipedia.output(u'Skipping %s (locked page)' + except pywikibot.LockedPage: + pywikibot.output(u'Skipping %s (locked page)' % page.title()) return (False, False, always) else: @@ -333,44 +336,44 @@ # Put the text above or below the text? up = False # Loading the arguments - for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-textfile'): if len(arg) == 9: - textfile = wikipedia.input( + textfile = pywikibot.input( u'Which textfile do you want to add?') else: textfile = arg[10:] elif arg.startswith('-text'): if len(arg) == 5: - addText = wikipedia.input(u'What text do you want to add?') + addText = pywikibot.input(u'What text do you want to add?') else: addText = arg[6:] elif arg.startswith('-summary'): if len(arg) == 8: - summary = wikipedia.input(u'What summary do you want to use?') + summary = pywikibot.input(u'What summary do you want to use?') else: summary = arg[9:] elif arg.startswith('-page'): if len(arg) == 5: - generator = [wikipedia.Page( - wikipedia.getSite(), - wikipedia.input(u'What page do you want to use?') + generator = [pywikibot.Page( + pywikibot.getSite(), + pywikibot.input(u'What page do you want to use?') )] else: - generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])] + generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])] elif arg.startswith('-excepturl'): if len(arg) == 10: - regexSkipUrl = wikipedia.input(u'What text should I skip?') + regexSkipUrl = pywikibot.input(u'What text should I skip?') else: regexSkipUrl = arg[11:] elif arg.startswith('-except'): if len(arg) == 7: - regexSkip = wikipedia.input(u'What text should I skip?') + regexSkip = pywikibot.input(u'What text should I skip?') else: regexSkip = arg[8:] elif arg.startswith('-untagged'): if len(arg) == 9: - untaggedProject = wikipedia.input( + untaggedProject = pywikibot.input( u'What project do you want to use?') else: untaggedProject = arg[10:] @@ -395,7 +398,7 @@ 'You have to specify the generator you want to use for the script!') if talkPage: generator = pagegenerators.PageWithTalkPageGenerator(generator) - site = wikipedia.getSite() + site = pywikibot.getSite() for namespace in site.namespaces(): index = site.getNamespaceIndex(namespace) if index%2==1 and index>0: @@ -412,4 +415,4 @@ try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/archivebot.py =================================================================== --- trunk/pywikipedia/archivebot.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/archivebot.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -67,14 +67,16 @@ -L LANG, --lang=LANG set the language code to work on """ # -# (C) Misza13, 2006-2007 +# (C) Misza13, 2006-2010 +# (C) Pywikipedia bot team, 2007-2010 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' # -import wikipedia, pagegenerators, query -Site = wikipedia.getSite() +import wikipedia as pywikibot +import pagegenerators, query +Site = pywikibot.getSite()
import os, re, time, locale, traceback, string, urllib
@@ -85,7 +87,6 @@ import md5 new_hash = md5.md5
- language = Site.language() messages = { '_default': { @@ -138,7 +139,6 @@ 'ArchiveSummary': u'Arkiverer %(count)d trådar frå [[%(from)s]].', 'OlderThanSummary': u'eldre enn', }, - # fix below 'pt': { 'ArchiveFull': u'(ARQUIVO COMPLETO)', 'InitialArchiveHeader': u'{{talkarchive}}', @@ -168,25 +168,20 @@ lang = '_default' return messages[lang][key]
- -class MalformedConfigError(wikipedia.Error): +class MalformedConfigError(pywikibot.Error): """There is an error in the configuration template."""
- -class MissingConfigError(wikipedia.Error): +class MissingConfigError(pywikibot.Error): """The config is missing in the header (either it's in one of the threads or transcluded from another page)."""
- class AlgorithmError(MalformedConfigError): """Invalid specification of archiving algorithm."""
- -class ArchiveSecurityError(wikipedia.Error): +class ArchiveSecurityError(pywikibot.Error): """Archive is not a subpage of page being archived and key not specified (or incorrect)."""
- def str2time(str): """Accepts a string defining a time period: 7d - 7 days @@ -199,7 +194,6 @@ else: return int(str)
- def str2size(str): """Accepts a string defining a size: 1337 - 1337 bytes @@ -218,23 +212,22 @@ else: return (int(str[:-1])*1024,'B')
- def int2month(num): """Returns the locale's full name of month 'num' (1-12).""" if hasattr(locale, 'nl_langinfo'): return locale.nl_langinfo(locale.MON_1+num-1).decode('utf-8') - Months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] + Months = ['january', 'february', 'march', 'april', 'may', 'june', + 'july', 'august', 'september', 'october', 'november', 'december'] return Site.mediawiki_message(Months[num-1])
- def int2month_short(num): """Returns the locale's abbreviated name of month 'num' (1-12).""" if hasattr(locale, 'nl_langinfo'): return locale.nl_langinfo(locale.ABMON_1+num-1).replace('\xa0', '').decode('utf-8') - Months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] + Months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', + 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] return Site.mediawiki_message(Months[num-1])
- def txt2timestamp(txt, format): """Attempts to convert the timestamp 'txt' according to given 'format'. On success, returns the time tuple; on failure, returns None.""" @@ -248,7 +241,6 @@ pass return None
- def generateTransclusions(Site, template, namespaces=[], eicontinue=''): qdata = { 'action' : 'query', @@ -261,18 +253,18 @@ if eicontinue: qdata['eicontinue'] = eicontinue
- wikipedia.output(u'Fetching template transclusions...') + pywikibot.output(u'Fetching template transclusions...') response, result = query.GetData(qdata, Site, back_response = True)
for page_d in result['query']['embeddedin']: - yield wikipedia.Page(Site, page_d['title']) + yield pywikibot.Page(Site, page_d['title'])
if 'query-continue' in result: eicontinue = result['query-continue']['embeddedin']['eicontinue'] - for page in generateTransclusions(Site, template, namespaces, eicontinue): + for page in generateTransclusions(Site, template, namespaces, + eicontinue): yield page
- class DiscussionThread(object): """An object representing a discussion thread on a page, that is something of the form:
@@ -288,7 +280,8 @@ self.timestamp = None
def __repr__(self): - return '%s("%s",%d bytes)' % (self.__class__.__name__,self.title,len(self.content)) + return '%s("%s",%d bytes)' \ + % (self.__class__.__name__,self.title,len(self.content))
def feedLine(self, line): if not self.content and not line: @@ -318,7 +311,7 @@ pat = re.compile(r'(\d\d):(\d\d), (\d\d?). (\S+).? (\d\d\d\d) (UTC)') TM = pat.search(line) if TM: -# wikipedia.output(TM) +# pywikibot.output(TM) TIME = txt2timestamp(TM.group(0),"%d. %b %Y kl. %H:%M (%Z)") if not TIME: TIME = txt2timestamp(TM.group(0), "%Y. %B %d., %H:%M (%Z)") @@ -344,9 +337,9 @@ TIME = txt2timestamp(TM.group(0),"%H:%M, %d. %b. %Y (%Z)") if TIME: self.timestamp = max(self.timestamp,time.mktime(TIME)) -# wikipedia.output(u'Time to be parsed: %s' % TM.group(0)) -# wikipedia.output(u'Parsed time: %s' % TIME) -# wikipedia.output(u'Newest timestamp in thread: %s' % TIME) +# pywikibot.output(u'Time to be parsed: %s' % TM.group(0)) +# pywikibot.output(u'Parsed time: %s' % TIME) +# pywikibot.output(u'Newest timestamp in thread: %s' % TIME)
def size(self): return len(self.title) + len(self.content) + 12 @@ -367,23 +360,23 @@ return message('OlderThanSummary') + ' ' + reT.group(1) return ''
- class DiscussionPage(object): - """A class that represents a single discussion page as well as an archive page. - Feed threads to it and run an update() afterwards.""" - #TODO: Make it a subclass of wikipedia.Page + """A class that represents a single discussion page as well as an archive + page. Feed threads to it and run an update() afterwards.""" + #TODO: Make it a subclass of pywikibot.Page
def __init__(self, title, archiver, vars=None): self.title = title self.threads = [] - self.Page = wikipedia.Page(Site,self.title) + self.Page = pywikibot.Page(Site,self.title) self.full = False self.archiver = archiver self.vars = vars try: self.loadPage() - except wikipedia.NoPage: - self.header = archiver.get('archiveheader',message('InitialArchiveHeader')) + except pywikibot.NoPage: + self.header = archiver.get('archiveheader', + message('InitialArchiveHeader')) if self.vars: self.header = self.header % self.vars
@@ -427,7 +420,7 @@
def update(self, summary, sortThreads = False): if sortThreads: - wikipedia.output(u'Sorting threads...') + pywikibot.output(u'Sorting threads...') self.threads.sort(key = lambda t: t.timestamp) newtext = re.sub('\n*$','\n\n',self.header) #Fix trailing newlines for t in self.threads: @@ -436,10 +429,9 @@ summary += ' ' + message('ArchiveFull') self.Page.put(newtext, minorEdit=True, comment=summary)
- class PageArchiver(object): """A class that encapsulates all archiving methods. - __init__ expects a wikipedia.Page object. + __init__ expects a pywikibot.Page object. Execute by running the .run() method."""
algo = 'none' @@ -472,10 +464,14 @@ self.attributes[attr] = [value, out]
def saveables(self): - return [a for a in self.attributes if self.attributes[a][1] and a != 'maxage'] + return [a for a in self.attributes if self.attributes[a][1] + and a != 'maxage']
def attr2text(self): - return '{{%s\n%s\n}}' % (self.tpl,'\n'.join(['|%s = %s'%(a,self.get(a)) for a in self.saveables()])) + return '{{%s\n%s\n}}' \ + % (self.tpl, + '\n'.join(['|%s = %s'%(a,self.get(a)) + for a in self.saveables()]))
def key_ok(self): s = new_hash() @@ -485,7 +481,7 @@
def loadConfig(self): hdrlines = self.Page.header.split('\n') -# wikipedia.output(u'Looking for: %s' % self.tpl) +# pywikibot.output(u'Looking for: %s' % self.tpl) mode = 0 for line in hdrlines: if mode == 0 and re.search('{{'+self.tpl,line): @@ -507,11 +503,14 @@ def feedArchive(self, archive, thread, maxArchiveSize, vars=None): """Feed the thread to one of the archives. If it doesn't exist yet, create it. - If archive name is an empty string (or None), discard the thread (/dev/null). + If archive name is an empty string (or None), + discard the thread (/dev/null). Also checks for security violations.""" if not archive: return - if not self.force and not self.Page.title+'/' == archive[:len(self.Page.title)+1] and not self.key_ok(): + if not self.force \ + and not self.Page.title+'/' == archive[:len(self.Page.title)+1] \ + and not self.key_ok(): raise ArchiveSecurityError if not archive in self.archives: self.archives[archive] = DiscussionPage(archive,self,vars) @@ -525,10 +524,12 @@ T = time.mktime(time.gmtime()) whys = [] for t in oldthreads: - if len(oldthreads) - self.archivedThreads <= int(self.get('minthreadsleft',5)): + if len(oldthreads) - self.archivedThreads \ + <= int(self.get('minthreadsleft',5)): self.Page.threads.append(t) continue #Because there's too little threads left. - #TODO: Make an option so that unstamped (unsigned) posts get archived. + # TODO: Make an option so that unstamped (unsigned) posts get + # archived. why = t.shouldBeArchived(self) if why: archive = self.get('archive') @@ -556,7 +557,9 @@ return whys = self.analyzePage() if self.archivedThreads < int(self.get('minthreadstoarchive',2)): - return #We might not want to archive a measly few threads (lowers edit frequency) + # We might not want to archive a measly few threads + # (lowers edit frequency) + return if whys: #Save the archives first (so that bugs don't cause a loss of data) for a in sorted(self.archives.keys()): @@ -568,14 +571,14 @@ rx = re.compile('{{'+self.tpl+'\n.*?\n}}',re.DOTALL) self.Page.header = rx.sub(self.attr2text(),self.Page.header) self.commentParams['count'] = self.archivedThreads - self.commentParams['archives'] = ', '.join(['[['+a.title+']]' for a in self.archives.values()]) + self.commentParams['archives'] \ + = ', '.join(['[['+a.title+']]' for a in self.archives.values()]) if not self.commentParams['archives']: self.commentParams['archives'] = '/dev/null' self.commentParams['why'] = ', '.join(whys) comment = self.pageSummary % self.commentParams self.Page.update(comment)
- def main(): global Site, language from optparse import OptionParser @@ -601,7 +604,8 @@ (options, args) = parser.parse_args()
if options.locale: - locale.setlocale(locale.LC_TIME,options.locale) #Required for english month names + #Required for english month names + locale.setlocale(locale.LC_TIME,options.locale)
if options.timezone: os.environ['TZ'] = options.timezone @@ -615,7 +619,7 @@ s = new_hash() s.update(options.salt+'\n') s.update(options.calc+'\n') - wikipedia.output(u'key = ' + s.hexdigest()) + pywikibot.output(u'key = ' + s.hexdigest()) return
if options.salt: @@ -629,14 +633,14 @@ force = False
if options.lang: - Site = wikipedia.getSite(options.lang) + Site = pywikibot.getSite(options.lang) language = Site.language() - if wikipedia.debug: print Site + if pywikibot.debug: print Site
for a in args: pagelist = [] if not options.filename and not options.pagename: - #for pg in wikipedia.Page(Site,a).getReferences(follow_redirects=False,onlyTemplateInclusion=True): + #for pg in pywikibot.Page(Site,a).getReferences(follow_redirects=False,onlyTemplateInclusion=True): if not options.namespace == None: ns = [str(options.namespace)] else: @@ -645,26 +649,28 @@ pagelist.append(pg) if options.filename: for pg in file(options.filename,'r').readlines(): - pagelist.append(wikipedia.Page(Site,pg)) + pagelist.append(pywikibot.Page(Site,pg)) if options.pagename: - pagelist.append(wikipedia.Page(Site,options.pagename)) + pagelist.append(pywikibot.Page(Site,options.pagename))
pagelist = sorted(pagelist) #if not options.namespace == None: # pagelist = [pg for pg in pagelist if pg.namespace()==options.namespace]
for pg in pagelist: - try: #Catching exceptions, so that errors in one page do not bail out the entire process + # Catching exceptions, so that errors in one page do not bail out + # the entire process + try: Archiver = PageArchiver(pg, a, salt, force) Archiver.run() time.sleep(10) except: - wikipedia.output(u'Error occured while processing page %s' % pg.aslink(True)) + pywikibot.output(u'Error occured while processing page %s' + % pg.aslink(True)) traceback.print_exc()
- if __name__ == '__main__': try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/articlenos.py =================================================================== --- trunk/pywikipedia/articlenos.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/articlenos.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -1,10 +1,12 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- """ -A script that displays the ordinal number of the new articles being created visible on the Recent Changes list. -The script doesn't make any edits, no bot account needed. +A script that displays the ordinal number of the new articles being created +visible on the Recent Changes list. The script doesn't make any edits, no bot +account needed.
-Note: the script requires the Python IRC library http://python-irclib.sourceforge.net/ +Note: the script requires the Python IRC library +http://python-irclib.sourceforge.net/ """
# Author: Balasyum @@ -13,8 +15,9 @@ __version__ = '$Id$'
from ircbot import SingleServerIRCBot -from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr -import wikipedia +from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad +from irclib import ip_quad_to_numstr +import wikipedia as pywikibot import re
class ArtNoDisp(SingleServerIRCBot): @@ -22,11 +25,13 @@ SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel self.site = site - self.other_ns = re.compile(u'14[[07(' + u'|'.join(site.namespaces()) + u')') + self.other_ns = re.compile( + u'14[[07(' + u'|'.join(site.namespaces()) + u')') self.api_url = self.site.api_address() self.api_url += 'action=query&meta=siteinfo&siprop=statistics&format=xml' self.api_found = re.compile(r'articles="(.*?)"') - self.re_edit = re.compile(r'^C14[[^C07(?P<page>.+?)^C14]]^C4 (?P<flags>.*?)^C10 ^C02(?P<url>.+?)^C ^C5*^C ^C03(?P<user>.+?)^C ^C5*^C (?^B?(?P<bytes>[+-]?\d+?)^B?) ^C10(?P<summary>.*)^C'.replace('^B', '\002').replace('^C', '\003').replace('^U', '\037')) + self.re_edit = re.compile( + r'^C14[[^C07(?P<page>.+?)^C14]]^C4 (?P<flags>.*?)^C10 ^C02(?P<url>.+?)^C ^C5*^C ^C03(?P<user>.+?)^C ^C5*^C (?^B?(?P<bytes>[+-]?\d+?)^B?) ^C10(?P<summary>.*)^C'.replace('^B', '\002').replace('^C', '\003').replace('^U', '\037'))
def on_nicknameinuse(self, c, e): c.nick(c.get_nickname() + "_") @@ -52,12 +57,12 @@ name = msg[8:msg.find(u'14',9)] text = self.site.getUrl(self.api_url) entry = self.api_found.findall(text) - page = wikipedia.Page(self.site, name) + page = pywikibot.Page(self.site, name) try: text = page.get() - except wikipedia.NoPage: + except pywikibot.NoPage: return - except wikipedia.IsRedirectPage: + except pywikibot.IsRedirectPage: return print entry[0], name
@@ -74,7 +79,7 @@ pass
def main(): - site = wikipedia.getSite() + site = pywikibot.getSite() site.forceLogin() chan = '#' + site.language() + '.' + site.family.name bot = ArtNoDisp(site, chan, site.loggedInAs(), "irc.wikimedia.org")
Modified: trunk/pywikipedia/basic.py =================================================================== --- trunk/pywikipedia/basic.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/basic.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -15,7 +15,14 @@ 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-2010 +# +# Distributed under the terms of the MIT license. +# __version__ = '$Id$' +# + import wikipedia as pywikibot import pagegenerators
@@ -69,10 +76,12 @@ # Load the page text = page.get() except pywikibot.NoPage: - pywikibot.output(u"Page %s does not exist; skipping." % page.aslink()) + pywikibot.output(u"Page %s does not exist; skipping." + % page.aslink()) return except pywikibot.IsRedirectPage: - pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink()) + pywikibot.output(u"Page %s is a redirect; skipping." + % page.aslink()) return
################################################################ @@ -87,7 +96,8 @@ if text != page.get(): # Show the title of the page we're working on. # Highlight the title in purple. - pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) + pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" + % page.title()) # show what was changed pywikibot.showDiff(page.get(), text) if not self.dry: @@ -97,11 +107,16 @@ # Save the page page.put(text, comment=self.summary) except pywikibot.LockedPage: - pywikibot.output(u"Page %s is locked; skipping." % page.aslink()) + pywikibot.output(u"Page %s is locked; skipping." + % page.aslink()) except pywikibot.EditConflict: - pywikibot.output(u'Skipping %s because of edit conflict' % (page.title())) + pywikibot.output( + u'Skipping %s because of edit conflict' + % (page.title())) except pywikibot.SpamfilterError, error: - pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url)) + pywikibot.output( +u'Cannot change %s because of spam blacklist entry %s' + % (page.title(), error.url))
def main():
Modified: trunk/pywikipedia/casechecker.py =================================================================== --- trunk/pywikipedia/casechecker.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/casechecker.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -16,12 +16,17 @@ for cc in xuniqueCombinations(items[i+1:],n-1): yield [items[i]]+cc # End of permutation code - +# +# (C) Pywikipedia bot team, 2006-2010 +# +# Distributed under the terms of the MIT license. +# __version__ = '$Id$'
# # Windows Concose colors -# This code makes this script Windows ONLY!!! Feel free to adapt it to another platform +# This code makes this script Windows ONLY!!! +# Feel free to adapt it to another platform # # Adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496901 # @@ -48,7 +53,8 @@ def SetColor(color): if std_out_handle: try: - return ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color) + return ctypes.windll.kernel32.SetConsoleTextAttribute( + std_out_handle, color) except: pass
@@ -59,7 +65,8 @@ # end of console code
import os -import sys, query, wikipedia, re, codecs +import sys, query, re, codecs +import wikipedia as pywikibot
class CaseChecker( object ): @@ -74,7 +81,8 @@ 'ru': u'[[ВП:КЛ]]', }
- # These words are always in one language, even though they could be typed in both + # These words are always in one language, even though they could be typed + # in both alwaysInLocal = [ u'СССР', u'Как', u'как' ] alwaysInLatin = [ u'II', u'III' ]
@@ -85,12 +93,16 @@ localSuspects = u'АВЕКМНОРСТХІЁЇаеорсухіёї' latinSuspects = u'ABEKMHOPCTXIËÏaeopcyxiëï'
- localKeyboard = u'йцукенгшщзфывапролдячсмить' # possibly try to fix one character mistypes in an alternative keyboard layout + # possibly try to fix one character mistypes in an alternative keyboard + # layout + localKeyboard = u'йцукенгшщзфывапролдячсмить' latinKeyboard = u'qwertyuiopasdfghjklzxcvbnm'
romanNumChars = u'IVXLMC' - romannumSuffixes = localLowerLtr # all letters that may be used as suffixes after roman numbers: "Iый" - romanNumSfxPtrn = re.compile(u'^[' + romanNumChars + ']+[' + localLowerLtr + ']+$') + # all letters that may be used as suffixes after roman numbers: "Iый" + romannumSuffixes = localLowerLtr + romanNumSfxPtrn = re.compile( + u'^[' + romanNumChars + ']+[' + localLowerLtr + ']+$')
whitelists = { 'ru': u'ВП:КЛ/Whitelist' @@ -118,12 +130,12 @@
def __init__(self):
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-from'): if arg.startswith('-from:'): self.apfrom = arg[6:] else: - self.apfrom = wikipedia.input(u'Which page to start from: ') + self.apfrom = pywikibot.input(u'Which page to start from: ') elif arg.startswith('-reqsize:'): self.aplimit = int(arg[9:]) elif arg == '-links': @@ -142,13 +154,14 @@ elif arg.startswith('-wikilog:'): self.wikilogfile = arg[9:] else: - wikipedia.output(u'Unknown argument %s.' % arg) - wikipedia.showHelp() + pywikibot.output(u'Unknown argument %s.' % arg) + pywikibot.showHelp() sys.exit()
if self.namespaces == []: if self.apfrom == u'': - self.namespaces = [14, 10, 12, 0] # 0 should be after templates ns + # 0 should be after templates ns + self.namespaces = [14, 10, 12, 0] else: self.namespaces = [0]
@@ -161,7 +174,7 @@ self.params['prop'] = 'links|categories'
- self.site = wikipedia.getSite() + self.site = pywikibot.getSite()
if len(self.localSuspects) != len(self.latinSuspects): raise ValueError(u'Suspects must be the same size') @@ -169,7 +182,7 @@ raise ValueError(u'Keyboard info must be the same size')
if not os.path.isabs(self.wikilogfile): - self.wikilogfile = wikipedia.config.datafilepath(self.wikilogfile) + self.wikilogfile = pywikibot.config.datafilepath(self.wikilogfile) try: self.wikilog = codecs.open(self.wikilogfile, 'a', 'utf-8') except IOError: @@ -177,29 +190,35 @@
self.lclToLatDict = dict([(ord(self.localSuspects[i]), self.latinSuspects[i]) - for i in range(len(self.localSuspects))]) + for i in range(len(self.localSuspects))]) self.latToLclDict = dict([(ord(self.latinSuspects[i]), self.localSuspects[i]) - for i in range(len(self.localSuspects))]) + for i in range(len(self.localSuspects))])
if self.localKeyboard is not None: - self.lclToLatKeybDict = dict([(ord(self.localKeyboard[i]), - self.latinKeyboard[i]) - for i in range(len(self.localKeyboard))]) - self.latToLclKeybDict = dict([(ord(self.latinKeyboard[i]), - self.localKeyboard[i]) - for i in range(len(self.localKeyboard))]) + self.lclToLatKeybDict = dict( + [(ord(self.localKeyboard[i]), + self.latinKeyboard[i]) + for i in range(len(self.localKeyboard))]) + self.latToLclKeybDict = dict( + [(ord(self.latinKeyboard[i]), + self.localKeyboard[i]) + for i in range(len(self.localKeyboard))]) else: self.lclToLatKeybDict = {} self.latToLclKeybDict = {}
- badPtrnStr = u'([%s][%s]|[%s][%s])' % (self.latLtr, self.localLtr, self.localLtr, self.latLtr) - self.badWordPtrn = re.compile(u'[%s%s]*%s[%s%s]*' % (self.latLtr, self.localLtr, badPtrnStr, self.latLtr, self.localLtr) ) + badPtrnStr = u'([%s][%s]|[%s][%s])' \ + % (self.latLtr, self.localLtr, self.localLtr, self.latLtr) + self.badWordPtrn = re.compile(u'[%s%s]*%s[%s%s]*' + % (self.latLtr, self.localLtr, + badPtrnStr, self.latLtr, + self.localLtr))
# Get whitelist if self.site.lang in self.whitelists: wlpage = self.whitelists[self.site.lang] - wikipedia.output(u'Loading whitelist from %s' % wlpage) + pywikibot.output(u'Loading whitelist from %s' % wlpage) wlparams = { 'action' : 'query', 'prop' : 'links', @@ -208,18 +227,22 @@ 'indexpageids' : '', }
- data = query.GetData(wlparams, wikipedia.getSite(self.site.lang), encodeTitle=False) + data = query.GetData(wlparams, pywikibot.getSite(self.site.lang), + encodeTitle=False) if len(data['query']['pageids']) == 1: pageid = data['query']['pageids'][0] links = data['query']['pages'][pageid]['links'] self.knownWords = set( [n['title'] for n in links] ) else: raise "The number of pageids is not 1" - wikipedia.output(u'Loaded whitelist with %i items' % len(self.knownWords)) - if wikipedia.verbose and len(self.knownWords) > 0: - wikipedia.output(u'Whitelist: [[%s]]' % u']], [['.join(self.knownWords)) + pywikibot.output(u'Loaded whitelist with %i items' + % len(self.knownWords)) + if pywikibot.verbose and len(self.knownWords) > 0: + pywikibot.output( + u'Whitelist: [[%s]]' % u']], [['.join(self.knownWords)) else: - wikipedia.output(u'Whitelist is not known for language %s' % self.site.lang) + pywikibot.output(u'Whitelist is not known for language %s' + % self.site.lang) self.knownWords = set()
def Run(self): @@ -235,7 +258,8 @@ self.params['gapfrom'] = self.apfrom data = query.GetData(self.params, self.site) try: - self.apfrom = data['query-continue']['allpages']['gapfrom'] + self.apfrom = data['query-continue'] \ + ['allpages']['gapfrom'] except: self.apfrom = None
@@ -248,7 +272,7 @@ if firstItem: if lastLetter != title[0]: try: - print 'Processing ' + title + print 'Processing %s' % title except: print 'Processing unprintable title' lastLetter = title[0] @@ -258,14 +282,24 @@ if err: changed = False if self.replace: - newTitle = self.PickTarget(False, title, title, err[1]) + newTitle = self.PickTarget(False, + title, + title, + err[1]) if newTitle: - editSummary = wikipedia.translate(self.site, self.msgRename) - src = wikipedia.Page(self.site, title) + editSummary = pywikibot.translate( + self.site, self.msgRename) + src = pywikibot.Page(self.site, + title) if page['ns'] == 14: import category - dst = wikipedia.Page(self.site, newTitle) - bot = category.CategoryMoveRobot(src.titleWithoutNamespace(), dst.titleWithoutNamespace(), self.autonomous, editSummary, True) + dst = pywikibot.Page(self.site, + newTitle) + bot = category.CategoryMoveRobot( + src.titleWithoutNamespace(), + dst.titleWithoutNamespace(), + self.autonomous, + editSummary, True) bot.run() else: src.move(newTitle, editSummary) @@ -296,47 +330,76 @@ if err: newTitle = None if self.replace: - newTitle = self.PickTarget(True, title, ltxt, err[1]) + newTitle = self.PickTarget( + True, title, ltxt, err[1]) if newTitle: if pageObj is None: - pageObj = wikipedia.Page(self.site, title) + pageObj = pywikibot.Page( + self.site, title) pageTxt = pageObj.get() - msg.append(u'[[%s]] => [[%s]]' % (ltxt, newTitle)) + msg.append( + u'[[%s]] => [[%s]]' + % (ltxt, newTitle)) # pageTxt = pageTxt.replace(ltxt, newTitle) # pageTxt = pageTxt.replace(ltxt[0].lower() + ltxt[1:], newTitle[0].lower() + newTitle[1:]) # pageTxt = pageTxt.replace(ltxt.replace(u' ', '_'), newTitle)
- frmParts = self.wordBreaker.split(ltxt) - toParts = self.wordBreaker.split(newTitle) - if len(frmParts) != len(toParts): - raise ValueError(u'Splitting parts do not match counts') - for i in range(0, len(frmParts)): - if len(frmParts[i]) != len(toParts[i]): - raise ValueError(u'Splitting parts do not match word length') + frmParts = self.wordBreaker.split( + ltxt) + toParts = self.wordBreaker.split( + newTitle) + if len(frmParts) != len( + toParts): + raise ValueError( +u'Splitting parts do not match counts') + for i in range(0, + len(frmParts)): + if len(frmParts[i]) != len( + toParts[i]): + raise ValueError( +u'Splitting parts do not match word length') if len(frmParts[i]) > 0: - pageTxt = pageTxt.replace(frmParts[i], toParts[i]) - pageTxt = pageTxt.replace(frmParts[i][0].lower() + frmParts[i][1:], toParts[i][0].lower() + toParts[i][1:]) + pageTxt = pageTxt.replace( + frmParts[i], toParts[i]) + pageTxt = pageTxt.replace( + frmParts[i][0].lower() + frmParts[i][1:], + toParts[i][0].lower() + toParts[i][1:])
if not newTitle: if not printed: - self.WikiLog(u"* [[:%s]]: link to %s" % (title, err[0])) + self.WikiLog( +u"* [[:%s]]: link to %s" % (title, err[0])) printed = True else: - self.WikiLog(u"** link to %s" % err[0]) + self.WikiLog( + u"** link to %s" + % err[0])
if pageObj is not None: - coloredMsg = u', '.join([self.ColorCodeWord(m) for m in msg]) + coloredMsg = u', '.join( + [self.ColorCodeWord(m) + for m in msg]) if pageObj.get() == pageTxt: - self.WikiLog(u"* Error: Text replacement failed in [[:%s]] (%s)" % (title, coloredMsg)) + self.WikiLog( +u"* Error: Text replacement failed in [[:%s]] (%s)" % (title, coloredMsg)) else: - wikipedia.output(u'Case Replacements: %s' % u', '.join(msg)) + pywikibot.output( + u'Case Replacements: %s' + % u', '.join(msg)) try: - pageObj.put(pageTxt, u'%s: %s' % (wikipedia.translate(self.site, self.msgLinkReplacement), u', '.join(msg))) + pageObj.put( + pageTxt, + u'%s: %s' + % (pywikibot.translate( + self.site, + self.msgLinkReplacement), + u', '.join(msg))) except KeyboardInterrupt: raise except: - self.WikiLog(u"* Error: Could not save updated page [[:%s]] (%s)" % (title, coloredMsg)) + self.WikiLog( +u"* Error: Could not save updated page [[:%s]] (%s)" % (title, coloredMsg))
count += 1 @@ -352,11 +415,11 @@
except: if self.apfrom is not None: - wikipedia.output(u'Exception at Title = %s, Next = %s' % (title, self.apfrom)) + pywikibot.output(u'Exception at Title = %s, Next = %s' % (title, self.apfrom)) raise
def WikiLog(self, text): - wikipedia.output(text) + pywikibot.output(text) self.wikilog.write(text + u'\n') self.wikilog.flush()
@@ -385,7 +448,8 @@ mapLat = {} found = True
- # See if it would make sense to treat the whole word as either cyrilic or latin + # See if it would make sense to treat the whole word as either + # cyrilic or latin mightBeLat = mightBeLcl = True for l in badWord: if l in self.localLtr: @@ -409,7 +473,8 @@ mapLat[badWord] = badWord.translate(self.lclToLatDict) if mightBeLcl and mightBeLat: ambigBadWords.add(badWord) - ambigBadWordsCount += 1 # Cannot do len(ambigBadWords) because they might be duplicates + # Cannot do len(ambigBadWords) because they might be duplicates + ambigBadWordsCount += 1 count += 1
if not found: @@ -422,7 +487,8 @@ # We cannot auto-translate - offer a list of suggested words suggestions = mapLcl.values() + mapLat.values() if len(suggestions) > 0: - infoText += u", word suggestions: " + u', '.join([self.ColorCodeWord(t) for t in suggestions]) + infoText += u", word suggestions: " + u', '.join( + [self.ColorCodeWord(t) for t in suggestions]) else: infoText += u", no suggestions" else: @@ -437,12 +503,14 @@ possibleAlternatives.append(title) infoText += u", convert to " + self.MakeLink(title) else: - # Try to pick 0, 1, 2, ..., len(ambiguous words) unique combinations - # from the bad words list, and convert just the picked words to cyrilic, - # whereas making all other words as latin character. + # Try to pick 0, 1, 2, ..., len(ambiguous words) unique + # combinations from the bad words list, and convert just the + # picked words to cyrilic, whereas making all other words as + # latin character. for itemCntToPick in range(0, len(ambigBadWords)+1): title2 = title - for uc in xuniqueCombinations(list(ambigBadWords), itemCntToPick): + for uc in xuniqueCombinations(list(ambigBadWords), + itemCntToPick): wordsToLat = ambigBadWords.copy() for bw in uc: title2 = title2.replace(bw, mapLcl[bw]) @@ -452,7 +520,8 @@ possibleAlternatives.append(title2)
if len(possibleAlternatives) > 0: - infoText += u", can be converted to " + u', '.join([self.MakeLink(t) for t in possibleAlternatives]) + infoText += u", can be converted to " + u', '.join( + [self.MakeLink(t) for t in possibleAlternatives]) else: infoText += u", no suggestions"
@@ -471,7 +540,7 @@ pagesExist = []
for newTitle in candidates: - dst = wikipedia.Page(self.site, newTitle) + dst = pywikibot.Page(self.site, newTitle) if not dst.exists(): pagesDontExist.append(newTitle) elif dst.isRedirectPage(): @@ -496,28 +565,31 @@ return pagesRedir.keys()[0]
if not self.autonomous: - wikipedia.output(u'Could not auto-decide for page [[%s]]. Which link should be chosen?' % title) - wikipedia.output(u'Original title: ', newline=False) + pywikibot.output(u'Could not auto-decide for page [[%s]]. Which link should be chosen?' % title) + pywikibot.output(u'Original title: ', newline=False) self.ColorCodeWord(original + "\n", True) count = 1 for t in candidates: if t in pagesDontExist: msg = u'missing' elif t in pagesRedir: msg = u'Redirect to ' + pagesRedir[t] else: msg = u'page exists' - self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True) + self.ColorCodeWord(u' %d: %s (%s)\n' + % (count, t, msg), True) count += 1
answers = [str(i) for i in range(0, count)] - choice = int(wikipedia.inputChoice(u'Which link to choose? (0 to skip)', answers, [a[0] for a in answers])) + choice = int(pywikibot.inputChoice( + u'Which link to choose? (0 to skip)', + answers, [a[0] for a in answers])) if choice > 0: return candidates[choice-1]
else: if len(candidates) == 1: newTitle = candidates[0] - dst = wikipedia.Page(self.site, newTitle) + dst = pywikibot.Page(self.site, newTitle) if not dst.exists(): - # choice = wikipedia.inputChoice(u'Move %s to %s?' % (title, newTitle), ['Yes', 'No'], ['y', 'n']) + # choice = pywikibot.inputChoice(u'Move %s to %s?' % (title, newTitle), ['Yes', 'No'], ['y', 'n']) return newTitle
return None @@ -544,7 +616,7 @@ if toScreen: SetColor(FOREGROUND_RED) else: res += self.suffixClr + self.latClrFnt lastIsCyr = False - if toScreen: wikipedia.output(l, newline=False) + if toScreen: pywikibot.output(l, newline=False) else: res += l
if toScreen: SetColor(FOREGROUND_WHITE) @@ -559,4 +631,4 @@ bot = CaseChecker() bot.Run() finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/catall.py =================================================================== --- trunk/pywikipedia/catall.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/catall.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -10,13 +10,15 @@ """ # # (C) Rob W.W. Hooft, Andre Engels, 2004 +# (C) Pywikipedia bot team, 2004-2010 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' #
-import wikipedia, sys +import wikipedia as pywikibot +import sys
msg={ 'ar':u'بوت: تغيير التصنيفات', @@ -50,7 +52,7 @@ print ("xx: if the first, remove all categories and add no new.") print ("q: quit.") while flag == False: - choice=wikipedia.input(u"?") + choice=pywikibot.input(u"?") if choice=="": flag=True elif choice=="-": @@ -61,7 +63,7 @@ editor = editarticle.TextEditor() newtext = editor.edit(pagetext) elif choice =="??": - wikipedia.output(pagetext[0:length]) + pywikibot.output(pagetext[0:length]) length = length+500 elif choice=="xx" and chosen==[]: chosen = None @@ -75,18 +77,18 @@
def make_categories(page, list, site = None): if site is None: - site = wikipedia.getSite() + site = pywikibot.getSite() pllist=[] for p in list: cattitle="%s:%s" % (site.category_namespace(), p) - pllist.append(wikipedia.Page(site,cattitle)) - page.put_async(wikipedia.replaceCategoryLinks(page.get(), pllist), - comment=wikipedia.translate(site.lang, msg)) + pllist.append(pywikibot.Page(site,cattitle)) + page.put_async(pywikibot.replaceCategoryLinks(page.get(), pllist), + comment=pywikibot.translate(site.lang, msg))
docorrections=True start=[]
-for arg in wikipedia.handleArgs(): +for arg in pywikibot.handleArgs(): if arg == '-onlynew': docorrections=False else: @@ -97,7 +99,7 @@ else: start=' '.join(start)
-mysite = wikipedia.getSite() +mysite = pywikibot.getSite()
try: for p in mysite.allpages(start = start): @@ -105,7 +107,7 @@ text=p.get() cats=p.categories() if cats == []: - wikipedia.output(u"========== %s ==========" % p.title()) + pywikibot.output(u"========== %s ==========" % p.title()) print "No categories" print "----------------------------------------" newcats=choosecats(text) @@ -113,16 +115,16 @@ make_categories(p, newcats, mysite) else: if docorrections: - wikipedia.output(u"========== %s ==========" % p.title()) + pywikibot.output(u"========== %s ==========" % p.title()) for c in cats: - wikipedia.output(c.title()) + pywikibot.output(c.title()) print "----------------------------------------" newcats=choosecats(text) if newcats is None: make_categories(p, [], mysite) elif newcats != []: make_categories(p, newcats, mysite) - except wikipedia.IsRedirectPage: - wikipedia.output(u'%s is a redirect' % p.title()) + except pywikibot.IsRedirectPage: + pywikibot.output(u'%s is a redirect' % p.title()) finally: - wikipedia.stopme() + pywikibot.stopme()
Modified: trunk/pywikipedia/commonscat.py =================================================================== --- trunk/pywikipedia/commonscat.py 2010-06-03 05:30:45 UTC (rev 8239) +++ trunk/pywikipedia/commonscat.py 2010-06-03 07:19:33 UTC (rev 8240) @@ -51,8 +51,10 @@ # Distributed under the terms of the MIT license. # __version__ = '$Id$' +#
-import wikipedia, config, pagegenerators, add_text, re +import wikipedia as pywikibot +import config, pagegenerators, add_text, re
docuReplacements = { '¶ms;': pagegenerators.parameterHelp @@ -63,44 +65,54 @@ commonscatTemplates = { '_default': (u'Commonscat', []), 'af' : (u'CommonsKategorie', [u'commonscat']), - 'ar' : (u'تصنيف كومنز', [u'Commonscat', u'تصنيف كومونز', u'Commons cat', u'CommonsCat']), + 'ar' : (u'تصنيف كومنز', [u'Commonscat', u'تصنيف كومونز', + u'Commons cat', u'CommonsCat']), 'az' : (u'CommonsKat', []), 'bn' : (u'কমন্সক্যাট', [u'Commonscat']), 'crh' : (u'CommonsKat', [u'Commonscat']), 'cs' : (u'Commonscat', [u'Commons cat']), 'da' : (u'Commonscat', [u'Commons cat', u'Commonskat', u'Commonscat2']), 'de' : (u'Commonscat', [u'CommonsCat',]), - 'en' : (u'Commons category', [u'Commoncat', u'Commons2', u'Cms-catlist-up', u'Catlst commons', u'Commonscategory', u'Commonscat', u'Commons cat']), - 'es' : (u'Commonscat', [u'Ccat', u'Commons cat', u'Categoría Commons', u'Commonscat-inline']), + 'en' : (u'Commons category', [u'Commoncat', u'Commons2', u'Cms-catlist-up', + u'Catlst commons', u'Commonscategory', + u'Commonscat', u'Commons cat']), + 'es' : (u'Commonscat', [u'Ccat', u'Commons cat', u'Categoría Commons', + u'Commonscat-inline']), 'eu' : (u'Commonskat', [u'Commonscat']), - 'fa' : (u'انبار-رده', [u'Commonscat', u'Commons cat', u'انبار رده', u'Commons category']), - 'fr' : (u'Commonscat', [u'CommonsCat', u'Commons cat', u'Commons category']), + 'fa' : (u'انبار-رده', [u'Commonscat', u'Commons cat', + u'انبار رده', u'Commons category']), + 'fr' : (u'Commonscat', [u'CommonsCat', u'Commons cat', + u'Commons category']), 'frp' : (u'Commonscat', [u'CommonsCat']), 'ga' : (u'Catcómhaoin', [u'Commonscat']), 'hi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category']), 'hu' : (u'Közvagyonkat', []), 'hy' : (u'Commons cat', [u'Commonscat']), - 'id' : (u'Commonscat', [u'Commons cat', u'Commons2', u'CommonsCat', u'Commons category']), + 'id' : (u'Commonscat', [u'Commons cat', u'Commons2', + u'CommonsCat', u'Commons category']), 'ja' : (u'Commonscat', [u'Commons cat', u'Commons category']), 'jv' : (u'Commonscat', [u'Commons cat']), 'kaa' : (u'Commons cat', [u'Commonscat']), 'kk' : (u'Commonscat', [u'Commons2']), 'ko' : (u'Commonscat', [u'Commons cat', u'공용분류']), 'la' : (u'CommuniaCat', []), - 'mk' : (u'Ризница-врска', [u'Commonscat', u'Commons cat', u'CommonsCat', u'Commons2', u'Commons category']), + 'mk' : (u'Ризница-врска', [u'Commonscat', u'Commons cat', u'CommonsCat', + u'Commons2', u'Commons category']), 'ml' : (u'Commonscat', [u'Commons cat', u'Commons2']), 'nn' : (u'Commonscat', [u'Commons cat']), 'os' : (u'Commonscat', [u'Commons cat']), 'pt' : (u'Commonscat', [u'Commons cat']), 'ro' : (u'Commonscat', [u'Commons cat']), 'ru' : (u'Commonscat', [u'Викисклад-кат']), - 'sl' : (u'Kategorija v Zbirki', [u'Commonscat', u'Kategorija v zbirki', u'Commons cat', u'Katzbirke']), + 'sl' : (u'Kategorija v Zbirki', [u'Commonscat', u'Kategorija v zbirki', + u'Commons cat', u'Katzbirke']), 'sv' : (u'Commonscat', [u'Commonscat-rad', u'Commonskat', u'Commons cat']), 'sw' : (u'Commonscat', [u'Commons2', u'Commons cat']), 'te' : (u'Commonscat', [u'Commons cat']), 'tr' : (u'CommonsKat', [u'Commonscat', u'Commons cat']), 'uk' : (u'Commonscat', [u'Commons cat', u'Category', u'Commonscat-inline']), - 'vi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category', u'Commons+cat']), + 'vi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category', + u'Commons+cat']), 'zh' : (u'Commonscat', [u'Commons cat']), 'zh-classical' : (u'共享類', [u'Commonscat']), 'zh-yue' : (u'同享類', [u'Commonscat', u'共享類 ', u'Commons cat']), @@ -110,10 +122,18 @@ 'af' : [u'commons'], 'ar' : [u'تحويلة تصنيف', u'كومنز', u'كومونز', u'Commons'], 'cs' : [u'Commons', u'Sestřičky', u'Sisterlinks'], - 'da' : [u'Commons', u'Commons left', u'Commons2', u'Commonsbilleder', u'Commonscat left', u'Commonscat2', u'GalleriCommons', u'Søsterlinks'], + 'da' : [u'Commons', u'Commons left', u'Commons2', u'Commonsbilleder', + u'Commonscat left', u'Commonscat2', u'GalleriCommons', + u'Søsterlinks'], 'de' : [u'Commons'], - 'en' : [u'Category redirect', u'Commons', u'Commonscat1A', u'Commoncats', u'Commonscat4Ra', u'Sisterlinks', u'Sisterlinkswp', u'Tracking category', u'Template category', u'Wikipedia category'], - 'eo' : [u'Commons', (u'Projekto/box', 'commons='), (u'Projekto', 'commons='), (u'Projektoj', 'commons='), (u'Projektoj', 'commonscat=')], + 'en' : [u'Category redirect', u'Commons', u'Commonscat1A', u'Commoncats', + u'Commonscat4Ra', u'Sisterlinks', u'Sisterlinkswp', + u'Tracking category', u'Template category', u'Wikipedia category'], + 'eo' : [u'Commons', + (u'Projekto/box', 'commons='), + (u'Projekto', 'commons='), + (u'Projektoj', 'commons='), + (u'Projektoj', 'commonscat=')], 'es' : [u'Commons', u'IprCommonscat'], 'eu' : [u'Commons'], 'fa' : [u'Commons', u'ویکیانبار'], @@ -124,7 +144,8 @@ 'it' : [(u'Ip', 'commons='), (u'Interprogetto', 'commons=')], 'ja' : [u'CommonscatS', u'SisterlinksN', u'Interwikicat'], 'nds-nl' : [u'Commons'], - 'nl' : [u'Commons', u'Commonsklein', u'Commonscatklein', u'Catbeg', u'Catsjab', u'Catwiki'], + 'nl' : [u'Commons', u'Commonsklein', u'Commonscatklein', u'Catbeg', + u'Catsjab', u'Catwiki'], 'om' : [u'Commons'], 'pt' : [u'Correlatos'], 'ru' : [u'Навигация'], @@ -153,16 +174,21 @@ return True else: for (inPageTemplate, param) in templatesWithParams: - if inPageTemplate == template[0] and template[1] in param[0]: + if inPageTemplate == template[0] \ + and template[1] in param[0]: return True return False
def updateInterwiki (wikipediaPage = None, commonsPage = None): ''' - Update the interwiki's at commons from a wikipedia page. The bot just replaces the interwiki links at the commons page with the interwiki's from the wikipedia page. - This should probably be more intelligent. We could use add all the interwiki's and remove duplicates. Or only remove language links if multiple language links to the same language exist. + Update the interwiki's at commons from a wikipedia page. The bot just + replaces the interwiki links at the commons page with the interwiki's from + the wikipedia page. This should probably be more intelligent. We could use + add all the interwiki's and remove duplicates. Or only remove language links + if multiple language links to the same language exist.
- This function is disabled for the moment untill i figure out what the best way is to update the interwiki's. + This function is disabled for the moment untill i figure out what the best + way is to update the interwiki's. ''' interwikis = {} comment= u'' @@ -173,75 +199,96 @@ interwikis[interwikiPage.site()]=interwikiPage oldtext = commonsPage.get() # The commonssite object doesnt work with interwiki's - newtext = wikipedia.replaceLanguageLinks(oldtext, interwikis, wikipedia.getSite(u'nl')) - comment = u'Updating interwiki's from [[' + wikipediaPage.site().language() + u':' + wikipediaPage.title() + u']]' + newtext = pywikibot.replaceLanguageLinks(oldtext, interwikis, + pywikibot.getSite(u'nl')) + comment = u'Updating interwiki's from [[' + \ + wikipediaPage.site().language() + \ + u':' + wikipediaPage.title() + u']]'
if newtext != oldtext: #This doesnt seem to work. Newtext has some trailing whitespace - wikipedia.showDiff(oldtext, newtext) + pywikibot.showDiff(oldtext, newtext) commonsPage.put(newtext=newtext, comment=comment)
def addCommonscat (page = None, summary = None, always = False): ''' Take a page. Go to all the interwiki page looking for a commonscat template. - When all the interwiki's links are checked and a proper category is found add it to the page. + When all the interwiki's links are checked and a proper category is found + add it to the page. ''' - wikipedia.output(u'Working on ' + page.title()); + pywikibot.output(u'Working on ' + page.title()); #Get the right templates for this page - primaryCommonscat, commonscatAlternatives=getCommonscatTemplate(page.site().language()) + primaryCommonscat, commonscatAlternatives = getCommonscatTemplate( + page.site().language()) commonscatLink = getCommonscatLink (page) if commonscatLink: - wikipedia.output(u'Commonscat template is already on ' + page.title()); + pywikibot.output(u'Commonscat template is already on %s' + % page.title()) (currentCommonscatTemplate, currentCommonscatTarget) = commonscatLink checkedCommonscatTarget = checkCommonscatLink(currentCommonscatTarget) if (currentCommonscatTarget==checkedCommonscatTarget): #The current commonscat link is good - wikipedia.output(u'Commonscat link at ' + page.title() + u' to Category:' + currentCommonscatTarget + u' is ok'); + pywikibot.output(u'Commonscat link at %s to Category:%s is ok' + % (page.title() , currentCommonscatTarget)); return (True, always) elif checkedCommonscatTarget!=u'': #We have a new Commonscat link, replace the old one - changeCommonscat (page, currentCommonscatTemplate, currentCommonscatTarget, primaryCommonscat, checkedCommonscatTarget) + changeCommonscat(page, currentCommonscatTemplate, + currentCommonscatTarget, primaryCommonscat, + checkedCommonscatTarget) return (True, always) else: #Commonscat link is wrong commonscatLink = findCommonscatLink(page) if (commonscatLink!=u''): - changeCommonscat (page, currentCommonscatTemplate, currentCommonscatTarget, primaryCommonscat, commonscatLink) + changeCommonscat (page, currentCommonscatTemplate, + currentCommonscatTarget, primaryCommonscat, + commonscatLink) #else #Should i remove the commonscat link?
elif skipPage(page): - wikipedia.output("Found a template in the skip list. Skipping " + page.title()); + pywikibot.output("Found a template in the skip list. Skipping %s" + % page.title()); else: commonscatLink = findCommonscatLink(page) if (commonscatLink!=u''): textToAdd = u'{{' + primaryCommonscat + u'|' + commonscatLink + u'}}' - (success, status, always) = add_text.add_text(page, textToAdd, summary, None, None, always); + (success, status, always) = add_text.add_text(page, textToAdd, + summary, None, None, + always); return (True, always);
return (True, always);
-def changeCommonscat (page = None, oldtemplate = u'', oldcat = u'', newtemplate = u'', newcat = u''): +def changeCommonscat (page=None, oldtemplate=u'', oldcat=u'', newtemplate=u'', + newcat=u''): ''' Change the current commonscat template and target. ''' - newtext = re.sub(u'(?i){{' + oldtemplate + u'|?[^}]*}}', u'{{' + newtemplate + u'|' + newcat + u'}}', page.get()) + newtext = re.sub(u'(?i){{' + oldtemplate + u'|?[^}]*}}', + u'{{' + newtemplate + u'|' + newcat + u'}}', + page.get()) comment = u'Changing commonscat link from [[:Commons:Category:' + oldcat + u'|' + oldcat + u']] to [[:Commons:Category:' + newcat + u'|' + newcat + u']]' - wikipedia.showDiff(page.get(), newtext) + pywikibot.showDiff(page.get(), newtext) page.put(newtext, comment)
def findCommonscatLink (page=None): for ipage in page.interwiki(): try: - if(ipage.exists() and not ipage.isRedirectPage() and not ipage.isDisambig()): + if(ipage.exists() and not ipage.isRedirectPage() + and not ipage.isDisambig()): commonscatLink = getCommonscatLink (ipage) if commonscatLink: (currentCommonscatTemplate, possibleCommonscat) = commonscatLink checkedCommonscat = checkCommonscatLink(possibleCommonscat) if (checkedCommonscat!= u''): - wikipedia.output("Found link for " + page.title() + " at [[" + ipage.site().language() + ":" + ipage.title() + "]] to " + checkedCommonscat + ".") + pywikibot.output( + u"Found link for %s at [[%s:%s]] to %s." + % (page.title(), ipage.site().language(), + ipage.title(), checkedCommonscat)) return checkedCommonscat - except wikipedia.BadTitle: + except pywikibot.BadTitle: #The interwiki was incorrect return u'' return u'' @@ -251,13 +298,15 @@ ''' Go through the page and return a tuple of (<templatename>, <target>) ''' - primaryCommonscat, commonscatAlternatives=getCommonscatTemplate(wikipediaPage.site().language()) + primaryCommonscat, commonscatAlternatives = getCommonscatTemplate( + wikipediaPage.site().language()) commonscatTemplate =u'' commonscatTarget = u'' #See if commonscat is present
for template in wikipediaPage.templatesWithParams(): - if template[0]==primaryCommonscat or template[0] in commonscatAlternatives: + if template[0]==primaryCommonscat \ + or template[0] in commonscatAlternatives: commonscatTemplate = template[0] if (len(template[1]) > 0): commonscatTarget = template[1][0] @@ -273,33 +322,38 @@ If the page is a redirect this function tries to follow it. If the page doesnt exists the function will return an empty string ''' - if wikipedia.verbose: - wikipedia.output("getCommonscat: " + name ); + if pywikibot.verbose: + pywikibot.output("getCommonscat: " + name ) try: - #This can throw a wikipedia.BadTitle - commonsPage = wikipedia.Page(wikipedia.getSite("commons", "commons"), "Category:" + name); + #This can throw a pywikibot.BadTitle + commonsPage = pywikibot.Page(pywikibot.getSite("commons", "commons"), + "Category:" + name)
if not commonsPage.exists(): - if wikipedia.verbose: - wikipedia.output("getCommonscat: The category doesnt exist."); + if pywikibot.verbose: + pywikibot.output(u"getCommonscat: The category doesnt exist.") return u'' elif commonsPage.isRedirectPage(): - if wikipedia.verbose: - wikipedia.output("getCommonscat: The category is a redirect"); - return checkCommonscatLink(commonsPage.getRedirectTarget().titleWithoutNamespace()); + if pywikibot.verbose: + pywikibot.output(u"getCommonscat: The category is a redirect") + return checkCommonscatLink( + commonsPage.getRedirectTarget().titleWithoutNamespace()) elif "Category redirect" in commonsPage.templates(): - if wikipedia.verbose: - wikipedia.output("getCommonscat: The category is a category redirect"); + if pywikibot.verbose: + pywikibot.output( + u"getCommonscat: The category is a category redirect") for template in commonsPage.templatesWithParams(): - if ((template[0]=="Category redirect") and (len(template[1]) > 0)): + if ((template[0]=="Category redirect") + and (len(template[1]) > 0)): return checkCommonscatLink(template[1][0]) elif commonsPage.isDisambig(): - if wikipedia.verbose: - wikipedia.output("getCommonscat: The category is disambiguation"); + if pywikibot.verbose: + pywikibot.output( + u"getCommonscat: The category is disambiguation") return u'' else: return commonsPage.titleWithoutNamespace() - except wikipedia.BadTitle: + except pywikibot.BadTitle: #Funky title so not correct return u''
@@ -314,16 +368,21 @@ # Load a lot of default generators genFactory = pagegenerators.GeneratorFactory()
- for arg in wikipedia.handleArgs(): + for arg in pywikibot.handleArgs(): if arg.startswith('-summary'): if len(arg) == 8: - summary = wikipedia.input(u'What summary do you want to use?') + summary = pywikibot.input(u'What summary do you want to use?') else: summary = arg[9:] elif arg.startswith('-checkcurrent'): checkcurrent = True - primaryCommonscat, commonscatAlternatives = getCommonscatTemplate(wikipedia.getSite().language()) - generator = pagegenerators.NamespaceFilterPageGenerator(pagegenerators.ReferringPageGenerator(wikipedia.Page(wikipedia.getSite(), u'Template:' + primaryCommonscat), onlyTemplateInclusion=True), ns) + primaryCommonscat, commonscatAlternatives = getCommonscatTemplate( + pywikibot.getSite().language()) + generator = pagegenerators.NamespaceFilterPageGenerator( + pagegenerators.ReferringPageGenerator( + pywikibot.Page(pywikibot.getSite(), + u'Template:' + primaryCommonscat), + onlyTemplateInclusion=True), ns)
elif arg == '-always': always = True @@ -333,19 +392,23 @@ if not generator: generator = genFactory.getCombinedGenerator() if not generator: - raise add_text.NoEnoughData('You have to specify the generator you want to use for the script!') + raise add_text.NoEnoughData( +u'You have to specify the generator you want to use for the script!')
pregenerator = pagegenerators.PreloadingGenerator(generator)
for page in pregenerator: if not page.exists(): - wikipedia.output(u'Page %s does not exist. Skipping.' % page.aslink()) + pywikibot.output(u'Page %s does not exist. Skipping.' + % page.aslink()) elif page.isRedirectPage(): - wikipedia.output(u'Page %s is a redirect. Skipping.' % page.aslink()) + pywikibot.output(u'Page %s is a redirect. Skipping.' % page.aslink()) elif page.isCategoryRedirect(): - wikipedia.output(u'Page %s is a category redirect. Skipping.' % page.aslink()) + pywikibot.output(u'Page %s is a category redirect. Skipping.' + % page.aslink()) elif page.isDisambig(): - wikipedia.output(u'Page %s is a disambiguation. Skipping.' % page.aslink()) + pywikibot.output(u'Page %s is a disambiguation. Skipping.' + % page.aslink()) else: (status, always) = addCommonscat(page, summary, always)
@@ -353,4 +416,4 @@ try: main() finally: - wikipedia.stopme() + pywikibot.stopme()
pywikipedia-svn@lists.wikimedia.org