-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_stream.py
53 lines (36 loc) · 1.36 KB
/
twitter_stream.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
49
50
51
52
53
import tweepy
import time
import sqlite3
import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
# replace sqlite3.server with "localhost" if you are running via your own server!
# server sqlite3 username sqlite pass Database name.
conn = sqlite3.connect("sqlite3.server","beginneraccount","cookies","beginneraccount$tutorial")
c = conn.cursor()
print (" Your application name: "),
app_name = input()
print (" Your consumer key: "),
ckey = input()
print (" Your consumer secret: "),
csecret = input()
atoken,asecret = tweepy.oauth_dance(app_name,ckey,csecret)
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())
print (" Enter tweet Keyword : "),
key=input()
twitterStream.filter(track=key)