forked from jherskowitz/tomahklet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
playgrub.py
executable file
·295 lines (230 loc) · 10.2 KB
/
playgrub.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import logging
import os
import re
import urlparse
import urllib
import base64
import datetime
import hashlib
import wsgiref.handlers
from django.utils import simplejson
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from models import PlaylistHeader
from models import PlaylistTrack
from models import PlaygrubAccount
from models import PlaygrubChartEntry
pghost = "http://tomahawkbar.appspot.com/"
class IndexHandler(webapp.RequestHandler):
def get(self):
heads = PlaylistHeader.gql("order by create_date desc limit 25");
template_values = {
'headers': heads,
}
path = os.path.join(os.path.dirname(__file__), 'html/index.html')
self.response.out.write(template.render(path, template_values))
class PlaylistHeaderHandler(webapp.RequestHandler):
def get(self):
rtitle = self.request.get('title')
rurl = self.request.get('url')
rsongs = self.request.get('songs')
rcreate_date = datetime.datetime.now()
h = hashlib.new('ripemd160')
h.update(rurl+rcreate_date.ctime())
rplaylist = h.hexdigest()
playlist_header = PlaylistHeader(title = rtitle,
url = rurl,
playlist = rplaylist,
songs = rsongs,
create_date = rcreate_date)
playlist_header.put()
# logging.error("playlist_header --> %s", playlist_header.title)
self.response.out.write("Playgrub.client.header_callback('"+rplaylist+"');")
class PlaylistTrackHandler(webapp.RequestHandler):
def get(self):
playlist_track = PlaylistTrack(artist = self.request.get('artist'),
track = self.request.get('track'),
index = int(self.request.get('index')),
playlist = self.request.get('playlist'),
create_date = datetime.datetime.now())
playlist_track.put()
# logging.error("playlist_track --> %s", playlist_track.artist)
self.response.out.write('Playgrub.client.broadcast_index++; Playgrub.client.write_playlist(Playgrub.playlist);')
class LatestXSPFHandler(webapp.RequestHandler):
def get(self):
playlist_key = self.request.get('url')
# logging.error("url -> %s",playlist_key)
q = PlaylistHeader.gql('WHERE url = :1 order by create_date desc limit 1', playlist_key)
if q.count() == 0:
return
head = q.fetch(1)[0]
# logging.error("head -> %s",head.title)
q = PlaylistTrack.gql('WHERE playlist = :1 ORDER BY index ASC', head.playlist)
songs = q.fetch(250)
# for r in songs:
# logging.error("index -> %s", r.index)
# logging.error("artist -> %s", r.artist)
# logging.error("track -> %s", r.track)
template_values = {
'header': head,
'songs': songs,
}
path = os.path.join(os.path.dirname(__file__), 'html/xspf-template.xspf')
self.response.headers['Content-Type'] = 'application/xspf+xml'
self.response.out.write(template.render(path, template_values))
class ChartXSPFHandler(webapp.RequestHandler):
def get(self):
q = PlaygrubChartEntry.all()
q.order('-score')
if q.count() == 0:
return
songs = q.fetch(50)
class ChartHeader(object):
title = "The Latest Tomahawklet Charts" # Include the local time maybe?
url = pghost+"#xspf="+pghost+"charts"
head = ChartHeader()
template_values = {
'header': head,
'songs': songs,
}
path = os.path.join(os.path.dirname(__file__), 'html/xspf-template.xspf')
self.response.headers['Content-Type'] = 'application/xspf+xml'
self.response.out.write(template.render(path, template_values))
class XSPFHandler(webapp.RequestHandler):
def get(self):
playlist_key = self.request.path.split('.xspf')[0]
playlist_key = playlist_key.lstrip('/')
# logging.error("XSPF key --> %s", playlist_key)
q = PlaylistHeader.gql('WHERE playlist = :1', playlist_key)
if q.count() == 0:
return
head = q.fetch(1)[0]
# logging.error("head -> %s",head.title)
q = PlaylistTrack.gql('WHERE playlist = :1 ORDER BY index ASC', playlist_key)
songs = q.fetch(500)
# for r in songs:
# logging.error("index -> %s", r.index)
# logging.error("artist -> %s", r.artist)
# logging.error("track -> %s", r.track)
template_values = {
'header': head,
'songs': songs,
}
path = os.path.join(os.path.dirname(__file__), 'html/xspf-template.xspf')
self.response.headers['Content-Type'] = 'application/xspf+xml'
self.response.out.write(template.render(path, template_values))
class BookmarkletIframeHandler(webapp.RequestHandler):
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'html/player-iframe.html')
self.response.out.write(template.render(path, template_values))
class RemoteXSPFHandler(webapp.RequestHandler):
def get(self):
url = self.request.get('xspf')
result = urlfetch.fetch(url, payload=None, method=urlfetch.GET, headers={})
self.response.headers['Content-Type'] = 'application/xspf+xml'
self.response.out.write(result.content)
class PlayerHandler(webapp.RequestHandler):
def get(self):
url = self.request.get('xspf')
template_values = {
'xspf': url,
}
path = os.path.join(os.path.dirname(__file__), 'html/standalone.html')
self.response.out.write(template.render(path, template_values))
class JSONXSPFHandler(webapp.RequestHandler):
def get(self):
playlist_key = self.request.path.split('.xspf')[0]
playlist_key = playlist_key.lstrip('/')
# logging.error("XSPF key --> %s", playlist_key)
q = PlaylistHeader.gql('WHERE playlist = :1', playlist_key)
head = q.fetch(1)[0]
# logging.error("head -> %s",head.title)
q = PlaylistTrack.gql('WHERE playlist = :1 ORDER BY index ASC', playlist_key)
songs = q.fetch(500)
# for r in songs:
# logging.error("index -> %s", r.index)
# logging.error("artist -> %s", r.artist)
# logging.error("track -> %s", r.track)
template_values = {
'header': head,
'songs': songs,
}
path = os.path.join(os.path.dirname(__file__), 'html/json-xspf-template.html')
self.response.headers['Content-Type'] = 'text/javascript'
self.response.out.write(template.render(path, template_values))
class ScrapeHandler(webapp.RequestHandler):
def get(self):
url = self.request.get('url')
url = urlparse.urlparse(url)
domain = url.netloc + url.path
scraper_path = os.path.join(os.path.dirname(__file__), 'scrapers/')
for root, dirs, files in os.walk(scraper_path):
self.response.headers['Content-Type'] = 'text/javascript'
# logging.error("checking -> %s",domain)
for filename in files:
if filename.endswith('.js'):
# logging.error("filename -> %s",re.escape(filename.split('.js')[0]))
sre = re.compile('(.+\.)?'+re.escape(filename.split('.js')[0])+'.*')
if sre.match(domain):
# logging.error("match -> %s",domain)
self.response.out.write(template.render(scraper_path+filename, {}))
return
self.response.out.write(template.render(scraper_path+'default.js', {}))
class TwitterPostHandler(webapp.RequestHandler):
def get(self):
playlist_key = self.request.get('playlist')
q = PlaylistHeader.gql('WHERE playlist = :1', playlist_key)
if q.count() == 0:
return
head = q.fetch(1)[0]
q = PlaygrubAccount.gql('WHERE service = :1', 'bit.ly')
if q.count(1) == 0:
return
bitly_account = q.fetch(1)[0]
play_url = pghost+urllib.quote('#xspf='+pghost+head.playlist+'.xspf')
login = bitly_account.user
password = bitly_account.password
shorten_url = 'http://api.bit.ly/shorten?version=2.0.1&login='+login+'&apiKey='+password+'&history=1&longUrl='+play_url
result = urlfetch.fetch(shorten_url, payload=None, method=urlfetch.GET, headers={})
# logging.error('bitly result -> %s', result.content)
sre = re.compile('.*shortUrl.*(http.*)"')
search = sre.search(result.content)
if not search:
return
shortened_url = search.group(1)
# logging.error('shortened_url -> %s', shortened_url)
q = PlaygrubAccount.gql('WHERE service = :1','twitter')
if q.count(1) == 0:
return
twitter_account = q.fetch(1)[0]
message = head.title + ' ' + shortened_url
login = twitter_account.user
password = twitter_account.password
payload= {'status' : message.encode('utf-8'), 'source' : 'Playgrub'}
payload = urllib.urlencode(payload)
base64string = base64.encodestring('%s:%s' % (login, password))[:-1]
headers = {'Authorization': "Basic %s" % base64string}
url = "http://twitter.com/statuses/update.xml"
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers)
# logging.error('twitter result -> %s', result.content)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(result.content)
def main():
application = webapp.WSGIApplication([('/bookmarklet_iframe', BookmarkletIframeHandler),
('/player', PlayerHandler),
('/latest', LatestXSPFHandler),
('/charts', ChartXSPFHandler),
('/remote_xspf', RemoteXSPFHandler),
('/json-xspf/', JSONXSPFHandler),
('/twitter_post', TwitterPostHandler),
('/scraper.js', ScrapeHandler),
('/playlist_header.js', PlaylistHeaderHandler),
('/playlist_track.js', PlaylistTrackHandler),
('/', IndexHandler),('/.*\.xspf', XSPFHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()