Guest User

Untitled

a guest
Sep 24th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #!usr/bin/python
  2.  
  3. import mysql.connector
  4. from mysql.connector import Error
  5. import tweepy
  6. import json
  7. from dateutil import parser
  8. import time
  9. import os
  10. import subprocess
  11.  
  12. #importing file which sets env variable
  13. subprocess.call("./settings.sh", shell = True)
  14.  
  15.  
  16. consumer_key = os.environ['CONSUMER_KEY']
  17. consumer_secret = os.environ['CONSUMER_SECRET']
  18. access_token = os.environ['ACCESS_TOKEN']
  19. access_token_secret = os.environ['ACCESS_TOKEN_SECRET']
  20. password = os.environ['PASSWORD']
  21.  
  22.  
  23. def connect(username, created_at, tweet, retweet_count, place , location):
  24. """
  25. connect to MySQL database and insert twitter data
  26. """
  27. try:
  28. con = mysql.connector.connect(host = 'localhost',
  29. database='twitterdb', user='root', password = password, charset = 'utf8')
  30.  
  31.  
  32. if con.is_connected():
  33. """
  34. Insert twitter data
  35. """
  36. cursor = con.cursor()
  37. # twitter, golf
  38. query = "INSERT INTO Golf (username, created_at, tweet, retweet_count,place, location) VALUES (%s, %s, %s, %s, %s, %s)"
  39. cursor.execute(query, (username, created_at, tweet, retweet_count, place, location))
  40. con.commit()
  41.  
  42.  
  43. except Error as e:
  44. print(e)
  45.  
  46. cursor.close()
  47. con.close()
  48.  
  49. return
  50.  
  51.  
  52. # Tweepy class to access Twitter API
  53. class Streamlistener(tweepy.StreamListener):
  54.  
  55.  
  56. def on_connect(self):
  57. print("You are connected to the Twitter API")
  58.  
  59.  
  60. def on_error(self):
  61. if status_code != 200:
  62. print("error found")
  63. # returning false disconnects the stream
  64. return False
  65.  
  66. """
  67. This method reads in tweet data as Json
  68. and extracts the data we want.
  69. """
  70. def on_data(self,data):
  71.  
  72. try:
  73. raw_data = json.loads(data)
  74.  
  75. if 'text' in raw_data:
  76.  
  77. username = raw_data['user']['screen_name']
  78. created_at = parser.parse(raw_data['created_at'])
  79. tweet = raw_data['text']
  80. retweet_count = raw_data['retweet_count']
  81.  
  82. if raw_data['place'] is not None:
  83. place = raw_data['place']['country']
  84. print(place)
  85. else:
  86. place = None
  87.  
  88.  
  89. location = raw_data['user']['location']
  90.  
  91. #insert data just collected into MySQL database
  92. connect(username, created_at, tweet, retweet_count, place, location)
  93. print("Tweet colleted at: {} ".format(str(created_at)))
  94. except Error as e:
  95. print(e)
  96.  
  97.  
  98. if __name__== '__main__':
  99.  
  100. # authentification so we can access twitter
  101. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  102. auth.set_access_token(access_token, access_token_secret)
  103. api =tweepy.API(auth, wait_on_rate_limit=True)
  104.  
  105. # create instance of Streamlistener
  106. listener = Streamlistener(api = api)
  107. stream = tweepy.Stream(auth, listener = listener)
  108.  
  109. track = ['golf', 'masters', 'reed', 'mcilroy', 'woods']
  110. # choose what we want to filter by
  111. stream.filter(track = track, languages = ['en'])
Add Comment
Please, Sign In to add comment