|
| 1 | +from tweepy import Stream |
| 2 | +from tweepy import OAuthHandler |
| 3 | +from tweepy.streaming import StreamListener |
| 4 | +import MySQLdb |
| 5 | +import time |
| 6 | +import json |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +# replace mysql.server with "localhost" if you are running via your own server! |
| 11 | +# server MySQL username MySQL pass Database name. |
| 12 | +conn = MySQLdb.connect("mysql.server","beginneraccount","cookies","beginneraccount$tutorial") |
| 13 | + |
| 14 | +c = conn.cursor() |
| 15 | + |
| 16 | + |
| 17 | +#consumer key, consumer secret, access token, access secret. |
| 18 | +ckey="asdfsafsafsaf" |
| 19 | +csecret="asdfasdfsadfsa" |
| 20 | +atoken="asdfsadfsafsaf-asdfsaf" |
| 21 | +asecret="asdfsadfsadfsadfsadfsad" |
| 22 | + |
| 23 | +class listener(StreamListener): |
| 24 | + |
| 25 | + def on_data(self, data): |
| 26 | + all_data = json.loads(data) |
| 27 | + |
| 28 | + tweet = all_data["text"] |
| 29 | + |
| 30 | + username = all_data["user"]["screen_name"] |
| 31 | + |
| 32 | + c.execute("INSERT INTO taula (time, username, tweet) VALUES (%s,%s,%s)", |
| 33 | + (time.time(), username, tweet)) |
| 34 | + |
| 35 | + conn.commit() |
| 36 | + |
| 37 | + print((username,tweet)) |
| 38 | + |
| 39 | + return True |
| 40 | + |
| 41 | + def on_error(self, status): |
| 42 | + print status |
| 43 | + |
| 44 | +auth = OAuthHandler(ckey, csecret) |
| 45 | +auth.set_access_token(atoken, asecret) |
| 46 | + |
| 47 | +twitterStream = Stream(auth, listener()) |
| 48 | +twitterStream.filter(track=["car"]) |
0 commit comments