Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import absolute_import, print_function
- from tweepy.streaming import StreamListener
- from tweepy import OAuthHandler
- from tweepy import Stream
- import json
- import mysql.connector
- import pymysql
- # ==========================================================
- consumer_key="BXAp8NuhcDD8l3k5NwTKWI0hX"
- consumer_secret="M6wMBJiID9VhAkwcURY28Rz2hick5QcQqltgfvlqU6RIuSYYJH"
- access_token="492323092-KM42dzPe1ZKjLhJC1uBoiduO2HcLxV6McwOSNJPB"
- access_token_secret="cO6Wc3hGVRXn9CvMLtWBQNRafK62X5MMi6v2N0si9Kwx6"
- class StdOutListener(StreamListener):
- """ A listener handles tweets are the received from the stream.
- This is a basic listener that just prints received tweets to stdout.
- """
- def __init__(self, conn):
- self.conn = conn
- def encode(self, text):
- """
- For printing unicode characters to the console.
- """
- return text.encode('utf-8')
- def on_data(self, data):
- json_str = json.loads(data)
- _id = json_str["id_str"]
- _text = (json_str["text"]).encode('ascii','ignore')
- _timestamp = (json_str["created_at"]).encode('ascii','ignore')
- _source = json_str["source"]
- _lang = json_str["lang"]
- _user = json_str["user"]
- _user_id = _user["id_str"]
- _username = _user["screen_name"]
- _statuses_count = _user["statuses_count"]
- _location = _user["location"]
- self.tweet = Tweet(_id, _text[:200], _timestamp, _source, _lang, _username, _user_id, _statuses_count, _location)
- self.tweet.db_write(self.conn)
- return True
- def on_error(self, status):
- print(status)
- class Tweet(object):
- """docstring for Tweet"""
- def __init__(self, _id, text, timestamp, source, lang, username, user_id, statuses_count, location):
- self.id = _id
- self.text = text
- self.timestamp = timestamp
- self.source = source
- self.lang = lang
- self.username = username
- self.user_id = user_id
- self.statuses_count = statuses_count
- self.location = location
- def db_write(self, conn):
- print (self.id)
- #insert_query = "INSERT INTO tweets.test_tweets (tweet_id, text, timestamp, source, username, user_id, statuses_count, location) VALUES (self.id, ....) "
- insert_query = "INSERT INTO test.tweets (tweet_id, text, timestamp) "
- "VALUES (%s, %s, %s)"
- vals = ("0000001111", "twewe324324%$#&@#defefe", "2016-04-12 11:34:55")
- try:
- cursor = conn.cursor()
- cursor.execute(insert_query, vals)
- print ("*********** new entry has been inserted ************")
- cursor.execute("SELECT * FROM tweets")
- row = cursor.fetchone()
- while row is not None:
- print(row)
- row = cursor.fetchone()
- except Error as e:
- print(e)
- def connect():
- """ Connect to MySQL database """
- conn = None
- try:
- conn = mysql.connector.connect(host='117.16.142.49',
- database='test',
- user='root',
- password='qwe123')
- if conn.is_connected():
- print('Connected to MySQL database')
- except Error as e:
- print(e)
- return conn
- if __name__ == '__main__':
- conn = connect()
- listener = StdOutListener(conn)
- auth = OAuthHandler(consumer_key, consumer_secret)
- auth.set_access_token(access_token, access_token_secret)
- stream = Stream(auth, listener)
- stream.filter(track=['obama', 'putin'], async=True)
- (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
- (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- *********** new entry has been inserted ************
- (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
- (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- *********** new entry has been inserted ************
- (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
- (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (19, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- *********** new entry has been inserted ************
- (1, u'1234565789', u'salom ftewafesfzsdfzsef213234r@4$##@(**', datetime.datetime(2016, 4, 27, 12, 43, 56))
- (17, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (18, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (19, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- (20, u'123434543', u'salomdwd', datetime.datetime(2016, 5, 23, 12, 42, 33))
- .........
- .........
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement