From 33bfe6f4ee51a9b66b1b902ac70706903258c7ee Mon Sep 17 00:00:00 2001 From: soju6jan Date: Mon, 7 Dec 2020 15:07:05 +0900 Subject: [PATCH] =?UTF-8?q?API=EB=A1=9C=20headers,=20cookiefile=20?= =?UTF-8?q?=EC=A0=84=EB=8B=AC=20=EB=B0=A9=EB=B2=95=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?(#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * API로 headers, cookiefile 전달 방법 추가 * API로 headers, cookiefile 전달 방법 추가 Co-authored-by: joyfuI --- logic_normal.py | 4 +++- my_youtube_dl.py | 18 ++++++++++++++---- plugin.py | 13 ++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/logic_normal.py b/logic_normal.py index cd20bb5..3782ab2 100644 --- a/logic_normal.py +++ b/logic_normal.py @@ -111,7 +111,9 @@ class LogicNormal(object): if 'ffmpeg_path' in kwagrs and kwagrs['ffmpeg_path']: opts['ffmpeg_location'] = kwagrs['ffmpeg_path'] dateafter = kwagrs.get('dateafter') - youtube_dl = MyYoutubeDL(plugin, url, filename, temp_path, save_path, opts, dateafter) + # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 + youtube_dl = MyYoutubeDL(plugin, url, filename, temp_path, save_path, opts, dateafter, headers=kwagrs['headers'], cookiefile=kwagrs['cookiefile']) + # by soju6jan youtube_dl.key = kwagrs.get('key') LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가 return youtube_dl diff --git a/my_youtube_dl.py b/my_youtube_dl.py index fc56cfb..07d4652 100644 --- a/my_youtube_dl.py +++ b/my_youtube_dl.py @@ -45,7 +45,7 @@ class MyYoutubeDL(object): __index = 0 _last_msg = '' - def __init__(self, plugin, url, filename, temp_path, save_path=None, opts=None, dateafter=None, datebefore=None): + def __init__(self, plugin, url, filename, temp_path, save_path=None, opts=None, dateafter=None, datebefore=None, headers=None, cookiefile=None): # from youtube_dl.utils import DateRange from .plugin import YOUTUBE_DL_PACKAGE DateRange = __import__('%s.utils' % YOUTUBE_DL_PACKAGE, fromlist=['DateRange']).DateRange @@ -92,6 +92,9 @@ class MyYoutubeDL(object): 'eta': None, # 예상 시간(s) 'speed': None # 다운로드 속도(bytes/s) } + # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 + self.headers = headers + self.cookiefile = cookiefile def start(self): if self.status != Status.READY: @@ -109,8 +112,12 @@ class MyYoutubeDL(object): try: self.start_time = datetime.now() self.status = Status.START + # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 + # headers는 전역으로 계속 사용하기 때문에 매번 세팅. + youtube_dl_utils = __import__('%s.utils' % YOUTUBE_DL_PACKAGE) + youtube_dl_utils.std_headers = {} if self.headers is None else self.headers # 동영상 정보 가져오기 - info_dict = MyYoutubeDL.get_info_dict(self.url, self.opts.get('proxy')) + info_dict = MyYoutubeDL.get_info_dict(self.url, self.opts.get('proxy'), cookiefile=self.cookiefile) if info_dict is None: self.status = Status.ERROR return @@ -126,6 +133,8 @@ class MyYoutubeDL(object): 'ignoreerrors': True, 'cachedir': False } + if self.cookiefile: + ydl_opts['cookiefile'] = self.cookiefile ydl_opts.update(self.opts) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([self.url]) @@ -164,11 +173,10 @@ class MyYoutubeDL(object): return __version__ @staticmethod - def get_info_dict(url, proxy=None): + def get_info_dict(url, proxy=None, cookiefile=None): # import youtube_dl from .plugin import YOUTUBE_DL_PACKAGE youtube_dl = __import__('%s' % YOUTUBE_DL_PACKAGE) - try: ydl_opts = { 'simulate': True, @@ -178,6 +186,8 @@ class MyYoutubeDL(object): } if proxy: ydl_opts['proxy'] = proxy + if cookiefile: + ydl_opts['cookiefile'] = cookiefile with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) except Exception as e: diff --git a/plugin.py b/plugin.py index 41392c2..cf5e3b5 100644 --- a/plugin.py +++ b/plugin.py @@ -209,6 +209,14 @@ def api(sub): dateafter = request.values.get('dateafter', None) archive = request.values.get('archive', None) start = request.values.get('start', False) + # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 + headers = None + tmp_headers = request.values.get('headers', None) + if tmp_headers is not None: + import json + headers = json.loads(tmp_headers) # header는 json.dumps로 넘어오는 것으로 함. unqoute 등을 해야하는지 고려해야함. + cookiefile = request.values.get('cookiefile', None) + # by soju6jan ret = { 'errorCode': 0, 'index': None @@ -234,7 +242,10 @@ def api(sub): archive=archive, proxy=ModelSetting.get('proxy'), ffmpeg_path=ModelSetting.get('ffmpeg_path'), - key=key) + key=key, + # 2020-12-06 by soju6jan. + headers=headers, + cookiefile=cookiefile) if youtube_dl is None: return LogicNormal.abort(ret, 10) # 실패 ret['index'] = youtube_dl.index