2020-03-15 15:29:32 +09:00
|
|
|
import traceback
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from flask import jsonify
|
|
|
|
|
|
2021-05-02 00:36:04 +09:00
|
|
|
from framework.logger import get_logger
|
|
|
|
|
|
2020-07-25 03:06:03 +09:00
|
|
|
from .my_youtube_dl import MyYoutubeDL, Status
|
2021-02-11 18:08:47 +09:00
|
|
|
|
2022-04-30 18:57:23 +09:00
|
|
|
package_name = __name__.split(".", maxsplit=1)[0]
|
2021-05-02 00:36:04 +09:00
|
|
|
logger = get_logger(package_name)
|
|
|
|
|
|
2020-03-15 15:29:32 +09:00
|
|
|
|
|
|
|
|
class LogicNormal(object):
|
2020-07-23 23:00:02 +09:00
|
|
|
youtube_dl_list = []
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-11-15 14:06:33 +09:00
|
|
|
@staticmethod
|
2021-01-22 14:56:53 +09:00
|
|
|
def get_youtube_dl_package(index=None, import_pkg=False):
|
2022-04-30 18:57:23 +09:00
|
|
|
packages = ["youtube-dl", "yt-dlp"]
|
|
|
|
|
import_name = ["youtube_dl", "yt_dlp"]
|
2021-01-22 14:56:53 +09:00
|
|
|
if import_pkg:
|
|
|
|
|
return import_name if index is None else import_name[int(index)]
|
2020-11-15 14:06:33 +09:00
|
|
|
else:
|
2021-01-22 14:56:53 +09:00
|
|
|
return packages if index is None else packages[int(index)]
|
2020-11-15 14:06:33 +09:00
|
|
|
|
2020-07-25 03:06:03 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_youtube_dl_version():
|
2021-02-26 00:08:01 +09:00
|
|
|
try:
|
|
|
|
|
return MyYoutubeDL.get_version()
|
2022-05-05 21:19:57 +09:00
|
|
|
except Exception as error:
|
|
|
|
|
logger.error("Exception:%s", error)
|
2021-02-26 00:08:01 +09:00
|
|
|
logger.error(traceback.format_exc())
|
2022-04-30 18:57:23 +09:00
|
|
|
return "패키지 임포트 실패"
|
2020-07-25 03:06:03 +09:00
|
|
|
|
2020-08-08 15:33:14 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_default_filename():
|
|
|
|
|
return MyYoutubeDL.DEFAULT_FILENAME
|
|
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_preset_list():
|
2020-07-25 03:06:03 +09:00
|
|
|
return [
|
2022-04-30 18:57:23 +09:00
|
|
|
["bestvideo+bestaudio/best", "최고 화질"],
|
|
|
|
|
["bestvideo[height<=1080]+bestaudio/best[height<=1080]", "1080p"],
|
|
|
|
|
["worstvideo+worstaudio/worst", "최저 화질"],
|
|
|
|
|
["bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]", "최고 화질(mp4)"],
|
|
|
|
|
[
|
|
|
|
|
"bestvideo[ext=mp4][height<=1080]+bestaudio[ext=m4a]/best[ext=mp4][height<=1080]",
|
|
|
|
|
"1080p(mp4)",
|
|
|
|
|
],
|
|
|
|
|
["bestvideo[filesize<50M]+bestaudio/best[filesize<50M]", "50MB 미만"],
|
|
|
|
|
["bestaudio/best", "오디오만"],
|
|
|
|
|
["_custom", "사용자 정의"],
|
2020-07-23 23:00:02 +09:00
|
|
|
]
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_postprocessor_list():
|
2020-07-25 03:06:03 +09:00
|
|
|
return [
|
2022-04-30 18:57:23 +09:00
|
|
|
["", "후처리 안함", None],
|
|
|
|
|
["mp4", "MP4", "비디오 변환"],
|
|
|
|
|
["flv", "FLV", "비디오 변환"],
|
|
|
|
|
["webm", "WebM", "비디오 변환"],
|
|
|
|
|
["ogg", "Ogg", "비디오 변환"],
|
|
|
|
|
["mkv", "MKV", "비디오 변환"],
|
|
|
|
|
["ts", "TS", "비디오 변환"],
|
|
|
|
|
["avi", "AVI", "비디오 변환"],
|
|
|
|
|
["wmv", "WMV", "비디오 변환"],
|
|
|
|
|
["mov", "MOV", "비디오 변환"],
|
|
|
|
|
["gif", "GIF", "비디오 변환"],
|
|
|
|
|
["mp3", "MP3", "오디오 추출"],
|
|
|
|
|
["aac", "AAC", "오디오 추출"],
|
|
|
|
|
["flac", "FLAC", "오디오 추출"],
|
|
|
|
|
["m4a", "M4A", "오디오 추출"],
|
|
|
|
|
["opus", "Opus", "오디오 추출"],
|
|
|
|
|
["vorbis", "Vorbis", "오디오 추출"],
|
|
|
|
|
["wav", "WAV", "오디오 추출"],
|
2020-07-23 23:00:02 +09:00
|
|
|
]
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_postprocessor():
|
|
|
|
|
video_convertor = []
|
|
|
|
|
extract_audio = []
|
|
|
|
|
for i in LogicNormal.get_postprocessor_list():
|
2022-04-30 18:57:23 +09:00
|
|
|
if i[2] == "비디오 변환":
|
2020-07-23 23:00:02 +09:00
|
|
|
video_convertor.append(i[0])
|
2022-04-30 18:57:23 +09:00
|
|
|
elif i[2] == "오디오 추출":
|
2020-07-23 23:00:02 +09:00
|
|
|
extract_audio.append(i[0])
|
|
|
|
|
return video_convertor, extract_audio
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-07-25 03:06:03 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def download(**kwagrs):
|
2020-11-08 22:58:35 +09:00
|
|
|
try:
|
|
|
|
|
logger.debug(kwagrs)
|
2022-04-30 18:57:23 +09:00
|
|
|
plugin = kwagrs["plugin"]
|
|
|
|
|
url = kwagrs["url"]
|
|
|
|
|
filename = kwagrs["filename"]
|
|
|
|
|
temp_path = kwagrs["temp_path"]
|
|
|
|
|
save_path = kwagrs["save_path"]
|
2020-11-08 22:58:35 +09:00
|
|
|
opts = {}
|
2022-04-30 18:57:23 +09:00
|
|
|
if "format" in kwagrs and kwagrs["format"]:
|
|
|
|
|
opts["format"] = kwagrs["format"]
|
2020-11-08 22:58:35 +09:00
|
|
|
postprocessor = []
|
2022-04-30 18:57:23 +09:00
|
|
|
if "preferedformat" in kwagrs and kwagrs["preferedformat"]:
|
|
|
|
|
postprocessor.append(
|
|
|
|
|
{
|
|
|
|
|
"key": "FFmpegVideoConvertor",
|
|
|
|
|
"preferedformat": kwagrs["preferedformat"],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
if "preferredcodec" in kwagrs and kwagrs["preferredcodec"]:
|
|
|
|
|
postprocessor.append(
|
|
|
|
|
{
|
|
|
|
|
"key": "FFmpegExtractAudio",
|
|
|
|
|
"preferredcodec": kwagrs["preferredcodec"],
|
|
|
|
|
"preferredquality": str(kwagrs["preferredquality"]),
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-11-08 22:58:35 +09:00
|
|
|
if postprocessor:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["postprocessors"] = postprocessor
|
|
|
|
|
if "playlist" in kwagrs and kwagrs["playlist"]:
|
|
|
|
|
if kwagrs["playlist"] == "reverse":
|
|
|
|
|
opts["playlistreverse"] = True
|
|
|
|
|
elif kwagrs["playlist"] == "random":
|
|
|
|
|
opts["playlistrandom"] = True
|
2021-04-28 20:45:42 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["playlist_items"] = kwagrs["playlist"]
|
|
|
|
|
if "archive" in kwagrs and kwagrs["archive"]:
|
|
|
|
|
opts["download_archive"] = kwagrs["archive"]
|
|
|
|
|
if "proxy" in kwagrs and kwagrs["proxy"]:
|
|
|
|
|
opts["proxy"] = kwagrs["proxy"]
|
|
|
|
|
if "ffmpeg_path" in kwagrs and kwagrs["ffmpeg_path"]:
|
|
|
|
|
opts["ffmpeg_location"] = kwagrs["ffmpeg_path"]
|
|
|
|
|
if "cookiefile" in kwagrs and kwagrs["cookiefile"]:
|
|
|
|
|
opts["cookiefile"] = kwagrs["cookiefile"]
|
|
|
|
|
dateafter = kwagrs.get("dateafter")
|
|
|
|
|
youtube_dl = MyYoutubeDL(
|
|
|
|
|
plugin, "video", url, filename, temp_path, save_path, opts, dateafter
|
|
|
|
|
)
|
|
|
|
|
youtube_dl.key = kwagrs.get("key")
|
2021-04-25 17:27:27 +09:00
|
|
|
LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가
|
|
|
|
|
return youtube_dl
|
2022-05-05 21:19:57 +09:00
|
|
|
except Exception as error:
|
|
|
|
|
logger.error("Exception:%s", error)
|
2021-04-25 17:27:27 +09:00
|
|
|
logger.error(traceback.format_exc())
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def thumbnail(**kwagrs):
|
|
|
|
|
try:
|
|
|
|
|
logger.debug(kwagrs)
|
2022-04-30 18:57:23 +09:00
|
|
|
plugin = kwagrs["plugin"]
|
|
|
|
|
url = kwagrs["url"]
|
|
|
|
|
filename = kwagrs["filename"]
|
|
|
|
|
temp_path = kwagrs["temp_path"]
|
|
|
|
|
save_path = kwagrs["save_path"]
|
|
|
|
|
opts = {"skip_download": True}
|
|
|
|
|
if (
|
|
|
|
|
"all_thumbnails" in kwagrs
|
|
|
|
|
and str(kwagrs["all_thumbnails"]).lower() != "false"
|
|
|
|
|
):
|
|
|
|
|
opts["write_all_thumbnails"] = True
|
2021-04-25 17:27:27 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["writethumbnail"] = True
|
|
|
|
|
if "playlist" in kwagrs and kwagrs["playlist"]:
|
|
|
|
|
if kwagrs["playlist"] == "reverse":
|
|
|
|
|
opts["playlistreverse"] = True
|
|
|
|
|
elif kwagrs["playlist"] == "random":
|
|
|
|
|
opts["playlistrandom"] = True
|
2021-04-28 20:45:42 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["playlist_items"] = kwagrs["playlist"]
|
|
|
|
|
if "archive" in kwagrs and kwagrs["archive"]:
|
|
|
|
|
opts["download_archive"] = kwagrs["archive"]
|
|
|
|
|
if "proxy" in kwagrs and kwagrs["proxy"]:
|
|
|
|
|
opts["proxy"] = kwagrs["proxy"]
|
|
|
|
|
if "ffmpeg_path" in kwagrs and kwagrs["ffmpeg_path"]:
|
|
|
|
|
opts["ffmpeg_location"] = kwagrs["ffmpeg_path"]
|
|
|
|
|
if "cookiefile" in kwagrs and kwagrs["cookiefile"]:
|
|
|
|
|
opts["cookiefile"] = kwagrs["cookiefile"]
|
|
|
|
|
dateafter = kwagrs.get("dateafter")
|
|
|
|
|
youtube_dl = MyYoutubeDL(
|
|
|
|
|
plugin,
|
|
|
|
|
"thumbnail",
|
|
|
|
|
url,
|
|
|
|
|
filename,
|
|
|
|
|
temp_path,
|
|
|
|
|
save_path,
|
|
|
|
|
opts,
|
|
|
|
|
dateafter,
|
|
|
|
|
)
|
|
|
|
|
youtube_dl.key = kwagrs.get("key")
|
2020-11-08 22:58:35 +09:00
|
|
|
LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가
|
|
|
|
|
return youtube_dl
|
2022-05-05 21:19:57 +09:00
|
|
|
except Exception as error:
|
|
|
|
|
logger.error("Exception:%s", error)
|
2020-11-08 22:58:35 +09:00
|
|
|
logger.error(traceback.format_exc())
|
|
|
|
|
return None
|
2020-07-25 03:06:03 +09:00
|
|
|
|
2021-04-27 21:54:48 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def sub(**kwagrs):
|
|
|
|
|
try:
|
|
|
|
|
logger.debug(kwagrs)
|
2022-04-30 18:57:23 +09:00
|
|
|
plugin = kwagrs["plugin"]
|
|
|
|
|
url = kwagrs["url"]
|
|
|
|
|
filename = kwagrs["filename"]
|
|
|
|
|
temp_path = kwagrs["temp_path"]
|
|
|
|
|
save_path = kwagrs["save_path"]
|
|
|
|
|
opts = {"skip_download": True}
|
|
|
|
|
sub_lang = map(
|
|
|
|
|
lambda x: x.strip(), kwagrs["sub_lang"].split(",")
|
|
|
|
|
) # 문자열을 리스트로 변환
|
|
|
|
|
if "all_subs" in kwagrs and str(kwagrs["all_subs"]).lower() != "false":
|
|
|
|
|
opts["allsubtitles"] = True
|
2021-04-27 21:54:48 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["subtitleslangs"] = sub_lang
|
|
|
|
|
if "auto_sub" in kwagrs and str(kwagrs["auto_sub"]).lower() != "false":
|
|
|
|
|
opts["writeautomaticsub"] = True
|
2021-04-27 21:54:48 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["writesubtitles"] = True
|
|
|
|
|
if "playlist" in kwagrs and kwagrs["playlist"]:
|
|
|
|
|
if kwagrs["playlist"] == "reverse":
|
|
|
|
|
opts["playlistreverse"] = True
|
|
|
|
|
elif kwagrs["playlist"] == "random":
|
|
|
|
|
opts["playlistrandom"] = True
|
2021-04-28 20:45:42 +09:00
|
|
|
else:
|
2022-04-30 18:57:23 +09:00
|
|
|
opts["playlist_items"] = kwagrs["playlist"]
|
|
|
|
|
if "archive" in kwagrs and kwagrs["archive"]:
|
|
|
|
|
opts["download_archive"] = kwagrs["archive"]
|
|
|
|
|
if "proxy" in kwagrs and kwagrs["proxy"]:
|
|
|
|
|
opts["proxy"] = kwagrs["proxy"]
|
|
|
|
|
if "ffmpeg_path" in kwagrs and kwagrs["ffmpeg_path"]:
|
|
|
|
|
opts["ffmpeg_location"] = kwagrs["ffmpeg_path"]
|
|
|
|
|
if "cookiefile" in kwagrs and kwagrs["cookiefile"]:
|
|
|
|
|
opts["cookiefile"] = kwagrs["cookiefile"]
|
|
|
|
|
dateafter = kwagrs.get("dateafter")
|
|
|
|
|
youtube_dl = MyYoutubeDL(
|
|
|
|
|
plugin, "subtitle", url, filename, temp_path, save_path, opts, dateafter
|
|
|
|
|
)
|
|
|
|
|
youtube_dl.key = kwagrs.get("key")
|
2021-04-27 21:54:48 +09:00
|
|
|
LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가
|
|
|
|
|
return youtube_dl
|
2022-05-05 21:19:57 +09:00
|
|
|
except Exception as error:
|
|
|
|
|
logger.error("Exception:%s", error)
|
2021-04-27 21:54:48 +09:00
|
|
|
logger.error(traceback.format_exc())
|
|
|
|
|
return None
|
|
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_data(youtube_dl):
|
|
|
|
|
try:
|
|
|
|
|
data = {}
|
2022-04-30 18:57:23 +09:00
|
|
|
data["plugin"] = youtube_dl.plugin
|
|
|
|
|
data["url"] = youtube_dl.url
|
|
|
|
|
data["filename"] = youtube_dl.filename
|
|
|
|
|
data["temp_path"] = youtube_dl.temp_path
|
|
|
|
|
data["save_path"] = youtube_dl.save_path
|
|
|
|
|
data["index"] = youtube_dl.index
|
|
|
|
|
data["status_str"] = youtube_dl.status.name
|
|
|
|
|
data["status_ko"] = str(youtube_dl.status)
|
|
|
|
|
data["end_time"] = ""
|
|
|
|
|
data["extractor"] = youtube_dl.type + (
|
|
|
|
|
" - " + youtube_dl.info_dict["extractor"]
|
|
|
|
|
if youtube_dl.info_dict["extractor"] is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
data["title"] = (
|
|
|
|
|
youtube_dl.info_dict["title"]
|
|
|
|
|
if youtube_dl.info_dict["title"] is not None
|
|
|
|
|
else youtube_dl.url
|
|
|
|
|
)
|
|
|
|
|
data["uploader"] = (
|
|
|
|
|
youtube_dl.info_dict["uploader"]
|
|
|
|
|
if youtube_dl.info_dict["uploader"] is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
data["uploader_url"] = (
|
|
|
|
|
youtube_dl.info_dict["uploader_url"]
|
|
|
|
|
if youtube_dl.info_dict["uploader_url"] is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
data["downloaded_bytes_str"] = ""
|
|
|
|
|
data["total_bytes_str"] = ""
|
|
|
|
|
data["percent"] = "0"
|
|
|
|
|
data["eta"] = (
|
|
|
|
|
youtube_dl.progress_hooks["eta"]
|
|
|
|
|
if youtube_dl.progress_hooks["eta"] is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
data["speed_str"] = (
|
|
|
|
|
LogicNormal.human_readable_size(
|
|
|
|
|
youtube_dl.progress_hooks["speed"], "/s"
|
|
|
|
|
)
|
|
|
|
|
if youtube_dl.progress_hooks["speed"] is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
2021-02-11 18:08:47 +09:00
|
|
|
if youtube_dl.status == Status.READY: # 다운로드 전
|
2022-04-30 18:57:23 +09:00
|
|
|
data["start_time"] = ""
|
|
|
|
|
data["download_time"] = ""
|
2020-07-23 23:00:02 +09:00
|
|
|
else:
|
2021-02-11 18:08:47 +09:00
|
|
|
if youtube_dl.end_time is None: # 완료 전
|
2020-07-23 23:00:02 +09:00
|
|
|
download_time = datetime.now() - youtube_dl.start_time
|
|
|
|
|
else:
|
|
|
|
|
download_time = youtube_dl.end_time - youtube_dl.start_time
|
2022-04-30 18:57:23 +09:00
|
|
|
data["end_time"] = youtube_dl.end_time.strftime("%m-%d %H:%M:%S")
|
|
|
|
|
if None not in (
|
|
|
|
|
youtube_dl.progress_hooks["downloaded_bytes"],
|
|
|
|
|
youtube_dl.progress_hooks["total_bytes"],
|
|
|
|
|
): # 둘 다 값이 있으면
|
|
|
|
|
data["downloaded_bytes_str"] = LogicNormal.human_readable_size(
|
|
|
|
|
youtube_dl.progress_hooks["downloaded_bytes"]
|
2021-02-11 18:08:47 +09:00
|
|
|
)
|
2022-04-30 18:57:23 +09:00
|
|
|
data["total_bytes_str"] = LogicNormal.human_readable_size(
|
|
|
|
|
youtube_dl.progress_hooks["total_bytes"]
|
|
|
|
|
)
|
|
|
|
|
data[
|
|
|
|
|
"percent"
|
2022-05-05 21:43:57 +09:00
|
|
|
] = f"{(float(youtube_dl.progress_hooks['downloaded_bytes']) / float(youtube_dl.progress_hooks['total_bytes']) * 100):.2f}"
|
2022-04-30 18:57:23 +09:00
|
|
|
data["start_time"] = youtube_dl.start_time.strftime("%m-%d %H:%M:%S")
|
|
|
|
|
data[
|
|
|
|
|
"download_time"
|
2022-05-05 21:43:57 +09:00
|
|
|
] = f"{int(download_time.seconds / 60):02d}:{int(download_time.seconds % 60):02d}"
|
2020-07-23 23:00:02 +09:00
|
|
|
return data
|
2022-05-05 21:19:57 +09:00
|
|
|
except Exception as error:
|
|
|
|
|
logger.error("Exception:%s", error)
|
2020-07-23 23:00:02 +09:00
|
|
|
logger.error(traceback.format_exc())
|
|
|
|
|
return None
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-07-25 03:06:03 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def get_info_dict(url, proxy):
|
2020-08-01 16:38:13 +09:00
|
|
|
return MyYoutubeDL.get_info_dict(url, proxy)
|
2020-07-25 03:06:03 +09:00
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
2022-04-30 18:57:23 +09:00
|
|
|
def human_readable_size(size, suffix=""):
|
|
|
|
|
for unit in ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB"):
|
2020-07-23 23:00:02 +09:00
|
|
|
if size < 1024.0:
|
2022-04-30 18:57:23 +09:00
|
|
|
return f"{size:3.1f} {unit}{suffix}"
|
2020-07-23 23:00:02 +09:00
|
|
|
size /= 1024.0
|
2022-04-30 18:57:23 +09:00
|
|
|
return f"{size:.1f} YB{suffix}"
|
2020-03-15 15:29:32 +09:00
|
|
|
|
2020-07-23 23:00:02 +09:00
|
|
|
@staticmethod
|
|
|
|
|
def abort(base, code):
|
2022-04-30 18:57:23 +09:00
|
|
|
base["errorCode"] = code
|
2020-07-23 23:00:02 +09:00
|
|
|
return jsonify(base)
|