-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
258 lines (192 loc) · 9.6 KB
/
Main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
To Do: 1. Remove non ASCII text (COMPLETE)
2. Make look Pretty (in Progress)
3. Save the settings (Dark / Light) Theme
4. Maybe analysis? (#ofTweets per User... ECT)
5. make it display on first click (COMPLETE)
6. Link to link (in tweet)
7. Save button for tweets
8. Text bigger in Tweet Text (COMPLETE)
9. Make it Display Picture (COMPLETE)
"""
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import json
import pandas as pd
from pandas import DataFrame
from twython import Twython
import webbrowser
from selenium import webdriver
from ui_Main import Ui_Main
class Main(QMainWindow, Ui_Main):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
self.pic = ""
self.index1 = 0
self.submitBtn.clicked.connect(self.OpenWindow1)
self.linkBtn.clicked.connect(self.openLink)
def OpenWindow1(self):
self.QtStack.setCurrentIndex(1)
query = self.query.text()
geo = self.geo.text()
self.getData(query, geo)
self.nextBadGuy()
self.nextBtn.clicked.connect(self.nextBadGuy)
self.prevBtn.clicked.connect(self.prevBadGuy)
self.showPic.clicked.connect(self.showPicture)
def getData(self, queryT, geoT):
with open("twitter_creds.JSON", "r") as file:
creds = json.load(file)
# Instantiate an object
python_tweets = Twython(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET'], creds['ACCESS_TOKEN'],
creds['ACCESS_SECRET'])
geocode = geoT.replace(" ", "")
queryI = queryT
# geocode = '40.006038,-105.257716,10mi' # latitude,longitude,distance(mi/km)
print(geocode)
count = 100
# make a Dict that has all fo the categorizes
self.dict_ = {'user': [], 'username': [], 'HomeTown': [], 'Bio': [], 'ProfilePicURL': [], 'text': [], 'geo_E': [],
'location': [], 'coordinates': [], 'date': [], 'place': [], 'ID': []}
# parse through the JSON and set it up so that it's in columns for Excel
for status in \
python_tweets.search(q=queryI, geocode=geocode, count=count, include_entities=True,
tweet_mode='extended')[
'statuses']:
status['user']['name'] = self.strip_non_ascii(status['user']['name'])
status['user']['screen_name'] = self.strip_non_ascii(status['user']['screen_name'])
status['user']['description'] = self.strip_non_ascii(status['user']['description'])
status['user']['location'] = self.strip_non_ascii(status['user']['location'])
status['full_text'] = self.strip_non_ascii(status['full_text'])
status['user']['location'] = self.strip_non_ascii(status['user']['location'])
status['created_at'] = self.strip_non_ascii(status['created_at'])
self.dict_['user'].append(status['user']['name'])
self.dict_['username'].append(status['user']['screen_name'])
self.dict_['Bio'].append(status['user']['description'])
self.dict_['HomeTown'].append(status['user']['location'])
self.dict_['ProfilePicURL'].append(status['user']['profile_image_url'])
idStr = str(status['id'])
twitterLi = "https://twitter.com/user/status/"
specTweetLi = twitterLi + idStr
self.dict_['ID'].append(specTweetLi)
self.dict_['text'].append(status['full_text'])
self.dict_['geo_E'].append(status['user']['geo_enabled']) # lets us know if another tweet from user might give us a location.
self.dict_['location'].append(status['user']['location'])
self.dict_['coordinates'].append(status['coordinates'])
var4 = str(status['place'])
index = var4.find('full_name')
self.dict_['place'].append(var4[(index + 11):(index + 30)])
self.dict_['date'].append(status['created_at'])
# Structure data in a pandas DataFrame for easier writing to Excel
df: DataFrame = pd.DataFrame(self.dict_)
df.sort_values(by='date', inplace=True, ascending=True)
df.to_csv('out.csv', float_format='%f')
# make a file with the entire JSON format
Sfile = str(
python_tweets.search(q=queryI, geocode=geocode, count=count, include_entities=True, tweet_mode='extended'))
f = open('JSON.txt', 'w')
#f.write(Sfile)
f.close()
def openLink(self):
webbrowser.open(self.dict_['ID'][self.index1-1]) # Go to example.com
def nextBadGuy(self):
self.showPic.setChecked(False)
self.picL.hide()
if not self.dict_['user']:
choice = QtWidgets.QMessageBox.question(self, 'Error!',
"No Tweets Found\n"
"Check your Geo / Query and try again",
QtWidgets.QMessageBox.Ok)
self.userL.setText(self.dict_['user'][self.index1])
self.usernameL.setText(self.dict_['username'][self.index1])
self.bioL.setText(self.dict_['Bio'][self.index1])
self.textL.setText(self.dict_['text'][self.index1])
self.dateL.setText(self.dict_['date'][self.index1])
self.homeTownL.setText(self.dict_['HomeTown'][self.index1])
self.geoL.setText(str(self.dict_['geo_E'][self.index1]))
self.index1 = self.index1+1
if self.darkBtn.isChecked():
self.BackColor = "background-color: #002b36;"
self.FontColor = "color: #fdf6e3"
self.bioL.setStyleSheet(self.FontColor)
self.textL.setStyleSheet(self.FontColor)
self.dateL.setStyleSheet(self.FontColor)
self.homeTownL.setStyleSheet(self.FontColor)
self.usernameL.setStyleSheet(self.FontColor)
self.userL.setStyleSheet(self.FontColor)
self.geoL.setStyleSheet(self.FontColor)
self.stack2.setStyleSheet(self.BackColor)
elif not self.darkBtn.isChecked():
self.BackColor = "background-color: #fdf6e3;"
self.FontColor = "color: #002b36"
self.textL.setStyleSheet(self.FontColor)
self.dateL.setStyleSheet(self.FontColor)
self.bioL.setStyleSheet(self.FontColor)
self.homeTownL.setStyleSheet(self.FontColor)
self.usernameL.setStyleSheet(self.FontColor)
self.userL.setStyleSheet(self.FontColor)
self.geoL.setStyleSheet(self.FontColor)
self.stack2.setStyleSheet(self.BackColor)
def prevBadGuy(self):
self.index1 = self.index1 - 2
self.showPic.setChecked(False)
self.picL.hide()
self.userL.setText(self.dict_['user'][self.index1])
self.usernameL.setText(self.dict_['username'][self.index1])
self.homeTownL.setText(self.dict_['HomeTown'][self.index1])
self.bioL.setText(self.dict_['Bio'][self.index1])
self.dateL.setText(self.dict_['date'][self.index1])
self.textL.setText(str(self.dict_['text'][self.index1]))
self.geoL.setText(str(self.dict_['geo_E'][self.index1]))
self.index1 = self.index1+1
if self.darkBtn.isChecked():
self.BackColor = "background-color: #002b36;"
self.FontColor = "color: #fdf6e3"
self.textL.setStyleSheet(self.FontColor)
self.dateL.setStyleSheet(self.FontColor)
self.bioL.setStyleSheet(self.FontColor)
self.homeTownL.setStyleSheet(self.FontColor)
self.usernameL.setStyleSheet(self.FontColor)
self.userL.setStyleSheet(self.FontColor)
self.geoL.setStyleSheet(self.FontColor)
self.stack2.setStyleSheet(self.BackColor)
elif not self.darkBtn.isChecked():
self.BackColor = "background-color: #fdf6e3;"
self.FontColor = "color: #002b36"
self.textL.setStyleSheet(self.FontColor)
self.dateL.setStyleSheet(self.FontColor)
self.bioL.setStyleSheet(self.FontColor)
self.homeTownL.setStyleSheet(self.FontColor)
self.usernameL.setStyleSheet(self.FontColor)
self.userL.setStyleSheet(self.FontColor)
self.geoL.setStyleSheet(self.FontColor)
self.stack2.setStyleSheet(self.BackColor)
def showPicture(self):
if self.showPic.isChecked():
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=options)
driver.set_window_size(1920, 1080) # set the window size that you need
self.index1 = self.index1 -1
driver.get(self.dict_['ID'][self.index1])
driver.save_screenshot('currentTweet.png')
driver.quit()
pixmap = QtGui.QPixmap('currentTweet.png')
rect = QtCore.QRect(650, 100, 600, 500)
cropped = pixmap.copy(rect)
pic_reszied = cropped.scaled(380, 250)
self.picL.show()
self.picL.setPixmap(pic_reszied)
self.index1 = self.index1 +1
elif not self.showPic.isChecked():
self.picL.hide()
def strip_non_ascii(self, string):
''' Returns the string without non ASCII characters'''
stripped = (c for c in string if 0 < ord(c) < 127)
return ''.join(stripped)
if __name__ == '__main__':
app = QApplication(sys.argv)
showMain = Main()
sys.exit(app.exec_())