-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamFinder.py
More file actions
37 lines (31 loc) · 1.59 KB
/
StreamFinder.py
File metadata and controls
37 lines (31 loc) · 1.59 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys, json, re
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.api import API
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx-xxx'
ACCESS_SECRET = 'xxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
class StdOutListener(StreamListener): #Listens for new tweets that fit a list of keywords
def on_data(self, data):
# Twitter returns data in JSON format - we need to decode it first
decoded = json.loads(data)
# Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
try:#try to get geolocation
print ('@%s: %s %s' % (decoded['user']['screen_name'], decoded['geo']['coordinates'], decoded['text'].encode('ascii', 'ignore')))
except:#if we cant, just print the text
print ('@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore')))
#pass - remove this line if you want to ignore the tweet, and only grab geolocation enabled tweets
return True
if __name__ == '__main__':
l = StdOutListener()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
stream = Stream(auth, l, timeout=None, compression=True) # Words to search for in the stream
stream.filter(track = ['follow me on insta ', 'snapchat me: ', 'text me: ', 'add me on snapchat: ', 'follow me on instagram: ', 'text me ('])