Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. from __future__ import absolute_import, print_function
  2.  
  3. from tweepy.streaming import StreamListener
  4. from tweepy import OAuthHandler
  5. from tweepy import Stream
  6.  
  7. import json
  8.  
  9. import mysql.connector
  10.  
  11. import pymysql
  12. # ==========================================================
  13.  
  14. consumer_key="BXAp8NuhcDD8l3k5NwTKWI0hX"
  15. consumer_secret="M6wMBJiID9VhAkwcURY28Rz2hick5QcQqltgfvlqU6RIuSYYJH"
  16.  
  17. access_token="492323092-KM42dzPe1ZKjLhJC1uBoiduO2HcLxV6McwOSNJPB"
  18. access_token_secret="cO6Wc3hGVRXn9CvMLtWBQNRafK62X5MMi6v2N0si9Kwx6"
  19.  
  20. class StdOutListener(StreamListener):
  21. """ A listener handles tweets are the received from the stream.
  22. This is a basic listener that just prints received tweets to stdout.
  23. """
  24. def __init__(self, conn):
  25. self.conn = conn
  26.  
  27. def encode(self, text):
  28. """
  29. For printing unicode characters to the console.
  30. """
  31. return text.encode('utf-8')
  32.  
  33. def on_data(self, data):
  34. json_str = json.loads(data)
  35.  
  36. _id = json_str["id_str"]
  37. _text = (json_str["text"]).encode('ascii','ignore')
  38. _timestamp = (json_str["created_at"]).encode('ascii','ignore')
  39. _source = json_str["source"]
  40. _lang = json_str["lang"]
  41.  
  42. _user = json_str["user"]
  43. _user_id = _user["id_str"]
  44. _username = _user["screen_name"]
  45. _statuses_count = _user["statuses_count"]
  46. _location = _user["location"]
  47.  
  48. self.tweet = Tweet(_id, _text[:200], _timestamp, _source, _lang, _username, _user_id, _statuses_count, _location)
  49. self.tweet.db_write(self.conn)
  50. return True
  51.  
  52. def on_error(self, status):
  53. print(status)
  54.  
  55.  
  56.  
  57. class Tweet(object):
  58. """docstring for Tweet"""
  59. def __init__(self, _id, text, timestamp, source, lang, username, user_id, statuses_count, location):
  60. self.id = _id
  61. self.text = text
  62. self.timestamp = timestamp
  63. self.source = source
  64. self.lang = lang
  65. self.username = username
  66. self.user_id = user_id
  67. self.statuses_count = statuses_count
  68. self.location = location
  69.  
  70. def db_write(self, conn):
  71. print (self.id)
  72. #insert_query = "INSERT INTO tweets.test_tweets (tweet_id, text, timestamp, source, username, user_id, statuses_count, location) VALUES (self.id, ....) "
  73. insert_query = "INSERT INTO test.tweets (tweet_id, text, timestamp) "
  74. "VALUES (%s, %s, %s)"
  75. vals = ("0000001111", "twewe324324%$#&@#defefe", "2016-04-12 11:34:55")
  76. try:
  77. cursor = conn.cursor()
  78. cursor.execute(insert_query, vals)
  79. print ("*********** new entry has been inserted ************")
  80.  
  81. cursor.execute("SELECT * FROM tweets")
  82. row = cursor.fetchone()
  83. while row is not None:
  84. print(row)
  85. row = cursor.fetchone()
  86.  
  87. except Error as e:
  88. print(e)
  89.  
  90. def connect():
  91. """ Connect to MySQL database """
  92. conn = None
  93. try:
  94. conn = mysql.connector.connect(host='117.16.142.49',
  95. database='test',
  96. user='root',
  97. password='qwe123')
  98. if conn.is_connected():
  99. print('Connected to MySQL database')
  100.  
  101. except Error as e:
  102. print(e)
  103.  
  104. return conn
  105.  
  106.  
  107. if __name__ == '__main__':
  108. conn = connect()
  109.  
  110. listener = StdOutListener(conn)
  111. auth = OAuthHandler(consumer_key, consumer_secret)
  112. auth.set_access_token(access_token, access_token_secret)
  113.  
  114. stream = Stream(auth, listener)
  115. stream.filter(track=['obama', 'putin'], async=True)
  116.  
  117. (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
  118. (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  119. *********** new entry has been inserted ************
  120. (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
  121. (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  122. (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  123. *********** new entry has been inserted ************
  124. (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
  125. (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  126. (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  127. (19, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  128. *********** new entry has been inserted ************
  129. (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
  130. (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  131. (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  132. (19, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  133. (20, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
  134. .........
  135. .........
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement