Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 135 lines (98 sloc) 3.868 kB
5b8e1a4 @progrium using spreedly module, better syncing, new fields
progrium authored
1 import urllib, xml.dom.minidom
2
3 __version__ = '0.1'
4
5 SITE_NAME = 'your-site'
6 SPREEDLY_BASE_URL = 'https://spreedly.com/api/v4/%(site)s/'
7 SPREEDLY_TOKEN = 'your-token'
8
9 def remove_whitespace_nodes(node, unlink=True):
10 remove_list = set()
11
12 for child in node.childNodes:
13 if child.nodeType == child.TEXT_NODE and not child.data.strip():
14 remove_list.add(child)
15 elif child.hasChildNodes():
16 remove_whitespace_nodes(child, unlink)
17
18 for node in remove_list:
19 node.parentNode.removeChild(node)
20 node.unlink()
21
22 def get_code(response):
23 if hasattr(response, 'code'):
24 return response.code # py 2.6
25
26 return int(response.headers['status'][0:3])
27
28 class UAOpener(urllib.FancyURLopener):
29 def __init__(self, token, *args, **kwargs):
30 self.token = token
31 urllib.FancyURLopener.__init__(self, *args, **kwargs)
32
33 def prompt_user_passwd(self, host, realm):
34 return (self.token, 'X')
35
36 version = 'pyspreedly/%s' % __version__
37
38 class XMLReply(object):
39 def __init__(self, payload):
40 self.raw_payload = payload
41 self.data = payload.read()
42 self.xml = None
43
44 dom = self.to_xml()
45 self.dict = self.to_dict(dom.documentElement)
46
47 def to_xml(self):
48 if self.xml is None:
49 self.xml = xml.dom.minidom.parseString(self.data)
50 remove_whitespace_nodes(self.xml.documentElement)
51 return self.xml
52
53 def to_dict(self, parent):
54 child = parent.firstChild
55
56 if not child:
57 return None
58 elif child.nodeType == child.TEXT_NODE:
59 return child.nodeValue
60
61 block = dict()
62
63 while child is not None:
64 if child.nodeType == child.ELEMENT_NODE:
65 block[child.tagName] = self.to_dict(child)
66
67 child = child.nextSibling
68
69 return block
70
71 # --
72
73 def __repr__(self):
74 return '<XMLReply: data=%d bytes>' % len(self.data)
75
76 class SpreedlyResponseError(Exception):
77 def __init__(self, response):
78 self.code = get_code(response)
79 self.headers = response.headers
80 self.url = self.safe_url(response.url)
81 self.body = response.read()
82
83 def safe_url(self, url):
84 if not '@' in url:
85 return url
86
87 return url[:url.index('//')+2]+url[url.index('@')+1:]
88
89 def __str__(self):
90 return '<SpreedlyResponseError: code=%d, url=%s>' % (self.code, self.url)
91
92 class Spreedly(object):
93 """
94 Stupid-simple Python library for talking
95 to spreedly.com.
96 """
97 def __init__(self, site=SITE_NAME, base_url=SPREEDLY_BASE_URL, token=SPREEDLY_TOKEN):
98 self.site = site
99 self.base_url = base_url
100 self.token = token
101
102 def url(self, rel_url):
103 return self.base_url % { 'site': self.site }+rel_url
104
105 def request(self, url, data=None):
106 opener = UAOpener(self.token)
107 return self.to_reply(opener.open(self.url(url), data))
108
109 def to_reply(self, response):
110 if not get_code(response) == 200:
111 raise SpreedlyResponseError(response)
112
113 return XMLReply(response).dict
114
115 # --
116
117 def url_factory(url):
118 def wrapped_request(self, data=None, **kwargs):
119 return self.request(url % kwargs, data)
120 return wrapped_request
121
122 subscribers = url_factory('subscribers.xml')
123 subscription_plans = url_factory('subscription_plans.xml')
124 subscriber_details = url_factory('subscribers/%(sub_id)d.xml')
125
126 def __repr__(self):
127 return '<Spreedly: site=%s, token=%s>' % (self.site, self.token)
128
129 if __name__ == "__main__":
130 sp = Spreedly()
131
132 print sp.subscription_plans()
133 print sp.subscriber_details(sub_id=1)
134 print sp.subscribers()
Something went wrong with that request. Please try again.