Difference between revisions of "Python: Twitter"

From OnnoWiki
Jump to navigation Jump to search
Line 17: Line 17:
 
==Test API Twitter==
 
==Test API Twitter==
  
 
+
Code
API:
 
 
 
The API is exposed via the twitter.Api class.
 
 
 
To create an instance of the twitter.Api class:
 
  
 
  import twitter
 
  import twitter
Line 29: Line 24:
 
       access_token_key='access_token',
 
       access_token_key='access_token',
 
       access_token_secret='access_token_secret')
 
       access_token_secret='access_token_secret')
  print api.VerifyCredentials() {"id": 16133, "location": "Philadelphia", "name": "bear"}  
+
  print api.VerifyCredentials()
 +
 
 +
Akan keluar informasi panjang sekali, misalnya
 +
 
 +
{"id": 16133, "location": "Philadelphia", "name": "bear"}  
  
 
Print Public TimeLine
 
Print Public TimeLine
Line 39: Line 38:
 
Print / fetch single "user" public message
 
Print / fetch single "user" public message
  
  statuses = api.GetUserTimeline(user)
+
  90VLyeoVrsDQjibUYnMATqUvfmvWnKpIhutex25YwQ7tm
print [s.text for s in statuses]
 
  
  

Revision as of 07:48, 3 December 2015

Sumber: https://code.google.com/p/python-twitter/


Instal Dependency dan Python Twitter

Lakukan

cd /usr/local/src
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
python ez_setup.py --insecure
easy_install simplejson
easy_install httplib2-master.zip
easy_install python-oauth2-master.zip 
easy_install python-twitter-1.1.tar.gz 


Test API Twitter

Code

import twitter
api = twitter.Api(consumer_key='consumer_key',
      consumer_secret='consumer_secret',
      access_token_key='access_token',
      access_token_secret='access_token_secret')
print api.VerifyCredentials()

Akan keluar informasi panjang sekali, misalnya

{"id": 16133, "location": "Philadelphia", "name": "bear"} 

Print Public TimeLine

statuses = api.GetPublicTimeline()
print [s.user.name for s in statuses]
 [u'DeWitt', u'Kesuke Miyagi', u'ev', u'Buzz Andersen', u'Biz Stone'] 

Print / fetch single "user" public message

90VLyeoVrsDQjibUYnMATqUvfmvWnKpIhutex25YwQ7tm


Print / fetch user friends

users = api.GetFriends()
print [u.name for u in users]

Post a Twitter status message

status = api.PostUpdate('I love python-twitter!')
print status.text
I love python-twitter!

Ada banyak API method, baca

$ pydoc twitter.Api
$ pydoc twitter.Status
$ pydoc twitter.User
$ pydoc twitter.DirectMessage

Referensi