From 2e6e31e223a6e84ae06a5bf3427a8af35b31e257 Mon Sep 17 00:00:00 2001 From: "guorong.zheng" <360996299@qq.com> Date: Wed, 18 Dec 2024 17:53:19 +0800 Subject: [PATCH] chore:update_time default url --- utils/channel.py | 89 ++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/utils/channel.py b/utils/channel.py index 76fdbd3673..ace2a7abd6 100644 --- a/utils/channel.py +++ b/utils/channel.py @@ -602,46 +602,55 @@ def write_channel_to_file(data, ipv6=False, callback=None): """ Write channel to file """ - path = "output/result_new.txt" - if config.open_update_time: - now = datetime.datetime.now() - if os.environ.get("GITHUB_ACTIONS"): - now += datetime.timedelta(hours=8) - update_time = now.strftime("%Y-%m-%d %H:%M:%S") - write_content_into_txt(f"更新时间,#genre#", path, newline=False) - write_content_into_txt(f"{update_time},url", path) - no_result_name = [] - open_empty_category = config.open_empty_category - ipv_type_prefer = list(config.ipv_type_prefer) - if any(pref in ipv_type_prefer for pref in ["自动", "auto"]) or not ipv_type_prefer: - ipv_type_prefer = ["ipv6", "ipv4"] if (ipv6 or os.environ.get("GITHUB_ACTIONS")) else ["ipv4", "ipv6"] - origin_type_prefer = config.origin_type_prefer - for cate, channel_obj in data.items(): - print(f"\n{cate}:", end=" ") - write_content_into_txt(f"{cate},#genre#", path) - channel_obj_keys = channel_obj.keys() - names_len = len(list(channel_obj_keys)) - for i, name in enumerate(channel_obj_keys): - info_list = data.get(cate, {}).get(name, []) - channel_urls = get_total_urls(info_list, ipv_type_prefer, origin_type_prefer) - end_char = ", " if i < names_len - 1 else "" - print(f"{name}:", len(channel_urls), end=end_char) - if not channel_urls: - if open_empty_category: - no_result_name.append(name) - continue - for url in channel_urls: - write_content_into_txt(f"{name},{url}", path, callback=callback) - print() - write_content_into_txt("", path) - if open_empty_category and no_result_name: - print("\n🈳 No result channel name:") - write_content_into_txt("🈳无结果频道,#genre#", path) - for i, name in enumerate(no_result_name): - end_char = ", " if i < len(no_result_name) - 1 else "" - print(name, end=end_char) - write_content_into_txt(f"{name},url", path) - print() + try: + path = "output/result_new.txt" + no_result_name = [] + open_empty_category = config.open_empty_category + ipv_type_prefer = list(config.ipv_type_prefer) + if any(pref in ipv_type_prefer for pref in ["自动", "auto"]) or not ipv_type_prefer: + ipv_type_prefer = ["ipv6", "ipv4"] if (ipv6 or os.environ.get("GITHUB_ACTIONS")) else ["ipv4", "ipv6"] + origin_type_prefer = config.origin_type_prefer + if config.open_update_time: + now = datetime.datetime.now() + if os.environ.get("GITHUB_ACTIONS"): + now += datetime.timedelta(hours=8) + update_time = now.strftime("%Y-%m-%d %H:%M:%S") + update_time_url = next( + (get_total_urls(info_list, ipv_type_prefer, origin_type_prefer)[0] + for channel_obj in data.values() + for info_list in channel_obj.values() if info_list), + "url" + ) + write_content_into_txt(f"🕘️更新时间,#genre#", path, newline=False) + write_content_into_txt(f"{update_time},{update_time_url}", path) + for cate, channel_obj in data.items(): + print(f"\n{cate}:", end=" ") + write_content_into_txt(f"{cate},#genre#", path) + channel_obj_keys = channel_obj.keys() + names_len = len(list(channel_obj_keys)) + for i, name in enumerate(channel_obj_keys): + info_list = data.get(cate, {}).get(name, []) + channel_urls = get_total_urls(info_list, ipv_type_prefer, origin_type_prefer) + end_char = ", " if i < names_len - 1 else "" + print(f"{name}:", len(channel_urls), end=end_char) + if not channel_urls: + if open_empty_category: + no_result_name.append(name) + continue + for url in channel_urls: + write_content_into_txt(f"{name},{url}", path, callback=callback) + print() + write_content_into_txt("", path) + if open_empty_category and no_result_name: + print("\n🈳 No result channel name:") + write_content_into_txt("🈳无结果频道,#genre#", path) + for i, name in enumerate(no_result_name): + end_char = ", " if i < len(no_result_name) - 1 else "" + print(name, end=end_char) + write_content_into_txt(f"{name},url", path) + print() + except Exception as e: + print(f"❌ Write channel to file failed: {e}") def get_multicast_fofa_search_org(region, type):