daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''Tweet Streaming API consumer'''
  2. #importing twitter and csv python packages
  3. import twitter
  4. import csv
  5.  
  6. # == OAuth Authentication ==
  7. consumer_key=""
  8. consumer_secret=""
  9. access_token=""
  10. access_token_secret=""
  11.  
  12.  
  13. AUTH = twitter.oauth.OAuth(access_token, access_token_secret, consumer_key, consumer_secret)
  14. TWITTER_API = twitter.Twitter(auth=AUTH)
  15.  
  16. # opens/creates a csv file in the present working directory.
  17. # remember to change the name of files from previous runs as this will overwrite without warning.
  18. csvfile = open('drWhoLive.csv', 'w')
  19. csvwriter = csv.writer(csvfile)
  20. # this gives the column headers, must make sure that they are in the same order as the "getVal"s below.
  21. csvwriter.writerow(['created_at',
  22.                     'user-screen_name',
  23.                     'text',
  24.                     'coordinates lng',
  25.                     'coordinates lat',
  26.                     'place',
  27.                     'user-location',
  28.                     'user-geo_enabled',
  29.                     'user-lang',
  30.                     'user-time_zone',
  31.                     'user-statuses_count',
  32.                     'user-followers_count',
  33.                     'user-friends_count',
  34.                     'user-created_at',
  35.                     'user-source',
  36.                     'truncated'])
  37. # added 'truncated' so that it was easy to see if the truncated if staement was working
  38. q = "#DoctorWho, #DrWho, #Neverthelessshepersisted"
  39. print 'Filtering the public timeline for keyword="%s"' % (q)
  40. twitter_stream = twitter.TwitterStream(auth=TWITTER_API.auth)
  41. stream = twitter_stream.statuses.filter(track=q)
  42.  
  43.  
  44. ''' helper functions, clean data, unpack dictionaries '''
  45. def getVal(val):
  46.     clean = ""
  47.     if isinstance(val, bool):
  48.         return val
  49.     if isinstance(val, int):
  50.         return val
  51.     if val:
  52.         clean = val.encode('utf-8')
  53.     return clean
  54.  
  55. def getLng(val):
  56.     if isinstance(val, dict):
  57.         return val['coordinates'][0]
  58.  
  59. def getLat(val):
  60.     if isinstance(val, dict):
  61.         return val['coordinates'][1]
  62.  
  63. def getPlace(val):
  64.     if isinstance(val, dict):
  65.         return val['full_name'].encode('utf-8')
  66.  
  67.  
  68. # main loop
  69. # loop that first queries the truncated and gets the fulltext, and if not continues to the get the rest of the fields.
  70. for tweet in stream:
  71.     try:
  72.         if tweet['truncated']:
  73.             tweet_text = tweet['extended_tweet']['fulltext']
  74.         else:
  75.             tweet_text = tweet['text']
  76.         csvwriter.writerow([tweet['created_at'],                        # write lots of data!!
  77.                             getVal(tweet['user']['screen_name']),
  78.                             getVal(tweet['text']),
  79.                             getLng(tweet['coordinates']),
  80.                             getLat(tweet['coordinates']),
  81.                             getPlace(tweet['place']),
  82.                             getVal(tweet['user']['location']),
  83.                             getVal(tweet['user']['geo_enabled']),
  84.                             getVal(tweet['user']['lang']),
  85.                             getVal(tweet['user']['time_zone']),
  86.                             getVal(tweet['user']['statuses_count']),
  87.                             getVal(tweet['user']['followers_count']),
  88.                             getVal(tweet['user']['friends_count']),
  89.                             getVal(tweet['user']['created_at']),
  90.                             getVal(tweet['source']),
  91.                             getVal(tweet['truncated'])
  92.                             ])
  93.         csvfile.flush()
  94.         print getVal(tweet['user']['screen_name']), getVal(tweet['text']), tweet['coordinates'], getPlace(tweet['place'])
  95.     except Exception as e:
  96.         print e.message
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top