@@ -43,6 +43,24 @@ if sys.version_info[0] < 3:
4343
4444import GetOldTweets3 as got
4545
46+
47+ def concatenate_concurrent_files (collected_directory , outputFileName ):
48+ from os import listdir
49+ from os .path import isfile , join
50+ file_paths = [collected_directory + "/" + f for f in listdir (collected_directory ) if isfile (join (collected_directory , f ))]
51+
52+ with open (outputFileName , "w+" , encoding = "utf8" ) as total_result_file :
53+ total_result_file .write ('date,username,to,replies,retweets,favorites,text,geo,mentions,hashtags,id,permalink\n ' )
54+ for file_path in reversed (file_paths ):
55+ with open (file_path , "r" , encoding = "utf8" ) as file :
56+ data = file .read ().split ("\n " , 1 )[1 ]
57+ total_result_file .write (data )
58+ os .remove (file_path )
59+ os .removedirs (collected_directory )
60+
61+
62+
63+
4664def main (argv ):
4765 if len (argv ) == 0 :
4866 print ('You must pass some parameters. Use \" -h\" to help.' )
@@ -64,12 +82,18 @@ def main(argv):
6482 "maxtweets=" ,
6583 "lang=" ,
6684 "output=" ,
67- "debug" ))
85+ "debug" ,
86+ "workers=" ,
87+ "forcemaxtweets" ))
6888
6989 tweetCriteria = got .manager .TweetCriteria ()
7090 outputFileName = "output_got.csv"
91+ outputDirNameConcurrent = "output_got.csv_collected"
92+ outputFileNameConcurrent = "{}_to_{}.csv"
7193
7294 debug = False
95+ force_max_tweets = False
96+ workers = 1
7397 usernames = set ()
7498 username_files = set ()
7599 for opt , arg in opts :
@@ -107,10 +131,17 @@ def main(argv):
107131
108132 elif opt == '--output' :
109133 outputFileName = arg
134+ outputDirNameConcurrent = arg + "_collected"
110135
111136 elif opt == '--debug' :
112137 debug = True
113138
139+ elif opt == '--workers' :
140+ workers = int (arg )
141+
142+ elif opt == '--forcemaxtweets' :
143+ force_max_tweets = True
144+
114145 if debug :
115146 print (' ' .join (sys .argv ))
116147 print ("GetOldTweets3" , got .__version__ )
@@ -136,8 +167,12 @@ def main(argv):
136167 else :
137168 tweetCriteria .username = usernames .pop ()
138169
139- outputFile = open (outputFileName , "w+" , encoding = "utf8" )
140- outputFile .write ('date,username,to,replies,retweets,favorites,text,geo,mentions,hashtags,id,permalink\n ' )
170+ if workers > 1 :
171+ if not os .path .exists (outputDirNameConcurrent ):
172+ os .makedirs (outputDirNameConcurrent )
173+ else :
174+ outputFile = open (outputFileName , "w+" , encoding = "utf8" )
175+ outputFile .write ('date,username,to,replies,retweets,favorites,text,geo,mentions,hashtags,id,permalink\n ' )
141176
142177 cnt = 0
143178 def receiveBuffer (tweets ):
@@ -167,8 +202,45 @@ def main(argv):
167202 else :
168203 print (cnt , end = ' ' , flush = True )
169204
205+ def receiveBufferConcurrent (tweets ):
206+ nonlocal cnt
207+
208+ last_date = tweets [0 ].date .strftime ("%Y-%m-%d" )
209+ first_date = tweets [len (tweets )- 1 ].date .strftime ("%Y-%m-%d" )
210+
211+ file = open (outputDirNameConcurrent + "/" + outputFileNameConcurrent .format (first_date , last_date ), "w+" , encoding = "utf8" )
212+ file .write ('date,username,to,replies,retweets,favorites,text,geo,mentions,hashtags,id,permalink\n ' )
213+
214+ for t in tweets :
215+ data_ = [t .date .strftime ("%Y-%m-%d %H:%M:%S" ),
216+ t .username ,
217+ t .to or '' ,
218+ t .replies ,
219+ t .retweets ,
220+ t .favorites ,
221+ '"' + t .text .replace ('"' , '""' ) + '"' ,
222+ t .geo ,
223+ t .mentions ,
224+ t .hashtags ,
225+ t .id ,
226+ t .permalink ]
227+ data_ [:] = [i if isinstance (i , str ) else str (i ) for i in data_ ]
228+ file .write (',' .join (data_ ) + '\n ' )
229+
230+ file .flush ()
231+ file .close ()
232+ cnt += len (tweets )
233+
234+ if sys .stdout .isatty ():
235+ print ("\r Saved %i" % cnt , end = '' , flush = True )
236+ else :
237+ print (cnt , end = ' ' , flush = True )
238+
170239 print ("Downloading tweets..." )
171- got .manager .TweetManager .getTweets (tweetCriteria , receiveBuffer , debug = debug )
240+ if workers > 1 :
241+ got .manager .ConcurrentTweetManager .getTweets (tweetCriteria , receiveBufferConcurrent , debug = debug , worker_count = workers , forceMaxTweets = force_max_tweets )
242+ else :
243+ got .manager .TweetManager .getTweets (tweetCriteria , receiveBuffer , debug = debug )
172244
173245 except getopt .GetoptError as err :
174246 print ('Arguments parser error, try -h' )
@@ -182,10 +254,14 @@ def main(argv):
182254 print (str (err ))
183255
184256 finally :
185- if "outputFile" in locals ():
186- outputFile .close ()
187- print ()
188- print ('Done. Output file generated "%s".' % outputFileName )
257+ if "workers" in locals () and workers > 1 :
258+ concatenate_concurrent_files (outputDirNameConcurrent , outputFileName )
259+ else :
260+ if "outputFile" in locals ():
261+ outputFile .close ()
262+ print ()
263+ print ('Done. Output file generated "%s".' % outputFileName )
264+
189265
190266if __name__ == '__main__' :
191267 main (sys .argv [1 :])
0 commit comments