-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetNTweets.py
119 lines (87 loc) · 3.4 KB
/
GetNTweets.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
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
# -*- coding: utf-8 -*-
"""
@author: jesseclark
Use Tweepy 3.2.0, not 3.3.0
Seems to erro with 3.3.0
"""
#Import the necessary methods from tweepy library
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import sys
#Variables that contains the user credentials to access Twitter API
access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
class StdOutListener(StreamListener):
def __init__(self, api=None,save_name = 'test.txt',tweets_total = 100):
super(StdOutListener, self).__init__()
# set the ~ total number of tweets to collet
self.tweets_total = tweets_total
# specify the file to write the tweets to
self.f = open(save_name, "w")
# init the counter
self.num_tweets = 0
def on_data(self, data):
# increment collected tweet counter
self.num_tweets += 1
if self.num_tweets <= self.tweets_total:
print str(self.num_tweets)
self.f.write( data )
return True
else:
self.f.close()
return False
def on_error(self, status):
print 'Error on status', status
def on_limit(self, status):
print 'Limit threshold exceeded', status
def on_timeout(self, status):
print 'Stream disconnected; continuing...'
def collectTweets(tweets_total, save_name ,location ):
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener(save_name = save_name, tweets_total = tweets_total)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
if len(location) != 0:
stream.filter(locations=[-122.75,36.8,-121.75,37.8],track = [location],languages = ['en'])
else:
stream.filter(languages = ['en'])
def collectTweetsLoc(tweets_total, save_name ,location ):
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener(save_name = save_name, tweets_total = tweets_total)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(locations=location,languages = ['en'])
def post_tweet(tweet,to_tweet = 1):
# use this to post a tweet to attached api acount
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#if to_tweet != 0:
api = tweepy.API(auth)
api.update_status(status = tweet)
print tweet+'\n'
if __name__ == '__main__':
# collect tweets from the fire hose
# see GetCityTweets.py to see how to collect from location
# tweets to collect, name and filter key
tweets_total = 75
save_name = 'test.txt'
key = ':)'
# save name if entered by user
if len(sys.argv) >= 2:
save_name = sys.argv[1]
# total tweets if entered by user
if len(sys.argv) >= 3:
tweets_total = int(sys.argv[2])
# what to filter on (text)
if len(sys.argv) >= 4:
key = (sys.argv[3])
print '\n Collecting tweets ['+str(tweets_total)+'].... \n'
print '\n '+key
print '\n '+save_name
collectTweets(tweets_total , save_name , key)