mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Drop ytdl
This commit is contained in:
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
||||
SUBDIRS := cmd/api cmd/nginx cmd/ytdl
|
||||
SUBDIRS := cmd/api cmd/nginx
|
||||
GOLANGCI := ./bin/golangci-lint
|
||||
|
||||
.PHONY: push
|
||||
|
@ -1,12 +0,0 @@
|
||||
FROM python:3.6.0-alpine
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
|
||||
ENV REDIS_CONNECTION_STRING localhost
|
||||
|
||||
RUN apk add --update --no-cache gcc g++ make && \
|
||||
pip install --no-cache-dir --requirement /app/requirements.txt
|
||||
|
||||
EXPOSE 5002
|
||||
ENTRYPOINT ["python", "/app/ytdl.py"]
|
@ -1,4 +0,0 @@
|
||||
.PHONY: push
|
||||
push:
|
||||
docker build -t mxpv/ytdl .
|
||||
docker push mxpv/ytdl
|
@ -1,4 +0,0 @@
|
||||
requests==2.19.1
|
||||
youtube_dl==2019.03.09
|
||||
sanic==18.12
|
||||
redis==2.10.6
|
@ -1,27 +0,0 @@
|
||||
import ytdl
|
||||
import unittest
|
||||
|
||||
|
||||
class TestYtdl(unittest.TestCase):
|
||||
def test_resolve(self):
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://youtube.com/watch?v=ygIUF678y40',
|
||||
{'format': 'video', 'quality': 'low', 'provider': 'youtube'}))
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://youtube.com/watch?v=WyaEiO4hyik',
|
||||
{'format': 'audio', 'quality': 'high', 'provider': 'youtube'}))
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://youtube.com/watch?v=5mjUF2j9dgA',
|
||||
{'format': 'video', 'quality': 'low', 'provider': 'youtube'})
|
||||
)
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://www.youtube.com/watch?v=2nH7xAMqD2g',
|
||||
{'format': 'video', 'quality': 'high', 'provider': 'youtube'})
|
||||
)
|
||||
|
||||
def test_vimeo(self):
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://vimeo.com/237715420', {'format': 'video', 'quality': 'low', 'provider': 'vimeo'}))
|
||||
self.assertIsNotNone(
|
||||
ytdl._resolve('https://vimeo.com/275211960', {'format': 'video', 'quality': 'high', 'provider': 'vimeo'})
|
||||
)
|
116
cmd/ytdl/ytdl.py
116
cmd/ytdl/ytdl.py
@ -1,116 +0,0 @@
|
||||
import os
|
||||
import requests
|
||||
import youtube_dl
|
||||
from sanic import Sanic
|
||||
from sanic.exceptions import InvalidUsage
|
||||
from sanic.response import text, redirect
|
||||
from youtube_dl.utils import DownloadError
|
||||
|
||||
METADATA_URL = os.getenv('METADATA_URL', 'http://app:5001/api/metadata/{feed_id}')
|
||||
print('Using metadata URL template: ' + METADATA_URL)
|
||||
|
||||
app = Sanic()
|
||||
|
||||
opts = {
|
||||
'quiet': True,
|
||||
'no_warnings': True,
|
||||
'forceurl': True,
|
||||
'simulate': True,
|
||||
'skip_download': True,
|
||||
'call_home': False,
|
||||
'nocheckcertificate': True
|
||||
}
|
||||
|
||||
url_formats = {
|
||||
'youtube': 'https://youtube.com/watch?v={}',
|
||||
'vimeo': 'https://vimeo.com/{}',
|
||||
}
|
||||
|
||||
|
||||
@app.route('/download/<feed_id>/<video_id>', methods=['GET', 'HEAD'])
|
||||
async def download(req, feed_id, video_id):
|
||||
if not feed_id:
|
||||
raise InvalidUsage('Invalid feed id')
|
||||
|
||||
# Remote extension and check if video id is ok
|
||||
video_id = os.path.splitext(video_id)[0]
|
||||
if not video_id:
|
||||
raise InvalidUsage('Invalid video id')
|
||||
|
||||
# Pull metadata from API server
|
||||
metadata_url = METADATA_URL.format(feed_id=feed_id, video_id=video_id)
|
||||
r = requests.get(url=metadata_url)
|
||||
json = r.json()
|
||||
|
||||
# Build URL
|
||||
provider = json['provider']
|
||||
tpl = url_formats[provider]
|
||||
if not tpl:
|
||||
raise InvalidUsage('Invalid feed')
|
||||
url = tpl.format(video_id)
|
||||
|
||||
try:
|
||||
redirect_url = _resolve(url, json)
|
||||
return redirect(redirect_url)
|
||||
except DownloadError as e:
|
||||
msg = str(e)
|
||||
return text(msg, status=511)
|
||||
|
||||
|
||||
def _resolve(url, metadata):
|
||||
if not url:
|
||||
raise InvalidUsage('Invalid URL')
|
||||
|
||||
try:
|
||||
provider = metadata['provider']
|
||||
|
||||
with youtube_dl.YoutubeDL(opts) as ytdl:
|
||||
info = ytdl.extract_info(url, download=False)
|
||||
if provider == 'youtube':
|
||||
return _yt_choose_url(info, metadata)
|
||||
elif provider == 'vimeo':
|
||||
return _vimeo_choose_url(info, metadata)
|
||||
else:
|
||||
raise ValueError('undefined provider')
|
||||
except DownloadError:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise
|
||||
|
||||
|
||||
def _yt_choose_url(info, metadata):
|
||||
is_video = metadata['format'] == 'video'
|
||||
|
||||
# Filter formats by file extension
|
||||
ext = 'mp4' if is_video else 'm4a'
|
||||
fmt_list = [x for x in info['formats'] if x['ext'] == ext and 'acodec' in x and x['acodec'] != 'none']
|
||||
if not len(fmt_list):
|
||||
return info['url']
|
||||
|
||||
# Sort list by field (width for videos, file size for audio)
|
||||
sort_field = 'width' if is_video else 'filesize'
|
||||
# Sometime 'filesize' field can be None
|
||||
if not all(x[sort_field] is not None for x in fmt_list):
|
||||
sort_field = 'format_id'
|
||||
ordered = sorted(fmt_list, key=lambda x: x[sort_field], reverse=True)
|
||||
|
||||
# Choose an item depending on quality, better at the beginning
|
||||
is_high_quality = metadata['quality'] == 'high'
|
||||
item = ordered[0] if is_high_quality else ordered[-1]
|
||||
return item['url']
|
||||
|
||||
|
||||
def _vimeo_choose_url(info, metadata):
|
||||
# Query formats with 'extension' = mp4 and 'format_id' = http-1080p/http-720p/../http-360p
|
||||
fmt_list = [x for x in info['formats'] if x['ext'] == 'mp4' and x['format_id'].startswith('http-')]
|
||||
|
||||
ordered = sorted(fmt_list, key=lambda x: x['width'], reverse=True)
|
||||
is_high_quality = metadata['quality'] == 'high'
|
||||
item = ordered[0] if is_high_quality else ordered[-1]
|
||||
|
||||
return item['url']
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5002, workers=32)
|
@ -29,14 +29,6 @@ services:
|
||||
awslogs-group: "Prod"
|
||||
awslogs-stream: "Backend"
|
||||
awslogs-multiline-pattern: "{"
|
||||
ytdl:
|
||||
image: mxpv/ytdl
|
||||
container_name: ytdl
|
||||
restart: always
|
||||
ports:
|
||||
- 5002
|
||||
environment:
|
||||
- METADATA_URL=http://api:5001/api/metadata/{feed_id}
|
||||
redis:
|
||||
image: redis:5.0.3
|
||||
container_name: redis
|
||||
|
Reference in New Issue
Block a user