-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtweet-scraper.py
More file actions
196 lines (132 loc) · 8.23 KB
/
tweet-scraper.py
File metadata and controls
196 lines (132 loc) · 8.23 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
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
import pandas as pd
import math
import snscrape.modules.twitter as sntwitter
import PySimpleGUI as gui
import webbrowser
gui.theme('systemdefault')
#TODO: change theme and text color based on OS
output_filepath = './'
start_button_disabled = False
layout = [
[gui.Text('Username', size=(10,1)),gui.Input(key='-USERNAME-', size=(45,1), tooltip="enter a twitter username")],
[gui.Text('Tweet Count', size=(10,1)),gui.Input(key='-NUM_OF_TWEETS-', size=(10,1), tooltip="n most recent tweets to be scraped")],
[gui.Text('Output Folder', size=(10,1)),gui.Input('current directory', key='-OUTPUT_FILEPATH-', readonly=True, size=(45,1), tooltip="choose a folder for your .csv output"), gui.FolderBrowse()],
[gui.Radio('Threads', 1 , default = True, pad=(1,20), key="-THREADS-"), gui.Radio('Tweets', 1, pad=(1,20), key="-TWEETS-"), gui.Radio('Replies', 1, pad=(1,20), key="-REPLIES-"), gui.Radio('All 3', 1, pad=(1,20), key="-ALL-")],
[gui.Button('Start')],
[gui.Text('Status', key='-STATUS-', pad=((5,0),(10,0)))],
[gui.ProgressBar(max_value=100, orientation='h', key='progressBar', size=(40, 17))],
[gui.Text('Repo', enable_events=True, text_color='dark violet', tooltip="link to Github repo", pad=((5,0),(15,0)))],
[gui.Text('Creator', enable_events=True, text_color='dark violet', tooltip="link to creator's website", pad=((5,0),(5,5)))]
]
window = gui.Window('Twitter Thread Scraper', layout, icon='threads_logo.ico')
def toggle_start_button():
global start_button_disabled
start_button_disabled = not start_button_disabled
window['Start'].update(disabled = start_button_disabled)
def create_csv(user, num_of_tweets, threads_panda, tweets_panda, replies_panda, all_panda):
if values['-THREADS-']:
if threads_panda.empty:
window['-STATUS-'].update('Done. No threads found in @' + user + '\'s last' + str(num_of_tweets) + ' tweets', text_color='orange')
else:
threads_panda.to_csv(output_filepath + "@" + user.lower() + '-last' + str(num_of_tweets) + '-' + str(len(threads_panda)) + 'threads' + '.csv', sep=',', index = False, encoding='utf-8')
window['-STATUS-'].update('Success! Found ' + str(len(threads_panda)) + ' complete threads in @' + user + '\'s last ' + str(num_of_tweets) + ' tweets', text_color='green')
elif values['-TWEETS-']:
if tweets_panda.empty:
window['-STATUS-'].update('Done. No singular tweets found in @' + user + "'s last" + str(num_of_tweets) + " tweets", text_color='orange')
else:
tweets_panda.to_csv(output_filepath + "@" + user.lower() + '-last' + str(num_of_tweets) + '-' + str(len(tweets_panda)) + 'tweets' + '.csv', sep=',', index = False, encoding='utf-8')
window['-STATUS-'].update('Success! Found ' + str(len(tweets_panda)) + ' ' + 'singular tweets in @' + user + '\'s last ' + str(num_of_tweets) + ' tweets', text_color='green')
elif values['-REPLIES-']:
if replies_panda.empty:
window['-STATUS-'].update('Done. No replies found in @' + user + "'s last" + str(num_of_tweets) + " tweets", text_color='orange')
else:
replies_panda.to_csv(output_filepath + "@" + user.lower() + '-last' + str(num_of_tweets) + '-' + str(len(replies_panda)) + 'replies' + '.csv', sep=',', index = False, encoding='utf-8')
window['-STATUS-'].update('Success! Found ' + str(len(replies_panda)) + ' ' + 'replies in @' + user + '\'s last ' + str(num_of_tweets) + ' tweets', text_color='green')
elif values['-ALL-']:
all_panda.to_csv(output_filepath + "@" + user.lower() + '-last' + str(num_of_tweets) + '-all' + '.csv', sep=',', index = False, encoding='utf-8')
window['-STATUS-'].update('Success! Scraped ' + str(len(all_panda)) + ' simple tweets, threads and replies from @' + user, text_color='green')
else:
window['-STATUS-'].update('OUTPUTERROR', text_color='red')
window['progressBar'].update(100)
def create_pandas(data, user, num_of_tweets):
all_panda = pd.DataFrame(data, columns=['username', 'content', 'likes', 'retweets', 'replies', 'id', 'conversationId', 'inReplyToUser', 'date'])
#------------------------------------
#THREADS
thread_conv_ids = []
threads_panda = all_panda
for index, row in threads_panda.iterrows():
if row['inReplyToUser'] != None:
if str(row['inReplyToUser'])[20:] != str(row['username']):
threads_panda.drop(index, inplace=True)
else:
thread_conv_ids.append(row['conversationId'])
#filtering to weed out singular tweets and incomplete threads
threads_panda = threads_panda.groupby('conversationId').filter(lambda x: len(x) > 1)
#------------------------------------
#REPLIES
replies_panda = pd.DataFrame(data, columns=['username', 'content', 'likes', 'retweets', 'replies', 'id', 'conversationId', 'inReplyToUser', 'date'])
for index, row in replies_panda.iterrows():
if row['inReplyToUser'] == None or str(row['inReplyToUser'])[20:] == str(row['username']):
replies_panda.drop(index, inplace=True)
#------------------------------------
#TWEETS TODO: this needs testing, could probably be implemented cleaner (remove thread_conv_ids)
tweets_panda = pd.DataFrame(data, columns=['username', 'content', 'likes', 'retweets', 'replies', 'id', 'conversationId', 'inReplyToUser', 'date'])
for index, row in tweets_panda.iterrows():
if row['inReplyToUser'] != None:
tweets_panda.drop(index, inplace=True)
elif row['conversationId'] in thread_conv_ids:
tweets_panda.drop(index, inplace=True)
#------------------------------------
create_csv(user, num_of_tweets, threads_panda, tweets_panda, replies_panda, all_panda)
def scrape_tweets(user, num_of_tweets):
if user[0] == "@":
user = user[1:]
tweets = []
num_of_tweets = int(num_of_tweets)
last_i = 0
progress_bar_update_limiter = num_of_tweets / 100
try:
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('from:' + user).get_items()):
if i >= num_of_tweets:
break
if (i - last_i) >= progress_bar_update_limiter:
last_i = i
window['progressBar'].update(math.ceil((i / num_of_tweets) * 100))
tweets.append([tweet.user.username, tweet.content, tweet.likeCount, tweet.retweetCount, tweet.replyCount, tweet.id, tweet.conversationId, tweet.inReplyToUser, tweet.date])
create_pandas(tweets, user, num_of_tweets)
except Exception as e:
if repr(e) == "ScraperException('Unable to find guest token')":
window['-STATUS-'].update('API Timeout: Wait a second, then try again', text_color='red')
else:
window['-STATUS-'].update(e, text_color='red')
while True:
event, values = window.read()
if event == 'Repo':
webbrowser.open('https://github.com/philparzer/twitter-thread-analysis')
if event == 'Creator':
webbrowser.open('https://philippparzer.com/')
#TODO: add cancel event and button
if event == 'Start':
if values['-OUTPUT_FILEPATH-'] == 'current directory':
output_filepath = "./"
else:
output_filepath = values["-OUTPUT_FILEPATH-"] + "/"
if values["-USERNAME-"] == '':
window['-STATUS-'].update('Enter a username', text_color='red')
continue
try:
num_of_tweets = int(values['-NUM_OF_TWEETS-'])
if (num_of_tweets < 1):
window['-STATUS-'].update('Enter a number bigger than 0', text_color='red')
continue
except Exception as e:
window['-STATUS-'].update('Enter a valid number of tweets', text_color='red')
continue
window["-STATUS-"].update("Scraping...", text_color='blue')
toggle_start_button()
scrape_tweets(values['-USERNAME-'], int(values['-NUM_OF_TWEETS-']))
toggle_start_button()
if event == gui.WIN_CLOSED or event == 'Exit':
break
window.close()
quit() #FIXME: when compiled