forked from ptwobrussell/Recipes-for-Mining-Twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe__streaming_api.py
70 lines (50 loc) · 2.2 KB
/
recipe__streaming_api.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
# -*- coding: utf-8 -*-
import sys
import tweepy
import webbrowser
# Query terms
Q = sys.argv[1:]
# Get these values from your application settings
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
# Get these values from the "My Access Token" link located in the
# margin of your application details, or perform the full OAuth
# dance
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Note: Had you wanted to perform the full OAuth dance instead of using
# an access key and access secret, you could have uses the following
# four lines of code instead of the previous line that manually set the
# access token via auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
#
# auth_url = auth.get_authorization_url(signin_with_twitter=True)
# webbrowser.open(auth_url)
# verifier = raw_input('PIN: ').strip()
# auth.get_access_token(verifier)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
# We'll simply print some values in a tab-delimited format
# suitable for capturing to a flat file but you could opt
# store them elsewhere, retweet select statuses, etc.
try:
print "%s\t%s\t%s\t%s" % (status.text,
status.author.screen_name,
status.created_at,
status.source,)
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
# Create a streaming API and set a timeout value of 60 seconds
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
# Optionally filter the statuses you want to track by providing a list
# of users to "follow"
print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)
streaming_api.filter(follow=None, track=Q)