def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) image_count = 1 video_count = 1 target_id = INIT_TARGET_ID first_post_id = "0" is_over = False need_make_image_dir = True need_make_video_dir = True while not is_over: # 获取一页日志信息 message_page_data = get_message_page_data(account_name, target_id) if message_page_data is None: log.error(account_name + " 媒体列表解析异常") tool.process_exit() # 没有了 if len(message_page_data) == 0: break for message_info in message_page_data: if not robot.check_sub_key(("post",), message_info): log.error(account_name + " 媒体信息解析异常 %s" % message_info) continue if not robot.check_sub_key(("body", "postId"), message_info["post"]): log.error(account_name + " 媒体信息解析异常 %s" % message_info) continue target_id = message_info["post"]["postId"] # 检查是否已下载到前一次的记录 if int(target_id) <= int(self.account_info[3]): is_over = True break # 将第一个媒体的postId做为新的存档记录 if first_post_id == "0": first_post_id = str(target_id) for media_info in message_info["post"]["body"]: if not robot.check_sub_key(("bodyType",), media_info): log.error(account_name + " 媒体列表bodyType解析异常") continue # bodyType = 1: text, bodyType = 3: image, bodyType = 8: video body_type = int(media_info["bodyType"]) if body_type == 1: # 文本 pass elif body_type == 2: # 表情 pass elif body_type == 3: # 图片 if IS_DOWNLOAD_IMAGE: if not robot.check_sub_key(("image",), media_info): log.error(account_name + " 第%s张图片解析异常%s" % (image_count, media_info)) continue image_url = str(media_info["image"]) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, image_file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) elif body_type == 8: # video if IS_DOWNLOAD_VIDEO: if not robot.check_sub_key(("movieUrlHq",), media_info): log.error(account_name + " 第%s个视频解析异常%s" % (video_count, media_info)) continue video_url = str(media_info["movieUrlHq"]) log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建视频下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False file_type = video_url.split(".")[-1] video_file_path = os.path.join(video_path, "%04d.%s" % (video_count, file_type)) if tool.save_net_file(video_url, video_file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) elif body_type == 7: # 转发 pass else: log.error(account_name + " 第%s张图片、第%s个视频,未知bodytype %s, %s" % (image_count, video_count, body_type, media_info)) # 排序 if IS_SORT: if image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() if video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[2]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_post_id != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = str(int(self.account_info[2]) + video_count - 1) self.account_info[3] = first_post_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_name) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) image_count = 1 page_count = 1 first_blog_time = "0" is_over = False need_make_image_dir = True while not is_over: # 获取一页日志 blog_data = get_blog_page_data(account_name, page_count) if blog_data is None: log.error(account_name + " 第%s页日志无法获取" % page_count) tool.process_exit() # 解析日志发布时间 blog_time = get_blog_time(blog_data) if blog_time is None: log.error(account_name + " 第%s页解析日志时间失败" % page_count) tool.process_exit() # 检查是否是上一次的最后blog if blog_time <= int(self.account_info[2]): break # 将第一个日志的时间做为新的存档记录 if first_blog_time == "0": first_blog_time = str(blog_time) # 从日志列表中获取全部的图片 image_url_list = get_image_url_list(blog_data) for image_url in image_url_list: # 使用默认图片的分辨率 image_url = image_url.split("?")[0] # 过滤表情 if image_url.find("http://emoji.ameba.jp") >= 0: continue log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 获取失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True if not is_over: if 0 < GET_PAGE_COUNT < page_count: is_over = True else: page_count += 1 log.step(account_name + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT and image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片子目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_blog_time != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_blog_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_name) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_id = self.account_info[0] # todo 是否有需要显示不同名字 account_name = account_id try: log.step(account_name + " 开始") # todo 是否需要下载图片或视频 # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) # todo 图片下载逻辑 # 图片 image_count = 1 first_image_time = "0" need_make_image_dir = True if IS_DOWNLOAD_IMAGE: pass # todo 视频下载逻辑 # 视频 video_count = 1 first_video_time = "0" need_make_video_dir = True if IS_DOWNLOAD_VIDEO: pass log.step(account_name + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: # todo 是否需要下载图片 if first_image_time != "0": destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() # todo 是否需要下载视频 if first_video_time != "0": destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[3]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # todo 是否需要下载图片或视频 # 新的存档记录 if first_image_time != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_image_time if first_video_time != "0": self.account_info[3] = str(int(self.account_info[3]) + video_count - 1) self.account_info[4] = first_video_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() # todo 是否需要下载图片或视频 TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 3 and self.account_info[2]: account_name = self.account_info[2] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) # 获取视频信息列表 video_info_list = get_video_info_list(account_id) if video_info_list is None: log.error(account_name + " 视频列表获取失败") tool.process_exit() video_count = 1 first_video_id = "0" need_make_video_dir = True for video_info in video_info_list: if not robot.check_sub_key(("item_data",), video_info) or \ not robot.check_sub_key(("watch_id", "title"), video_info["item_data"]): log.error(account_name + " 视频信息%s解析失败" % video_info) tool.process_exit() # sm30043563 video_id = str(video_info["item_data"]["watch_id"]) # 过滤标题中不支持的字符 video_title = robot.filter_text(video_info["item_data"]["title"]) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False # 获取视频下载地址 video_url = get_video_url(video_id) log.step(account_name + " 开始下载第%s个视频 %s %s" % (video_count, video_id, video_url)) print video_title print "%s %s" % (video_id, video_title) file_path = os.path.join(video_path, "%s %s.mp4" % (video_id, video_title)) if tool.save_net_file(video_url, file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s %s 下载失败" % (video_count, video_id, video_url)) log.step(account_name + " 下载完毕,总共获得%s个视频" % (video_count - 1)) # 排序 if IS_SORT: if first_video_id != "0": destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[3]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_video_id != "0": self.account_info[1] = first_video_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) # 图片 image_count = 1 page_count = 1 first_blog_id = "0" need_make_image_dir = True is_over = False is_big_image_over = False while not is_over: # 获取一页日志信息 blog_page = get_one_page_blog(account_id, page_count) if blog_page is None: log.error(account_name + " 第%s页日志获取失败" % page_count) tool.process_exit() if not blog_page: log.error(account_name + " 第%s页日志解析失败" % page_count) tool.process_exit() blog_data_list = get_blog_data_list(blog_page) if len(blog_data_list) == 0: log.error(account_name + " 第%s页日志分组失败" % page_count) tool.process_exit() for blog_data in blog_data_list: # 获取日志id blog_id = get_blog_id(account_id, blog_data) if blog_id is None: log.error(account_name + " 日志解析日志id失败,日志内容:%s" % blog_data) tool.process_exit() # 检查是否已下载到前一次的日志 if blog_id <= int(self.account_info[2]): is_over = True break # 将第一个日志的ID做为新的存档记录 if first_blog_id == "0": first_blog_id = str(blog_id) # 获取该页日志的全部图片地址列表 image_url_list = get_image_url_list(blog_data) if len(image_url_list) == 0: continue # 获取日志页面中存在的所有大图显示地址,以及对应的小图地址 big_2_small_list = get_big_image_url_list(blog_data) # 下载图片 for image_url in image_url_list: # 检查是否存在大图可以下载 if not is_big_image_over: image_url, is_big_image_over = check_big_image(image_url, big_2_small_list) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] if file_type.find("?") != -1: file_type = "jpeg" file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if not is_over: # 达到配置文件中的下载页数,结束 if 0 < GET_PAGE_COUNT <= page_count: is_over = True # 判断当前页数是否大等于总页数 elif page_count >= get_max_page_count(blog_page): is_over = True else: page_count += 1 log.step(account_name + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT: if first_blog_id != "0": destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_blog_id != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_blog_id # 保存最后的信息 self.thread_lock.acquire() tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) image_count = 1 page_count = 1 first_diary_id = "0" is_over = False need_make_image_dir = True while not is_over: # 获取一页博客信息 diary_list = get_one_page_diary_data(account_id, page_count) if diary_list is None: log.error(account_name + " 第%s页日志列表解析异常" % page_count) tool.process_exit() # 没有获取到任何日志,所有日志已经全部获取完毕了 if len(diary_list) == 0: break for diary_info in list(diary_list): # 日志id diary_id = tool.find_sub_string(diary_info, "id=", "&") if not diary_id: log.error(account_name + " 日志id解析异常,日志信息:%s" % diary_info) continue # 检查是否是上一次的最后视频 if int(diary_id) <= int(self.account_info[2]): is_over = True break # 将第一个日志的id做为新的存档记录 if first_diary_id == "0": first_diary_id = diary_id log.trace(account_name + " 日志id %s" % diary_id) # 获取这个日志中的全部图片地址列表 image_url_list = get_image_url_list(diary_info) for image_url in image_url_list: # 如果图片地址没有域名,表示直接使用当前域名下的资源,需要拼接成完整的地址 if image_url[:7] != "http://" and image_url[:8] != "https://": if image_url[0] == "/": image_url = "http://www.keyakizaka46.com%s" % image_url else: image_url = "http://www.keyakizaka46.com/%s" % image_url log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 获取失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if not is_over: # 达到配置文件中的下载页数,结束 if 0 < GET_PAGE_COUNT <= page_count: is_over = True else: page_count += 1 log.step(account_name + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT and image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片子目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_diary_id != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_diary_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) video_count = 1 page_time = 0 first_video_time = "0" need_make_video_dir = True is_over = False while not is_over: # 获取一页视频信息 video_data = get_one_page_video_data(account_id, page_time) if video_data is None: log.error(account_name + " 视频列表获取失败") tool.process_exit() for video_info in video_data["info"]: if not robot.check_sub_key(("newvideos", "id", "timestamp"), video_info): log.error(account_name + " 视频信息 %s 解析失败" % video_info) continue page_time = int(video_info["timestamp"]) # 检查是否已下载到前一次的视频 if page_time <= int(self.account_info[2]): is_over = True break # 将第一个视频的上传时间做为新的存档记录 if first_video_time == "0": first_video_time = str(page_time) # todo 处理如果有多个视频 if len(video_info["newvideos"]) != 1: log.error(account_name + " 视频信息 %s 发现多个视频下载信息" % video_info) continue if not robot.check_sub_key(("vid",), video_info["newvideos"][0]): log.error(account_name + " 视频信息 %s 解析vid失败" % video_info) continue # 获取视频下载地址 video_url = get_video_url(video_info["newvideos"][0]["vid"], video_info["id"]) log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False file_type = video_url.split(".")[-1].split("?")[0] file_path = os.path.join(video_path, "%04d.%s" % (video_count, file_type)) if tool.save_net_file(video_url, file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_VIDEO_COUNT < video_data: is_over = True break if not is_over: if not video_data["hasNext"]: is_over = True log.step(account_name + " 下载完毕,总共获得%s个视频" % (video_count - 1)) # 排序 if IS_SORT: if first_video_time != "0": destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[3]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_video_time != "0": self.account_info[3] = str(int(self.account_info[3]) + video_count - 1) self.account_info[4] = first_video_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def main(self): # 解析存档文件 last_blog_id = "" image_start_index = 0 if os.path.exists(self.save_data_path): save_file = open(self.save_data_path, "r") save_info = save_file.read() save_file.close() save_info = save_info.split("\t") if len(save_info) >= 2: image_start_index = int(save_info[0]) last_blog_id = save_info[1] # 下载 page_index = 1 image_count = 1 is_over = False new_last_blog_id = "" if self.is_sort: image_path = self.image_temp_path else: image_path = self.image_download_path while not is_over: index_url = "http://blog.mariko-shinoda.net/page%s.html" % (page_index - 1) index_page_return_code, index_page = tool.http_request(index_url)[:2] if index_page_return_code == 1: image_name_list = re.findall('data-original="./([^"]*)"', index_page) for image_name in image_name_list: blog_id = image_name.split("-")[0] # 检查是否已下载到前一次的图片 if blog_id == last_blog_id: is_over = True break # 将第一个博客的id做为新的存档记录 if new_last_blog_id == "": new_last_blog_id = blog_id image_url = "http://blog.mariko-shinoda.net/%s" % image_name # 文件类型 file_type = image_url.split(".")[-1].split(":")[0] file_path = os.path.join(image_path, "%05d.%s" % (image_count, file_type)) log.step("开始下载第%s张图片 %s" % (image_count, image_url)) if tool.save_net_file(image_url, file_path): log.step("第%s张图片下载成功" % image_count) image_count += 1 else: log.step("第%s张图片 %s 下载失败" % (image_count, image_url)) page_index += 1 else: log.error("无法访问博客页面 %s" % index_url) is_over = True log.step("下载完毕") # 排序复制到保存目录 if self.is_sort: if robot.sort_file(self.image_temp_path, self.image_download_path, image_start_index, 5): log.step(" 图片从下载目录移动到保存目录成功") else: log.error(" 创建图片保存目录 %s 失败" % self.image_download_path) tool.process_exit() # 保存新的存档文件 new_save_file_path = robot.get_new_save_file_path(self.save_data_path) log.step("保存新存档文件 %s" % new_save_file_path) new_save_file = open(new_save_file_path, "w") new_save_file.write(str(image_start_index) + "\t" + new_last_blog_id) new_save_file.close() log.step("全部下载完毕,耗时%s秒,共计图片%s张" % (self.get_run_time(), image_count - 1))
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) image_count = 1 video_count = 1 data_tweet_id = INIT_MAX_ID first_tweet_id = "0" is_over = False is_download_image = IS_DOWNLOAD_IMAGE is_download_video = IS_DOWNLOAD_VIDEO need_make_image_dir = True need_make_video_dir = True while not is_over: # 获取指定时间点后的一页图片信息 media_page = get_media_page_data(account_name, data_tweet_id) if media_page is None: log.error(account_name + " 媒体列表解析异常") tool.process_exit() # 上一页正好获取了全部的媒体信息,所以这一页没有任何内容,完成了,直接退出 if media_page["new_latent_count"] == 0 and not media_page["has_more_items"]: break tweet_list = get_tweet_list(media_page["items_html"]) if len(tweet_list) == 0: log.error(account_name + " 媒体列表拆分异常,items_html:%s" % media_page["items_html"]) tool.process_exit() if media_page["new_latent_count"] != len(tweet_list): log.error(account_name + " 解析的媒体数量不等于new_latent_count的数值") # tool.process_exit() for tweet_data in tweet_list: tweet_id = tool.find_sub_string(tweet_data, 'data-tweet-id="', '"') if not tweet_id: log.error(account_name + " tweet id解析异常,tweet数据:%s" % tweet_data) continue # 检查是否tweet的id小于上次的记录 if int(tweet_id) <= int(self.account_info[3]): is_over = True break # 将第一个tweet的id做为新的存档记录 if first_tweet_id == "0": first_tweet_id = tweet_id # 视频 if is_download_image: # 这个tweet是否包含视频 if check_has_video(tweet_data): video_file_type, video_url_list = get_video_url_list(tweet_id) if len(video_url_list) > 0: log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url_list)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False video_file_path = os.path.join(video_path, "%04d.%s" % (video_count, video_file_type)) if save_video(video_url_list, video_file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url_list)) else: log.error(account_name + " 第%s个视频 没有获取到源地址,tweet id:%s" % (video_count, tweet_id)) # 达到配置文件中的下载数量,结束图片下载 if 0 < GET_IMAGE_COUNT < image_count: is_download_image = False # 图片 if is_download_video: # 匹配获取全部的图片地址 image_url_list = get_image_url_list(tweet_data) for image_url in image_url_list: image_url = str(image_url) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) image_return_code, image_byte = tool.http_request(image_url)[:2] # 404,不算做错误,图片已经被删掉了 if image_return_code == -404: log.error(account_name + " 第%s张图片 %s 已被删除,跳过" % (image_count, image_url)) elif image_return_code == 1: # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1].split(":")[0] image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) save_image(image_byte, image_file_path) log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 获取失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束视频下载 if 0 < GET_VIDEO_COUNT < video_count: is_download_video = False # 全部达到配置文件中的下载数量,结束 if not is_download_image and not is_download_video: is_over = True break if not is_over: # 查找下一页的data_tweet_id if media_page["has_more_items"]: data_tweet_id = str(media_page["min_position"]) else: is_over = True log.step(account_name + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: if image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片子目录 %s 失败" % destination_path) tool.process_exit() if video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[2]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_tweet_id != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = str(int(self.account_info[2]) + video_count - 1) self.account_info[3] = first_tweet_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_name) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) # 获取user id api_info = get_api_info(account_name) if api_info is None: log.error(account_name + " API信息查找失败") tool.process_exit() if not api_info["user_id"]: log.error(account_name + " user_id解析失败") tool.process_exit() if not api_info["site_key"]: log.error(account_name + " site_key解析失败") tool.process_exit() # 生成一个随机的request id用作访问(使用原理暂时不明,只管模拟页面传入) request_id = tool.generate_random_string(8) # 图片 image_count = 1 page_count = 1 first_image_time = "0" is_over = False need_make_image_dir = True while not is_over: # 获取一页图片信息 page_data = get_one_page_image_data(api_info["user_id"], page_count, api_info["site_key"], request_id) if page_data is None: log.error(account_name + " 第%s页图片信息获取失败" % page_count) tool.process_exit() for photo_info in page_data["photos"]["photo"]: if "dateupload" not in photo_info: log.error(account_name + " 第%s张图片上传时间获取失败,图片信息:%s" % (image_count, photo_info)) continue # 检查是否是上一次的最后视频 if int(self.account_info[2]) >= int(photo_info["dateupload"]): is_over = True break # 将第一张图片的上传时间做为新的存档记录 if first_image_time == "0": first_image_time = str(photo_info["dateupload"]) if "url_o_cdn" in photo_info: image_url = str(photo_info["url_o_cdn"]) elif "url_o" in photo_info: image_url = str(photo_info["url_o"]) else: log.error(account_name + " 第%s张图片下载地址获取失败,图片信息:%s" % (image_count, photo_info)) continue log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if not is_over: # 达到配置文件中的下载数量,结束 if 0 < GET_PAGE_COUNT <= page_count: is_over = True elif page_count >= int(page_data["photos"]["pages"]): is_over = True else: page_count += 1 log.step(account_name + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT: if first_image_time != "0": destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_image_time != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_image_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_name) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 6 and self.account_info[5]: account_name = self.account_info[5] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) # 视频 video_count = 1 account_page_id = None first_video_url = "" is_over = False need_make_video_dir = True since_id = INIT_SINCE_ID while IS_DOWNLOAD_VIDEO and (not is_over): # 获取page_id if account_page_id is None: account_page_id = get_account_page_id(account_id) if account_page_id is None: log.error(account_name + " 微博主页没有获取到page_id") break # 获取指定时间点后的一页视频信息 video_page_data = get_one_page_video_data(account_page_id, since_id) if video_page_data is None: log.error(account_name + " 视频列表解析异常") first_video_url = "" # 存档恢复 break # 匹配获取全部的视频页面 video_play_url_list = get_video_play_url_list(video_page_data) log.trace(account_name + "since_id:%s中的全部视频:%s" % (since_id, video_play_url_list)) for video_play_url in video_play_url_list: # 检查是否是上一次的最后视频 if self.account_info[4] == video_play_url: is_over = True break # 将第一个视频的地址做为新的存档记录 if first_video_url == "": first_video_url = video_play_url # 获取这个视频的下载地址 return_code, video_url_list = get_video_url(video_play_url) if return_code != 1: if return_code == -1: log.error(account_name + " 第%s个视频 %s 没有获取到源地址" % (video_count, video_play_url)) elif return_code == -2: log.error(account_name + " 第%s个视频 %s 无法访问" % (video_count, video_play_url)) elif return_code == -3: log.error(account_name + " 第%s个视频 %s 暂不支持的视频源" % (video_count, video_play_url)) continue log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_play_url)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False video_file_path = os.path.join(video_path, "%04d.mp4" % video_count) for video_url in video_url_list: if tool.save_net_file(video_url, video_file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_VIDEO_COUNT < video_count: is_over = True break if not is_over: # 获取下一页的since_id since_id = tool.find_sub_string(video_page_data, "type=video&owner_uid=&since_id=", '">') if not since_id: break # 有历史记录,并且此次没有获得正常结束的标记,说明历史最后的视频已经被删除了 if self.account_info[4] != "" and video_count > 1 and not is_over: log.error(account_name + " 没有找到上次下载的最后一个视频地址") # 图片 image_count = 1 page_count = 1 first_image_time = "0" unique_list = [] is_over = False need_make_image_dir = True while IS_DOWNLOAD_IMAGE and (not is_over): # 获取指定一页图片的信息 photo_page_data = get_one_page_photo_data(account_id, page_count) if photo_page_data is None: log.error(account_name + " 图片列表获取失败") first_image_time = "0" # 存档恢复 break log.trace(account_name + "第%s页的全部图片信息:%s" % (page_count, photo_page_data)) for image_info in photo_page_data["photo_list"]: if not robot.check_sub_key(("pic_host", "pic_name", "timestamp"), image_info): log.error(account_name + " 第%s张图片信息解析失败 %s" % (image_count, image_info)) continue # 检查是否图片时间小于上次的记录 if int(image_info["timestamp"]) <= int(self.account_info[2]): is_over = True break # 新增图片导致的重复判断 if image_info["pic_name"] in unique_list: continue else: unique_list.append(image_info["pic_name"]) # 将第一张图片的上传时间做为新的存档记录 if first_image_time == "0": first_image_time = str(image_info["timestamp"]) image_url = str(image_info["pic_host"]) + "/large/" + str(image_info["pic_name"]) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 获取图片的二进制数据,并且判断这个图片是否是可用的 image_status, image_byte = get_image_byte(image_url) if image_status != 1: if image_status == -1: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) elif image_status == -2: log.error(account_name + " 第%s张图片 %s 资源已被删除,跳过" % (image_count, image_url)) continue # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] if file_type.find("/") != -1: file_type = "jpg" image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) save_image(image_byte, image_file_path) log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if not is_over: # 根据总的图片数量和每页显示的图片数量,计算是否还有下一页 if (photo_page_data["total"] / IMAGE_COUNT_PER_PAGE) > (page_count - 1): page_count += 1 else: # 全部图片下载完毕 is_over = True log.step(account_name + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: if first_image_time != "0": destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() if first_video_url != "": destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[3]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_image_time != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_image_time if first_video_url != "": self.account_info[3] = str(int(self.account_info[3]) + video_count - 1) self.account_info[4] = first_video_url # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) account_id = get_account_id(account_name) if account_id is None: log.error(account_name + " account id 查找失败") tool.process_exit() image_count = 1 video_count = 1 cursor = INIT_CURSOR first_created_time = "0" is_over = False need_make_image_dir = True need_make_video_dir = True while not is_over: # 获取指定时间后的一页媒体信息 media_data = get_one_page_media_data(account_id, cursor) if media_data is None: log.error(account_name + " 媒体列表解析异常") tool.process_exit() nodes_data = media_data["nodes"] for photo_info in nodes_data: if not robot.check_sub_key(("is_video", "display_src", "date"), photo_info): log.error(account_name + " 媒体信息解析异常") break if photo_info["is_video"] and not robot.check_sub_key(("code",), photo_info): log.error(account_name + " 视频code解析异常") break # 检查是否已下载到前一次的图片 if int(photo_info["date"]) <= int(self.account_info[3]): is_over = True break # 将第一张图片的上传时间做为新的存档记录 if first_created_time == "0": first_created_time = str(int(photo_info["date"])) # 图片 if IS_DOWNLOAD_IMAGE: image_url = str(photo_info["display_src"].split("?")[0]) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, image_file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) # 视频 if IS_DOWNLOAD_VIDEO and photo_info["is_video"]: # 根据日志ID获取视频下载地址 video_url = get_video_url(photo_info["code"]) if video_url is None: log.error(account_name + " 第%s个视频code:%s 无法访问" % (video_count, photo_info["code"])) continue if not video_url: log.error(account_name + " 第%s个视频code:%s 没有获取到下载地址" % (video_count, photo_info["code"])) continue log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建视频下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False file_type = video_url.split(".")[-1] video_file_path = os.path.join(video_path, "%04d.%s" % (video_count, file_type)) if tool.save_net_file(video_url, video_file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if not is_over: if media_data["page_info"]["has_next_page"]: cursor = str(media_data["page_info"]["end_cursor"]) else: is_over = True log.step(account_name + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: if image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() if video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[2]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_created_time != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = str(int(self.account_info[2]) + video_count - 1) self.account_info[3] = first_created_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_name) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_id = self.account_info[0] try: log.step(account_id + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_id) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_id) # 图片下载 page_count = 1 image_count = 1 first_post_id = "" unique_list = [] is_over = False need_make_download_dir = True while not is_over: post_url_list = get_one_page_post_url_list(account_id, page_count) # 无法获取信息首页 if post_url_list is None: log.error(account_id + " 无法访问第%s页相册页" % page_count) tool.process_exit() if len(post_url_list) == 0: # 下载完毕了 break # 去重排序 log.trace(account_id + " 相册第%s页获取的所有信息页:%s" % (page_count, post_url_list)) post_url_list = sorted(list(set(post_url_list)), reverse=True) log.trace(account_id + " 相册第%s页去重排序后的信息页:%s" % (page_count, post_url_list)) for post_url in post_url_list: post_id = post_url.split("/")[-1].split("_")[-1] # 检查是否已下载到前一次的图片 if post_id <= self.account_info[2]: is_over = True break # 新增信息页导致的重复判断 if post_id in unique_list: continue else: unique_list.append(post_id) # 将第一个信息页的id做为新的存档记录 if first_post_id == "": first_post_id = post_id post_page_return_code, post_page = tool.http_request(post_url)[:2] if post_page_return_code != 1: log.error(account_id + " 第%s张图片,无法获取信息页 %s" % (image_count, post_url)) continue image_url_list = get_image_url_list(post_page) log.trace(account_id + " 信息页 %s 获取的所有图片:%s" % (post_url, image_url_list)) if len(image_url_list) == 0: log.error(account_id + " 第%s张图片,信息页 %s 中没有找到图片" % (image_count, post_url)) continue for image_url in image_url_list: if image_url.rfind("?") > image_url.rfind("."): image_url = image_url.split("?")[0] log.step(account_id + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_download_dir: if not tool.make_dir(image_path, 0): log.error(account_id + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_download_dir = False file_type = image_url.split(".")[-1] file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_id + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_id + " 第%s张图片 %s 下载失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if is_over: break if not is_over: # 达到配置文件中的下载数量,结束 if 0 < GET_PAGE_COUNT <= page_count: is_over = True else: page_count += 1 log.step(account_id + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT and image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_id) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_id + " 图片从下载目录移动到保存目录成功") else: log.error(account_id + " 创建图片子目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_post_id != "": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_post_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_id + " 完成") except SystemExit, se: if se.code == 0: log.step(account_id + " 提前退出") else: log.error(account_id + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] if len(self.account_info) >= 5 and self.account_info[4]: account_file_path = self.account_info[4] else: account_file_path = "" try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_file_path, account_name) # 图片下载 image_count = 1 key = "" first_album_id = "0" unique_list = [] is_over = False need_make_download_dir = True while not is_over: # 获取一页相册 album_page = get_one_page_album(account_id, key) if album_page is None: log.error(account_name + " 无法访问相册页,token:%s" % key) tool.process_exit() # 获取相册页中的所有picasweb地址列表 picasaweb_url_list = get_picasaweb_url_list(album_page) log.trace(account_name + " 相册获取的所有picasaweb页:%s" % picasaweb_url_list) for picasaweb_url in picasaweb_url_list: # 有可能拿到带authkey的,需要去掉 # https://picasaweb.google.com/116300481938868290370/2015092603?authkey\u003dGv1sRgCOGLq-jctf-7Ww#6198800191175756402 picasaweb_url = picasaweb_url.replace("\u003d", "=") # 获取picasaweb页的album id album_id = get_picasaweb_page_album_id(account_id, picasaweb_url) if album_id is None: log.error(account_name + " 第%s张图片,无法访问picasaweb页 %s" % (image_count, picasaweb_url)) continue if not album_id: log.error(account_name + " 第%s张图片,picasaweb页 %s 获取album id失败" % (image_count, picasaweb_url)) continue log.trace(account_name + " picasaweb页 %s 的album id:%s" % (picasaweb_url, album_id)) # 检查是否已下载到前一次的图片 if int(album_id) <= int(self.account_info[2]): is_over = True break # # 相同的album_id判断 if album_id in unique_list: continue else: unique_list.append(album_id) # 将第一个album_id做为新的存档记录 if first_album_id == "0": first_album_id = album_id # 获取album id对应相册存档页的全部图片地址列表 image_url_list = get_image_url_list(account_id, album_id) if image_url_list is None: log.error(account_name + " 第%s张图片,无法访问album id:%s 的相册存档页" % (image_count, album_id)) continue if len(image_url_list) == 0: log.error(account_name + " 第%s张图片,album id:%s 的相册存档页没有解析到图片" % (image_count, album_id)) continue log.trace(account_name + " album id:%s 的相册存档页获取的所有图片:%s" % (album_id, image_url_list)) for image_url in list(image_url_list): image_url = generate_max_resolution_image_url(image_url) # 下载 log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_download_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_download_dir = False if image_url.rfind("/") < image_url.rfind("."): file_type = image_url.split(".")[-1] else: file_type = "jpg" file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, file_path): log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_name + " 第%s张图片 %s 下载失败" % (image_count, image_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: is_over = True break if is_over: break if not is_over: # 查找下一页的token key key_find = re.findall('"([.]?[a-zA-Z0-9-_]*)"', album_page) if len(key_find) > 0 and len(key_find[0]) > 80: key = key_find[0] else: # 不是第一次下载 if self.account_info[2] != "0": log.error(account_name + " 没有找到下一页的token,将该页保存:") log.error(album_page) is_over = True log.step(account_name + " 下载完毕,总共获得%s张图片" % (image_count - 1)) # 排序 if IS_SORT and image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_file_path, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片子目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_album_id != "0": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = first_album_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) page_count = 1 video_count = 1 first_video_id = "0" unique_list = [] is_over = False need_make_download_dir = True while not is_over: # 获取指定一页的视频信息 medias_data = get_one_page_video_data(account_id, page_count) if medias_data is None: log.error(account_name + " 视频列表获取失败") tool.process_exit() for media in medias_data: if not robot.check_sub_key(("video", "id"), media): log.error(account_name + " 第%s个视频信:%s解析失败" % (video_count, media)) continue video_id = str(media["id"]) # 检查是否图片时间小于上次的记录 if int(video_id) <= int(self.account_info[2]): is_over = True break # 新增视频导致的重复判断 if video_id in unique_list: continue else: unique_list.append(video_id) # 将第一张图片的上传时间做为新的存档记录 if first_video_id == "0": first_video_id = video_id video_url = str(media["video"]) log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_download_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建视频下载目录 %s 失败" % video_path) tool.process_exit() need_make_download_dir = False file_path = os.path.join(video_path, "%04d.mp4" % video_count) if tool.save_net_file(video_url, file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_VIDEO_COUNT < video_count: is_over = True break if not is_over: if len(medias_data) >= VIDEO_COUNT_PER_PAGE: page_count += 1 else: # 获取的数量小于请求的数量,已经没有剩余视频了 is_over = True log.step(account_name + " 下载完毕,总共获得%s个视频" % (video_count - 1)) # 排序 if IS_SORT and video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_video_id != "": self.account_info[1] = str(int(self.account_info[1]) + video_count - 1) self.account_info[2] = first_video_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_id = self.account_info[0] try: log.step(account_id + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_id) video_path = os.path.join(VIDEO_TEMP_PATH, account_id) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_id) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_id) page_count = 1 image_count = 1 video_count = 1 first_post_id = "" unique_list = [] is_over = False need_make_image_dir = True need_make_video_dir = True while not is_over: post_url_list = get_one_page_post_url_list(account_id, page_count) if post_url_list is None: log.error(account_id + " 无法访问第%s页相册页" % page_count) tool.process_exit() if len(post_url_list) == 0: # 下载完毕了 break log.trace(account_id + " 相册第%s页获取的所有信息页:%s" % (page_count, post_url_list)) post_url_list_group_by_post_id = filter_post_url(post_url_list) log.trace(account_id + " 相册第%s页去重排序后的信息页:%s" % (page_count, post_url_list_group_by_post_id)) log.step(account_id + " 相册第%s页获取到%s页信息页" % (page_count, len(post_url_list_group_by_post_id))) for post_id in sorted(post_url_list_group_by_post_id.keys(), reverse=True): # 检查信息页id是否小于上次的记录 if post_id <= self.account_info[3]: is_over = True break # 将第一个信息页的id做为新的存档记录 if first_post_id == "": first_post_id = post_id # 获取信息页并截取head标签内的内容 post_url = "http://%s.tumblr.com/post/%s" % (account_id, post_id) post_page_head = get_post_page_head(post_url, post_url_list_group_by_post_id[post_id]) if post_page_head is None: log.error(account_id + " 无法访问信息页 %s" % post_url) continue if not post_page_head: log.error(account_id + " 信息页 %s 截取head标签异常" % post_url) continue # 获取og_type(页面类型的是视频还是图片或其他) og_type = tool.find_sub_string(post_page_head, '<meta property="og:type" content="', '" />') if not og_type: log.error(account_id + " 信息页 %s,'og:type'获取异常" % post_url) continue # 空、音频、引用,跳过 if og_type in ["tumblr-feed:entry", "tumblr-feed:audio", "tumblr-feed:quote", "tumblr-feed:link"]: continue # 新增信息页导致的重复判断 if post_id in unique_list: continue else: unique_list.append(post_id) # 视频下载 if IS_DOWNLOAD_VIDEO and og_type == "tumblr-feed:video": video_list = get_video_list(account_id, post_id) if video_list is None: log.error(account_id + " 第%s个视频 日志id:%s无法访问播放页" % (video_count, post_id)) else: if len(video_list) > 0: for video_url, video_type in list(video_list): log.step(account_id + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_id + " 创建视频下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False file_type = video_type.split("/")[-1] video_file_path = os.path.join(video_path, "%04d.%s" % (video_count, file_type)) if tool.save_net_file(video_url, video_file_path): log.step(account_id + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_id + " 第%s个视频 %s 下载失败" % (video_count, video_url)) else: log.error(account_id + " 第%s个视频 日志id:%s 中没有找到视频" % (video_count, post_id)) # 图片下载 if IS_DOWNLOAD_IMAGE: if og_type == "tumblr-feed:video": page_image_url_list = [] video_image_url = tool.find_sub_string(post_page_head, '<meta property="og:image" content="', '" />') if video_image_url: page_image_url_list.append(video_image_url) else: page_image_url_list = re.findall('"(http[s]?://\w*[.]?media.tumblr.com/[^"]*)"', post_page_head) log.trace(account_id + " 信息页 %s 过滤前的所有图片:%s" % (post_url, page_image_url_list)) # 过滤头像以及页面上找到不同分辨率的同一张图 page_image_url_list = filter_different_resolution_images(page_image_url_list) log.trace(account_id + " 信息页 %s 获取的的所有图片:%s" % (post_url, page_image_url_list)) if len(page_image_url_list) > 0: for image_url in page_image_url_list: log.step(account_id + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_id + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1] image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) if tool.save_net_file(image_url, image_file_path): log.step(account_id + " 第%s张图片下载成功" % image_count) image_count += 1 else: log.error(account_id + " 第%s张图片 %s 下载失败" % (image_count, image_url)) else: log.error(account_id + " 第%s张图片 信息页 %s 中没有找到图片" % (image_count, post_url)) if not is_over: # 达到配置文件中的下载数量,结束 if 0 < GET_PAGE_COUNT <= page_count: is_over = True else: page_count += 1 log.step(account_id + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: if image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_id) if robot.sort_file(image_path, destination_path, int(self.account_info[1]), 4): log.step(account_id + " 图片从下载目录移动到保存目录成功") else: log.error(account_id + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() if video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_id) if robot.sort_file(video_path, destination_path, int(self.account_info[2]), 4): log.step(account_id + " 视频从下载目录移动到保存目录成功") else: log.error(account_id + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_post_id != "": self.account_info[1] = str(int(self.account_info[1]) + image_count - 1) self.account_info[2] = str(int(self.account_info[2]) + video_count - 1) self.account_info[3] = first_post_id # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_id + " 完成") except SystemExit, se: if se.code == 0: log.step(account_id + " 提前退出") else: log.error(account_id + " 异常退出")
def run(self): global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 4 and self.account_info[3]: account_name = self.account_info[3] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) suid = get_suid(account_id) if suid is None: log.error(account_name + " suid获取失败") page_count = 1 video_count = 1 first_video_scid = "" unique_list = [] is_over = False need_make_download_dir = True while suid != "" and (not is_over): # 获取指定一页的视频信息 media_page = get_one_page_video_data(suid, page_count) if media_page is None: log.error(account_name + " 视频列表获取失败") tool.process_exit() # 获取视频scid列表 scid_list = get_scid_list(media_page["msg"]) if len(scid_list) == 0: log.error(account_name + " 在视频列表:%s 中没有找到视频scid" % str(media_page["msg"])) tool.process_exit() for scid in scid_list: scid = str(scid) # 检查是否已下载到前一次的图片 if first_video_scid == self.account_info[2]: is_over = True break # 新增视频导致的重复判断 if scid in unique_list: continue else: unique_list.append(scid) # 将第一个视频的id做为新的存档记录 if first_video_scid == "": first_video_scid = scid # 获取视频下载地址 video_url = get_video_url_by_video_id(scid) if video_url is None: log.error(account_name + " 第%s个视频 %s 获取下载地址失败" % (video_count, scid)) continue log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, video_url)) # 第一个视频,创建目录 if need_make_download_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建视频下载目录 %s 失败" % video_path) tool.process_exit() need_make_download_dir = False file_path = os.path.join(video_path, "%04d.mp4" % video_count) if tool.save_net_file(video_url, file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, video_url)) # 达到配置文件中的下载数量,结束 if 0 < GET_VIDEO_COUNT < video_count: is_over = True break if not is_over: if media_page["isall"]: is_over = True else: page_count += 1 log.step(account_name + " 下载完毕,总共获得%s个视频" % (video_count - 1)) # 排序 if IS_SORT and video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() # 新的存档记录 if first_video_scid != "": self.account_info[1] = str(int(self.account_info[1]) + video_count - 1) self.account_info[2] = first_video_scid # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")
def run(self): global TOTAL_IMAGE_COUNT global TOTAL_VIDEO_COUNT account_id = self.account_info[0] if len(self.account_info) >= 6 and self.account_info[5]: account_name = self.account_info[5] else: account_name = self.account_info[0] try: log.step(account_name + " 开始") # 如果需要重新排序则使用临时文件夹,否则直接下载到目标目录 if IS_SORT: image_path = os.path.join(IMAGE_TEMP_PATH, account_name) video_path = os.path.join(VIDEO_TEMP_PATH, account_name) else: image_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) video_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) image_count = 1 first_image_time = "0" need_make_image_dir = True while IS_DOWNLOAD_IMAGE: # 获取全部图片地址列表 image_url_list = get_image_url_list(account_id) if image_url_list is None: log.error(account_name + " 图片列表获取失败") break for image_url in list(image_url_list): # 不使用缩略图 image_url = image_url.split("@")[0] image_return_code, image_byte, image_response = tool.http_request(image_url) if image_return_code != 1: log.step(account_name + " 第%s张图片下载失败" % image_count) continue # 获取图片的上传时间(字符串) response_last_modified_time = tool.get_response_info(image_response.info(), "Last-Modified") # 字符串转换为时间戳 image_created_time = tool.response_time_to_timestamp(response_last_modified_time) # 检查是否已下载到前一次的图片 if int(image_created_time) <= int(self.account_info[4]): break # 将第一张图片的上传时间做为新的存档记录 if first_image_time == "0": first_image_time = str(image_created_time) log.step(account_name + " 开始下载第%s张图片 %s" % (image_count, image_url)) # 第一张图片,创建目录 if need_make_image_dir: if not tool.make_dir(image_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % image_path) tool.process_exit() need_make_image_dir = False file_type = image_url.split(".")[-1].split(":")[0] image_file_path = os.path.join(image_path, "%04d.%s" % (image_count, file_type)) save_image(image_byte, image_file_path) log.step(account_name + " 第%s张图片下载成功" % image_count) image_count += 1 # 达到配置文件中的下载数量,结束 if 0 < GET_IMAGE_COUNT < image_count: break break # 视频 video_count = 1 first_video_time = "0" need_make_video_dir = True while IS_DOWNLOAD_VIDEO: # 获取全部视频ID列表 video_id_list = get_video_id_list(account_id) if video_id_list is None: log.error(account_name + " 视频列表获取失败") break for video_id in list(video_id_list): # 获取视频的时间和下载地址 video_info = get_video_info(video_id) if video_info is None: log.error(account_name + " 第%s个视频 %s 信息获取失败" % (video_count, video_id)) continue # 检查是否已下载到前一次的视频 if int(video_info["data"]["createtime"]) <= int(self.account_info[2]): break # 将第一个视频的上传时间做为新的存档记录 if first_video_time == "0": first_video_time = str(video_info["data"]["createtime"]) # m3u8文件的地址 link_url = str(video_info["data"]["linkurl"]) # 视频的真实下载地址列表 ts_url_list = get_ts_url_list(link_url) if ts_url_list is None: log.error(account_name + " 第%s个视频下载地址列表 %s 获取失败" % (video_count, link_url)) continue log.step(account_name + " 开始下载第%s个视频 %s" % (video_count, ts_url_list)) # 第一个视频,创建目录 if need_make_video_dir: if not tool.make_dir(video_path, 0): log.error(account_name + " 创建图片下载目录 %s 失败" % video_path) tool.process_exit() need_make_video_dir = False video_file_path = os.path.join(video_path, "%04d.ts" % video_count) if save_video(ts_url_list, video_file_path): log.step(account_name + " 第%s个视频下载成功" % video_count) video_count += 1 else: log.error(account_name + " 第%s个视频 %s 下载失败" % (video_count, ts_url_list)) # 达到配置文件中的下载数量,结束 if 0 < GET_VIDEO_COUNT < video_count: break break log.step(account_name + " 下载完毕,总共获得%s张图片和%s个视频" % (image_count - 1, video_count - 1)) # 排序 if IS_SORT: if image_count > 1: destination_path = os.path.join(IMAGE_DOWNLOAD_PATH, account_name) if robot.sort_file(image_path, destination_path, int(self.account_info[3]), 4): log.step(account_name + " 图片从下载目录移动到保存目录成功") else: log.error(account_name + " 创建图片保存目录 %s 失败" % destination_path) tool.process_exit() if video_count > 1: destination_path = os.path.join(VIDEO_DOWNLOAD_PATH, account_name) if robot.sort_file(video_path, destination_path, int(self.account_info[1]), 4): log.step(account_name + " 视频从下载目录移动到保存目录成功") else: log.error(account_name + " 创建视频保存目录 %s 失败" % destination_path) tool.process_exit() if first_image_time != "0": self.account_info[3] = str(int(self.account_info[3]) + image_count - 1) self.account_info[4] = first_image_time if first_video_time != "0": self.account_info[1] = str(int(self.account_info[1]) + video_count - 1) self.account_info[2] = first_video_time # 保存最后的信息 tool.write_file("\t".join(self.account_info), NEW_SAVE_DATA_PATH) self.thread_lock.acquire() TOTAL_IMAGE_COUNT += image_count - 1 TOTAL_VIDEO_COUNT += video_count - 1 ACCOUNTS.remove(account_id) self.thread_lock.release() log.step(account_name + " 完成") except SystemExit, se: if se.code == 0: log.step(account_name + " 提前退出") else: log.error(account_name + " 异常退出")