Reputation: 70
This is my code. Instead of all the garbage values that I receive, how can I just output the text of the tweet, username and the timestamp?
import oauth2 as oauth
import urllib2 as urllib
access_token_key = "secret"
access_token_secret = "secret
consumer_key = "secret"
consumer_secret = "secret"
_debug = 0
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method = "GET"
http_handler = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)
def twitterreq(url, method, parameters):
req = oauth.Request.from_consumer_and_token(oauth_consumer,
token=oauth_token,
http_method=http_method,
http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if http_method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
return response
def fetchsamples():
url = "https://userstream.twitter.com/1.1/user.json"
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
print line.strip()
if __name__ == '__main__':
fetchsamples()
The output I receive is like this
{"created_at":"Mon Dec 09 15:10:41 +0000 2013","id":410063960465891329,"id_str":"410063960465891329","text":"Django Hacker, Next Gen E-Commerce Technology http://t.co/de6gdIqJvL #Hyderabad","source":"\u003ca href=\"http://jobs.hasgeek.com\" rel=\"nofollow\"\u003eHasGeek Job Board\u003c/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":260654014,"id_str":"260654014","name":"HasGeek Job Board","screen_name":"hasjob","location":"","url":"http://jobs.hasgeek.in","description":"Job listings from the HasGeek Job Board.","protected":false,"followers_count":1530,"friends_count":1,"listed_count":32,"created_at":"Fri Mar 04 09:21:06 +0000 2011","favourites_count":0,"utc_offset":19800,"time_zone":"Mumbai","geo_enabled":true,"verified":false,"statuses_count":10528,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/1271127705/logo-star_normal.png","profile_image_url_https":"https://pbs.twimg.com/profile_images/1271127705/logo-star_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Hyderabad","indices":[69,79]}],"symbols":[],"urls":[{"url":"http://t.co/de6gdIqJvL","expanded_url":"http://hsgk.in/1aOhn8V","display_url":"hsgk.in/1aOhn8V","indices":[46,68]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium","lang":"en"}
Upvotes: 1
Views: 440
Reputation: 1263
It's json. In python, at a very basic level, you can import the json library, load the tweet as a python object from json and print the fields as below.
import json
...
tweetObj = json.loads(response)
print tweetObj['user']['id_str'], tweetObj['created_at'], tweetObj['text']
Upvotes: 1