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