-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdribbblefeed.py
142 lines (115 loc) · 4.25 KB
/
dribbblefeed.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
#-*- coding: utf-8 -*-
#2012 Ole Trenner
import collections
import json
import traceback
import urllib
import urllib2
from xml.sax.saxutils import escape
import web
class DribbbleApi(object):
"""Dribbble API client.
>>> d = DribbbleApi(printrequests=True)
>>> d.players_shots_following('someplayer')
'http://api.dribbble.com/players/someplayer/shots/following'
>>> d.players_shots_following('someplayer', page=3)
'http://api.dribbble.com/players/someplayer/shots/following?page=3'
"""
DRIBBBLE = "http://api.dribbble.com/"
FOLLOWING = "players/%(user)s/shots/following"
def __init__(self, printrequests=False):
self._debug = printrequests
def players_shots_following(self, player, **kwargs):
return self._request(self.FOLLOWING % dict(user=player), **kwargs)
def _request(self, path, **kwargs):
url = '%s%s?%s' % (self.DRIBBBLE, path, urllib.urlencode(kwargs))
url = url.strip('?')
if self._debug:
return url
try:
response = urllib2.urlopen(url)
return json.load(response)
except Exception, e:
raise Exception('Couldn\'t access "%s", error "%s"' % (url, e))
class DribbbleFeed(object):
"""Dribbble feed generator.
>>> d = DribbbleFeed()
>>> d.players_shots_following(dict(shots=[])) #doctest:+ELLIPSIS
'<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0">...</rss>'
"""
SKELETON = """<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0">
<channel>
<title>%(title)s</title>
<description>%(description)s</description>
<link>%(link)s</link>
<lastBuildDate>%(builddate)s</lastBuildDate>
<pubDate>%(pubdate)s</pubDate>
<ttl>%(ttl)s</ttl>
%(items)s
</channel>
</rss>"""
ITEM = """<item>
<title>%(title)s</title>
<description>%(content)s</description>
<link>%(link)s</link>
<guid>%(guid)s</guid>
<pubDate>%(pubdate)s</pubDate>
</item>"""
CONTENT = """<div class="player">
<a href="%(player_url)s">
<img alt="" src="%(player_avatar_url)s">
%(player_name)s (%(player_username)s)
</a>
</div>
<div class="shot">
<a href="%(url)s">
<img alt="" src="%(image_url)s"> %(title)s
</a>
</div>"""
def players_shots_following(self, data):
def itemize(data):
return self.ITEM % dict(
title=data['title'],
content=escape(self.CONTENT % flatten(data)),
link=data['url'],
guid=data['url'],
pubdate=data['created_at']
)
items = ''.join((itemize(i) for i in data['shots']))
return self._feed(title='Shots', items=items, ttl=21600)
def _feed(self, **data):
data = collections.defaultdict(str, data)
return self.SKELETON % data
class DribbbleFeeder(object):
"""Dribbble feed request handler.
"""
def GET(self, username):
username = username or '_'
try:
d = DribbbleApi()
f = DribbbleFeed()
data = d.players_shots_following(username)
rss = f.players_shots_following(data)
return rss
except Exception, e:
return e
def flatten(dictionary, prefix=''):
"""Creates a flattened version of the dictionary where inner dicts
are inserted on the top level with a prefix of their key.
>>> flatten(dict(a=1, b=dict(c=2))) == dict(a=1, b_c=2)
True
"""
f = {}
for k, v in dictionary.items():
key = prefix + k
if isinstance(v, dict):
f.update(flatten(v, key + '_'))
else:
f[key] = v
return f
urls = ('/(\w+)', 'DribbbleFeeder')
application = web.application(urls, globals())
if __name__ == '__main__':
wsgiapp = application.wsgifunc()
web.wsgi.runwsgi(wsgiapp)