What follows is the script asked for on [1]. Well, mostly. As this is my very first Python script that actually performs a useful function, I couldn't get it to work as a logged-in user. Maybe someone else has better luck. I'm too tired now.
Magnus
[1] http://commons.wikimedia.org/wiki/Commons:Transfer_script
--------------------------
import httplib import urllib import md5
class AppURLopener(urllib.FancyURLopener): def __init__(self, *args): self.version = "Mozilla/4.0" urllib.FancyURLopener.__init__(self, *args)
def imagetocommons ( language , imagenamespace , image ) : server = 'http://' + language + '.wikipedia.org' url = server + '/wiki/' + imagenamespace + ':' + image ; srcurl = server + '/w/wiki.phtml?title=' + imagenamespace + ':' + image + '&action=raw' ;
# Loading image description into source urllib._urlopener = AppURLopener() sock = urllib.urlopen ( srcurl ) ; source = sock.read() sock.close()
# Loading the image md = md5.md5(image).hexdigest() imageurl = 'http://upload.wikimedia.org/wikipedia/' + language + '/' + md[:1] + '/'+ md[:2] + '/' + image ; localimage = image ; # For now... x = urllib.urlretrieve ( imageurl , localimage ) localimage = x[0]
# POST command uploadurl = 'http://commons.wikimedia.org/wiki/Special:Upload' params = urllib.urlencode({'wpUploadFile': localimage, 'wpUploadDescription': 'Via script from ' + language , 'wpUploadAffirm': 1, 'wpUpload':1}) postit = urllib.urlopen ( uploadurl , params ) print postit.read() postit.close()
imagetocommons ( "en" , "Image" , "GlassofGuinness.jpg" ) # Testing...
Magnus Manske schrieb:
What follows is the script asked for on [1]. Well, mostly. As this is my very first Python script that actually performs a useful function, I couldn't get it to work as a logged-in user. Maybe someone else has better luck. I'm too tired now.
Well, reinventing the wheel seems to be a common problem. :) You might just head over to the pywikipedia bot project at http://sourceforge.net/projects/pywikipediabot/ and use the imagetransfer.py bot for this task...
Ciao, Michael.
Actually, for the current issue, the bot to use from the pywikipediabot-project would be upload.py with the -wiki option. For example: "upload.py -lang:commons -wiki:en Imagename.png"
imagetransfer.py is meant in case you have not yet found the image, but have an article to which you want to add the image which does have interwiki-links. It then looks for an image in the interwiki-linked pages.
Andre Engels
Well, reinventing the wheel seems to be a common problem. :) You might just head over to the pywikipedia bot project at http://sourceforge.net/projects/pywikipediabot/ and use the imagetransfer.py bot for this task...
wikitech-l@lists.wikimedia.org