From 9f4bb00337eb07d35c75ca45bf12f391d9f1d812 Mon Sep 17 00:00:00 2001 From: CoolZxp <54017765+CoolZxp@users.noreply.github.com> Date: Sun, 6 Aug 2023 23:26:30 +0800 Subject: [PATCH] =?UTF-8?q?Youtube=E7=BC=93=E5=AD=98=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E4=B8=8A=E7=BA=BF=20(#600)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Youtube现在会不检测after_date日期之后的视频了 Youtube配置文件说明修改 --- biliup/plugins/youtube.py | 85 ++++++++++++++++++++++++++++++++------- public/config.toml | 5 +++ public/config.yaml | 8 ++-- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/biliup/plugins/youtube.py b/biliup/plugins/youtube.py index 31d58f5..20e8397 100644 --- a/biliup/plugins/youtube.py +++ b/biliup/plugins/youtube.py @@ -47,15 +47,24 @@ class Youtube(DownloadBase): info = ydl.extract_info(self.download_url, download=False) else: info = ydl.extract_info(self.url, download=False, process=False) - if type(info) is not dict: logger.warning(f"{Youtube.__name__}: {self.url}: 获取错误") return False + cache = KVFileStore(f"./cache/youtube/{self.fname}.txt") + def loop_entries(entrie): if type(entrie) is not dict: return None - elif entrie.get('_type') == 'url': + elif entrie.get('_type') == 'playlist': + # 播放列表递归 + for e in entrie.get('entries'): + le = loop_entries(e) + if type(le) is dict: + return le + elif le == "stop": + return None + elif type(entrie) is dict: # is_upcoming 等待开播 is_live 直播中 was_live结束直播(回放) if entrie.get('live_status') == 'is_upcoming': return None @@ -68,22 +77,32 @@ class Youtube(DownloadBase): if not self.enable_download_playback: return None - return loop_entries(ydl.extract_info(entrie.get('url'), download=False, process=False)) - elif entrie.get('_type') == 'playlist': - # 播放列表递归 - for e in entrie.get('entries'): - le = loop_entries(e) - if type(le) is dict: - return le - elif type(entrie) is dict: # 检测是否已下载 if ydl._make_archive_id(entrie) in ydl_archive: # 如果已下载但是还在直播则不算下载 if entrie.get('live_status') != 'is_live': return None + upload_date = cache.query(entrie.get('id')) + if upload_date is None: + if entrie.get('upload_date') is not None: + upload_date = entrie['upload_date'] + else: + entrie = ydl.extract_info(entrie.get('url'), download=False, process=False) + if type(entrie) is dict and entrie.get('upload_date') is not None: + upload_date = entrie['upload_date'] + + # 时间是必然存在的如果不存在说明出了问题 暂时跳过 + if upload_date is None: + return None + else: + cache.add(entrie.get('id'), upload_date) + + if self.afterdate is not None and upload_date < self.afterdate: + return 'stop' + # 检测时间范围 - if entrie.get('upload_date') not in DateRange(self.afterdate, self.beforedate): + if upload_date not in DateRange(self.afterdate, self.beforedate): return None return entrie @@ -95,9 +114,12 @@ class Youtube(DownloadBase): self.is_download = False else: self.is_download = True - self.room_title = download_entry.get('title') - self.live_cover_url = download_entry.get('thumbnail') - self.download_url = download_entry.get('webpage_url') + if not is_check: + if download_entry.get('_type') == 'url': + download_entry = ydl.extract_info(download_entry.get('url'), download=False, process=False) + self.room_title = download_entry.get('title') + self.live_cover_url = download_entry.get('thumbnail') + self.download_url = download_entry.get('webpage_url') return True else: return False @@ -153,3 +175,38 @@ class Youtube(DownloadBase): except: logger.error(f"{Youtube.__name__}: {self.url}: 清理残留文件失败,请手动删除{download_dir}") return True + + +class KVFileStore: + def __init__(self, file_path): + self.file_path = file_path + self.cache = {} + self._preload_data() + + def _ensure_file_and_folder_exists(self): + folder_path = os.path.dirname(self.file_path) + # 如果文件夹不存在,则创建文件夹 + if not os.path.exists(folder_path): + os.makedirs(folder_path) + # 如果文件不存在,则创建空文件 + if not os.path.exists(self.file_path): + with open(self.file_path, "w") as f: + pass + + def _preload_data(self): + self._ensure_file_and_folder_exists() + with open(self.file_path, "r", encoding="utf-8") as f: + for line in f: + k, v = line.strip().split("=") + self.cache[k] = v + + def add(self, key, value): + with open(self.file_path, "a", encoding="utf-8") as f: + f.write(f"{key}={value}\n") + # 更新缓存 + self.cache[key] = value + + def query(self, key, default=None): + if key in self.cache: + return self.cache[key] + return default diff --git a/public/config.toml b/public/config.toml index 957663b..860fd4a 100644 --- a/public/config.toml +++ b/public/config.toml @@ -132,6 +132,7 @@ check_sourcecode = 15 #------YouTube------# ### 设置偏好的YouTube下载封装格式 +### 默认不限制 ### 请务必记得安装ffmpeg ### 如无特殊需求不建议筛选封装格式 特别是录制直播时 多数直播mp4都是不可用的 ### bilibili支持 mp4 mkv webm 无需筛选也能上传 @@ -142,17 +143,21 @@ check_sourcecode = 15 #youtube_prefer_vcodec = "av01|vp9|avc" ### 可用av01,vp9,avc #youtube_prefer_acodec = "opus|mp4a" ### 可用opus,mp4a ### 设置偏好的YouTube下载最高纵向分辨率 +### 默认不限制 ### 可以用此限制画质 ### 例如设置为1080最高只会下载1080P画质 #youtube_max_resolution = "1080" ### 限制单个视频的最大大小 +### 默认不限制 ### 直播无此功能 ### 注意:此参数优先级高于分辨率设置,并且不包括音频部分的大小,仅仅只是视频部分的大小。 ### 此功能在一部分视频上无法使用 推荐使用画质限制不开启此功能 #youtube_max_videosize = "10G" ### 参考格式,例如100M,5G,10G。 ### 仅下载该日期之后的视频 +### 默认不限制 #youtube_after_date = '20220201' ### 仅下载该日期之前的视频(可与上面的youtube_after_date配合使用,构成指定下载范围区间) +### 默认不限制 #youtube_before_date = '20230501' ### 是否下载直播 默认开启 ### 关闭后将忽略直播下载(可以下载回放) 避免网络被风控(有些网络只能下载回放无法下载直播)的时候还会尝试下载直播 diff --git a/public/config.yaml b/public/config.yaml index 5dd5e62..9dfbb98 100644 --- a/public/config.yaml +++ b/public/config.yaml @@ -130,6 +130,7 @@ check_sourcecode: 15 #------YouTube------# ### 设置偏好的YouTube下载封装格式 +### 默认不限制 ### 请务必记得安装ffmpeg ### 如无特殊需求不建议筛选封装格式 特别是录制直播时 多数直播mp4都是不可用的 ### bilibili支持 mp4 mkv webm 无需筛选也能上传 @@ -140,21 +141,22 @@ check_sourcecode: 15 #youtube_prefer_vcodec: "av01|vp9|avc" ### 可用av01,vp9,avc #youtube_prefer_acodec: "opus|mp4a" ### 可用opus,mp4a ### 设置偏好的YouTube下载最高纵向分辨率 +### 默认不限制 ### 可以用此限制画质 ### 例如设置为1080最高只会下载1080P画质 #youtube_max_resolution: 1080 ### 限制单个视频的最大大小 +### 默认不限制 ### 直播无此功能 ### 注意:此参数优先级高于分辨率设置,并且不包括音频部分的大小,仅仅只是视频部分的大小。 ### 此功能在一部分视频上无法使用 推荐使用画质限制不开启此功能 #youtube_max_videosize: "10G" ###参考格式,例如100M,5G,10G。 ### 仅下载该日期之后的视频 +### 默认不限制 #youtube_after_date: '20220201' ### 仅下载该日期之前的视频(可与上面的youtube_after_date配合使用,构成指定下载范围区间) +### 默认不限制 #youtube_before_date: '20230501' -### YouTube的Streamlink下载器模式,开启之后会调用Streamlink获取直播流并且使用FFMPEG下载,默认关闭【仅当下载器是FFMPEG时才有效】 -### 开启之后上面关于YOUTUBE的选项全部无效【我也不知道这个功能有什么屌用】 -#use_new_ytb_downloader: false ### 是否下载直播 默认开启 ### 关闭后将忽略直播下载(可以下载回放) 避免网络被风控(有些网络只能下载回放无法下载直播)的时候还会尝试下载直播 ### 大量下载时极易风控 如对实时性要求不高推荐关闭