-
Notifications
You must be signed in to change notification settings - Fork 24
/
collect_tweets.py
68 lines (52 loc) · 1.92 KB
/
collect_tweets.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
'''
collect_tweets.py
Includes functions for retrieving and storing tweets
Created by Miles Luders
'''
import tweepy, tweetwise_config, datetime, uuid
def authenticate():
try:
# Get api credentials from config file
# (config file is placed in .gitignore for security purposes)
consumer_key = tweetwise_config.CONSUMER_KEY
consumer_secret = tweetwise_config.CONSUMER_SECRET
access_token = tweetwise_config.ACCESS_TOKEN
access_token_secret = tweetwise_config.ACCESS_TOKEN_SECRET
# Connect to api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
except:
print("Failed to authorize Tweepy.")
return None
def get_latest_tweets(query, count):
public_tweets = []
api = authenticate()
if (api):
try:
public_tweets = [(tweet.author.screen_name,
tweet.text,
tweet.created_at.isoformat())
for tweet in api.search(q=query, count=count, lang='en')]
except:
print("Tweepy request failed.")
return public_tweets
def write_tweets_to_file(fname, tweets):
if len(tweets) == 0:
print("Number of tweets to write is zero.")
return
try:
with open(fname, 'a') as f:
for t in tweets:
for a in t:
f.write(a+"\n")
#f.write(str(uuid.uuid(4))
f.write("\n")
print("Successfully wrote", len(tweets), "tweets")
except:
print("Error writing tweets to '" + fname + "'")
if __name__ == '__main__':
d = datetime.datetime.now()
print("Getting tweets... (", d, ")")
T = get_latest_tweets('bitcoin', 11)
write_tweets_to_file('output.txt', T)