2013-12-11 5 views
9

我想用PyTumblredit_post function編輯我的tumblr博客中的一些帖子,但是我無法弄清楚需要什麼參數。我試着把標籤參數,但它不被接受。如何在PyTumblr中使用edit_post函數?

我已經試過這樣:

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) 
+0

確切地說,你試圖通過什麼作爲參數(&它沒有工作)? –

+2

首先,我不明白我在哪裏放置我想要的帖子的ID。 – GiannisIordanou

回答

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向開發者指出這一點。

+0

獲得了在函數中使用參數的詳細解釋的賞金。 – GiannisIordanou

+0

很好的回答和值得的賞金!還學到了一些東西,這是一種獎勵。 – mikedidthis

+0

@mikedidthis:TBH我對因錯過驗證問題而感到內疚。我在驗證函數中直接跳過'如果不是params',假設有一個空的有效參數名稱列表的提前返回。 –

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

啊!我錯過了空白列表的部分,如果有效參數*仍*導致參數被驗證。這*必須*是一個錯誤;我對mike開放的問題發表了評論。 –