首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PyTumblr中使用edit_post函数?

如何在PyTumblr中使用edit_post函数?
EN

Stack Overflow用户
提问于 2013-12-11 19:48:42
回答 3查看 799关注 0票数 9

我正在尝试使用PyTumblredit_post function编辑我的tumblr博客中的一些帖子,但是我不能确定到底需要哪些参数。我试着把tags参数放进去,但它不被接受。

我已经尝试过了:

代码语言:javascript
复制
client = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET,
                                   OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
client.edit_post('nameofblog', {'id': 39228373})

它给出了以下错误:

代码语言:javascript
复制
TypeError: edit_post() takes exactly 2 arguments (3 given)

有什么想法吗?

这是函数:

代码语言:javascript
复制
    def edit_post(self, blogname, **kwargs):
            """
    Edits a post with a given id

    :param blogname: a string, the url of the blog you want to edit
    :param tags: a list of tags that you want applied to the post
    :param tweet: a string, the customized tweet that you want
    :param date: a string, the GMT date and time of the post
    :param format: a string, sets the format type of the post. html or markdown
    :param slug: a string, a short text summary to the end of the post url

    :returns: a dict created from the JSON response
    """
      url = "/v2/blog/%s/post/edit" % blogname
      return self.send_api_request('post', url, kwargs)
EN

回答 3

Stack Overflow用户

发布于 2013-12-20 12:08:31

PyTumblr库在Tumblr REST API上提供了一个薄层,除了博客名称之外的所有参数都应该作为关键字参数传入。

然后,TumblrRestClient.edit_post()方法充当/post/edit endpoint的代理,它接受所有相同的参数。

因此,你可以这样称呼它:

代码语言:javascript
复制
client = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET,
                               OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
client.edit_post('nameofblog', id=39228373)

这并不是说,如果你有一个带有post细节的字典对象,你就不能使用它。

如果你想设置一个给定帖子id的标题,你可以使用:

代码语言:javascript
复制
post = {'id': 39228373, 'title': 'New title!'}
client.edit_post('nameofblog', **post)

这里,使用**语法将post字典作为单独的关键字参数应用于.edit_post()方法调用。然后,Python获取输入字典中的每个键值对,并将该对作为关键字参数应用。

您应该能够设置适用于您的帖子类型的任何参数,这些参数列在posting documentation下面。

因此,问题是.edit_post()方法将self. send_api_request()valid_params参数保留为默认的空列表,从而导致您传入的任何内容都会出现有保证的验证异常。这一定是个bug,我commented on Mike's issue向开发人员指出这一点。

票数 3
EN

Stack Overflow用户

发布于 2013-12-11 23:10:43

传递ID没有很好的文档记录,所以我问:

代码语言:javascript
复制
client.edit_post("nameofblog", id=39228373, other="details", tags=["are", "cool"])

参考:http://github.com/tumblr/pytumblr/issues/29

票数 1
EN

Stack Overflow用户

发布于 2013-12-20 18:18:05

前面提到的函数edit_post依赖于以下函数:

代码语言:javascript
复制
def send_api_request(self, method, url, params={}, valid_parameters=[], needs_api_key=False):
        """
Sends the url with parameters to the requested url, validating them
to make sure that they are what we expect to have passed to us

:param method: a string, the request method you want to make
:param params: a dict, the parameters used for the API request
:param valid_parameters: a list, the list of valid parameters
:param needs_api_key: a boolean, whether or not your request needs an api key injected

:returns: a dict parsed from the JSON response
"""
        if needs_api_key:
            params.update({'api_key': self.request.consumer.key})
            valid_parameters.append('api_key')

        files = []
        if 'data' in params:
            if isinstance(params['data'], list):
                files = [('data['+str(idx)+']', data, open(data, 'rb').read()) for idx, data in enumerate(params['data'])]
            else:
                files = [('data', params['data'], open(params['data'], 'rb').read())]
            del params['data']

        validate_params(valid_parameters, params)
        if method == "get":
            return self.request.get(url, params)
        else:
            return self.request.post(url, params, files)

因此,问题是下面一行中的edit_post函数:

代码语言:javascript
复制
return self.send_api_request('post', url, kwargs)

不提供有效选项的选择,就像最后一行中的函数一样:

代码语言:javascript
复制
def reblog(self, blogname, **kwargs):
        """
Creates a reblog on the given blogname

:param blogname: a string, the url of the blog you want to reblog to
:param id: an int, the post id that you are reblogging
:param reblog_key: a string, the reblog key of the post

:returns: a dict created from the JSON response
"""
        url = "/v2/blog/%s/post/reblog" % blogname

        valid_options = ['id', 'reblog_key', 'comment', 'type', 'state', 'tags', 'tweet', 'date', 'format', 'slug']
        if 'tags' in kwargs:
            # Take a list of tags and make them acceptable for upload
            kwargs['tags'] = ",".join(kwargs['tags'])
        return self.send_api_request('post', url, kwargs, valid_options)

为了解决这个问题,我将返回行修改为:

代码语言:javascript
复制
send_api_request('post', url, {'id':post_id, 'tags':tags}, ['id', 'tags']

我只添加了我想要的标签。它也应该与其他人一起工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20518343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档