mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Rework project structure
This commit is contained in:
83
cmd/app/main.go
Normal file
83
cmd/app/main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/mxpv/podsync/pkg/api"
|
||||
"github.com/mxpv/podsync/pkg/builders"
|
||||
"github.com/mxpv/podsync/pkg/config"
|
||||
"github.com/mxpv/podsync/pkg/feeds"
|
||||
"github.com/mxpv/podsync/pkg/id"
|
||||
"github.com/mxpv/podsync/pkg/server"
|
||||
"github.com/mxpv/podsync/pkg/storage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
stop := make(chan os.Signal)
|
||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Create core sevices
|
||||
|
||||
cfg, err := config.ReadConfiguration()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
hashIds, err := id.NewIdGenerator()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
redis, err := storage.NewRedisStorage(cfg.RedisURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Builders
|
||||
|
||||
youtube, err := builders.NewYouTubeBuilder(cfg.YouTubeApiKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
vimeo, err := builders.NewVimeoBuilder(ctx, cfg.VimeoApiKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
feed := feeds.NewFeedService(
|
||||
feeds.WithIdGen(hashIds),
|
||||
feeds.WithStorage(redis),
|
||||
feeds.WithBuilder(api.Youtube, youtube),
|
||||
feeds.WithBuilder(api.Vimeo, vimeo),
|
||||
)
|
||||
|
||||
srv := http.Server{
|
||||
Addr: fmt.Sprintf(":%d", 8080),
|
||||
Handler: server.MakeHandlers(feed),
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Println("running listener")
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-stop
|
||||
|
||||
log.Printf("shutting down server")
|
||||
|
||||
srv.Shutdown(ctx)
|
||||
|
||||
log.Printf("server gracefully stopped")
|
||||
}
|
3
cmd/nginx/Dockerfile
Normal file
3
cmd/nginx/Dockerfile
Normal file
@@ -0,0 +1,3 @@
|
||||
FROM nginx
|
||||
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
21
cmd/nginx/nginx.conf
Normal file
21
cmd/nginx/nginx.conf
Normal file
@@ -0,0 +1,21 @@
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
gzip on;
|
||||
server_tokens off;
|
||||
|
||||
location /download {
|
||||
proxy_pass http://ytdl:5002;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://app:5001;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection keep-alive;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
}
|
||||
}
|
12
cmd/ytdl/Dockerfile
Normal file
12
cmd/ytdl/Dockerfile
Normal file
@@ -0,0 +1,12 @@
|
||||
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"]
|
3
cmd/ytdl/requirements.txt
Normal file
3
cmd/ytdl/requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
youtube_dl
|
||||
sanic
|
||||
redis
|
115
cmd/ytdl/ytdl.py
Normal file
115
cmd/ytdl/ytdl.py
Normal file
@@ -0,0 +1,115 @@
|
||||
import youtube_dl
|
||||
import redis
|
||||
import os
|
||||
from youtube_dl.utils import DownloadError
|
||||
from sanic import Sanic
|
||||
from sanic.exceptions import InvalidUsage, NotFound
|
||||
from sanic.response import text, redirect
|
||||
from datetime import timedelta
|
||||
|
||||
app = Sanic()
|
||||
|
||||
db = redis.from_url(os.getenv('REDIS_CONNECTION_STRING', 'redis://localhost:6379'))
|
||||
db.ping()
|
||||
|
||||
default_opts = {
|
||||
'quiet': True,
|
||||
'no_warnings': True,
|
||||
'forceurl': True,
|
||||
'simulate': True,
|
||||
'skip_download': True,
|
||||
'call_home': False,
|
||||
'nocheckcertificate': True
|
||||
}
|
||||
|
||||
youtube_quality = {
|
||||
'videohigh': 'best[ext=mp4]',
|
||||
'videolow': 'worst[ext=mp4]',
|
||||
'audiohigh': 'bestaudio[ext=m4a]/worstaudio[ext=m4a]',
|
||||
'audiolow': 'worstaudio[ext=m4a]/bestaudio[ext=m4a]'
|
||||
}
|
||||
|
||||
vimeo_quality = {
|
||||
'videohigh': 'Original/http-1080p/http-720p/http-360p/http-270p',
|
||||
'videolow': 'http-270p/http-360p/http-540p/http-720p/http-1080p'
|
||||
}
|
||||
|
||||
url_formats = {
|
||||
'youtube': 'https://youtube.com/watch?v={}',
|
||||
'vimeo': 'https://vimeo.com/{}',
|
||||
}
|
||||
|
||||
|
||||
@app.route('/download/<feed_id>/<video_id>', methods=['GET'])
|
||||
async def download(request, 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')
|
||||
|
||||
# Query redis
|
||||
data = db.hgetall(feed_id)
|
||||
if not data:
|
||||
raise NotFound('Feed not found')
|
||||
|
||||
# Delete this feed if no requests within 90 days
|
||||
db.expire(feed_id, timedelta(days=90))
|
||||
|
||||
entries = {k.decode().lower(): v.decode().lower() for k, v in data.items()}
|
||||
|
||||
# Build URL
|
||||
provider = entries.get('provider')
|
||||
tpl = url_formats[provider]
|
||||
if not tpl:
|
||||
raise InvalidUsage('Invalid feed')
|
||||
|
||||
url = tpl.format(video_id)
|
||||
quality = entries.get('quality')
|
||||
|
||||
try:
|
||||
redirect_url = _resolve(url, quality)
|
||||
return redirect(redirect_url)
|
||||
except DownloadError as e:
|
||||
msg = str(e)
|
||||
return text(msg, status=511)
|
||||
|
||||
|
||||
def _resolve(url, quality):
|
||||
if not url:
|
||||
raise InvalidUsage('Invalid URL')
|
||||
|
||||
if not quality:
|
||||
quality = 'videohigh'
|
||||
|
||||
opts = default_opts.copy()
|
||||
fmt = _choose_format(quality, url)
|
||||
|
||||
if fmt:
|
||||
opts.update(format=fmt)
|
||||
|
||||
try:
|
||||
with youtube_dl.YoutubeDL(opts) as ytdl:
|
||||
info = ytdl.extract_info(url, download=False)
|
||||
return info['url']
|
||||
except DownloadError:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(e)
|
||||
raise
|
||||
|
||||
|
||||
def _choose_format(quality, url):
|
||||
fmt = None
|
||||
if 'youtube.com' in url:
|
||||
fmt = youtube_quality.get(quality)
|
||||
elif 'vimeo.com' in url:
|
||||
fmt = vimeo_quality.get(quality)
|
||||
|
||||
return fmt
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5002, workers=16)
|
Reference in New Issue
Block a user