Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
d0u9 committed Sep 7, 2017
1 parent c07ebd9 commit bf99e57
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 67 deletions.
33 changes: 22 additions & 11 deletions youtube_dl_webui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from .utils import state_name
from .db import DataBase
from .utils import TaskInexistenceError
from .utils import TaskRunningError
from .utils import TaskExistenceError
from .utils import TaskPausedError
from .utils import TaskError
from .server import Server
from .worker import Worker

Expand All @@ -32,8 +31,8 @@ class WebMsgDispatcher(object):
TaskExistenceErrorMsg = {'status': 'error', 'errmsg': 'URL is already added'}
TaskInexistenceErrorMsg = {'status': 'error', 'errmsg': 'Task does not exist'}
UrlErrorMsg = {'status': 'error', 'errmsg': 'URL is invalid'}
InvalidStateMsg = {'status': 'error', 'errmsg': 'invalid query state'}
RequestErrorMsg = {'status': 'error', 'errmsg': 'request error'}
InvalidStateMsg = {'status': 'error', 'errmsg': 'Invalid query state'}
RequestErrorMsg = {'status': 'error', 'errmsg': 'Request error'}

_task_mgr = None
_conf = None
Expand Down Expand Up @@ -75,11 +74,19 @@ def event_manipulation(cls, svr, event, data, args):

ret_val = cls.RequestErrorMsg
if act == 'pause':
cls._task_mgr.pause_task(tid)
ret_val = cls.SuccessMsg
try:
cls._task_mgr.pause_task(tid)
except TaskError as e:
ret_val = {'status': 'error', 'errmsg': e.msg}
else:
ret_val = cls.SuccessMsg
elif act == 'resume':
cls._task_mgr.start_task(tid)
ret_val = cls.SuccessMsg
try:
cls._task_mgr.start_task(tid)
except TaskError as e:
ret_val = {'status': 'error', 'errmsg': e.msg}
else:
ret_val = cls.SuccessMsg

svr.put(ret_val)

Expand Down Expand Up @@ -144,20 +151,24 @@ def event_batch(cls, svr, event, data, arg):
try:
cls._task_mgr.pause_task(tid)
except TaskInexistenceError:
errors.append([tid, 'inexistence error'])
errors.append([tid, 'Inexistence error'])
except TaskError as e:
errors.append([tid, e.msg])
elif act == 'resume':
for tid in tids:
try:
cls._task_mgr.start_task(tid)
except TaskInexistenceError:
errors.append([tid, 'inexistence error'])
errors.append([tid, 'Inexistence error'])
except TaskError as e:
errors.append([tid, e.msg])
elif act == 'delete':
del_file = True if detail.get('del_file', 'false') == 'true' else False
for tid in tids:
try:
cls._task_mgr.delete_task(tid, del_file)
except TaskInexistenceError:
errors.append([tid, 'inexistence error'])
errors.append([tid, 'Inexistence error'])

if errors:
ret_val = {'status': 'success', 'detail': errors}
Expand Down
2 changes: 0 additions & 2 deletions youtube_dl_webui/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from .utils import state_index, state_name
from .utils import TaskExistenceError
from .utils import TaskInexistenceError
from .utils import TaskPausedError
from .utils import TaskRunningError
from .utils import url2tid

class DataBase(object):
Expand Down
27 changes: 17 additions & 10 deletions youtube_dl_webui/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

from .config import ydl_conf
from .utils import TaskInexistenceError
from .utils import TaskRunningError
from .utils import TaskExistenceError
from .utils import TaskPausedError
from .utils import TaskError
from .utils import url2tid
from .utils import state_index

Expand All @@ -26,6 +25,7 @@ def __init__(self, tid, msg_cli, ydl_opts={}, info={}, status={}, log_size=10):
self.ydl_opts = ydl_opts
self.ydl_conf = ydl_conf(ydl_opts)
self.info = info
self.url = info['url']
self.log = deque(maxlen=log_size)
self.msg_cli = msg_cli
self.touch = time()
Expand All @@ -38,7 +38,7 @@ def __init__(self, tid, msg_cli, ydl_opts={}, info={}, status={}, log_size=10):
self.log.appendleft(log)

def start(self):
self.logger.debug('Task starts, tid = %s' %(self.tid))
self.logger.info('Task starts, url - %s(%s)' %(self.url, self.tid))
tm = time()
self.state = state_index['downloading']

Expand All @@ -54,7 +54,7 @@ def start(self):
self.worker.start()

def pause(self):
self.logger.debug('Task pauses, tid = %s' %(self.tid))
self.logger.info('Task pauses, url - %s(%s)' %(self.url, self.tid))
tm = time()
self.state = state_index['paused']

Expand All @@ -66,7 +66,7 @@ def pause(self):
self.log.appendleft({'time': int(tm), 'type': 'debug', 'msg': 'Task pauses...'})

def halt(self):
self.logger.debug('Task halts, tid = %s' %(self.tid))
self.logger.info('Task halts, url - %s(%s)' %(self.url, self.tid))
tm = time()
self.state = state_index['invalid']

Expand All @@ -79,7 +79,7 @@ def halt(self):
self.log.appendleft({'time': int(tm), 'type': 'debug', 'msg': 'Task halts...'})

def finish(self):
self.logger.debug('Task finishes, tid = %s' %(self.tid))
self.logger.info('Task finishes, url - %s(%s)' %(self.url, self.tid))
tm = time()
self.state = state_index['finished']

Expand Down Expand Up @@ -134,6 +134,8 @@ def start_task(self, tid, ignore_state=False):
task = None
if tid in self._tasks_dict:
task = self._tasks_dict[tid]
if task.state == state_index['downloading']:
raise TaskError('Task is downloading')
else:
try:
ydl_opts = self._db.get_ydl_opts(tid)
Expand All @@ -156,9 +158,12 @@ def pause_task(self, tid):
self.logger.debug('task paused (%s)' %(tid))

if tid not in self._tasks_dict:
raise TaskInexistenceError('task does not exist')
raise TaskError('Task is finished or invalid or inexistent')

task = self._tasks_dict[tid]
if task.state == state_index['paused']:
raise TaskError('Task already paused')

task.pause()
self._db.pause_task(tid, pause_time=task.pause_time, elapsed=task.elapsed)
self._db.update_log(tid, task.log)
Expand All @@ -170,10 +175,10 @@ def finish_task(self, tid):
raise TaskInexistenceError('task does not exist')

task = self._tasks_dict[tid]
del self._tasks_dict[tid]
task.finish()
self._db.finish_task(tid, finish_time=task.finish_time, elapsed=task.elapsed)
self._db.update_log(tid, task.log)
del self._tasks_dict[tid]

def halt_task(self, tid):
self.logger.debug('task halted (%s)' %(tid))
Expand All @@ -182,10 +187,10 @@ def halt_task(self, tid):
raise TaskInexistenceError('task does not exist')

task = self._tasks_dict[tid]
del self._tasks_dict[tid]
task.halt()
self._db.halt_task(tid, finish_time=task.halt_time, elapsed=task.elapsed)
self._db.update_log(tid, task.log)
del self._tasks_dict[tid]

def delete_task(self, tid, del_file=False):
self.logger.debug('task deleted (%s)' %(tid))
Expand Down Expand Up @@ -243,7 +248,9 @@ def update_info(self, tid, info_dict):

def update_log(self, tid, log):
if tid not in self._tasks_dict:
raise TaskInexistenceError('task does not exist')
# raise TaskInexistenceError('task does not exist')
self.logger.error('Task does not active, tid=%s' %(tid))
return

task = self._tasks_dict[tid]
task.update_log(log)
Expand Down
44 changes: 1 addition & 43 deletions youtube_dl_webui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,14 @@ class YoutubeDLWebUI(Exception):
class TaskError(YoutubeDLWebUI):
"""Error related to download tasks."""
def __init__(self, msg, tid=None):
if tid:
msg += ' tid={}'.format(tid)
if tid: msg += ' tid={}'.format(tid)

super(TaskError, self).__init__(msg)
self.msg = msg

def __str__(self):
return repr(self.msg)

class TaskPausedError(TaskError):
def __init__(self, msg, tid=None, url=None, state=None):
msg = 'Task running error'
if tid:
msg += ' tid={}'.format(tid)
if url:
msg += ' url={}'.format(url)
if state:
msg += ' state={}'.format(state)

super(TaskPausedError, self).__init__(msg)
self.msg = msg


class TaskRunningError(TaskError):
def __init__(self, msg, tid=None, url=None, state=None):
msg = 'Task running error'
if tid:
msg += ' tid={}'.format(tid)
if url:
msg += ' url={}'.format(url)
if state:
msg += ' state={}'.format(state)

super(TaskRunningError, self).__init__(msg)
self.msg = msg


class TaskFinishedError(TaskError):
def __init__(self, msg, tid=None, url=None, state=None):
msg = 'Task already finished'
if tid:
msg += ' tid={}'.format(tid)
if url:
msg += ' url={}'.format(url)
if state:
msg += ' state={}'.format(state)

super(TaskFinishedError, self).__init__(msg)
self.msg = msg


class TaskInexistenceError(TaskError):
def __init__(self, msg, tid=None, url=None, state=None):
Expand Down
2 changes: 1 addition & 1 deletion youtube_dl_webui/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def run(self):
payload = {'tid': self.tid, 'data': info_dict}
self.msg_cli.put('info_dict', payload)

self.logger.info('start downloading ...')
self.logger.info('start downloading, url - %s' %(self.url))
ydl.download([self.url])
except DownloadError as e:
# url error
Expand Down

0 comments on commit bf99e57

Please sign in to comment.