Repository navigation
Expand file tree
/
Copy pathtwitterdb.py
More file actions
126 lines (81 loc) · 2.84 KB
/
Copy pathtwitterdb.py
File metadata and controls
126 lines (81 loc) · 2.84 KB
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
103
104
105
106
107
108
import mysql.connector
from mysql.connector import Error
import json
from dateutil import parser
import os
import time
import subprocess
import tweepy
consumer_key = os.environ.get('consumer_key')
consumer_secret = os.environ.get('consumer_secret')
access_token = os.environ.get('access_token')
access_token_secret = os.environ.get('access_token_secret')
password = os.environ.get('mysql_password_twitterdb')
def connect(username, created_at, tweet, retweet_count, place , location):
# Connect to a MySQL dataabase and insert twitter data
try:
conn = mysql.connector.connect(user = 'root', host = 'localhost',
password = password, database = twitterdb,
charset = 'utf8')
if conn.is_connected():
#Insert twitter data in to the MySQL DB
cursor = conn.cursor()
# Add tweet
tweet = ("Insert INTO tweets "
"(username, created_at, tweet, retweet_count, location, place) "
"VALUES (%s,%s,%s,%s,%s,%s)")
# Insert the new tweet
cursor.execute(tweet, (username, created_at, tweet, retweet_count, location, place))
conn.commit()
except Error as e:
print(e)
cursor.close()
conn.close()
class Streamlistener(tweepy.StreamListener):
def on_connect(self):
print('You are connected to the Twitter API')
def on_error(self):
if error_code != 200:
print('error found')
# returning false disconnects the stream
return False
def on_data(self, data):
try:
raw_data = json.loads(data)
#If the raw_data object actually has a tweet in it - pull the values from each key
if 'text' in raw_data:
username = raw_data['user']['screen_name']
created_at = parser.parse(raw_data['created_at'])
tweet = raw_data['text']
retweet_count = raw_data['retweet_count']
if raw_data['place'] is not None:
place = raw_data['place']['country']
print(place)
else:
place = None
location = raw_data['user']['location']
# Write all data to the DB
connect(username, created_at, tweet, retweet_count, location, place)
print('Tweet collected at: {}'.format(str(created_at)))
except Error as e:
print(e)
if __name__ == '__main__':
# Allow user input
track = []
while True:
input1 = input('What do you want to collect tweets on: ')
track.append(input1)
input2 = input('Do you wish to enter nother word (y/n): ')
if input2 == 'n' or input2 =='N':
break
print('You want to search for {}'.format(track))
print('Initializing connection to Twitter API...')
time.sleep(1)
# authentification so we can access twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit = True)
# Create instance of Stream Listener
listener = Streamlistener(api=api)
stream = tweepy.Stream(auth, listener = listener)
stream.filter(track=track, languages = ['en'])