python - 如何使用Python将文件上传到MediaWiki?

标签 python file-upload mediawiki

我正在尝试将图像(目前只是随机图片)上传到我的 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")
>>> 

根据我在google上找到的信息,当前的MediaWiki不支持上传文件。但这太荒谬了……一定有办法,对吧?

我并没有与 wikitools 包结婚——任何方式都值得赞赏。

编辑:我在 LocalSettings.php 中设置 $wgEnableUploads = true ,并且我可以手动上传文件,但不能通过 python 上传。

编辑:我认为 wikitools 会自动获取编辑 token 。我附上了上传方法。在执行 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 1.16(目前处于 Begta 版本)才能通过 API 上传文件。或者您可以尝试mwclient ,如果使用旧版本的 MediaWiki,它会自动回退到通过 Special:Upload 上传(功能减少,例如没有错误处理等)

关于python - 如何使用Python将文件上传到MediaWiki?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3182009/

相关文章:

Python 集合并集引发 TypeError

java - 大文件上传时ClientAbortException : java. io.IOException

mediawiki - 如何让 MediaWiki 搜索忽略重音?

mediawiki - 卡住(锁定)MediaWiki 表中的顶行(标题)

python - 我如何找到模型将输入分类为 [0,1] 的概率

python - 将 Header 和 Dataframe 放入新的 CSV 中

php - 无限图像上传到服务器和名称、类别等和多个图像路径(1 行)到数据库

javascript - 无法使用 $_FILES 过程在 PHP 中上传图像

templates - 使用 api 获取 wiki 页面的模板数据

python - 我应该使用属性还是 getter 和 setter?