API로 headers, cookiefile 전달 방법 추가 (#5)
* API로 headers, cookiefile 전달 방법 추가 * API로 headers, cookiefile 전달 방법 추가 Co-authored-by: joyfuI <jong970105@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
13
plugin.py
13
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
|
||||
|
||||
Reference in New Issue
Block a user