Twittering with Python Python libraries for the Twitter API: Overview and applications



Similar documents
Programming Mobile Apps with Python

Beyond The Web Drupal Meets The Desktop (And Mobile) Justin Miller Code Sorcery Workshop, LLC

Getting Started Guide for Developing tibbr Apps

latest Release 0.2.6

Android Development Exercises Version Hands On Exercises for. Android Development. v

About This Document 3. Integration and Automation Capabilities 4. Command-Line Interface (CLI) 8. API RPC Protocol 9.

Facebook apps in Python

Katy Young s Guide to... Twitter

OpenShift on you own cloud. Troy Dawson OpenShift Engineer, Red Hat November 1, 2013

2sms SMS API Overview

DESIGN AND DEVELOPMENT OF SOCIAL NETWORK AGGREGATOR

Open Source Software Development within DLR. Andreas Schreiber

Web app AAI Integration How to integrate web applications with AAI in general?

Mixing Python and Java How Python and Java can communicate and work together

Retailer Operating Manual for e-books

socketio Documentation

A RESTful Web Service for Whois. Andy Newton Chief Engineer, ARIN

Programming Social Applications

Web Security Testing Cookbook*

itext SMS-based Web Services for Low-end Cell Phones

Enter Here -> Directory Submitter Software For One > Visit Here <

AdRadionet to IBM Bluemix Connectivity Quickstart User Guide

Contents. 2 Alfresco API Version 1.0

How To Use The Rss Feeder On Firstclass (First Class) And First Class (Firstclass) For Free

BS1000 command and backlog protocol

Leveraging Cloud Storage Through Mobile Applications Using Mezeo Cloud Storage Platform REST API. John Eastman Mezeo

SETTING UP YOUR JAVA DEVELOPER ENVIRONMENT

THE CHALLENGE OF ADMINISTERING WEBSITES OR APPLICATIONS THAT REQUIRE 24/7 ACCESSIBILITY

the missing log collector Treasure Data, Inc. Muga Nishizawa

FIREEYE APP FOR SPLUNK ENTERPRISE 6.X. Configuration Guide Version 1.3 SECURITY REIMAGINED

Authenticate and authorize API with Apigility. by Enrico Zimuel Software Engineer Apigility and ZF2 Team

Fairsail REST API: Guide for Developers

Technical documentation

INTERFACE CATALOG SHORETEL DEVELOPER NETWORK. ShoreTel Professional Services

8x8 Virtual Office Online with Softphone User Guide

Apigee Gateway Specifications

Single Sign On. SSO & ID Management for Web and Mobile Applications

Accelerating Rails with

How To Develop A Facebook App

E*TRADE Developer Platform. Developer Guide and API Reference. October 24, 2012 API Version: v0

FireEye App for Splunk Enterprise

XML Processing and Web Services. Chapter 17

Middleware integration in the Sympa mailing list software. Olivier Salaün - CRU

ITP 140 Mobile Technologies. Mobile Topics

Manage cloud infrastructures using Zend Framework

Cloud Elements ecommerce Hub Provisioning Guide API Version 2.0 BETA

HTTPS hg clone plugin library SSH hg clone plugin library

FileMaker Server 9. Custom Web Publishing with PHP

Open Source Telemedicine Android Client Development Introduction

Flash and Python. Dynamic Object oriented Rapid development. Flash and Python. Dave Thompson

Manual. Netumo NETUMO HELP MANUAL Copyright Netumo 2014 All Rights Reserved

Amazon Glacier. Developer Guide API Version

MIT Tech Talk, May 2013 Justin Richer, The MITRE Corporation

Cloud to Cloud Integrations with Force.com. Sandeep Bhanot Developer

TweetAttacks Pro. User Manual

1

SOCIAL NETWORKING IN SMARTPHONE THROUGH A PROTOTYPE IMPLEMENTATION USING ANDROID

Elgg 1.8 Social Networking

Sonatype CLM for Maven. Sonatype CLM for Maven

The Cloud to the rescue!

Getting Started Guide with WIZ550web

Automatic measurement of Social Media Use

Salesforce Files Connect Implementation Guide

Do I need anything special to use it? All you need to use Twitter is an Internet connection or a mobile phone with Internet capability.

An Oracle White Paper June RESTful Web Services for the Oracle Database Cloud - Multitenant Edition

alternative solutions, including: STRONG SECURITY for managing these security concerns. PLATFORM CHOICE LOW TOTAL COST OF OWNERSHIP

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

How-To Guide: Twitter Marketing. Content Provided By

Smartphone Enterprise Application Integration

RepoGuard Validation Framework for Version Control Systems

Enter Here --->> New Instant Directory Profits Software - ebook

Overview of Web Services API

Some Experiences With Python For Android (Py4A) Nik Klever University of Applied Sciences Augsburg

FileMaker Server 12. Custom Web Publishing with PHP

OpenID Single Sign On and OAuth Data Access for Google Apps. Ryan Dave Primmer May 2010

Social Application Guide

Identity. Provide. ...to Office 365 & Beyond

How to secure your Apache Camel deployment

VOL. 2, NO. 1, January 2012 ISSN ARPN Journal of Science and Technology ARPN Journals. All rights reserved

The Decaffeinated Robot

Web Frameworks. web development done right. Course of Web Technologies A.A. 2010/2011 Valerio Maggio, PhD Student Prof.

Adobe ColdFusion 11 Enterprise Edition

IBM Endpoint Manager Version 9.1. Patch Management for Red Hat Enterprise Linux User's Guide

Next Generation Open Source Messaging with Apache Apollo

I) Add support for OAuth in CAS server

Force.com REST API Developer's Guide

Z Documentation. Release 2. Bart Thate

ULTEO OPEN VIRTUAL DESKTOP OVD WEB APPLICATION GATEWAY

Introduction to Oracle Mobile Application Framework Raghu Srinivasan, Director Development Mobile and Cloud Development Tools Oracle

Hootsuite instructions

Transcription:

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

Follow this presentation Live! PowerPoint to Twitter! twitter.com/python_demo Folie 2

Final Version of this Slides is available on SlideShare http://tr.im/ep09twt Folie 3

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

Folie 5

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

Folie 7

Guido van Rossum Folie 8

DLR_de Folie 9

Plone Folie 10

Python Package Index Folie 11

BBC Breaking News Folie 12

EuroPython Folie 13

Super Computing Folie 14

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

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

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

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

Hash tags Hash tags in Twitter messages Begins with a hash # Often used for events, cities, countries, Examples #birmingham #europython Folie 19

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

Twitter API Folie 21

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

API Methods (1) Status Methods public_timeline friends_timeline user_timeline show update replies destroy Account Methods verify_credentials end_session archive update_location update_delivery_device User Methods friends followers featured show Direct Message Methods direct_messages sent new destroy Friendship Methods: create destroy exists Folie 23

API Methods (2) Favorite Methods favorites create destroy Help Methods test downtime_schedule Notification Methods follow leave Block Methods create destroy Folie 24

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

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

Twitter API: Getting Started (2) Timeline of your own Friends curl -u python_demo:*** http://twitter.com/statuses/friends_timeline.xml Folie 27

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

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

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

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

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

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

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

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

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

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

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

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

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

Comparison Python Twitter Libraries Library Supported API Methods Supported Authentication Requirements Python-twitter Status, User, Direct message Basic Auth simplejson Twyt Status, User, Direct Message, Friendship, Social Graph, Block Basic Auth, OAuth simplejson Twitty-twister Status, User, Direct message, Friendship Basic Auth, OAuth Twisted Python Twitter Tools Status, User, Direct message Basic Auth simplejson, (irclib) Folie 41

Examples and Demos Folie 42

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

Example Posting an Update import twitter api = twitter.api(username='python_demo', password='***') api.postupdate('moin!') Folie 44

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

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

Example tail f to Twitter Folie 47

Demo Twitter in Software Engineering Notifications from SVNChecker http://svnchecker.tigris.org/ Checks on source code Coding Style Source code analysis Access rights automated test & build results Notification of results Commit and other messages E-Mail, Log file, data bases, RSS feeds, Twitter IDE e.g. Eclipse a failed check Check Commit Folie 48

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

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

Demo Twitter in PowerPoint Twitter message on every PowerPoint OnSlideShowNextSlide event The speaker note will be the message, if existing python-twitter twitter Folie 51

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

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

Demo Twitter Notification from MoinMoin Wiki Twitter handler for the MoinMoin Event System python-twitter twitter Folie 54

Questions? Contact http://tr.im/schreiber tr.im/schreiber twitter.com/onyame python@dlr.de Folie 55