-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
102 lines (74 loc) · 2.94 KB
/
sql.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tweepy
from sqlalchemy import false
from sqlalchemy.cprocessors import str_to_date
from tweepy import StreamListener
import pymysql
from keys.keys import *
from tweepy import Stream
class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_status(self, status):
#Not listeninng to the retweets in the stream, just ignoring them
if (status.text.startswith("RT @") == False):
ID_TWEET = status.id_str
CREATED_AT = status.created_at
TEXT = status.text
LOCATION = status.user.location
USER_CREATED_AT = status.user.created_at
FOLLOWERS = status.user.followers_count
LANGUAGE = status.lang
db = pymysql.connect("localhost", "me", "@Carmen0721287743", "tweeter")
cur = db.cursor()
"""
cur.execute('''
INSERT INTO tweeter.tweets (ID_TWEET, CREATED_AT, TEXT,LOCATION,USER_CREATED_AT,FOLLOWERS,LANGUAGE)
VALUES
(ID_TWEET, CREATED_AT,TEXT,LOCATION,USER_CREATED_AT,FOLLOWERS,LANGUAGE)
''')
"""
cur.execute(
'INSERT INTO tweets(ID_TWEET, CREATED_AT,TEXT,LOCATION,USER_CREATED_AT,FOLLOWERS,LANGUAGE) VALUES (%s,%s,%s,%s,%s,%s,%s)',
(ID_TWEET, CREATED_AT,TEXT,LOCATION,USER_CREATED_AT,FOLLOWERS,LANGUAGE)
)
db.commit()
print(cur.rowcount,'recorded inserted')
def on_error(self, status_code):
if status_code == 420:
return False
if __name__ == '__main__':
#Simulaniously downloading 2 diferent streams in ordder to compare
db = pymysql.connect("localhost", "me", "@Carmen0721287743", "tweeter")
#db = pymysql.connect("localhost", "me", "@Carmen0721287743", "secondTwitter")
l = StdOutListener()
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
cur = db.cursor()
sql = '''CREATE TABLE IF NOT EXISTS TWEETS(
SENTIMENT VARCHAR(10),
ID_TWEET VARCHAR(50),
CREATED_AT DATETIME,
TEXT VARCHAR(200),
LOCATION VARCHAR(100),
USER_CREATED_AT DATE,
FOLLOWERS INT,
LANGUAGE VARCHAR(10)
)'''
cur.execute(sql)
"""
sql = '''CREATE TABLE IF NOT EXISTS secondTweets(
SENTIMENT VARCHAR(10),
ID_TWEET VARCHAR(50),
CREATED_AT DATETIME,
TEXT VARCHAR(280),
USER_CREATED_AT DATE,
FOLLOWERS INT,
LANGUAGE VARCHAR(10)
)'''
cur.execute(sql)
"""
stream = Stream(auth, l)
stream.filter(track=['Bill Gates'])
#stream.filter(track=['Jeff Bezos','Bezos'])