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)
4

3 回答 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()留给了默认的空列表,从而导致您传入的任何内容都有保证的验证异常。这一定是一个错误,我对 Mike 的问题发表了评论以向开发人员指出这一点。valid_paramsself. send_api_request()

于 2013-12-20T04:08:30.687 回答
1

传递身份证没有很好的记录,所以我问:

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

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

于 2013-12-11T15:10:43.307 回答
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']

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

于 2013-12-20T10:18:04.910 回答