def __init__(self, name: str, ydl_format: str = None, ydl_template: str = None): self.name = name if not ydl_format: ydl_format = conf.get('DEFAULT_YDL_FORMAT') self.ydl_format = ydl_format if not ydl_template: ydl_template = conf.get('DEFAULT_YDL_TEMPLATE') self.ydl_template = ydl_template
def get_download_paths(): paths = [] base_dir = conf.get('BASE_DOWNLOAD_PATH') base_length = len(base_dir) + 1 # strip leading slash for root, subdirs, files in os.walk(base_dir): paths.append(root[base_length:]) return paths
def add_download(): data = request.get_json() if not data: raise InvalidPayload(message='Payload missing') urls = data.get('url', '').split('\n') name = data.get('format') playlist = data.get('playlist', False) # Prepare the download path path = data.get('path') if path not in get_download_paths(): raise InvalidPayload(message='path is not valid') extendedPath = secure_filename(data.get('extendedPath', '')) if extendedPath: path = os.path.join(path, extendedPath) path = os.path.join(conf.get('BASE_DOWNLOAD_PATH'), path) if not os.path.exists(path): os.makedirs(path) df = DownloadFormat.query.filter_by(name=name).first() if df and urls and path: for url in urls: existing = Download.query.filter_by(url=url).first() if existing is None and url != '': # Save the download with session_scope(db.session) as session: dl = Download(url, path, playlist, df) session.add(dl) # Create the download task from project.tasks import ydl_download task = ydl_download.delay(dl.id) dl.task_id = task.id dl.save() return { 'status': 'success', 'message': 'Added' } raise InvalidPayload()
from datetime import datetime import click from flask.cli import AppGroup from project import conf, db from project.models import DownloadFormat from project.common.utils import session_scope cli = AppGroup('format', help='Commands for download formats.') @cli.command('add') @click.option('--name', prompt=True) @click.option('--ydl-format', prompt=True, default=conf.get('DEFAULT_YDL_FORMAT'), show_default=conf.get('DEFAULT_YDL_FORMAT')) @click.option('--ydl-template', prompt=True, default=conf.get('DEFAULT_YDL_TEMPLATE'), show_default=conf.get('DEFAULT_YDL_TEMPLATE')) def add(name, ydl_format, ydl_template): print(name, ydl_format, ydl_template) df = DownloadFormat(name=name, ydl_format=ydl_format, ydl_template=ydl_template) with session_scope(db.session) as session: session.add(df)
import pytest import urllib.parse import sys import subprocess from flask.cli import FlaskGroup #from flask_migrate import MigrateCommand from project import create_app, conf, db cli = FlaskGroup(create_app=create_app) @cli.command() @click.argument('queue', default=conf.get('CELERY_DEFAULT_QUEUE')) @click.option('--concurrency', '-c', default=1, type=int) def celery_worker(queue, concurrency): """Starts the celery worker.""" ret = subprocess.call([ 'celery', 'worker', '-A', 'project.celery', '-l', 'INFO', '-c', str(concurrency), '-Q', queue ]) sys.exit(ret) @cli.command() def celery_beat(): """Starts the celery task schedule.""" ret = subprocess.call( ['celery', 'beat', '-A', 'project.celery', '-l', 'INFO'])