From d8be81c466a08d8ec8e5c4e2077ee43270f37334 Mon Sep 17 00:00:00 2001 From: projectdx Date: Fri, 13 May 2022 20:34:11 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B8=ED=94=84=EB=9F=B0=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api_youtube_dl.py | 193 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 api_youtube_dl.py diff --git a/api_youtube_dl.py b/api_youtube_dl.py new file mode 100644 index 0000000..0c580fe --- /dev/null +++ b/api_youtube_dl.py @@ -0,0 +1,193 @@ +# 최종 업데이트 20210501 +import json +from datetime import datetime + +import requests + +from system.model import ModelSetting as SystemModelSetting + +HOST_URL = "http://localhost:%s" % SystemModelSetting.get("port") + + +class APIYoutubeDL(object): + ERROR_CODE = { + 0: "성공", + 1: "필수 요청 변수가 없음", + 2: "잘못된 동영상 주소", + 3: "인덱스 범위를 벗어남", + 4: "키 값이 일치하지 않음", + 5: "허용되지 않은 값이 있음", + 10: "실패", + } + + STATUS = { + "READY": "준비", + "START": "분석중", + "DOWNLOADING": "다운로드중", + "ERROR": "실패", + "FINISHED": "변환중", + "STOP": "중지", + "COMPLETED": "완료", + } + + @staticmethod + def info_dict(plugin, url): + data = {"plugin": plugin, "url": url} + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + + tmp = requests.post("%s/youtube-dl/api/info_dict" % HOST_URL, data=data) + # print(tmp) + # json.loads(tmp.text) + return requests.post("%s/youtube-dl/api/info_dict" % HOST_URL, data=data).json() + # return json.loads( + # requests.post("%s/youtube-dl/api/info_dict" % HOST_URL, data=data).text + # ) + + @staticmethod + def download( + plugin, + key, + url, + filename=None, + save_path=None, + format_code=None, + preferedformat=None, + preferredcodec=None, + preferredquality=None, + dateafter=None, + playlist=None, + archive=None, + start=None, + cookiefile=None, + ): + data = {"plugin": plugin, "key": key, "url": url} + if filename: + data["filename"] = filename + if save_path: + data["save_path"] = save_path + if format_code: + data["format"] = format_code + if preferedformat: + data["preferedformat"] = preferedformat + if preferredcodec: + data["preferredcodec"] = preferredcodec + if preferredquality: + data["preferredquality"] = preferredquality + if dateafter: + data["dateafter"] = dateafter + if playlist: + data["playlist"] = playlist + if archive: + data["archive"] = archive + if start: + data["start"] = start + if cookiefile: + data["cookiefile"] = cookiefile + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + return requests.post("%s/youtube-dl/api/download" % HOST_URL, data=data).json() + + @staticmethod + def thumbnail( + plugin, + key, + url, + filename=None, + save_path=None, + all_thumbnails=None, + dateafter=None, + playlist=None, + archive=None, + start=None, + cookiefile=None, + ): + data = {"plugin": plugin, "key": key, "url": url} + if filename: + data["filename"] = filename + if save_path: + data["save_path"] = save_path + if all_thumbnails: + data["all_thumbnails"] = all_thumbnails + if dateafter: + data["dateafter"] = dateafter + if playlist: + data["playlist"] = playlist + if archive: + data["archive"] = archive + if start: + data["start"] = start + if cookiefile: + data["cookiefile"] = cookiefile + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + return requests.post("%s/youtube-dl/api/thumbnail" % HOST_URL, data=data).json() + + @staticmethod + def sub( + plugin, + key, + url, + filename=None, + save_path=None, + all_subs=None, + sub_lang=None, + auto_sub=None, + dateafter=None, + playlist=None, + archive=None, + start=None, + cookiefile=None, + ): + data = {"plugin": plugin, "key": key, "url": url} + if filename: + data["filename"] = filename + if save_path: + data["save_path"] = save_path + if all_subs: + data["all_subs"] = all_subs + if sub_lang: + data["sub_lang"] = sub_lang + if auto_sub: + data["auto_sub"] = auto_sub + if dateafter: + data["dateafter"] = dateafter + if playlist: + data["playlist"] = playlist + if archive: + data["archive"] = archive + if start: + data["start"] = start + if cookiefile: + data["cookiefile"] = cookiefile + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + return requests.post("%s/youtube-dl/api/sub" % HOST_URL, data=data).json() + + @staticmethod + def start(plugin, index, key): + data = {"plugin": plugin, "index": index, "key": key} + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + return requests.post("%s/youtube-dl/api/start" % HOST_URL, data=data).json() + + @staticmethod + def stop(plugin, index, key): + data = {"plugin": plugin, "index": index, "key": key} + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + return requests.post("%s/youtube-dl/api/stop" % HOST_URL, data=data).json() + + @staticmethod + def status(plugin, index, key): + data = {"plugin": plugin, "index": index, "key": key} + if SystemModelSetting.get_bool("auth_use_apikey"): # APIKEY + data["apikey"] = SystemModelSetting.get("auth_apikey") + res = requests.post("%s/youtube-dl/api/status" % HOST_URL, data=data).json() + if res["start_time"]: + res["start_time"] = datetime.strptime( + res["start_time"], "%Y-%m-%dT%H:%M:%S" + ) + if res["end_time"]: + res["end_time"] = datetime.strptime(res["end_time"], "%Y-%m-%dT%H:%M:%S") + return res