如何在PyTumblr中使用edit_post函数?

时间:2013-12-11 11:48:42

标签: python tumblr function-parameter

我正在尝试使用PyTumblredit_post function编辑tumblr博客中的一些帖子,但我无法弄清楚究竟需要哪些参数。我尝试使用tags参数,但不接受它。

我试过这个:

client = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET,
                                   OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
client.edit_post('nameofblog', {'id': 39228373})

它给了我以下错误:

TypeError: edit_post() takes exactly 2 arguments (3 given)

有什么想法吗?

这是功能:

    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)

3 个答案:

答案 0 :(得分:3)

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

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

因此,你可以这样称呼:

client = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET,
                               OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
client.edit_post('nameofblog', id=39228373)

这并不是说如果你有一个包含帖子细节的字典对象,你就无法使用它。

如果您想设置给定帖子ID的标题,可以使用:

post = {'id': 39228373, 'title': 'New title!'}
client.edit_post('nameofblog', **post)

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

您应该可以设置适用于您的帖子类型的任何参数,列在posting documentation下。

问题是,.edit_post()方法将valid_params参数留给self. send_api_request()到默认的空列表,导致任何你传入。这个必须是一个错误,我commented on Mike's issue将此指向开发人员。

答案 1 :(得分:1)

传递ID没有详细记录,所以我问:

client.edit_post("nameofblog", id=39228373, other="details", tags=["are", "cool"])

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

答案 2 :(得分:1)

上述函数edit_post依赖于以下函数:

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函数:

return self.send_api_request('post', url, kwargs)

不提供有效选项的选择,最后一行中的此函数也是如此:

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)

要修复它,我将返回行修改为:

send_api_request('post', url, {'id':post_id, 'tags':tags}, ['id', 'tags']

我添加了我想要的标签。它也应该与其他人合作。