Jump to content

Pywikipediabot: Difference between revisions

From IdeaWazaWiki
wikademia>Millosh
wikademia>Millosh
No edit summary
Line 12: Line 12:
== Some general basics ==
== Some general basics ==


=== Installing pywikipediabot framework ===
* [[Pywikipediabot/Installing pywikipediabot framework|Lecture 1: Installing pywikipediabot framework]]
 
* [[Pywikipediabot/Logging in|Lecture 2: Logging in]]
[[meta:Using the python wikipediabot|Documentation on Pywikipediabot page]] is very good. Use it for initial instructions.
* [[Pywikipediabot/Invoking Python|Lecture 3: Invoking Python]]
 
In short, make a subdirectory inside of your working directory and go there:
 
<source lang="bash">
$ mkdir mybot
$ cd mybot
</source>
 
Invoke Subversion checkout to download pywikipediabot:
 
<source lang="bash">
$ svn checkout http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia
</source>
 
If you have a really slow connection, don't download spell-checking files:
 
<source lang="bash">
$ svn checkout --ignore-externals http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia
</source>
 
Pywikipediabot is a very active project. Because of that you should periodically (maybe daily, maybe weekly) update your copy:
 
<source lang="bash">
$ cd pywikipedia/
$ svn update
</source>
 
=== Logging in ===
 
Your bot should be logged in to the particular project.
 
If you don't have a bot account on some project, open it. On bot's user page state clearly that this is a bot account, that you are the owner of the bot and that bot will not do anything outside of getting pages and testing inside of its and your user space. Also, note that you will ask community for any other bot action. Such bot doesn't need a bot flag. However, be careful with contamination of Recent changes. This means that you shouldn't have more then, let's say, 10 edits per day on some bigger project, which means that you ''should'' use some bigger project for learning.
 
Go into the directory "pywikipedia":
 
<source lang="bash">
$ cd pywikipedia/
</source>
 
Make file "user-config.py" by using your favorite editor ([[w:emacs|emacs]], [[w:vim (text editor)|vim]], [[w:Joe's Own Editor|joe]], [[w:nano (text editor)|nano]]...). It should contain something like:
 
<source lang="python">
mylang = 'en'
family = 'wikinews'
usernames['wikinews']['en'] = 'My Bot Name'
usernames['wikinews']['pl'] = 'My Bot Name'
usernames['wikinews']['sr'] = 'My Bot Name II' # you may use different names
</source>
 
By invoking "login.py" program...
 
<source lang="bash">
$ python login.py
</source>
 
... you will get a prompt for inserting your password...
 
<pre>
Checked for running processes. 1 processes currently running, including the current process.
Password for user My Bot Name on wikinews:en:
</pre>
 
... and, if everything is good, program will give to you a message like:
 
<pre>
Logging in to wikinews:en as My Bot Name
Should be logged in now
</pre>
 
However, if you have more then one account (like it was described) and you have one password for all bots (which is little bit less safe, but much more practical), you should use options "-all" and "-pass":
 
<pre>
millosh@Malatesta:~/wikinews/pywikipedia$ python login.py -all -pass
Checked for running processes. 1 processes currently running, including the current process.
Password for all accounts:
Logging in to wikinews:sr as My Bot Name II
Should be logged in now
Logging in to wikinews:en as My Bot Name
Should be logged in now
Logging in to wikinews:pl as My Bot Name
Should be logged in now
millosh@Malatesta:~/wikinews/pywikipedia$
</pre>
 
=== Invoking Python ===
 
Your bot should assume that it is multilingual. Because of that, use [[w:UTF-8|UTF-8]] (or some better character set) and declare it at the beginning of your Python script:
 
<source lang="python">
#!/usr/bin/python
# -*- coding: utf-8 -*-
</source>
 
However, we will use Python interpreter for examples, invoked by simple typing "python". Interpreter doesn't require first two lines, but scripts written into a file have to have the first line and should have the second line.
 
If you are writing examples into the file and you want to execute it, you should choose one of two approaches:
 
* You may give to your file executable mode (you need to do that only once!):
 
<source lang="bash">
$ chmod +x mybot.py
</source>
 
...  after which you would be able to execute your script directly:
 
<source lang="bash">
$ ./mybot.py
</source>
 
* Or you may invoke scripts by typing "python" before them:
 
<source lang="bash">
$ python mybot.py
</source>


== Pywikipediabot basics ==
== Pywikipediabot basics ==


=== Importing basic modules ===
* [[Pywikipediabot/Importing basic modules|Lecture 4: Importing basic modules]]
 
* [[Pywikipediabot/Getting one page|Lecture 5: Getting one page]]
It is possible to make some other approach to calling basic PWB functions. However, the best idea is to keep your bot separately of PWB distribution. Because of that you should be inside of "mybot/" directory, '''not''' inside of "mybot/pywikipedia/" directory.
* [[Pywikipediabot/Writing simple "Hello, world!" in PWB|Lecture 6: Writing simple "Hello, world!" in PWB]]
 
* [[Pywikipediabot/Practice|Lecture 7: Practice]]
<source lang="python">
mydir = "./"
pwbdir = mydir + "pywikipedia/"
import sys
sys.path.append(pwbdir)
from wikipedia import *
</source>
 
=== Getting one page ===
 
Software should know with which site you are trying to operate. Usually, it is your default language and family inside of your user-config.py file:
 
<source lang="python">
mylang = 'en'
family = 'wikinews'
</source>
 
By defining object "site" (or whatever name you give to this object) bot will start to use this site:
 
<source lang="python">
site = getSite()
</source>
 
While it is usual that you are working with only one site, it will be better to always define used language and family:
 
<source lang="python">
language = "en"
family = "wikinews"
site = getSite(language,family)
</source>
 
Now, we have defined object "site" and we are ready to get our first page. To do so, we have to define object "page" (or whatever name you give to this object). Let's say that we want to get source of Main Page:
 
<source lang="python">
pagename = "Main Page"
page = Page(site,pagename)
pagetext = page.get()
</source>
 
And if you invoke "print pagetext", you will get [[wikinews:en:Main Page|Main Page of English Wikinews]]:
 
<pre>
{{Main page header}}
{{Ticker}}
 
{|{|cellpadding="5" cellspacing="3" class="mainpagetable"
|-
| style="border: 1px solid #FFFFFF; padding: .5em 1em 1em; color: #000000; background-color: #ffffff; width:60%; vertical-align: top;" rowspan="3" |
{{Lead article}}
----
<div class="MainPageLatestNews">
=== Latest news <span class="plainlinks" style="font-size:70%;text-align:right;">[http://feeds.feedburner.com/WikinewsLatestNews <span class="rss">RSS</span>]</span>===
{{Latest news}}</div>
...
</pre>
 
=== Writing simple "Hello, world!" in PWB ===
 
So, let's try to make a page "Hello, world!" with content "Hello, world!" inside of bot's name space.
 
<source lang="python">
mynick = "My Bot Name"
hello_world = "Hello, world!"
myuserpage = "User:" + mynick
mypage = myuserpage + "/" + hello_world
 
site = getSite(language,family)
page = Page(site,mypage)
page.put(hello_world)
</source>
 
And here is the full script:
 
<source lang="python">
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
# let's declare all global values here
mydir = "./"
pwbdir = mydir + "pywikipedia/"
language = "en"
family = "wikinews"
mynick = "My Bot Name"
hello_world = "Hello, world!"
myuserpage = "User:" + mynick
mypage = myuserpage + "/" + hello_world
 
# importing modules
import sys
sys.path.append(pwbdir)
from wikipedia import *
 
# doing the job
site = getSite(language,family)
page = Page(site,mypage)
page.put(hello_world)
</source>
 
== Practice ==
# Open accounts for you and bot on all Wikimedia projects. Note that it is a long process and if you don't want to spend 30 hours at once, you will need a month of work. As a starting point, use [http://www.wikimedia.org/ www.wikimedia.org] and pages on [[meta:|Meta]] which are in the form "List of <projectname>s" (like [[meta:List of Wikipedias|List of Wikipedias]]).
# ...


[[Category:Pywikipediabot]]
[[Category:Python]]
[[Category:Python]]
[[Category:MediaWiki]]
[[Category:MediaWiki]]
[[Category:Computer programming]]
[[Category:Computer programming]]

Revision as of 19:42, 23 December 2007

Pywikipediabot (PWB) is the "Python Wikipediabot Framework is a set of bot scripts to do things on Wikipedia or other wikis using MediaWiki. It has been written by several people using the language Python. On this page, we give the general information for people who want to use the bot software."

Needed skills

This course assumes that you know/have:

  • Installed Python on your operating system
  • Using necessary tools
  • Python basics
  • Introduction into Wikimedia projects

Some general basics

Pywikipediabot basics