Difference between revisions of "Python: Twitter"

From OnnoWiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 15: Line 15:
  
  
==Test API Twitter==
+
==Test API Twitter Credential==
  
 
Code
 
Code
Line 29: Line 29:
  
 
  {"id": 16133, "location": "Philadelphia", "name": "bear"}  
 
  {"id": 16133, "location": "Philadelphia", "name": "bear"}  
 +
 +
 +
==Interaksi dengan Twitter==
 +
  
 
Print Public TimeLine
 
Print Public TimeLine
  
  statuses = api.GetPublicTimeline()
+
  statuses = api.GetTimeline()
 
  print [s.user.name for s in statuses]
 
  print [s.user.name for s in statuses]
 
   [u'DeWitt', u'Kesuke Miyagi', u'ev', u'Buzz Andersen', u'Biz Stone']  
 
   [u'DeWitt', u'Kesuke Miyagi', u'ev', u'Buzz Andersen', u'Biz Stone']  
Line 38: Line 42:
 
Print / fetch single "user" public message
 
Print / fetch single "user" public message
  
 
+
statuses = api.GetUserTimeline('onnowpurbo')
 
+
print [s.text for s in statuses]
  
 
Print / fetch user friends
 
Print / fetch user friends
Line 52: Line 56:
  
 
  I love python-twitter!
 
  I love python-twitter!
 +
 +
==Bacaan==
  
 
Ada banyak API method, baca
 
Ada banyak API method, baca

Latest revision as of 07:58, 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 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