Manual:Pywikipediabot example python 2.5
This page was moved from MetaWiki.
It probably requires cleanup – please feel free to help out. In addition, some links on the page may be red; respective pages might be found at Meta. Remove this template once cleanup is complete.
[edit] Document purpose
To provide an example of interfacing to a wiki via pywikipediabot and python 2.5
[edit] Installation
- make sure that the wiki we are trying to access supports the mediaWiki API
- attempt to navigate to http://<hostname>/wiki/api.php or something similar. If the api.php cannot be found, it is likely the target wiki does not support the API.
- install simplejson for python2.5
- to install, simply copy the unzipped simplejson folder to /python25/Lib/site-packages
- test sucessfull installation by typing >>>import simplejson #this should not return an error</span
- download and unzip PyWikipediaBot to some directory
- make a custom config-user.py file by doing the following:
- open to edit any file found in the /families folder and make the following changes. We will save this file as a different name, just using an existing file as a template.
# -*- coding: utf-8 -*-
import family
class Family(family.Family):
def __init__(self):
family.Family.__init__(self)
self.name = 'somewiki'
self.langs = {
'en': 'wiki.somewiki.org', #change this to match your target wiki host
}
self.namespaces[4] = {
'_default': [u'SomeWiki', self.namespaces[4]['_default']], #change this if needed
}
self.namespaces[5] = {
'_default': [u'SomeWiki talk', self.namespaces[5]['_default']],
}
self.content_id = "mainContent"
def RversionTab(self, code):
return r'<li\s*><a href=".*?title=.*?&action=history".*?>.*?</a></li>'
def version(self, code):
return "1.16.1" #make sure this is correct! took me days to figure this out....
def path(self, code): #make sure this matches your target wiki
return '/index.php'
def scriptpath(self, code):
return '/wiki' #not really sure what this does
def apipath(self, code):
return '/wiki/api.php' #make sure this matches your target wiki
- double click to execute generate_user_files.py filing out the appropriate information. This will creat a custom user-config.pyfile
[edit] get() put() a wiki page example
- try the following to get a wiki page and to make changes
>>>import login
>>>import wikipedia
>>>login.main()
Password for user highvelcty on somewiki:en:
Logging in to somewiki:en as highvelcty via API.
Should be logged in now
>>>site = wikipedia.getSite()
>>>page = wikipedia.Page(site, 'something')
>>>text = page.get(force = True)
>>>text += '\n\n\nediting the text of the page!!!'
>>>page.put(text)
Updating page [[something]] via API
(302, 'OK', {u'pageid': 2, u'title': u'something', u'newtimestamp': u'2011-01-27T19:46:02Z',
: 7})
