Skip to content

Instantly share code, notes, and snippets.

@philippbosch
Last active July 19, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippbosch/359ebb131ee3ca660b82c406b7af0e60 to your computer and use it in GitHub Desktop.
Save philippbosch/359ebb131ee3ca660b82c406b7af0e60 to your computer and use it in GitHub Desktop.
Chalice MP3 duration app
from io import BytesIO
from urllib2 import urlopen
from chalice import Chalice, BadRequestError
from mutagen.mp3 import MP3
app = Chalice(app_name='mp3info')
@app.route('/')
def index():
request = app.current_request
url = request.query_params.get('url')
if url is None:
raise BadRequestError('Usage: http://{0}/?url=http://url/to/file.mp3'.format(
request.headers.get('Host')))
try:
u = urlopen(url)
except Exception as e:
raise BadRequestError(e.message)
file_obj = BytesIO(u.read())
mp3 = MP3(file_obj)
info = mp3.info.__dict__
return {'duration': info.get('length')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment