def get_all_video_data(self, base_path='', name_path=False, max_length=None, exclude_list=None): if len(self.album_list) == 0: self.get_all_video_info() if name_path: # 检查路径名中的特殊字符 temp_name = re.sub(r"[\/\\\:\*\?\"\<\>\|\s'‘’]", '_', self.name) if len(temp_name) == 0: temp_name = self.uid cache_path = base_path + '/{}'.format(temp_name) else: cache_path = base_path + '/{}'.format(self.uid) if not os.path.exists(cache_path): os.makedirs(cache_path) f.print_1('正在获取用户头像--', end='') f.print_b('user:{}'.format(self.name)) http_result = requests.get(self.face) with open(cache_path + '/face.jpg', 'wb') as file: file.write(http_result.content) f.print_g('[OK]', end='') f.print_1('用户头像已保存') for album in self.album_list: if exclude_list is not None and album.aid in exclude_list: continue album.get_album_data(cache_path, name_path, max_length) with open(cache_path + '/info.json', 'w', encoding='utf8') as file: file.write(str(json.dumps(self.get_dict_info())))
def get_album_data(self, base_path='', name_path=False, max_length=None): if len(self.video_list) == 0: try: self.get_album_info() except LookupError as e: f.print_r('{},已跳过'.format(e)) return base_path = os.path.abspath(base_path) # 获取绝对路径地址 if name_path: # 检查路径名中的特殊字符 temp_name = re.sub(r"[\/\\\:\*\?\"\<\>\|\s'‘’]", '_', self.name) if len(temp_name) == 0: temp_name = self.aid cache_path = base_path + '/{}'.format(temp_name) else: cache_path = base_path + '/{}'.format(self.aid) if not os.path.exists(cache_path): os.makedirs(cache_path) f.print_1('正在获取视频封面--', end='') f.print_b('av:{}'.format(self.aid)) http_result = requests.get(self.cover) with open(cache_path + '/cover.jpg', 'wb') as file: file.write(http_result.content) f.print_g('[OK]', end='') f.print_1('视频封面已保存') for video in self.video_list: video.get_video_data(cache_path, name_path, max_length) with open(cache_path + '/info.json', 'w', encoding='utf8') as file: file.write(str(json.dumps(self.get_dict_info())))
def get_video_data(self, base_path='', name_path=False, max_length=None): if self.video is None and self.audio is None: self.get_video_info() if max_length is not None and max_length < self.length: f.print_y('视频:{},超出限定长度取消下载') return base_path = os.path.abspath(base_path) # 获取绝对路径地址 if name_path: # 检查路径名中的特殊字符 temp_name = re.sub(r"[\/\\\:\*\?\"\<\>\|\s'‘’]", '_', self.name) temp_name = re.sub(r'[‘’]', '_', temp_name) if len(temp_name) == 0: temp_name = self.cid cache_path = base_path + '/{}'.format(temp_name) else: cache_path = base_path + '/{}'.format(self.cid) if not os.path.exists(cache_path): os.makedirs(cache_path) # 使用两个进程分别下载视频和音频 f.print_1('正在下载视频和配套音--', end='') f.print_b('av:{},cv:{}'.format(self.aid, self.cid)) if self.audio is not None and self.video is not None: self.aria2c_download(cache_path, '{}_{}.aac'.format(self.cid, self.quality_des), self.audio) self.aria2c_download(cache_path, '{}_{}.flv'.format(self.cid, self.quality_des), self.video) if self.video is not None and self.audio is None: self.aria2c_download(cache_path, '{}_{}.mp4'.format(self.cid, self.quality_des), self.video) else: f.print_y('无需独立下载音频') f.print_cyan('==============================================================') with open(cache_path + '/info.json', 'w', encoding='utf8') as file: file.write(str(json.dumps(self.get_dict_info())))
def get_channel_video_data(self, base_path='', name_path=False, max_length=None): # 获取UP主的所有视频的数据 if len(self.channel_list) == 0: self.get_channel_video_info() base_path = os.path.abspath(base_path) # 获取绝对路径地址 if name_path: # 检查路径名中的特殊字符 temp_name = re.sub(r"[\/\\\:\*\?\"\<\>\|\s'‘’]", '_', self.name) if len(temp_name) == 0: temp_name = self.uid cache_path = base_path + '/{}'.format(temp_name) else: cache_path = base_path + '/{}'.format(self.uid) if not os.path.exists(cache_path): os.makedirs(cache_path) f.print_1('正在获取用户头像--', end='') f.print_b('user:{}'.format(self.name)) http_result = requests.get(self.face) with open(cache_path + '/face.jpg', 'wb') as file: file.write(http_result.content) f.print_g('[OK]', end='') f.print_1('用户头像已保存') for channel in self.channel_list: channel.get_channel_data(cache_path, name_path, max_length) with open(cache_path + '/info.json', 'w', encoding='utf8') as file: file.write(str(json.dumps(self.get_dict_info())))
def aria2c_download(self, cache_path, file_name, download_url): referer = 'https://www.bilibili.com/video/av' + str(self.aid) shell = 'aria2c -c -s 16 -d "{}" -o "{}" --referer="{}" "{}"' shell = shell.format(cache_path, file_name, referer, download_url) process = subprocess.Popen(shell, shell=True) process.wait() file_path = '{}/{}'.format(cache_path, file_name) if os.path.exists(file_path): f.print_g('[OK]', end='') f.print_1('文件{}下载成功--'.format(file_name), end='') f.print_b('av:{},cv:{}'.format(self.aid, self.cid)) else: f.print_r('[ERR]', end='') f.print_1('文件{}下载失败--'.format(file_name), end='') f.print_b('av:{},cv:{}'.format(self.aid, self.cid)) f.print_r(shell.format(file_path, referer, download_url)) raise BaseException('av:{},cv:{},下载失败'.format(self.aid, self.cid))