如何用Python上传文件到MediaWiki?
我正在尝试把一张图片(现在只是随便找的图片)上传到我的MediaWiki网站,但我一直收到这个错误:
“参数'action'的值无法识别:upload”
这是我做的事情(网站网址和密码已更改):
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wikitools
>>> import poster
>>> wiki = wikitools.wiki.Wiki("mywikiurl/api.php")
>>> wiki.login(username="admin", password="mypassword")
True
>>> screenshotPage = wikitools.wikifile.File(wiki=wiki, title="screenshot")
>>> screenshotPage.upload(fileobj=open("/Users/jeff/Pictures/02273_magensbay_1280x800.jpg", "r"), ignorewarnings=True)
Traceback (most recent call last):
File "", line 1, in
File "/Library/Python/2.6/site-packages/wikitools/wikifile.py", line 228, in upload
res = req.query()
File "/Library/Python/2.6/site-packages/wikitools/api.py", line 143, in query
raise APIError(data['error']['code'], data['error']['info'])
wikitools.api.APIError: (u'unknown_action', u"Unrecognized value for parameter 'action': upload")
>>>
根据我在谷歌上找到的信息,目前的MediaWiki不支持上传文件。但这太荒谬了……总得有办法吧,对吧?
我并不执着于wikitools这个包——任何方法都很感谢。
编辑:我在LocalSettings.php中设置了$wgEnableUploads = true,现在我可以手动上传文件,只是通过python上传不行。
编辑:我觉得wikitools会自动获取编辑令牌。在它进行API请求之前,会调用self.getToken('edit'),我想这应该能解决问题?不过我会再试试,看看手动添加这个是否能修复问题。
def upload(self, fileobj=None, comment='', url=None, ignorewarnings=False, watch=False): """Upload a file, requires the "poster" module fileobj - A file object opened for reading comment - The log comment, used as the inital page content if the file doesn't already exist on the wiki url - A URL to upload the file from, if allowed on the wiki ignorewarnings - Ignore warnings about duplicate files, etc. watch - Add the page to your watchlist """ if not api.canupload and fileobj: raise UploadError("The poster module is required for file uploading") if not fileobj and not url: raise UploadError("Must give either a file object or a URL") if fileobj and url: raise UploadError("Cannot give a file and a URL") params = {'action':'upload', 'comment':comment, 'filename':self.unprefixedtitle, 'token':self.getToken('edit') # There's no specific "upload" token } if url: params['url'] = url else: params['file'] = fileobj if ignorewarnings: params['ignorewarnings'] = '' if watch: params['watch'] = '' req = api.APIRequest(self.site, params, write=True, multipart=bool(fileobj)) res = req.query() if 'upload' in res and res['upload']['result'] == 'Success': self.wikitext = '' self.links = [] self.templates = [] self.exists = True return res
另外,这是我第一次提问,如果有人告诉我不能发布别人的代码或者其他什么的,请告诉我。谢谢!
4 个回答
也许你需要先“获取一个令牌”吧?
要上传文件,你需要一个令牌。这个令牌和编辑令牌是一样的,不管你要上传的文件名是什么,它都是相同的,但每次登录后都会改变。跟其他令牌不一样的是,这个令牌不能直接获取,所以你必须先获取并使用一个编辑令牌。
详细信息请查看这里: http://www.mediawiki.org/wiki/API:Edit_-_Uploading_files
我对这些麻烦感到非常沮丧,于是我自己写了一些简单的程序,使用了poster库和Python标准库中的cookielib和httplib2。你可以在这里找到它:https://github.com/gandrewstone/mediawiki_python_bot
你需要至少使用MediaWiki 1.16版本(目前还在测试阶段),才能通过API上传文件。或者你可以试试mwclient,这个工具会自动切换到通过Special:Upload上传文件,如果你用的是旧版本的MediaWiki(功能会减少,比如没有错误处理等)。