html5-img
1 / 7

Overview of Twitter API

Overview of Twitter API. Nathan Liu. Twitter API Essentials. Twitter API is a Representational State Transfer(REST) style web services exposed over HTTP(S). Can be accessed with any HTTP client using either GET( accessor ) and POST( mutator ) methods. Examples: Getting a user’s profile

rea
Télécharger la présentation

Overview of Twitter API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview of Twitter API Nathan Liu

  2. Twitter API Essentials • Twitter API is a Representational State Transfer(REST) style web services exposed over HTTP(S). • Can be accessed with any HTTP client using either GET(accessor) and POST(mutator) methods. • Examples: • Getting a user’s profile • https://api.twitter.com/1/users/show.xml?id=comp621u • Retrieving a user’s latest tweets • https://api.twitter.com/1/statuses/user_timeline.xml?id=comp621u • Search for all tweets containing the term “twitter” • https://search.twitter.com/search.json?q=twitter

  3. Method Categories • Account: authentication • Block: blockingand unblock users • Direct message: managing messages sent between pairs of users • Favorites: getting and setting favorite tweets of users. • Friendship: checking if two users and following each other. • Social graph: getting follower and friends. • Status: retrieve, post, delete tweets • Timeline: retrieve latest collection of tweets • Trends: retrieve “trending” topics • Users: getting and setting user details.

  4. Three Major APIs • REST API: access to core data about individual users, update and retrieve timelines, status data. Most used for building • Search API: retrieving tweets with constrains on keyword, locations, time. • Streaming API: receiving real time feed for access large scale tweet stream. Can filter with keywords, user lists, locations.

  5. Working with Twitter API • Results are returned in JSON, XML or RSS format. • Documentation: • http://dev.twitter.com/doc • Client libraries available in many different languages: • http://dev.twitter.com/pages/libraries • Each free account can make a maximum of 150 requests per hour. • A web console for you to try out different methods: • https://dev.twitter.com/console • A very good python package is Tweepy (https://github.com/joshthecoder/tweepy), which supports all three APIs.

  6. Example 1: Working with Tweepy auth = tweepy.BasicAuthHandler("username", "password") api = tweepy.API(auth) auth = tweepy.OAuthHandler("consumer_key", "consumer_secret") # Redirect user to Twitter to authorize redirect_user(auth.get_authorization_url()) # Get access token auth.get_access_token("verifier_value") # Construct the API instance api = tweepy.API(auth) # Iterate through all of the authenticated user's friends for friend in tweepy.Cursor(api.friends).items(): # Process the friend here process_friend(friend) # Iterate through the first 200 statuses in the friends timeline for status in tweepy.Cursor(api.friends_timeline).items(200): # Process the status here process_status(status) # follow every follower of the authenticated user for follower in tweepy.Cursor(api.followers).items(): follower.follow()

  7. Example 2: Listen for All Tweets Containing “Obama” from tweepy.streaming import StreamListener, Stream class Listener ( StreamListener ): def on_status( self, status ): print '-' * 20 print status.text return if __name__ == "__main__":   USERNAME = "YourUsernameHere" PASSWORD = "YourPasswordHere"   listener = Listener() stream = Stream( USERNAME, PASSWORD, listener );   stream.filter( track=( "obama", ) )

More Related