Skip to content

Commit

Permalink
feat:update_time_position(#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guovin committed Jan 8, 2025
1 parent b24e20a commit 9bf6a3a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ https://cdn.jsdelivr.net/gh/Guovin/iptv-api@gd/source.json
| source_file | 模板文件路径 | config/demo.txt |
| subscribe_num | 结果中偏好的订阅源接口数量 | 10 |
| urls_limit | 单个频道接口数量 | 10 |
| update_time_position | 更新时间显示位置,需要开启 open_update_time 才能生效,可选值:top、bottom,top: 显示于结果顶部,bottom: 显示于结果底部 | top |

## 快速上手

Expand Down
1 change: 1 addition & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ https://cdn.jsdelivr.net/gh/Guovin/iptv-api@gd/source.json
| source_file | Template file path | config/demo.txt |
| subscribe_num | The number of preferred subscribe source interfaces in the results | 10 |
| urls_limit | Number of interfaces per channel | 10 |
| update_time_position | Update time display position, need to enable open_update_time to take effect, optional values: top, bottom, top: display at the top of the result, bottom: display at the bottom of the result | top |

## Quick Start

Expand Down
4 changes: 3 additions & 1 deletion config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ source_file = config/demo.txt
# 结果中偏好的订阅源接口数量 | Preferred number of subscription source interfaces in the result
subscribe_num = 10
# 单个频道接口数量 | Number of interfaces per channel
urls_limit = 10
urls_limit = 10
# 更新时间显示位置,需要开启 open_update_time 才能生效,可选值:top、bottom,top: 显示于结果顶部,bottom: 显示于结果底部 | Update time display position, need to enable open_update_time to take effect, optional values: top, bottom, top: display at the top of the result, bottom: display at the bottom of the result
update_time_position = top
3 changes: 2 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
| sort_timeout | 单个接口测速超时时长,单位秒(s);数值越大测速所属时间越长,能提高获取接口数量,但质量会有所下降;数值越小测速所需时间越短,能获取低延时的接口,质量较好;调整此值能优化更新时间 | 10 |
| source_file | 模板文件路径 | config/demo.txt |
| subscribe_num | 结果中偏好的订阅源接口数量 | 10 |
| urls_limit | 单个频道接口数量 | 10 |
| urls_limit | 单个频道接口数量 | 10 |
| update_time_position | 更新时间显示位置,需要开启 open_update_time 才能生效,可选值:top、bottom,top: 显示于结果顶部,bottom: 显示于结果底部 | top |
3 changes: 2 additions & 1 deletion docs/config_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
| sort_timeout | The timeout duration for speed testing of a single interface, in seconds (s). A larger value means a longer testing period, which can increase the number of interfaces obtained but may decrease their quality. A smaller value means a shorter testing time, which can obtain low-latency interfaces with better quality. Adjusting this value can optimize the update time. | 10 |
| source_file | Template file path | config/demo.txt |
| subscribe_num | The number of preferred subscribe source interfaces in the results | 10 |
| urls_limit | Number of interfaces per channel | 10 |
| urls_limit | Number of interfaces per channel | 10 |
| update_time_position | Update time display position, need to enable open_update_time to take effect, optional values: top, bottom, top: display at the top of the result, bottom: display at the bottom of the result | top |
18 changes: 18 additions & 0 deletions tkinter_ui/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ def init_ui(self, root):
)
self.open_update_time_checkbutton.pack(side=tk.LEFT, padx=4, pady=8)

self.update_time_position_label = tk.Label(
frame_default_open_update_info_column1, text="位置:", width=3
)
self.update_time_position_label.pack(side=tk.LEFT, padx=4, pady=8)
self.update_time_position_combo = ttk.Combobox(frame_default_open_update_info_column1, width=5)
self.update_time_position_combo.pack(side=tk.LEFT, padx=4, pady=8)
self.update_time_position_combo["values"] = ("顶部", "底部")
if config.update_time_position == "bottom":
self.update_time_position_combo.current(1)
else:
self.update_time_position_combo.current(0)
self.update_time_position_combo.bind("<<ComboboxSelected>>", self.update_update_time_position)

self.open_url_info_label = tk.Label(
frame_default_open_update_info_column2, text="显示接口信息:", width=12
)
Expand Down Expand Up @@ -450,6 +463,10 @@ def update_ipv6_support(self):
def update_ipv_type(self, event):
config.set("Settings", "ipv_type", self.ipv_type_combo.get())

def update_update_time_position(self, event):
config.set("Settings", "update_time_position",
'bottom' if self.update_time_position_combo.get() == '底部' else 'top')

def edit_whitelist_file(self):
path = resource_path(constants.whitelist_path)
if os.path.exists(path):
Expand Down Expand Up @@ -478,6 +495,7 @@ def change_entry_state(self, state):
"open_keep_all_checkbutton",
"open_m3u_result_checkbutton",
"urls_limit_entry",
"update_time_position_combo",
"open_update_time_checkbutton",
"open_url_info_checkbutton",
"open_empty_category_checkbutton",
Expand Down
41 changes: 22 additions & 19 deletions utils/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,23 +634,11 @@ def write_channel_to_file(data, ipv6=False, callback=None):
if any(pref in ipv_type_prefer for pref in ["自动", "auto"]) or not ipv_type_prefer:
ipv_type_prefer = ["ipv6", "ipv4"] if ipv6 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)
write_content_into_txt("", path)
first_cate = True
for cate, channel_obj in data.items():
print(f"\n{cate}:", end=" ")
write_content_into_txt(f"{cate},#genre#", path)
write_content_into_txt(f"{'\n\n' if not first_cate else ''}{cate},#genre#", path)
first_cate = False
channel_obj_keys = channel_obj.keys()
names_len = len(list(channel_obj_keys))
for i, name in enumerate(channel_obj_keys):
Expand All @@ -663,17 +651,32 @@ def write_channel_to_file(data, ipv6=False, callback=None):
no_result_name.append(name)
continue
for url in channel_urls:
write_content_into_txt(f"{name},{url}", path, callback=callback)
write_content_into_txt(f"\n{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)
write_content_into_txt("\n\n🈳无结果频道,#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)
write_content_into_txt(f"\n{name},url", path)
print()
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"
)
update_time_position = config.update_time_position
write_content_into_txt(
f"{'\n\n' if update_time_position == 'bottom' else ''}🕘️更新时间,#genre#\n{update_time},{update_time_url}{'\n' if update_time_position == 'top' else ''}",
path,
position=update_time_position)
except Exception as e:
print(f"❌ Write channel to file failed: {e}")

Expand Down
4 changes: 4 additions & 0 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ def app_port(self):
def open_supply(self):
return self.config.getboolean("Settings", "open_supply", fallback=True)

@property
def update_time_position(self):
return self.config.get("Settings", "update_time_position", fallback="top")

def load(self):
"""
Load the config
Expand Down
11 changes: 7 additions & 4 deletions utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,19 @@ def resource_path(relative_path, persistent=False):
return total_path


def write_content_into_txt(content, path=None, newline=True, callback=None):
def write_content_into_txt(content, path=None, position=None, callback=None):
"""
Write content into txt file
"""
if not path:
return

with open(path, "a", encoding="utf-8") as f:
if newline:
f.write(f"\n{content}")
mode = "r+" if position == "top" else "a"
with open(path, mode, encoding="utf-8") as f:
if position == "top":
existing_content = f.read()
f.seek(0, 0)
f.write(f"{content}\n{existing_content}")
else:
f.write(content)

Expand Down

0 comments on commit 9bf6a3a

Please sign in to comment.