Revision: 4521 Author: filnik Date: 2007-11-09 16:09:36 +0000 (Fri, 09 Nov 2007)
Log Message: ----------- Some fixes to make it work better (tested, working perfectly)
Modified Paths: -------------- trunk/pywikipedia/pageimport.py
Modified: trunk/pywikipedia/pageimport.py =================================================================== --- trunk/pywikipedia/pageimport.py 2007-11-09 15:28:31 UTC (rev 4520) +++ trunk/pywikipedia/pageimport.py 2007-11-09 16:09:36 UTC (rev 4521) @@ -4,6 +4,20 @@ This is a script to import pages from a certain wiki to another.
This requires administrator privileges. + +Here there is an example of how to use it: + +from pageimport import * +def main(): + # Defing what page to load.. + pagetoload = 'Apple' + site = wikipedia.getSite() + importerbot = Importer(site) # Inizializing + importerbot.Import(pagetoload, prompt = True) +try: + main() +finally: + wikipedia.stopme() """ # # (C) Filnik, 2007 @@ -16,46 +30,35 @@
__version__ = '$Id: pageimport.py 4336 2007-09-20 14:51:04Z wikipedian $'
-import wikipedia - -# Global variables -site = wikipedia.getSite() -# ################################################################ # -def main(): - # Defing what page to load.. - pagetoload = 'Apple' - importerbot = Importer(site) # Inizializing - importerbot.Import(pagetoload, prompt = True) - -""" -**************************************************************************** -System functions follow: no changes should be necessary! -**************************************************************************** -""" - import urllib -import login, config +import wikipedia, login, config
class Importer(wikipedia.Page): def __init__(self, site): + self.importsite = site wikipedia.Page.__init__(self, site, 'Special:Import', None, 0)
def Import(self, target, project = 'w', crono = '1', namespace = '', prompt = True): """Import the page from the wiki. Requires administrator status. If prompt is True, asks the user if he wants to delete the page. """ + if project == 'w': + site = wikipedia.getSite(fam = 'wikipedia') + elif project == 'b': + site = wikipedia.getSite(fam = 'wikibooks') + elif project == 'wikt': + site = wikipedia.getSite(fam = 'wiktionary') + elif project == 's': + site = wikipedia.getSite(fam = 'wikisource') + elif project == 'q': + site = wikipedia.getSite(fam = 'wikiquote') + else: + site = wikipedia.getSite() # Fixing the crono value... if crono == True: crono = '1' elif crono == False: crono = '0' - elif crono == '0': - pass - elif crono == '1': - pass - else: - wikipedia.output(u'Crono value set wrongly.') - wikipedia.stopme() # Fixing namespace's value. if namespace == '0': namespace == '' @@ -92,14 +95,12 @@ response, data = self.site().postForm(address, predata, sysop = True) if data: wikipedia.output(u'Page imported, checking...') - if wikipedia.Page(site, target).exists(): + if wikipedia.Page(self.importsite, target).exists(): wikipedia.output(u'Import success!') return True else: wikipedia.output(u'Import failed!') return False if __name__=='__main__': - try: - main() - finally: - wikipedia.stopme() \ No newline at end of file + wikipedia.output(u'This is just a module! Read the documentation and write your own script!') + wikipedia.stopme()