-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterFetcher.py
123 lines (101 loc) · 4.88 KB
/
TwitterFetcher.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import time
import tweepy
from mysql.connector import Error
from textblob import TextBlob
import re
import mysql.connector
# Authentication
consumerKey = ''
consumerSecret = ''
accessToken = ''
accessTokenSecret = ''
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
def clean_tweet(tweet):
return ''.join(
re.sub(r"(@[A-Za-z0-9_]+)|(#[A-Za-z0-9_]+)|(http\S+)|(www.\S+)|([^0-9A-Za-z\t])|(\w+:\/\/\S+)", " ", tweet))
def get_tweet_sentiment(tweet):
# create TextBlob object of passed tweet text
analysis = TextBlob(clean_tweet(tweet))
# set sentiment
time.sleep(10)
if analysis.sentiment.polarity > 0:
return 'positive'
elif analysis.sentiment.polarity == 0:
return 'neutral'
else:
return 'negative'
def join_hstg(hstg):
return ' '.join(hstg)
fetch_tweets = []
players = ['Djokovic', 'Tsitsipas', 'Nadal']
limit = 1000
#connection to the local database, creation of the cursor and the query
try:
db = mysql.connector.connect(host=' 127.0.0.1', database='twitterdb', user='root', password='')
if db.is_connected():
print("connected to mysql database!")
cur = db.cursor()
q = """INSERT INTO
tweet_sentiment (searchParam, user_name, text, hashtags, date, location, number_of_followers, number_of_tweets, number_of_account_retweets, sentiment)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
#inserting in the databse each fetched tweet from the twitter API and his "sentiment" founde with our function
for player in players:
counter = limit
tweets = tweepy.Cursor(api.search_tweets, q=player + '-filter:retweets', count=100, tweet_mode='extended',
lang="en").items(limit)
for tweet in tweets:
# find the hashtags in the tweet
hstgs = []
for hstg in tweet.entities['hashtags']:
hstgs.append(hstg['text'])
# ------------------------------------ WITH THE DATAFRAME ----------------------------------------------#
# tweet_dict = {'searchParam': player,
# 'user_name': tweet.user.screen_name,
# 'text': clean_tweet(tweet.full_text),
# 'hashtags': join_hstg(hstgs),
# 'date': tweet.created_at,
# 'location': tweet.user.location,
# 'number_of_followers': tweet.user.followers_count,
# 'number_of_tweets': tweet.user.statuses_count,
# 'number_of_account_retweets': tweet.retweet_count,
# 'sentiment': get_tweet_sentiment(tweet.full_text)
# }
# fetch_tweets.append(tweet_dict)
# df_tweets = pd.DataFrame(fetch_tweets)
# print(df_tweets)
# ------------------------------------ WITH THE DATAFRAME ----------------------------------------------#
user_name = tweet.user.screen_name
text = clean_tweet(tweet.full_text)
hashtags = join_hstg(hstgs)
date = tweet.created_at
location = tweet.user.location
number_of_followers = tweet.user.followers_count
number_of_tweets = tweet.user.statuses_count
number_of_account_retweets = tweet.retweet_count
sentiment = get_tweet_sentiment(tweet.full_text)
insert_tuple = (player, user_name, text, hashtags, date, location, number_of_followers, number_of_tweets,
number_of_account_retweets, sentiment)
result = cur.execute(q, insert_tuple)
db.commit()
print(f'Record inserted successfully into tweets_table table still {counter} record to insert')
counter -= 1
except Error as error:
db.rollback()
print('Failed to insert into MySQL table {db}'.format(error))
finally:
# closing database connection.
if db.is_connected():
cur.close()
db.close()
print("MySQL connection is closed")
# ------------------------------------ WITH THE DATAFRAME ----------------------------------------------#
# create sqlalchemy engine
# engine = create_engine("mysql+pymysql://{user}:{pw}@localhost/{db}"
# .format(user="root",
# pw="f18_kd0=?",
# db="twitterdb"))
# Insert whole DataFrame into MySQL
# df_tweets.to_sql('tweet_sentiment', con = engine, index = False if_exists = 'append', chunksize = 1000)
# ------------------------------------ WITH THE DATAFRAME ----------------------------------------------#