Twittering with Python

  • 18,164 views
Uploaded on

Python libraries for the Twitter API: Overview and applications

Python libraries for the Twitter API: Overview and applications

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
18,164
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
315
Comments
0
Likes
16

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • EuroPython talk “Twittering with Python” by @onyame http://tr.im/schreiber #europython
  • Follow this presentation @ python_demo
  • Final version of this presentation is available on @slideshare http://tr.im/ep09twt
  • What is Twitter? Summary from http://en.wikipedia.org/wiki/Twitter
  • History of My Blog http://tr.im/pXOi by Hugh MacLeod @gapingvoid
  • Many, many people are on Twitter, e.g. see http://tr.im/pXBr by @ timesonline
  • Many many people are on Twitter! For examples, see http://tr.im/pXBr
  • Guido „Creator of Python“ van Rossum on Twitter: @gvanrossum
  • German Aerospace Center (DLR) on Twitter: @DLR_de
  • The Open Source CMS Plone on Twitter: @plone
  • Python Package Index on Twitter: @pypi #python
  • BBC News on Twitter: @bbcbreaking (btw. I’m so sad about #michaeljackson)
  • EuroPython on Twitter: @europython
  • Supercomputing conference SC09 on Twitter: @SuperComputing
  • http://twitter.com uses Ruby on Rails http://rubyonrails.org/
  • I
  • Search for hash tags, such as http://search.twitter.com/search?q=%23europython
  • There are many Twitter application, see http://twitter.pbwiki.com/Apps
  • Coming to the Twitter Application Programming Interface…
  • Twitter has an REST-based API: http://apiwiki.twitter.com
  • Twitter API methods include Status, Account, User, Direct Message, and Friendship …
  • Twitter API methods include Favorite, Help, Notification, and Block
  • Twitter supports Basic HTTP authentication and OAuth http://oauth.net/
  • There are Twitter libraries for many languages http://apiwiki.twitter.com/Libraries … of course, we are interested in #python ;-)
  • At least 4 Python Twitter libraries exist: http://tr.im/pytwitter http://tr.im/twyt http://tr.im/twittytwister http://tr.im/pytwittertools
  • Library #1: python-twitter http://tr.im/pytwitter by Dewitt Clinton @dewitt
  • API and Data model of python-twitter http://tr.im/pytwitter
  • Library #2: Twyt http://tr.im/twyt by Andrew Price @andyprice
  • API and Data model of Twyt http://tr.im/twyt
  • Library #3: twitty-twister http://tr.im/twittytwister by Dustin Sallings @ dlsspy
  • API and data model of twitty-twister http://tr.im/twittytwister
  • Library #4: Python Twitter Tools (PTT) http://tr.im/pytwittertools by Mike Verdone @verdonethestone
  • API and data model of Python Twitter Tools http://tr.im/pytwittertools
  • Some simple examples for the Python Twitter Tools
  • Direct comparison of all Python Twitter Libraries
  • Now showing some examples and demos… #twitter #python
  • Demo: Show recent updates of user @DLR_de
  • Demo: Post an update… very simple ;-)
  • Demo: Add the Python User Group Cologne @pycologne as a friend #köln
  • The tail2twitter is an extension of http://code.activestate.com/recipes/157035
  • tail2twitter can be used to monitor server log files.
  • Demo: Notification from SVNChecker (see presentation from #europython 2008 http://tr.im/q38T)
  • It’s easy to develop Twitter application on Smart phones with PyS60 http://tr.im/q4wm
  • Demo: Slide change notification from Microsoft PowerPoint. If you read this message, the demo is successful!
  • In Python, it’s very easy to catch the slide change event http://msdn.microsoft.com/en-us/library/bb231077.aspx
  • For details see Roy Han’s #pycon tutorial Automating Windows Applications with win32com http://tr.im/q43o
  • Demo: Twitter notification from MoinMoin Wiki http://moinmo.in/
  • Thank You! For further contact: follow @onyame, visit http://tr.im/schreiber, or send E-Mail to python@dlr.de

Transcript

  • 1. Twittering with Python Python libraries for the Twitter API: Overview and applications EuroPython 2009 (June 30th 2009, Birmingham) Andreas Schreiber <Andreas.Schreiber@dlr.de> German Aerospace Center (DLR), Cologne, Germany http://www.dlr.de/sc | http://tr.im/schreiber Folie 1 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 2. Follow this presentation… Live! PowerPoint to Twitter! twitter.com/python_demo Folie 2 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 3. Final Version of this Slides is available on SlideShare http://tr.im/ep09twt Folie 3 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 4. What is Twitter? Micro-Blogging Service Users can send text messages Users can receive text messages of others Text messages can have up to 140 characters The text messages are called „Updates“ or „Tweets“ Social Network One can subscribe to other users („Following“) Subscriber of own updates are „Follower“ On the Twitter web site one can enter updates and … See updates of all Friends Users can control visibility http://en.wikipedia.org/wiki/Twitter Folie 4 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 5. Folie 5 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 6. Who twitters? Persons (private persons, „Celebrities“, politicians, …) Guido van Rossum, Ian Foster, Al Gore, Barack Obama, William Shatner Research centers & universities DLR, ESA, NASA, Fraunhofer, DHBW Mannheim, FH Köln, Cardiff Uni. Companies (publishers, IT companies, Dienstleister, …) O‘Reilly, Heise, Sun Microsystems, Google, XING, Starbucks, Bahn Software projects & products Digsby, Dropbox, Plone, Jython, SharePoint, SlideShare, Camtasia Media (Newspapers, TV stations, TV shows, …) Bild, The Times, Focus, ZEIT, BBC, CNN, Pro7, TV Total Conferences and organizations EuroPython, PyCon, Supercomputing, EclipseCon, Greenpeace Folie 6 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 7. Folie 7 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 8. Guido van Rossum Folie 8 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 9. DLR_de Folie 9 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 10. Plone Folie 10 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 11. Python Package Index Folie 11 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 12. BBC Breaking News Folie 12 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 13. EuroPython Folie 13 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 14. Super Computing Folie 14 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 15. Web Sites and Technologies http://twitter.com Web application developed with Ruby on Rails Message Queue Server Kestrel (fka. Starling) developed in Scala Available under Apache 2.0 License http://github.com/robey/kestrel/tree/master http://search.twitter.com Originally develop by Summize Real time search over Twitter-XMPP-Stream Requests with Atom and JSON: http://search.twitter.com/search.atom?q=<query> http://search.twitter.com/search.json?q=<query> Folie 15 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 16. Twitter Basics (1) Tweet A Post (message, update) at Twitter Max. 140 Characters (incl. white space), only text Timeline History of the latest updates „Public Timeline“: Updates of all users („Everyone“) Folie 16 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 17. Twitter Basics (2) Follow Selection of „Friends“ for your own timeline („Stream“) „Followers“ are following your own updates Personal Page You own Timeline Status information Folie 17 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 18. Twitter Basics (3) Replies Response to an other user To reply, add „@username“ to the message Replies can be from your „Friends“ or any other user Direct Messages Private messages Can be sent to your “Follower” only, not to “Friends” Retweets „Forward“ of interesting updates to own friends Usually, Retweets starts with a „RT“ Folie 18 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 19. Hash tags Hash tags in Twitter messages Begins with a hash „#“ Often used for events, cities, countries, … Examples #birmingham #europython Folie 19 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 20. Twitter Applications Web applications Native Clients Windows, Mac OS X, Linux iPod/iPhone Smartphones Integration in existing applications Instant Messenger Social Networking Services See list at http://twitter.pbwiki.com/Apps Folie 20 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 21. Twitter API Folie 21 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 22. Twitter API REST-based API HTTP-Requests Data formats: XML, JSON, RSS, Atom Authentication with OAuth or HTTP Basic Authentication Error messages are provided (e.g., in XML): <?xml version="1.0" encoding="UTF-8"?> <hash> <request>/direct_messages/destroy/456.xml</request> <error>No direct message with that ID found.</error> </hash> Limitation: Max. 100 Requests in 60 minutes per client Based on: IP (unauthorized) or User ID (authorized) Documentation: http://apiwiki.twitter.com Folie 22 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 23. API Methods (1) Status Methods User Methods public_timeline friends friends_timeline followers user_timeline featured show show update Direct Message Methods replies direct_messages destroy sent Account Methods new verify_credentials destroy end_session Friendship Methods: archive create update_location destroy update_delivery_device exists Folie 23 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 24. API Methods (2) Favorite Methods Notification Methods favorites follow create leave destroy Block Methods Help Methods create test destroy downtime_schedule Folie 24 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 25. Authentication Two Techniques to Authenticate with the REST API Basic Auth Sends user credentials in the header of the HTTP request Easy to use, but insecure and difficult to track OAuth Token-passing mechanism Allows users to control which application have access to their data without giving away their passwords Specification: http://oauth.net/ Registration of clients: http://twitter.com/oauth_clients http://oauth.net/core/1.0/ Folie 25 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 26. Twitter API: Getting Started (1) Public Timeline Getting the Public Timeline (last 20 entries) As RSS curl http://twitter.com/statuses/public_timeline.rss As JSON curl http://twitter.com/statuses/public_timeline.json As XML curl http://twitter.com/statuses/public_timeline.xml Folie 26 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 27. Twitter API: Getting Started (2) Timeline of your own „Friends“ curl -u python_demo:*** http://twitter.com/statuses/friends_timeline.xml Folie 27 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 28. Twitter API: Getting Started (3) Posting of „Updates“ curl -u python_demo:**** -d status="This message was sent using cURL" http://twitter.com/statuses/update.json Folie 28 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 29. Libraries List of libraries: http://apiwiki.twitter.com/Libraries ActionScript / Flash C++ C#/.NET Java Objective-C/Cocoa Perl PHP PL/SQL Python Ruby Scala Folie 29 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 30. Twitter Libraries in Python Four Implementations python-twitter by DeWitt Clinton. This library provides a pure Python interface for the Twitter API. python-twyt by Andrew Price. BSD licensed Twitter API interface library and command line client. twitty-twister by Dustin Sallings. A Twisted interface to Twitter. Python Twitter Tools (PTT) by Mike Verdone. A Twitter API, command-line tool, and IRC bot. Folie 30 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 31. python-twitter Pure Python Interface for the Twitter API Project information Project site: http://code.google.com/p/python-twitter Author: DeWitt Clinton (Google; http://unto.net) Apache License 2.0 Provided functionality Supported Methods: Status, User, Direct message Authentication: Basic Auth Provides a Python wrapper around the Twitter API and data model Requirements simplejson (http://cheeseshop.python.org/pypi/simplejson) Folie 31 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 32. python-twitter API and Data Model API Class twitter.Api import twitter # without authentication api = twitter.Api() # with authentication api = twitter.Api(username=‘username', password=‘password') Data model Three model classes: twitter.Status, twitter.User, and twitter.DirectMessage API method return instances of these classes statuses = api.GetPublicTimeline() users = api.GetFriends() Folie 32 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 33. Twyt A Twitter API interface for Python Project information Project site: http://andrewprice.me.uk/projects/twyt Author: Andrew Price BSD License Provided functionality Supported Methods: Status, User, Direct Message, Friendship, Social Graph, Block Authentication: Basic Auth, OAuth Requirements simplejson (http://cheeseshop.python.org/pypi/simplejson) Folie 33 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 34. Twyt API and Data Model API Class twyt.twitter.Twitter, methods return JSON from twyt import twitter, data api = twitter.Twitter() # authenticate api.set_auth(‘username', ‘password') Data model Model classes in module twyt.data: User, Status, DirectMsg, StatusList, DirectList, RateLimit Provides easy access to JSON data timeline = api.status_public_timeline() statuses = data.StatusList(timeline) print statuses[0].user.name, statuses[0].text Folie 34 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 35. twitty-twister A Twisted interface to Twitter Project information Project site: http://github.com/dustin/twitty-twister/tree/master Author: Dustin Sallings License: ? Provided functionality Supported methods: Status, User, Direct message, Friendship Authentication: Basic Auth, OAuth Requirements Twisted (http://twistedmatrix.com) Folie 35 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 36. twitty-twister API and Data Model API Class twitter.Twitter, with Basic or OAuth authentication from twisted.internet import reactor, protocol, defer, task import twitter api = twitter.Twitter(‘username', ‘password') Data model Module txml parses XML and provides access classes: Author, Entry, Status, User, DirectMessage Callback functions can be used. Folie 36 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 37. twitty-twister Example Get public timeline def gotEntry(msg): print “Message from %s: %s" % (msg.author.name, msg.title) api.public_timeline(gotEntry).addBoth(lambda x: reactor.stop()) reactor.run() Update („post message“) def callback(x): print "Posted id", x def errorback(e): print e api.update(‘text').addCallback(cb).addErrback(eb). addBoth(lambda x: reactor.stop()) reactor.run() Folie 37 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 38. Python Twitter Tools (PTT) Twitter API, command-line tool, and IRC bot Project information Project site: http://mike.verdone.ca/twitter Author: Mike Verdone MIT License Provided functionality Supported methods: status, user, direct message Authentication: Basic Auth IRC bot that can announce Twitter updates to an IRC channel Requirements irclib (http://python-irclib.sourceforge.net) simplejson (http://cheeseshop.python.org/pypi/simplejson) Folie 38 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 39. Python Twitter Tools (PTT) API and Data Model API Class twitter.api.Twitter, with Basic Auth import twitter.api # without authentication api = twitter.api.Twitter() # with authentication api = twitter.api.Twitter(‘username', ‘password') Data model Class TwitterCall generates dynamics calls and returns decoded JSON (as Python lists, dicts, ints, strings) Data in XML format api = twitter.api.Twitter(format=“XML”) Folie 39 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 40. Python Twitter Tools (PTT) Example Get public timeline timeline = api.statuses.public_timeline() print timeline[0][‘user’][‘screen_name’] print timeline[0][‘text’] Get friends timeline # two types of invocation possible: api.statuses.friends_timeline(id=‘europython’) api.statuses.friends_timeline.europython() Update („post message“) api.statuses.update(status=‘message’) Folie 40 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 41. Comparison Python Twitter Libraries Library Supported API Supported Requirements Methods Authentication Python-twitter Status, User, Direct Basic Auth simplejson message Twyt Status, User, Direct Basic Auth, simplejson Message, OAuth Friendship, Social Graph, Block Twitty-twister Status, User, Direct Basic Auth, Twisted message, OAuth Friendship Python Status, User, Direct Basic Auth simplejson, Twitter Tools message (irclib) Folie 41 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 42. Examples and Demos Folie 42 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 43. Example Get all Updates of a User import twitter api = twitter.Api() statuses = api.GetUserTimeline('DLR_de') print [s.text for s in statuses[:5]] [u"Video: @NASA's SOFIA mission update http://tinyurl.com/cmmna3 #SOFIA (Stratospheric Observatory For Infrared Astronomy)", u'Video zur GOCE-Mission (Messung der Schwerkraft) http://tinyurl.com/c5zhdb #euronews', u'(EN) Now following @ESAGermany and @ESAGOCE', u'@AndreasSchepers Gute Frage. Immerhin hat die NASA-Mission keinen Vornamen. #Johannes #ATV #NASA', u'RT @AndreasSchepers Thomas Reiter: Wir wissen mehr xfcber den Mars als den Mond. http://is.gd/k7qZ #Mp3 #DeutschlandRadioKultur #DLR-Vorstand'] Folie 43 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 44. Example Posting an Update import twitter api = twitter.Api(username='python_demo', password='***') api.PostUpdate('Moin!') Folie 44 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 45. Example Adding a Friend („Follow“) import twitter api = twitter.Api(username='python_demo', password='***') user = api.CreateFriendship('pycologne') print user {"description": "Python User Group Cologne", "id": 23515458, "location": "Cologne, Germany", "name": "PyCologne", "profile_image_url": "http://s3.amazonaws.com/twitter_production/profile_images/91413444/p ycologne_logo_small_quadratisch_normal.jpg", "screen_name": "pycologne", "url": "http://www.pycologne.de"} Folie 45 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 46. Example „tail –f“ to Twitter # based on http://code.activestate.com/recipes/157035/ import time, os import twitter api = twitter.Api(username='python_demo', password=‘*') file = open('test.log','r') # ... <Find the size of the file and move to the end> while 1: where = file.tell() line = file.readline() if not line: time.sleep(1) file.seek(where) else: api.PostUpdate(line) Folie 46 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 47. Example „tail –f“ to Twitter Folie 47 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 48. Demo Twitter in Software Engineering Notifications from SVNChecker IDE e.g. Eclipse http://svnchecker.tigris.org/ Checks on source code Commit Coding Style Source code analysis  Access rights a failed check automated test & build results Notification of results Commit and other messages E-Mail, Log file, data bases, Check RSS feeds, Twitter Folie 48 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 49. Demo Twitter on Smart Phones The Twitter libraries work on S60 phones with PyS60 Example: Posting incoming SMS messages to Twitter account import sys sys.path.append("e:Python") # PyS60 import appuifw, e32, inbox # Twyt from twyt import twitter box = inbox.Inbox() box.bind(message_received) print "Waiting for new SMS messages..." appuifw.app.exit_key_handler = quit app_lock = e32.Ao_lock() app_lock.wait() Folie 49 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 50. Demo Posting incoming SMS messages to Twitter account Message evaluation: post SMS as Twitter message def message_received(msg_id): box = inbox.Inbox() sms_text = box.content(msg_id) appuifw.note(u"SMS content: " + sms_text , "info") api = twitter.Twitter() api.set_auth('python_demo', ‘*') api.status_update(sms_text) app_lock.signal() Folie 50 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 51. Demo Twitter in PowerPoint Twitter message on every PowerPoint OnSlideShowNextSlide event The speaker note will be the message, if existing python-twitter Folie 51 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 52. Twitter in PowerPoint Implementation using Python’s win32com (1) Event handler for PowerPoint import twitter api = twitter.Api(username='python_demo', password='*') class EventManager(object): def OnSlideShowNextSlide(self, Wn): i = powerpoint.ActivePresentation. SlideShowWindow.View.Slide.SlideIndex for shape in powerpoint.ActivePresentation. Slides[i-1].NotesPage.Shapes: if shape.TextFrame.HasText: notes = shape.TextFrame.TextRange.Text api.PostUpdate(notes) Folie 52 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 53. Twitter in PowerPoint Implementation using Python’s win32com (2) Dispatch PowerPoint with event handler and listen for events from win32com.client import DispatchWithEvents powerpoint = DispatchWithEvents('PowerPoint.Application', EventManager) powerpoint.Visible = 1 # Listen for events import threading, pythoncom stopEvent = threading.Event() while True: pythoncom.PumpWaitingMessages() stopEvent.wait(.2) if stopEvent.isSet(): stopEvent.clear() break Source: Roy Han’s PyCon 2008 tutorial “Automating Windows Applications with win32com” http://tr.im/q43o Folie 53 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 54. Demo Twitter Notification from MoinMoin Wiki Twitter handler for the MoinMoin Event System python-twitter Folie 54 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009
  • 55. Questions? Contact http://tr.im/schreiber twitter.com/onyame python@dlr.de Folie 55 EuroPython 2009 > Andreas Schreiber> Twittering with Python > 30.06.2009