Python: Twitter

From OnnoWiki
Revision as of 07:58, 3 December 2015 by Onnowpurbo (talk | contribs) (→‎Test API Twitter)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 Credential

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"} 


Interaksi dengan Twitter

Print Public TimeLine

statuses = api.GetTimeline()
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

statuses = api.GetUserTimeline('onnowpurbo')
print [s.text for s in statuses]

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!

Bacaan

Ada banyak API method, baca

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

Referensi