-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (66 loc) · 2.51 KB
/
utils.py
File metadata and controls
85 lines (66 loc) · 2.51 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
83
84
85
import oauth2 as oauth
import sqlite3
import urllib
import time
import md5
import random
import urllib2
from secrets import *
DATABASE = 'followers.db'
TWITTER_HANDLE ='singthattweet'
STREAM_URL = "https://userstream.twitter.com/2/user.json"
def _generate_nonce():
random_number = ''.join(str(random.randint(0, 9)) for i in range(40))
m = md5.new(str(time.time()) + str(random_number))
return m.hexdigest()
def stream(stream_url=STREAM_URL):
access_token = oauth.Token(ACCESS_TOKEN, ACCESS_SECRET)
consumer = oauth.Token(CONSUMER_KEY, CONSUMER_SECRET)
parameters = {
'oauth_consumer_key': CONSUMER_KEY,
'oauth_token': access_token.key,
'oauth_signature_method': 'HMAC-SHA1',
'oauth_timestamp': str(int(time.time())),
'oauth_nonce': _generate_nonce(),
'oauth_version': '1.0',
'track': TWITTER_HANDLE
}
oauth_request = oauth.Request.from_token_and_callback(access_token,
http_url=stream_url,
parameters=parameters)
signature_method = oauth.SignatureMethod_HMAC_SHA1()
signature = signature_method.sign(oauth_request, consumer, access_token)
parameters['oauth_signature'] = signature
data = urllib.urlencode(parameters)
req = urllib2.urlopen("%s?%s" % (stream_url, data))
buffer = ''
# We're using urllib2 to avoid external dependencies
# even though pyCurl actually handles the callbacks
# much more gracefully than this clumsy method.
# We read a byte at a time until we find a newline
# which indicates the end of a chunk.
while True:
chunk = req.read(1)
if not chunk:
yield buffer
break
chunk = unicode(chunk)
buffer += chunk
tweets = buffer.split("\n", 1)
if len(tweets) > 1:
yield tweets[0]
buffer = tweets[1]
def oauth_req(url, params={}, http_method="GET", consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET, access_token=ACCESS_TOKEN,
access_secret=ACCESS_SECRET):
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
token = oauth.Token(key=access_token, secret=access_secret)
client = oauth.Client(consumer, token=token)
body=urllib.urlencode(params)
print url
print body
request = client.request(url, method=http_method, body=body)
#this is a tuple of a response header and the response we care about
return request
def get_conn():
return sqlite3.connect(DATABASE)