@@ -49,24 +49,33 @@ def get_all_tweets4args(screen_name, api, amount, filename):
49
49
get_all_tweets5args (screen_name , api , amount , filename , lastTweetId )
50
50
51
51
52
- def get_all_tweets5args (screen_name , api , amount , filename , lastTweetScrapedFile ):
53
- #Twitter only allows access to a users most recent 3240 tweets with this method
52
+ def writeTweets (filename , mode , tweets ):
53
+ f = open (filename , mode )
54
+ # print len(alltweets)
55
+ for tweet in tweets :
56
+ if tweet != "\n " or tweet != "" :
57
+ f .write (tweet )
58
+ f .write ("\n " )
59
+ f .close ()
54
60
55
- #initialize a list to hold all the tweepy Tweets
61
+ def get_all_tweets5args (screen_name , api , amount , filename , lastTweetScrapedFile ):
62
+ #Twitter only allows access to a users most recent 3240 tweets with this method
63
+ #initialize a list to hold all the tweepy Tweets
56
64
alltweets = []
57
-
58
- # make request for most recent tweets (200 is the maximum allowed count)
65
+
66
+ # make request for most recent tweets (200 is the maximum allowed count)
59
67
new_tweets = api .user_timeline (screen_name = screen_name ,count = 200 , include_rts = True )
60
68
numTweets = len (new_tweets )
61
69
MostRecentTweetPulled = getMostRecentTweet (lastTweetScrapedFile , screen_name )
62
70
print "Most Recent Tweet Pulled =" , MostRecentTweetPulled
63
-
64
- # record most recent tweet obtained
71
+
72
+ # record most recent tweet id.
73
+ # The id of the last tweet the user tweeted.
65
74
if len (new_tweets ) > 0 :
66
75
most_recent_tweet = new_tweets [0 ].id
67
76
else :
68
77
most_recent_tweet = 0
69
- # loop over first request and keep requesting until amount
78
+ # loop over first request and keep requesting until amount
70
79
# is reached or the 3240 threshold is reached
71
80
while len (new_tweets ) > 0 :
72
81
for tweet in new_tweets :
@@ -100,19 +109,10 @@ def get_all_tweets5args(screen_name, api, amount, filename, lastTweetScrapedFile
100
109
new_tweets = api .user_timeline (screen_name = screen_name ,count = 200 ,max_id = oldest )
101
110
if numTweets >= amount :
102
111
break
103
- numTweets += len (new_tweets )
112
+ numTweets += len (new_tweets )
104
113
114
+ writeTweets (filename , 'a' , alltweets )
105
115
# write to txt file
106
- f = open (filename , 'a' )
107
- # print len(alltweets)
108
- numbs = 0
109
- for tweet in alltweets :
110
- if tweet != "\n " :
111
- f .write (tweet )
112
- f .write ("\n " )
113
- numbs += 1
114
- f .close ()
115
-
116
116
updateLastTweet (lastTweetScrapedFile , screen_name , most_recent_tweet )
117
117
118
118
0 commit comments