Skip to content

Commit

Permalink
Fixing tape bug when other files are present
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Mar 3, 2009
1 parent dd4f792 commit 2612eb7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/controllers/s3.py
Expand Up @@ -81,7 +81,7 @@ def _get(self):

# Get handler for format
if format == 'rss': handler = RSSResponse(self)
elif format == 'xspf': handler = tape.XSPFResponse(self)
elif format.startswith('xspf'): handler = tape.XSPFResponse(self)
elif format == 'tape': handler = tape.TapeResponse(self)
elif format == 'json': handler = JSONResponse(self)
elif format == 'error': handler = ErrorResponse(self)
Expand Down
74 changes: 37 additions & 37 deletions app/controllers/tape.py
Expand Up @@ -10,58 +10,58 @@

class TapeResponse(BaseResponse):

def __init__(self, request_handler):
self.request_handler = request_handler

def handle(self, s3response):

list_url = self.request_handler.request.path_url
xspf_url = '%s?format=xspf' % list_url

tracks = []
id3_urls = []
for file in s3response.files:
if file.extension == 'mp3':
tracks.append(file.xspf_track)
id3_urls.append('%s?format=id3-json' % file.appspot_url)

xspf_url = '%s?format=xspfm' % list_url

tracks = [file.xspf_track for file in s3response.files if file.extension == 'mp3']
id3_urls = ['%s?format=id3-json' % file.appspot_url for file in s3response.files if file.extension == 'mp3']

values = dict(title='Mix Tape (%s)' % s3response.path, xspf_url=xspf_url, list_url=list_url, tracks=tracks, id3_urls=id3_urls, s3response=s3response)

self.render("muxtape.mako", values)


class XSPFResponse(BaseResponse):

def handle(self, s3response):
url = s3response.url
files = s3response.files
path = s3response.path


exts = self.request.get('exts', None)
extensions = None
if exts:
extensions = exts.split(',')

# Special case for muxtape; Not sure why player can't handle larger param values.
if self.request.get('format', '') == 'xspfm':
extensions = ['mp3']

files.sort(cmp=lambda x, y: shrub.utils.file_comparator(x, y, 'name', True))

tracks = []

for file in files:
tracks.append(file.xspf_track)


tracks = [file.xspf_track for file in files if not extensions or file.extension in extensions]
logging.info("Tracks: %s" % ([str(track) for track in tracks]))

values = dict(title=path, creator='Shrub', info='http://shrub.appspot.com', location=url, tracks=tracks)

self.render("xspf.mako", values, 'text/xml; charset=utf-8')


class ID3Response(JSONResponse):

def load_url(self, url, format='json', cache_key=None):

if self.render_json_from_cache(cache_key):
return

callback = self.request.get("callback", None)

logging.info("Loading url: %s" % url)
fetch_headers = dict(Range='bytes=0-1024')
response = urlfetch.fetch(url, headers=fetch_headers, allow_truncated=True)

try:
data = id3data.ID3Data(response.content)
id3r = id3reader.Reader(data, only_v2=True)
Expand All @@ -70,18 +70,18 @@ def load_url(self, url, format='json', cache_key=None):
self.render_json(dict(error='Not found'))
return

values = dict(album=id3r.getValue('album'),
performer=id3r.getValue('performer'),
title=id3r.getValue('title'),
track=id3r.getValue('track'),
year=id3r.getValue('year'),
isTruncated=id3r.is_truncated,
values = dict(
album=id3r.getValue('album'),
performer=id3r.getValue('performer'),
title=id3r.getValue('title'),
track=id3r.getValue('track'),
year=id3r.getValue('year'),
isTruncated=id3r.is_truncated,
)

if format == 'json':
self.render_json(values, cache_key=cache_key, callback=callback)

except id3reader.Id3Error, detail:
self.render_json(dict(error=str(detail)))


2 changes: 1 addition & 1 deletion app/helpers/base.py
Expand Up @@ -8,4 +8,4 @@ def to_json(context, value):
return simplejson.dumps(value)

def shrub_version(context):
return "1.2.8"
return "1.2.9"
14 changes: 14 additions & 0 deletions app/views/index.mako
Expand Up @@ -148,6 +148,20 @@ myCallback({"maxKeys": "1000", "prefix": "", ...})
For example, <a href="/m1xes/sub-pop-mix-1/?format=xspf">http://shrub.appspot.com/m1xes/sub-pop-mix-1/?format=xspf</a>
</p>
<br/>
<h3>URL parameters</h3>

<table class="tabular">
<thead>
<tr><th>Parameter</th> <th>Description</th></tr>
<tbody>
<tr>
<td>exts</td>
<td>List of extensions to filter (comma-delimited). For example, exts=mp3,foo,bar
</td>
</tr>
</tbody>
</table>
<br/>
<br/>

<pre style="overflow:auto; width=380px">
Expand Down
2 changes: 2 additions & 0 deletions public/javascripts/shrub.js
Expand Up @@ -268,13 +268,15 @@ var shrubTape = (function() {
},

onMetaChange: function(track, time) {
if (isNaN(time)) return;
var node = $("#song-" + track + " .song-duration");
node.html(time);
if (time == "0:00") node.addClass("unknown");
else node.removeClass("unknown");
},

onLoadedChange: function(track, percentage) {
if (isNaN(percentage)) return;
var node = $("#song-" + track + " .song-loaded");
node.html(percentage + "%")
if (percentage > 0) node.removeClass("unknown");
Expand Down
3 changes: 3 additions & 0 deletions shrub/feeds/xspf.py
Expand Up @@ -7,4 +7,7 @@ def __init__(self, location, meta, title, info):
self.meta = meta
self.title = title
self.info = info

def __str__(self):
return 'location=%s,meta=%s,title=%s,info=%s' % (self.location, self.meta, self.title, self.info)

0 comments on commit 2612eb7

Please sign in to comment.