Difference between revisions of "Python: Twitter"

From OnnoWiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
  
 +
==Instal Dependency dan Python Twitter==
  
NOTE
+
Lakukan
  
GitHub is now the "source of truth" but I will always try to update to this project page.
+
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
  
https://github.com/bear/python-twitter
 
  
Python Twitter
+
==Test API Twitter Credential==
  
A Python wrapper around the Twitter API
+
Code
  
Author: The Python-Twitter Developers <python-twitter@googlegroups.com>
+
import twitter
Introduction
+
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()
  
This library provides a pure Python interface for the Twitter API.
+
Akan keluar informasi panjang sekali, misalnya
  
Twitter (http://twitter.com) provides a service that allows people to connect via the web, IM, and SMS. Twitter exposes a web services API (http://dev.twitter.com/doc) and this library is intended to make it even easier for Python programmers to use.
+
{"id": 16133, "location": "Philadelphia", "name": "bear"}
Building
 
  
From source:
 
  
Install the dependencies:
+
==Interaksi dengan Twitter==
  
http://cheeseshop.python.org/pypi/simplejson
 
http://code.google.com/p/httplib2/
 
http://github.com/simplegeo/python-oauth2
 
  
Download the latest python-twitter library from:
+
Print Public TimeLine
  
  http://code.google.com/p/python-twitter/
+
  statuses = api.GetTimeline()
 
+
print [s.user.name for s in statuses]
Extract the source distribution and run:
+
   [u'DeWitt', u'Kesuke Miyagi', u'ev', u'Buzz Andersen', u'Biz Stone']
 
 
  $ python setup.py build
 
  $ python setup.py install
 
 
 
Testing
 
 
 
With setuptools installed:
 
 
 
   $ python setup.py test
 
 
 
Without setuptools installed:
 
 
 
  $ python twitter_test.py
 
 
 
Getting the code
 
 
 
View the trunk at:
 
  
    http://code.google.com/p/python-twitter/source/
+
Print / fetch single "user" public message
 
 
Check out the latest development version anonymously with:
 
 
 
  $ hg clone http://python-twitter.googlecode.com/hg/ python-twitter
 
  $ cd python-twitter
 
  $ hg update
 
 
 
Documentation
 
 
 
View the last release API documentation at:
 
 
 
    http://dev.twitter.com/doc
 
 
 
Using
 
 
 
The library provides a Python wrapper around the Twitter API and the Twitter data model.
 
 
 
Model:
 
 
 
The three model classes are twitter.Status, twitter.User, and twitter.DirectMessage. The API methods return instances of these classes.
 
 
 
To read the full API for twitter.Status, twitter.User, or twitter.DirectMessage, run:
 
 
 
  $ pydoc twitter.Status
 
  $ pydoc twitter.User
 
  $ pydoc twitter.DirectMessage
 
 
 
API:
 
 
 
The API is exposed via the twitter.Api class.
 
 
 
To create an instance of the twitter.Api class:
 
 
 
  >>> import twitter
 
  >>> api = twitter.Api()
 
 
 
To create an instance of the twitter.Api with login credentials (many API calls required the client to be authenticated.)
 
 
 
The python-twitter library now only supports oAuth authentication as the Twitter devs have indicated that oAuth is the only method that will be supported moving forward.
 
 
 
    >>> api = twitter.Api(consumer_key='consumer_key',
 
 
 
        consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret')
 
 
 
To see if your credentials are successful:
 
 
 
    NOTE - much more than the small sample given here will print
 
 
 
    >>> print api.VerifyCredentials() {"id": 16133, "location": "Philadelphia", "name": "bear"}
 
 
 
To fetch the most recently posted public Twitter status messages:
 
 
 
  >>> statuses = api.GetPublicTimeline()
 
  >>> print [s.user.name for s in statuses]
 
  [u'DeWitt', u'Kesuke Miyagi', u'ev', u'Buzz Andersen', u'Biz Stone']
 
  
To fetch a single user's public status messages, where "user" is either a Twitter "short name" or their user id.
+
statuses = api.GetUserTimeline('onnowpurbo')
 +
print [s.text for s in statuses]
  
  >>> statuses = api.GetUserTimeline(user)
+
Print / fetch user friends
  >>> print [s.text for s in statuses]
 
  
To fetch a list a user's friends (requires authentication):
+
users = api.GetFriends()
 +
print [u.name for u in users]
  
  >>> users = api.GetFriends()
+
Post a Twitter status message
  >>> print [u.name for u in users]
 
  
To post a Twitter status message (requires authentication):
+
status = api.PostUpdate('I love python-twitter!')
 +
print status.text
  
  >>> status = api.PostUpdate('I love python-twitter!')
+
I love python-twitter!
  >>> print status.text
 
  I love python-twitter!
 
  
There are many more API methods, to read the full API documentation:
+
==Bacaan==
  
  $ pydoc twitter.Api
+
Ada banyak API method, baca
  
 +
$ pydoc twitter.Api
 +
$ pydoc twitter.Status
 +
$ pydoc twitter.User
 +
$ pydoc twitter.DirectMessage
  
 
==Referensi==
 
==Referensi==
  
 
* https://code.google.com/p/python-twitter/
 
* https://code.google.com/p/python-twitter/

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