用python自動發推(twitter)

這是catcam第二集~ 用python自動發推 因爲在美國所以還是也搞個自動發推的吧~那天萬一新浪北美服務器掛了還能看推~~~~

這次就太簡單了,找到了個非常棒的教程:http://nodotcom.org/python-twitter-tutorial.html

基本就是無腦操作了。此處複製黏貼教程~ 因爲太懶所以完全不想翻譯呢╮(╯▽╰)╭ 

Step 1

  • You must add your mobile phone to your Twitter profile before creating an application.
  • Go to: Settings -> Add Phone -> Add number -> Confirm -> Save.
  • Do not forget to turn off all text notifications.

Step 2

  • Set up a new app
  • Go to: Twitter Apps -> Create New App -> Leave Callback URL empty -> Create your Twitter application.
  • You should see "Your application has been created. Please take a moment to review and adjust your application's settings".

Step 3

  • By default, app's access level is read-only. To send out tweets, it requires write permission.
  • Go to: Permissions tab -> What type of access does your application need? -> ChooseRead and Write -> Update settings.
  • You should see "The permission settings have been successfully updated. It may take a moment for the changes to reflect."

Step 4

  • Time to get the keys and access tokens for OAuth.
  • Go to: Keys and Access Tokens tab . You'll see this under "Your Access Token" : You haven't authorized this application for your own account yet. By creating your access token here, you will have everything you need to make API calls right away. The access token generated will be assigned your application's current permission level.
  • Click Create my access token
  • You should see "Your application access token has been successfully generated. It may take a moment for changes you've made to reflect. Refresh if your changes are not yet indicated. This access token can be used to make API requests on your own account's behalf. Do not share your access token secret with anyone."
  • Verify that you see access token/secret - and the permission is set to "read and write".
  • From this page, note down the Access TokenAccess Token SecretConsumer Key (API Key)Consumer Secret (API Secret). Consumer Key/Secret help twitter identify the app and Access Token/Secret help twitter identify the user (that is you).

Step 5

  • We will use tweepy to access Twitter's API. You can install it using pip: pip install tweepy(try to setup a virtualenv for this - they are very useful).

最後,上代碼

'''
checkout http://nodotcom.org/python-twitter-tutorial.html for more instructions
'''
import tweepy

def get_api(cfg):
  auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
  auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
  return tweepy.API(auth)

def main():
  # Fill in the values noted in previous step here
  cfg = { 
    "consumer_key"        : "xxxxx",
    "consumer_secret"     : "xxxxx",
    "access_token"        : "xxxxx",
    "access_token_secret" : "xxxxx" 
    }

  api = get_api(cfg)

  # send tweet
  # tweet_1 = "this is a test tweet without pic"
  # status = api.update_status(status = tweet_1) 

  # send tweet with image
  photo_path = '../test.jpg'
  tweet_2 = "this is a test tweet with pic"
  api.update_with_media(photo_path, status = tweet_2)


if __name__ == "__main__":
  main()


效果~~~~~~~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章