-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtwitter_mysql.py
123 lines (105 loc) · 5.31 KB
/
twitter_mysql.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
#!/usr/bin/python3
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import pymysql.cursors
import geocoder
import prediction_models
import torch
from BERT import BERT
import text_predict
from datetime import datetime
ckey=""
csecret=""
atoken=""
asecret=""
connection = pymysql.connect(host='192.168.12.207',
port=3306,
user='root',
password='0000',
db='tweet_monitoring',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
image_model = torch.load("models/model_nsfw.pt",map_location=torch.device('cpu'))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
text_model = BERT().to(device)
def load_checkpoint(load_path, model):
if load_path==None:
return
state_dict = torch.load(load_path, map_location=device)
print(f'Model loaded from <== {load_path}')
model.load_state_dict(state_dict['model_state_dict'])
return state_dict['valid_loss']
load_checkpoint('models/model_initial_text.pt', text_model)
class listener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
if not all_data['retweeted'] and 'RT @' not in all_data['text']:
if(all_data.get('lang')=='en' and len(all_data['entities']['hashtags'])>0):
#print(json.dumps(all_data,indent=2,sort_keys=True))
tweet_type = "text"
tweet_timestamp = all_data.get("timestamp_ms")
tweet_created = all_data.get("created_at")
username = all_data.get('user').get('name')
profile_picture = all_data.get('user').get('profile_image_url')
media_url = ''
tweet_text = all_data.get("text")
if(all_data.get('extended_tweet',"None")!="None"):
tweet_text = all_data.get("extended_tweet").get("full_text")
if(all_data.get("extended_tweet").get('entities').get('media', "None") == "None"):
pass
else:
tweet_type = "image"
media_url = all_data.get("extended_tweet").get('entities').get('media')[0].get('media_url')
if(all_data.get("extended_tweet").get('entities').get('media')[0].get("type") == 'video'):
tweet_type = "video"
media_url = all_data.get("extended_tweet").get('entities').get('media')[0].get('video_info').get('variants')[0].get('url')
hashtags = []
raw_hashtags = all_data['entities']['hashtags']
location = all_data.get('user').get('location')
id_tweet = all_data.get('id')
latitude = ""
longitude = ""
if(location!=None):
result = geocoder.arcgis(location)
if(result.x!=None and result.y!=None):
latitude = str(result.y)
longitude = str(result.x)
else:
location = ''
#Predicting toxicity for a post with image and text
post_content_prediction = text_predict.predict_string(tweet_text)
post_content_prediction = max(post_content_prediction)
image_prediction = ''
if(tweet_type=="image"):
image_prediction = prediction_models.predict_image(image_model,media_url)
text_toxicity = post_content_prediction
image_class = image_prediction
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `tweets` VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
dt_object = datetime.fromtimestamp(int(tweet_timestamp)/1000)
cursor.execute(sql,(id_tweet,dt_object,tweet_created,username,profile_picture,tweet_type,tweet_text,media_url,location,latitude,longitude,text_toxicity,image_class))
# if(location!=None and latitude!=None and longitude!=None):
# sql = "INSERT INTO `tweets_location` VALUES (%
# s,%s,%s,%s)"
# cursor.execute(sql, (id_tweet,location,latitude,longitude))
for i in raw_hashtags:
hashtags.append(i['text'])
sql = "INSERT INTO `hashtags` VALUES (%s)"
cursor.execute(sql, (i['text']))
# if(location!=None and latitude!=None and longitude!=None):
# sql = "INSERT INTO `hashtags_location` VALUES (%s,%s,%s,%s)"
# cursor.execute(sql,(i['text'],location,latitude,longitude))
connection.commit()
finally:
pass
print("Post ID:",id_tweet,"inserted!")
return True
def on_error(self, status):
print (status)
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener(),tweet_mode='extended')
twitterStream.filter(track=["covid-19"])