forked from ryanmcgrath/twython
/
streaming.py
60 lines (50 loc) · 1.91 KB
/
streaming.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
#!/usr/bin/python
"""
TwythonStreamer is an implementation of the Streaming API for Twython.
Pretty self explanatory by reading the code below. It's worth noting
that the end user should, ideally, never import this library, but rather
this is exposed via a linking method in Twython's core.
Questions, comments? ryan@venodesigns.net
"""
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
__version__ = "1.0.0"
import urllib
import urllib2
import urlparse
import httplib
import httplib2
import re
from urllib2 import HTTPError
# There are some special setups (like, oh, a Django application) where
# simplejson exists behind the scenes anyway. Past Python 2.6, this should
# never really cause any problems to begin with.
try:
# Python 2.6 and up
import json as simplejson
except ImportError:
try:
# Python 2.6 and below (2.4/2.5, 2.3 is not guranteed to work with this library to begin with)
import simplejson
except ImportError:
try:
# This case gets rarer by the day, but if we need to, we can pull it from Django provided it's there.
from django.utils import simplejson
except:
# Seriously wtf is wrong with you if you get this Exception.
raise Exception("Twython requires the simplejson library (or Python 2.6) to work. http://www.undefined.org/python/")
class TwythonStreamingError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return "%s" % str(self.msg)
feeds = {
"firehose": "http://stream.twitter.com/firehose.json",
"gardenhose": "http://stream.twitter.com/gardenhose.json",
"spritzer": "http://stream.twitter.com/spritzer.json",
"birddog": "http://stream.twitter.com/birddog.json",
"shadow": "http://stream.twitter.com/shadow.json",
"follow": "http://stream.twitter.com/follow.json",
"track": "http://stream.twitter.com/track.json",
}
class Stream(object):
def __init__(self, username = None, password = None, feed = "spritzer", user_agent = "Twython Streaming"):