improved error handling

pull/1/head
Alex 2019-12-06 16:30:07 +02:00
parent 91cee0339a
commit 3cf7d25f4c
3 changed files with 27 additions and 14 deletions

View File

@ -11,7 +11,7 @@ log = logging.getLogger('ytdl')
class DownloadInfo: class DownloadInfo:
def __init__(self, id, title, url): def __init__(self, id, title, url):
self.id, self.title, self.url = id, title, url self.id, self.title, self.url = id, title, url
self.status = self.percent = self.speed = self.eta = None self.status = self.msg = self.percent = self.speed = self.eta = None
class Download: class Download:
manager = None manager = None
@ -25,14 +25,19 @@ class Download:
self.loop = None self.loop = None
def _download(self): def _download(self):
youtube_dl.YoutubeDL(params={ try:
'quiet': True, ret = youtube_dl.YoutubeDL(params={
'no_color': True, 'quiet': True,
#'skip_download': True, 'no_color': True,
'outtmpl': os.path.join(self.download_dir, '%(title)s.%(ext)s'), #'skip_download': True,
'socket_timeout': 30, 'outtmpl': os.path.join(self.download_dir, '%(title)s.%(ext)s'),
'progress_hooks': [lambda d: self.status_queue.put(d)], 'cachedir': False,
}).download([self.info.url]) 'socket_timeout': 30,
'progress_hooks': [lambda d: self.status_queue.put(d)],
}).download([self.info.url])
self.status_queue.put({'status': 'finished' if ret == 0 else 'error'})
except youtube_dl.utils.YoutubeDLError as exc:
self.status_queue.put({'status': 'error', 'msg': str(exc)})
async def start(self): async def start(self):
if Download.manager is None: if Download.manager is None:
@ -53,7 +58,10 @@ class Download:
self.status_queue.put(None) self.status_queue.put(None)
def running(self): def running(self):
return self.proc is not None and self.proc.is_alive() try:
return self.proc is not None and self.proc.is_alive()
except ValueError:
return False
async def update_status(self, updated_cb): async def update_status(self, updated_cb):
await updated_cb() await updated_cb()
@ -63,6 +71,7 @@ class Download:
return return
self.tmpfilename = status.get('tmpfilename') self.tmpfilename = status.get('tmpfilename')
self.info.status = status['status'] self.info.status = status['status']
self.info.msg = status.get('msg')
if 'downloaded_bytes' in status: if 'downloaded_bytes' in status:
total = status.get('total_bytes') or status.get('total_bytes_estimate') total = status.get('total_bytes') or status.get('total_bytes_estimate')
if total: if total:
@ -109,12 +118,15 @@ class DownloadQueue:
info = await asyncio.get_running_loop().run_in_executor(None, self.__extract_info, url) info = await asyncio.get_running_loop().run_in_executor(None, self.__extract_info, url)
except youtube_dl.utils.YoutubeDLError as exc: except youtube_dl.utils.YoutubeDLError as exc:
return {'status': 'error', 'msg': str(exc)} return {'status': 'error', 'msg': str(exc)}
if info.get('_type') == 'playlist': etype = info.get('_type') or 'video'
if etype == 'playlist':
entries = info['entries'] entries = info['entries']
log.info(f'playlist detected with {len(entries)} entries') log.info(f'playlist detected with {len(entries)} entries')
else: elif etype == 'video':
entries = [info] entries = [info]
log.info('single video detected') log.info('single video detected')
else:
return {'status': 'error', 'msg': f'Unsupported resource requested: {etype}, please enter video or playlist URLs only'}
for entry in entries: for entry in entries:
if entry['id'] not in self.queue: if entry['id'] not in self.queue:
dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url']) dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'])

View File

@ -81,11 +81,11 @@
<app-slave-checkbox [id]="download.key" [master]="doneMasterCheckbox" [checkable]="download.value"></app-slave-checkbox> <app-slave-checkbox [id]="download.key" [master]="doneMasterCheckbox" [checkable]="download.value"></app-slave-checkbox>
</td> </td>
<td> <td>
<div style="display: inline-block; width: 1.3rem;"> <div style="display: inline-block; width: 1.5rem;">
<fa-icon *ngIf="download.value.status == 'finished'" [icon]="faCheckCircle" style="color: green;"></fa-icon> <fa-icon *ngIf="download.value.status == 'finished'" [icon]="faCheckCircle" style="color: green;"></fa-icon>
<fa-icon *ngIf="download.value.status == 'error'" [icon]="faTimesCircle" style="color: red;"></fa-icon> <fa-icon *ngIf="download.value.status == 'error'" [icon]="faTimesCircle" style="color: red;"></fa-icon>
</div> </div>
{{ download.value.title }} <span ngbTooltip="{{download.value.msg}}">{{ download.value.title }}</span>
</td> </td>
<td><button type="button" class="btn btn-link" (click)="delDownload('done', download.key)"><fa-icon [icon]="faTrashAlt"></fa-icon></button></td> <td><button type="button" class="btn btn-link" (click)="delDownload('done', download.key)"><fa-icon [icon]="faTrashAlt"></fa-icon></button></td>
</tr> </tr>

View File

@ -14,6 +14,7 @@ interface Download {
title: string; title: string;
url: string, url: string,
status: string; status: string;
msg: string;
percent: number; percent: number;
speed: number; speed: number;
eta: number; eta: number;