Friday, May 17, 2013

Python - Part 2 Playing with Twitter API


Install Python if never installed before:
bowen@bowen:~$ sudo apt-get install python-pip python-dev build-essential

bowen@bowen:~$ sudo pip install python-twitter

Run Python
bowen@bowen:~$ python
>>> import twitter
>>> api = twitter.Api()
>>> api = twitter.Api(consumer_key='consumer_key',consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret')

#Replace all ' ' into your own key.

Some functionality that we can play with:
>>> users=api.GetFriends()
>>> print [u.name for u in users]

>>> statuses = api.GetUserTimeline()
>>> print [s.user.name for s in statuses]

>>> status = api.PostUpdate('Test Twitter message via Python')


And acquiring more information even without third party package:

import urllib
import simplejson

def searchTweets(query):
 search = urllib.urlopen("http://search.twitter.com/search.json?q="+query)
 dict = simplejson.loads(search.read())
 for result in dict["results"]: # result is a list of dictionaries
  print "*",result["text"],"\n"

# we will search tweets about "gator game" 
searchTweets("gator+game&rpp=50") # &rpp here is to offer arbitrary results that is more than 15.

Just list a few results here:
* RT @colebutler787: Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* RT @colebutler787: Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* @BrandtSnedeker at a Florida Gator football game, duh

* RT @colebutler787: Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* RT @colebutler787: Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* RT @colebutler787: Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* RT @CHCAWarriors: Awesome crowd at tonight's game.
 Final CHCA 6 St. Luke's 6
Good jobs guys, time to hit the weight room and get ready for the fall.

* Great game tonight guys! @eLEMONator17 @BrandonMoz89 @JonathanW37 @JustenKinder @sirtwinksalots @khumphries734 @gator_dav15 @TheDaveMaster0

* I liked a @YouTube video from @iheartmakeup92 http://t.co/Tflm5sFnt4 Get Ready With Me│GATOR GAME STYLE

* You can't have a conscience in the pimp game. #gator

* RT @Jhalapio33: Had a lot of tackles today with two assist tackles and 2 sacks ! Blessed with a good game !

* @0hMelissaRegan yay Zoey!!!!! Man I might have to go to a Gator game to see her cheer... Eeeeee.... We will have to make it the FSU/UF game

* If Gametracker is tracking the UGA-UF game in real time, then it took Gator reliever Parker Danciu 12 minutes to throw to one batter.

* I can't belive that was my last game as a gator, I'm going to miss it so much 😭

* RT @DoubleO_55: Pumped for the the blue and gold game.🏈 #blueteamswag @AlRoberts16 @J_Flinno @gator_68 @T_sully42 @JaredEidson6 @CadenRicketts


No comments:

Post a Comment