forked from neuralinfo/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3_1_1.py
More file actions
executable file
·82 lines (62 loc) · 2 KB
/
Assignment3_1_1.py
File metadata and controls
executable file
·82 lines (62 loc) · 2 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
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
#! /usr/bin/env python
#
# David Paculdo
# W205
# Assignment 3
import sys
import pymongo
import tweepy
import signal
import json
import os
import string
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ckey = os.environ.get("twitter_consumer_key");
csecret = os.environ.get("twitter_consumer_secret");
atoken = os.environ.get("twitter_access_token");
asecret = os.environ.get("twitter_access_token_secret");
# Defaults for Twitter Stream
try:
termList=str(sys.argv[1]) #term list must be in quotes on command line e.g. "#microsoft,#mojang"
print "Search terms: "+termList
except:
print "Usage: Assignment3_1_1.py \"[search terms separated by comma]\""
# DB name for mongodb
db_name="db_streamT"
# Instance of the Twitter stream listener
class listener(StreamListener):
def __init__(self, api):
self.api = api
super(StreamListener, self).__init__()
#change name of client and database to what we want it to be
self.db = pymongo.MongoClient()
self.db = self.db[db_name]
def on_data(self, data):
data = json.loads(data)
#data=data.encode("utf-8")
text=filter(lambda x: x in string.printable, data['text'])
print text
# Writing to mongodb. Can change tweets collection to something else
self.db.tweets.insert(data)
return True
def on_error(self, status):
print >> sys.stderr, 'Encountered error with status code:', status
if status==420:
time.sleep(600)
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
#Begin Twitter stream access
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
api=tweepy.API(auth, retry_count=5, retry_delay=5, retry_errors=set([401,404,500,503]),timeout=120)
try:
twitterStream = Stream(auth, listener(api))
twitterStream.filter(track=[termList])
except KeyboardInterrupt:
print("Interrupt called")
sys.exit()