Guest User

Untitled

a guest
Nov 29th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import tweepy
  2. from tweepy.streaming import StreamListener
  3.  
  4. from tweepy import OAuthHandler
  5. from tweepy import Stream
  6. import re
  7. import sys
  8. import json
  9. import pytz
  10. from pytz import timezone
  11.  
  12. #connect to pymysql
  13. import pymysql
  14.  
  15. conn = pymysql.connect(host='hosting',
  16. user='username',
  17. password='password',
  18. db='database',
  19. autocommit=True,
  20. charset='utf8mb4',)
  21.  
  22.  
  23.  
  24. consumer_key = '******'
  25. consumer_secret = '********'
  26. access_token = '****-****'
  27. access_token_secret = '*****'
  28.  
  29. KEYWORDS = ['singaporepoly','singapore poly', 'sp courses', 'sp open house',
  30. 'sp mcdonald', 'sp singapore', 'sp map', 'spoh18', 'sp cca', 'sp career', 'sp library',
  31. 'sp part time', 'sp starbucks', '#speye', '#singaporepoly','sp facilities', 'sp polite',
  32. 'sp memories','spgusto', 'sp engineering', 'sp campus','sp bowling', 'sp dmit','sp wifi',
  33. 'sp bowling','sp FC1', 'sp FC2', 'sp FC3', 'sp FC4', 'sp FC5', 'sp FC6', 'sp mae', 'sp eee',
  34. 'sp aircon', 'sp internet','sp mac', 'spdb', 'sp goodies','sp goodie','sp kfc', 'sp bridge',
  35. 'sp mrt', 'sp cls','sp sb','#spbizsch', 'sp jae','sp results','sp result','sp jpsae','sp dsa',
  36. 'sp interview', 'sp eat','sp student','sp camp','sp freshies','sp foc','sp pool','sp class',
  37. 'sp freshmen','sp floorball','sp sports','sp semester','sp cass','sp mass comm','sp information technology',
  38. 'sp event','sp dover','sp moberly','sp performance','spsdz','sp subway','sp chicken rice','sp mini wok',
  39. 'sp mala','sp koufu','sp mcdonalds','Singapore Polytechnic','sp student union','spsu']
  40.  
  41. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  42. auth.set_access_token(access_token, access_token_secret)
  43.  
  44. sgtz = timezone('Asia/Singapore')
  45. utc = pytz.timezone('UTC')
  46.  
  47. class StdOutListener(StreamListener):
  48. def on_status(self, status):
  49. # Printing to Terminal
  50. if ('Singapore' == status.user.location) or ('Central Region, Singapore' == status.user.location) or ('North-East Region, Singapore' == status.user.location) or ('East Region, Singapore' == status.user.location) or ('West Region, Singapore' == status.user.location):
  51. d = status.created_at
  52. d_tz = utc.localize(d)
  53. localtime = d_tz.astimezone(sgtz)
  54. status_list = [localtime, status.author.screen_name, status.text]
  55. print (status_list)
  56.  
  57. #write to database
  58. cur = conn.cursor()
  59. cur.execute("USE database")
  60. cur.execute("INSERT INTO database.tweets_sp_sg(tweet_time_sp_sg,screen_name_sp_sg,tweets_sp_sg) values (%s,%s,%s)",(status_list))
  61.  
  62. def on_error(self, status_code):
  63. print ('Encountered error with status code:', status_code)
  64. return True
  65.  
  66. if __name__ == '__main__':
  67. l = StdOutListener()
  68. auth = OAuthHandler(consumer_key, consumer_secret)
  69. auth.set_access_token(access_token, access_token_secret)
  70.  
  71. while True:
  72. stream = Stream(auth, l, timeout = None)
  73. stream.filter(track = KEYWORDS, languages=['en'])
Add Comment
Please, Sign In to add comment