forked from JeffAMcGee/TwitterStreamSaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basestream.py
61 lines (49 loc) · 1.67 KB
/
basestream.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
#!/usr/bin/env python
# encoding: utf-8
"""
basestream.py
Created by Brian Eoff on 2011-02-21.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
import pycurl
import os
import signal
from urllib import urlencode
class Stream:
def __init__(self, url, username, password, on_receive_method, initial_params=[], filter_type=None):
self.url, self.username, self.password = url, username, password
self.on_receive, self.childPid = on_receive_method, -1
self.params = initial_params
self.filter_type = filter_type
self.conn = pycurl.Curl()
def resetFilterParameters(self, params):
self.params = params
self.restart()
def restart(self):
self.stop()
self.start()
def stop(self):
os.kill(self.childPid, signal.SIGTERM)
os.waitpid(self.childPid, 0)
def start(self):
self.conn.setopt(pycurl.VERBOSE ,1)
self.conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
self.conn.setopt(pycurl.USERPWD, "%s:%s" %
(self.username, self.password))
self.conn.setopt(pycurl.URL, self.url)
self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)
if len(self.params) > 0:
self.conn.setopt(pycurl.POST, 1)
self.conn.setopt(pycurl.POSTFIELDS,
urlencode({self.filter_type: ','.join(self.params)}))
cpid = os.fork()
if cpid == 0:
try:
self.conn.perform()
except:
print 'Unable to start Curl'
pass
else:
self.childPid = cpid
if __name__ == '__main__':
pass