Skip to content

Commit

Permalink
Change API of adding new task
Browse files Browse the repository at this point in the history
Instead of posting 'url' as a form filed, the new API needs browser to post a
json file, in which 'url' field is contained. Besides the 'url' field,
'ydl_opts` field is added to support setting youtube-dl options to a
specific task.
  • Loading branch information
d0u9 committed Sep 25, 2017
1 parent 22d783c commit c7f0d66
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
12 changes: 12 additions & 0 deletions youtube_dl_webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import json

from copy import deepcopy
from os.path import expanduser

class conf_base(object):
Expand Down Expand Up @@ -52,11 +53,22 @@ class ydl_conf(conf_base):
('format', None, 'string', None, None),
]

_task_settable_fields = set(['format'])

def __init__(self, conf_dict={}):
self.logger = logging.getLogger('ydl_webui')

super(ydl_conf, self).__init__(self._valid_fields, conf_dict)

def merge_conf(self, task_conf_dict={}):
ret = deepcopy(self.dict())
for key, val in task_conf_dict.items():
if key not in self._task_settable_fields:
continue
ret[key] = val

return ret


class svr_conf(conf_base):
_valid_fields = [
Expand Down
11 changes: 8 additions & 3 deletions youtube_dl_webui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ def init(cls, conf, task_mgr):

@classmethod
def event_create(cls, svr, event, data, args):
cls.logger.debug('url = %s' %(data['url']))
url, ydl_opts = data.get('url', None), data.get('ydl_opts', {})
cls.logger.debug('url = %s' %(url))

if url is None:
svr.put(cls.UrlErrorMsg)
return

try:
ydl_opts = cls._task_mgr.ydl_conf.dict()
tid = cls._task_mgr.new_task(data['url'], ydl_opts=ydl_opts)
tid = cls._task_mgr.new_task(url, ydl_opts=ydl_opts)
except TaskExistenceError:
svr.put(cls.TaskExistenceErrorMsg)
return
Expand Down
2 changes: 1 addition & 1 deletion youtube_dl_webui/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def delete_task(self, tid):
return dl_file

def query_task(self, tid):
self.db.execute('SELECT * FROM task_status, task_info WHERE task_status.tid=(?) and task_info.tid=(?)', (tid, tid))
self.db.execute('SELECT * FROM task_status, task_info, task_ydl_opt WHERE task_status.tid=(?) and task_info.tid=(?) and task_ydl_opt.tid=(?)', (tid, tid, tid))
row = self.db.fetchone()
if row is None:
raise TaskInexistenceError('')
Expand Down
2 changes: 1 addition & 1 deletion youtube_dl_webui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index():

@app.route('/task', methods=['POST'])
def add_task():
payload = {'url': request.form['url'], 'ydl_opts': {}}
payload = request.get_json()

MSG.put('create', payload)
return json.dumps(MSG.get())
Expand Down
6 changes: 4 additions & 2 deletions youtube_dl_webui/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def __init__(self, db, msg_cli, conf):
def new_task(self, url, ydl_opts={}):
"""Create a new task and put it in inactive type"""

return self._db.new_task(url, ydl_opts)
# stripe out necessary fields
ydl_opts = ydl_conf(ydl_opts)
return self._db.new_task(url, ydl_opts.dict())

def start_task(self, tid, ignore_state=False):
"""make an inactive type task into active type"""
Expand All @@ -138,7 +140,7 @@ def start_task(self, tid, ignore_state=False):
raise TaskError('Task is downloading')
else:
try:
ydl_opts = self._db.get_ydl_opts(tid)
ydl_opts = self.ydl_conf.merge_conf(self._db.get_ydl_opts(tid))
info = self._db.get_info(tid)
status = self._db.get_stat(tid)
except TaskInexistenceError as e:
Expand Down

0 comments on commit c7f0d66

Please sign in to comment.