Skip to content

Instantly share code, notes, and snippets.

@mallendeo
Created September 12, 2015 18:45
Show Gist options
  • Save mallendeo/5fc41f0d6e4354890df0 to your computer and use it in GitHub Desktop.
Save mallendeo/5fc41f0d6e4354890df0 to your computer and use it in GitHub Desktop.
Get the best audio from a YouTube video
youtube-dl==2015.9.9
CherryPy==3.8.0
Routes==2.2
from __future__ import unicode_literals
import youtube_dl
import cherrypy
import json
import os
ydl_opts = {
'format': 'bestaudio',
'dump_single_json': True,
'simulate': True,
}
@cherrypy.popargs('video_id')
class YouTubeDownload(object):
@cherrypy.expose
def index(self, video_id=None, download=None):
url = 'http://www.youtube.com/watch?v=%s' %video_id
if video_id:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url)
info_json = json.dumps(info_dict)
if download:
raise cherrypy.HTTPRedirect(info_dict['url'])
return
cherrypy.response.headers['Content-Type'] = 'json'
return info_json
else:
return "/VIDEO_ID?download=1"
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',})
cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')),})
cherrypy.config.update({'environment' : 'production'})
cherrypy.quickstart(YouTubeDownload())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment