我想要的图像(只是现在随机图片)上传到我的MediaWiki站点,但我不断收到此错误:如何使用Python将文件上传到MediaWiki?
"Unrecognized value for parameter '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
而且这是我的第一个问题所以有人让我知道,如果你不能发表其他人的代码什么的。谢谢!
您是否在MediaWiki上启用了文件上传? http://www.mediawiki.org/wiki/Manual:Configuring_file_uploads – jfs 2010-07-06 00:11:40