From cce361fe791a2d6ee7e43580d0a851594c28acc7 Mon Sep 17 00:00:00 2001 From: joyfuI Date: Thu, 19 Mar 2020 00:32:24 +0900 Subject: [PATCH] =?UTF-8?q?v1.4.0=20socketio=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ info.json | 2 +- logic_normal.py | 2 +- my_youtube_dl.py | 14 +++++++-- plugin.py | 23 ++++---------- templates/youtube-dl_list.html | 56 ++++++++++++++++++++++------------ 6 files changed, 61 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index e1c2bcc..545e22c 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,10 @@ API에선 직접 비트레이트를 설정할 수 있습니다. 물론 해당 정보가 없으면 null입니다. ## Changelog +v1.4.0 +* socketio 적용 + 목록 페이지에서 목록이 실시간으로 업데이트됩니다. + v1.3.5 v1.3.4 diff --git a/info.json b/info.json index cfd9e72..e9e9b59 100644 --- a/info.json +++ b/info.json @@ -1 +1 @@ -{"description": "\uc720\ud29c\ube0c, \ub124\uc774\ubc84TV \ub4f1 \ub3d9\uc601\uc0c1 \uc0ac\uc774\ud2b8\uc5d0\uc11c \ub3d9\uc601\uc0c1 \ub2e4\uc6b4\ub85c\ub4dc", "name": "youtube-dl", "more": "", "version": "1.3.5", "home": "https://github.com/joyfuI/youtube-dl", "category_name": "vod", "developer": "joyfuI"} \ No newline at end of file +{"description": "\uc720\ud29c\ube0c, \ub124\uc774\ubc84TV \ub4f1 \ub3d9\uc601\uc0c1 \uc0ac\uc774\ud2b8\uc5d0\uc11c \ub3d9\uc601\uc0c1 \ub2e4\uc6b4\ub85c\ub4dc", "name": "youtube-dl", "more": "", "version": "1.4.0", "home": "https://github.com/joyfuI/youtube-dl", "category_name": "vod", "developer": "joyfuI"} \ No newline at end of file diff --git a/logic_normal.py b/logic_normal.py index 2cf486f..89c98f5 100644 --- a/logic_normal.py +++ b/logic_normal.py @@ -109,7 +109,7 @@ class LogicNormal(object): @staticmethod def human_readable_size(size, suffix=''): - for unit in ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']: + for unit in ('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'): if size < 1024.0: return '%3.1f %s%s' % (size, unit, suffix) size /= 1024.0 diff --git a/my_youtube_dl.py b/my_youtube_dl.py index 0959b2c..d3ca305 100644 --- a/my_youtube_dl.py +++ b/my_youtube_dl.py @@ -58,7 +58,7 @@ class Youtube_dl(object): self.postprocessor = postprocessor self.index = Youtube_dl._index Youtube_dl._index += 1 - self.status = Status.READY + self._status = Status.READY self._thread = None self.key = None self.start_time = None # 시작 시간 @@ -87,6 +87,7 @@ class Youtube_dl(object): return True def run(self): + import glob2 try: self.start_time = datetime.now() self.status = Status.START @@ -112,7 +113,6 @@ class Youtube_dl(object): with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([self.url]) if self.status == Status.FINISHED: # 다운로드 성공 - import glob2 for i in glob2.glob(self.temp_path + '/**/*'): path = i.replace(self.temp_path, self.save_path, 1) if os.path.isdir(i): @@ -178,6 +178,16 @@ class Youtube_dl(object): self.thumbnail = info_dict['thumbnail'] return None + @property + def status(self): + return self._status + + @status.setter + def status(self, value): + from .plugin import socketio_emit + self._status = value + socketio_emit('status', self) + class MyLogger(object): def debug(self, msg): diff --git a/plugin.py b/plugin.py index 46a60ba..47c4766 100644 --- a/plugin.py +++ b/plugin.py @@ -34,7 +34,7 @@ menu = { } plugin_info = { - 'version': '1.3.5', + 'version': '1.4.0', 'name': 'youtube-dl', 'category_name': 'vod', 'developer': 'joyfuI', @@ -117,6 +117,7 @@ def ajax(sub): youtube_dl = Youtube_dl(package_name, url, filename, temp_path, save_path, format_code, postprocessor) LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가 youtube_dl.start() + socketio_emit('add', youtube_dl) return jsonify([]) elif sub == 'list': @@ -154,7 +155,7 @@ def api(sub): 'errorCode': 0, 'info_dict': None } - if None == url: + if None in (url,): return LogicNormal.abort(ret, 1) # 필수 요청 변수가 없음 if not url.startswith('http'): return LogicNormal.abort(ret, 2) # 잘못된 동영상 주소 @@ -204,6 +205,7 @@ def api(sub): ret['index'] = youtube_dl.index if start: youtube_dl.start() + socketio_emit('add', youtube_dl) return jsonify(ret) # 다운로드 시작을 요청하는 API @@ -283,18 +285,5 @@ def api(sub): ######################################################### # socketio ######################################################### -@socketio.on('connect', namespace='/%s' % package_name) -def connect(): - try: - logger.debug('socket_connect') - except Exception as e: - logger.error('Exception:%s', e) - logger.error(traceback.format_exc()) - -@socketio.on('disconnect', namespace='/%s' % package_name) -def disconnect(): - try: - logger.debug('socket_disconnect') - except Exception as e: - logger.error('Exception:%s', e) - logger.error(traceback.format_exc()) +def socketio_emit(cmd, data): + socketio.emit(cmd, LogicNormal.get_data(data), namespace='/%s' % package_name, broadcast=True) diff --git a/templates/youtube-dl_list.html b/templates/youtube-dl_list.html index c2043ba..52ad847 100644 --- a/templates/youtube-dl_list.html +++ b/templates/youtube-dl_list.html @@ -40,7 +40,15 @@ var package_name = '{{ arg["package_name"] }}'; $(function () { - // var socket = io.connect(location.origin + "/" + package_name); + var socket = io.connect(location.origin + "/" + package_name); + + socket.on('add', function (data) { + $('#list').append(make_item(data)); + }); + + socket.on('status', function (data) { + status_html(data); + }); $.ajax({ url: '/' + package_name + '/ajax/list', @@ -76,24 +84,8 @@ }); function make_item(data) { - var str = ''; - str += '' + (data.index + 1) + ''; - str += '' + data.plugin + ''; - str += '' + data.start_time + ''; - str += '' + data.extractor + ''; - str += '' + data.title + ''; - str += '' + data.status_ko + ''; - var visi = 'hidden'; - if (parseInt(data.percent) > 0 && data.status_str !== 'STOP') { - visi = 'visible'; - } - str += '
' + data.percent + '%
'; - str += '' + data.download_time + ''; - str += ''; - if (data.status_str === 'START' || data.status_str === 'DOWNLOADING' || data.status_str === 'FINISHED') { - str += ''; - } - str += ''; + var str = ''; + str += get_item(data); str += ''; str += ''; str += ''; @@ -105,6 +97,27 @@ return str; } + function get_item(data) { + var str = '' + (data.index + 1) + ''; + str += '' + data.plugin + ''; + str += '' + data.start_time + ''; + str += '' + data.extractor + ''; + str += '' + data.title + ''; + str += '' + data.status_ko + ''; + var visi = 'hidden'; + if (parseInt(data.percent) > 0 && data.status_str !== 'STOP') { + visi = 'visible'; + } + str += '
' + data.percent + '%
'; + str += '' + data.download_time + ''; + str += ''; + if (data.status_str === 'START' || data.status_str === 'DOWNLOADING' || data.status_str === 'FINISHED') { + str += ''; + } + str += ''; + return str; + } + function get_detail(data) { var str = info_html('URL', data.url, data.url); str += info_html('업로더', data.uploader, data.uploader_url); @@ -140,6 +153,11 @@ str += ''; return str; } + + function status_html(data) { + $('#item_' + data.index).html(get_item(data)); + $('#detail_' + data.index).html(get_detail(data)); + } {% endblock %} \ No newline at end of file