如何在PyTumblr中使用edit_post函数?

9
我正在尝试使用PyTumblredit_post函数编辑我的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)

你尝试传递什么参数(但它没有起作用)? - shad0w_wa1k3r
2
首先,我不知道在哪里放置我想要的帖子的ID。 - IordanouGiannis
3个回答

3
PyTumblr库提供了一个薄层封装在Tumblr REST API上,除了博客名称之外的所有参数都应作为关键字参数传递。
然后,TumblrRestClient.edit_post()方法充当/post/edit端点的代理,并且它接受所有相同的参数。
因此,您可以这样调用它:
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然后将输入字典中的每个键值对作为关键字参数应用。

您应该能够设置适用于您的帖子类型的任何参数,列在发布文档中。

问题在于.edit_post()方法将valid_params参数留给默认的空列表,并导致保证验证异常的任何传递。这一定是一个错误,我评论了Mike的问题以向开发人员指出这一点。


因为详细解释了如何在函数中使用参数,所以获得了赏金。 - IordanouGiannis
非常好的回答,值得奖励!同时还学到了一些东西,这是额外的收获。 - mikedidthis
@mikedidthis:说实话,我对错过这里的验证问题感到内疚。在验证函数中,我跳过了if not params,假设对于有效参数名称的空列表也会有早期返回。 - Martijn Pieters
@MartijnPieters 好的,不用了。楼主得到了很好的回答,问题也已经报告了。顺便说一下,Boba帽看起来很棒! :D - mikedidthis

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']

我只添加了我想要的标签。它也应该适用于其他标签。


1
啊!我错过了一个空列表仍然会导致参数被验证的有效参数部分。那一定是个bug;我已经在Mike打开的问题上进行了评论。 - Martijn Pieters

1

以上代码给我返回了以下错误: 异常:id不是允许的字段。 - IordanouGiannis
@evil_inside 我根据所有者的回应更新了我的答案。 - mikedidthis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接