Skip to content

Commit 26b79c8

Browse files
committed
Added testing file and fixed issue with leaving credentials in files
1 parent 34247ac commit 26b79c8

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

getTweets.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22
import tweet_dumper
33
import sys
44
import re
5+
import json
6+
7+
# looks for json file with the proper credentials for the user
8+
# {
9+
# "consumer_key" : "value",
10+
# "consumer_secret" : "value",
11+
# "access_token" : "value",
12+
# "access_token_secret" : "value"
13+
# }
14+
def get_credentials(filename):
15+
with open(filename) as file:
16+
cfg = json.load(file)
17+
return cfg
518

619
def get_api(cfg):
720
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
821
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
922
return tweepy.API(auth)
1023

1124
def main():
12-
if sys.argv[1] and sys.argv[2]:
25+
try:
1326
username = sys.argv[1]
1427
amount = int(sys.argv[2])
15-
else:
28+
except IndexError as e:
1629
print "Usage: python getTweets.py username amount [filename]"
1730
sys.exit(1)
1831
# Fill in the values noted in previous step here
19-
cfg = {
20-
"consumer_key" : "Ximk8dlrGGejLqL65TO39SqMD",
21-
"consumer_secret" : "SiwVIoNUygs8FdZRAb4D3n6IdQUYPfUG82UNu2OwWUFhO9fq0l",
22-
"access_token" : "705285627-hsGmup5zp84eZ6BPCqjmAWaR3g2YOXad3gPBnUac",
23-
"access_token_secret" : "Z6ws8O78em89H4ErpD5O2A0UM90t7hBXLxXRVH2Rson6V"
24-
}
25-
32+
cfg = get_credentials("the_Tmonster.json")
2633
api = get_api(cfg)
2734
user = api.get_user(username)
2835
if sys.argv[3]:
@@ -31,4 +38,4 @@ def main():
3138
tweet_dumper.get_all_tweets3args(username, api, amount)
3239

3340
if __name__ == "__main__":
34-
main()
41+
main()

tweet_dumper.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,33 @@ def get_all_tweets4args(screen_name, api, amount, filename):
4949
get_all_tweets5args(screen_name, api, amount, filename, lastTweetId)
5050

5151

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()
5460

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
5664
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)
5967
new_tweets = api.user_timeline(screen_name = screen_name,count=200, include_rts = True)
6068
numTweets = len(new_tweets)
6169
MostRecentTweetPulled = getMostRecentTweet(lastTweetScrapedFile, screen_name)
6270
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.
6574
if len(new_tweets) > 0:
6675
most_recent_tweet = new_tweets[0].id
6776
else:
6877
most_recent_tweet = 0
69-
# loop over first request and keep requesting until amount
78+
# loop over first request and keep requesting until amount
7079
# is reached or the 3240 threshold is reached
7180
while len(new_tweets) > 0:
7281
for tweet in new_tweets:
@@ -100,19 +109,10 @@ def get_all_tweets5args(screen_name, api, amount, filename, lastTweetScrapedFile
100109
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
101110
if numTweets >= amount:
102111
break
103-
numTweets += len(new_tweets)
112+
numTweets += len(new_tweets)
104113

114+
writeTweets(filename, 'a', alltweets)
105115
# 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-
116116
updateLastTweet(lastTweetScrapedFile, screen_name, most_recent_tweet)
117117

118118

0 commit comments

Comments
 (0)