API로 headers, cookiefile 전달 방법 추가 (#5)

* API로 headers, cookiefile 전달 방법 추가

* API로 headers, cookiefile 전달 방법 추가

Co-authored-by: joyfuI <jong970105@gmail.com>
This commit is contained in:
soju6jan
2020-12-07 15:07:05 +09:00
committed by GitHub
parent a7ee24d436
commit 33bfe6f4ee
3 changed files with 29 additions and 6 deletions

View File

@@ -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: