Skip to content

Instantly share code, notes, and snippets.

@viperfx
Created February 14, 2015 12:36
Show Gist options
  • Save viperfx/c3ef6126113c6fdce0ec to your computer and use it in GitHub Desktop.
Save viperfx/c3ef6126113c6fdce0ec to your computer and use it in GitHub Desktop.
JukeTube API server
from flask import Flask, jsonify, request, Response, send_file
import youtube_dl
import eyed3
"""
Workflow script: https://workflow.is/workflows/7b80d0daa48847ff867882bed7df8032
Requires python installed with packages Flask, youtube_dl and eyed3 installed through pip
Required FFmpeg for mp3 conversion
"""
class SimpleYDL(youtube_dl.YoutubeDL):
def __init__(self, *args, **kargs):
super(SimpleYDL, self).__init__(*args, **kargs)
self.add_default_info_extractors()
app = Flask(__name__)
@app.route('/download/<ytid>/')
def download(ytid):
opt = {
'format': 'bestaudio/best',
'outtmpl': 'test.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with SimpleYDL(opt) as ydl:
ydl.download([ytid])
af = eyed3.load("test.mp3")
af.tag.artist = request.args.get('artist')
af.tag.album = request.args.get('album')
af.tag.title = request.args.get('title')
af.tag.save()
return send_file('test.mp3', mimetype="audio/mp3", as_attachment=True, attachment_filename='%s.mp3'%ytid)
@app.route('/stream/<ytid>')
def stream(ytid):
res = SimpleYDL().extract_info(ytid, download=False)
data = {item['ext']: item['url'] for item in res['formats'] if item['format_id'] == "171" or item['format_id'] == "140"}
data['title'] = res['title']
data['info'] = res['description']
return jsonify(data)
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment