Skip to content

Commit

Permalink
feat:copy_config
Browse files Browse the repository at this point in the history
  • Loading branch information
Guovin committed Aug 22, 2024
1 parent 27f6bc1 commit 35395f3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
build
build
result_new.log
2 changes: 1 addition & 1 deletion config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ multicast_region_list = 广东
multicast_page_num = 5
open_proxy = False
open_driver = False
open_hotel = False
open_hotel = True
open_hotel_tonkiang = True
open_hotel_fofa = True
hotel_region_list = 广东
Expand Down
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from utils.config import config
from utils.config import config, copy_config
from utils.channel import (
get_channel_items,
append_data_to_info_data,
Expand Down Expand Up @@ -94,6 +94,8 @@ def sort_pbar_update(self):
async def main(self):
try:
self.channel_items = get_channel_items()
if self.run_ui:
copy_config()
channel_names = [
name
for channel_obj in self.channel_items.values()
Expand Down
6 changes: 6 additions & 0 deletions tkinter_ui/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tkinter import scrolledtext
from tkinter import filedialog
import os
from utils.channel import get_channel_items


class DefaultUI:
Expand Down Expand Up @@ -305,6 +306,11 @@ def select_source_file(self):
self.source_file_entry.delete(0, tk.END)
self.source_file_entry.insert(0, filepath)
config.set("Settings", "source_file", filepath)
get_channel_items(change_source_path=True)
self.source_channels_text.delete(1.0, tk.END)
self.source_channels_text.insert(
tk.END, config.get("Settings", "source_channels")
)

def update_source_channels(self, event):
config.set(
Expand Down
35 changes: 25 additions & 10 deletions utils/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
)


def get_channel_data_from_file(channels=None, file=None, names=None, from_result=False):
def get_channel_data_from_file(
channels=None, file=None, names=None, from_result=False, change_source_path=False
):
"""
Get the channel data from the file
"""
Expand All @@ -43,15 +45,15 @@ def get_channel_data_from_file(channels=None, file=None, names=None, from_result
match = re.search(pattern, line)
if match is not None:
name = match.group(1).strip()
if name not in names:
if not change_source_path and name not in names:
continue
url = match.group(2).strip()
if url and url not in channels[current_category][name]:
channels[current_category][name].append(url)
return channels


def get_channel_items():
def get_channel_items(change_source_path=False):
"""
Get the channel items from the source file
"""
Expand All @@ -63,7 +65,10 @@ def get_channel_items():
if os.path.exists(resource_path(user_source_file)):
with open(resource_path(user_source_file), "r", encoding="utf-8") as file:
channels = get_channel_data_from_file(
channels=channels, file=file, names=source_channel_names
channels=channels,
file=file,
names=source_channel_names,
change_source_path=change_source_path,
)

if config.getboolean("Settings", "open_use_old_result") and os.path.exists(
Expand All @@ -75,14 +80,16 @@ def get_channel_items():
file=file,
names=source_channel_names,
from_result=True,
change_source_path=change_source_path,
)

channel_names = [
name for channel_obj in channels.values() for name in channel_obj.keys()
]
for source_name in source_channel_names:
if source_name not in channel_names:
channels["自定义频道"][source_name] = []
if not change_source_path:
for source_name in source_channel_names:
if source_name not in channel_names:
channels["自定义频道"][source_name] = []
total_channel_names = ",".join(
[name for channel_obj in channels.values() for name in channel_obj.keys()]
)
Expand Down Expand Up @@ -514,6 +521,10 @@ def append_all_method_data(
("online_search", online_search_result),
]:
if config.getboolean("Settings", f"open_{method}"):
if (
method == "hotel_tonkiang" or method == "hotel_fofa"
) and config.getboolean("Settings", f"open_hotel") == False:
continue
data = append_data_to_info_data(
data,
cate,
Expand Down Expand Up @@ -556,17 +567,21 @@ def append_all_method_data_keep_all(
Append all method data to total info data, keep all channel name and urls
"""
for cate, channel_obj in items:
for result_name, result in [
for method, result in [
("subscribe", subscribe_result),
("multicast", multicast_result),
("hotel_tonkiang", hotel_tonkiang_result),
("hotel_fofa", hotel_fofa_result),
("online_search", online_search_result),
]:
if result and config.getboolean("Settings", f"open_{result_name}"):
if result and config.getboolean("Settings", f"open_{method}"):
if (
method == "hotel_tonkiang" or method == "hotel_fofa"
) and config.getboolean("Settings", f"open_hotel") == False:
continue
for name, urls in result.items():
data = append_data_to_info_data(data, cate, name, urls)
print(name, f"{result_name.capitalize()} num:", len(urls))
print(name, f"{method.capitalize()} num:", len(urls))
if config.getboolean("Settings", "open_use_old_result"):
old_urls = channel_obj.get(name, [])
data = append_data_to_info_data(
Expand Down
23 changes: 22 additions & 1 deletion utils/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import configparser
import shutil


def resource_path(relative_path, persistent=False):
Expand Down Expand Up @@ -50,6 +51,26 @@ def save_config():
else "config.ini"
)
user_config_path = resource_path(user_config_file, persistent=True)
os.makedirs(os.path.dirname(user_config_path), exist_ok=True)
if not os.path.exists(user_config_path):
os.makedirs(os.path.dirname(user_config_path), exist_ok=True)
with open(user_config_path, "w", encoding="utf-8") as configfile:
config.write(configfile)


def copy_config():
user_source_file = resource_path(config.get("Settings", "source_file"))
user_config_path = resource_path("config/user_config.ini")
default_config_path = resource_path("config/config.ini")
user_config_file = (
user_config_path if os.path.exists(user_config_path) else default_config_path
)
dest_folder = os.path.join(os.getcwd(), "config")
files_to_copy = [user_source_file, user_config_file]
try:
for src_file in files_to_copy:
dest_path = os.path.join(dest_folder, os.path.basename(src_file))
if os.path.abspath(src_file) == os.path.abspath(dest_path):
continue
shutil.copy(src_file, dest_folder)
except Exception as e:
print(f"Failed to copy files: {str(e)}")

0 comments on commit 35395f3

Please sign in to comment.