我正在尝试使用PyTumblr和edit_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)发布于 2013-12-20 12:08:31
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)这并不是说,如果你有一个带有post细节的字典对象,你就不能使用它。
如果你想设置一个给定帖子id的标题,你可以使用:
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向开发人员指出这一点。
发布于 2013-12-11 23:10:43
传递ID没有很好的文档记录,所以我问:
client.edit_post("nameofblog", id=39228373, other="details", tags=["are", "cool"])参考:http://github.com/tumblr/pytumblr/issues/29
发布于 2013-12-20 18:18:05
前面提到的函数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']我只添加了我想要的标签。它也应该与其他人一起工作。
https://stackoverflow.com/questions/20518343
复制相似问题