Skip to content

Commit

Permalink
Merge pull request #302 from Guovin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Guovin authored Sep 9, 2024
2 parents 71a895f + 3a7114c commit 9b6e61e
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 164 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ jobs:
elif [[ -f "output/result.log" ]]; then
git add -f "output/result.log"
fi
if [[ -f "updates/fofa/fofa_hotel_region_result.pkl" ]]; then
git add -f "updates/fofa/fofa_hotel_region_result.pkl"
fi
if [[ -f "updates/fofa/fofa_multicast_region_result.pkl" ]]; then
git add -f "updates/fofa/fofa_multicast_region_result.pkl"
fi
if ! git diff --staged --quiet; then
git commit -m "Github Action Auto Updated"
git push --force
Expand Down
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ opencc-python-reimplemented = "*"
fake-useragent = "*"
gunicorn = "*"
pillow = "*"
rapidfuzz = "*"

[requires]
python_version = "3.8"
117 changes: 1 addition & 116 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ async def visit_page(self, channel_names=None):
continue
if config.getboolean("Settings", setting):
if setting == "open_subscribe":
subscribe_urls = config.get("Settings", "subscribe_urls").split(",")
subscribe_urls = [
url.strip()
for url in config.get("Settings", "subscribe_urls").split(",")
if url.strip()
]
task = asyncio.create_task(
task_func(subscribe_urls, callback=self.update_progress)
)
Expand Down
2 changes: 1 addition & 1 deletion tkinter_ui/hotel.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def init_ui(self, root):
self.region_list_label.pack(side=tk.LEFT, padx=4, pady=8)
regions = ["全部"] + list(getattr(fofa_map, "region_url").keys())
region_selected_values = [
value
value.strip()
for value in config.get("Settings", "hotel_region_list").split(",")
if value.strip()
]
Expand Down
2 changes: 1 addition & 1 deletion tkinter_ui/multicast.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def init_ui(self, root):
regions.remove("全部")
regions.insert(0, "全部")
region_selected_values = [
value
value.strip()
for value in config.get("Settings", "multicast_region_list").split(",")
if value.strip()
]
Expand Down
2 changes: 1 addition & 1 deletion tkinter_ui/select_combobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def on_select(self, event):

def on_text_change(self, event):
text_value = self.get().strip()
value_list = [value.strip() for value in text_value.split(",")]
value_list = [value.strip() for value in text_value.split(",") if value.strip()]
self.selected_values = [
value for value in self.selected_values if value in value_list
]
Expand Down
2 changes: 2 additions & 0 deletions tkinter_ui/tkinter_ui.spec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ a = Analysis(
('../config/rtp', 'config/rtp'),
('../updates/multicast/multicast_map.json', 'updates/multicast'),
('../updates/multicast/multicast_region_result.json', 'updates/multicast'),
('../updates/fofa/fofa_hotel_region_result.pkl', 'updates/fofa'),
('../updates/fofa/fofa_multicast_region_result.pkl', 'updates/fofa'),
('../static/images/favicon.ico', 'static/images'),
('../static/images/appreciate.jpg', 'static/images'),
('../static/images/settings_icon.png', 'static/images'),
Expand Down
Binary file added updates/fofa/fofa_hotel_region_result.pkl
Binary file not shown.
Binary file added updates/fofa/fofa_multicast_region_result.pkl
Binary file not shown.
70 changes: 63 additions & 7 deletions updates/fofa/request.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from utils.config import config
from utils.config import config, resource_path
from tqdm.asyncio import tqdm_asyncio
from time import time
from requests import get
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed
import updates.fofa.fofa_map as fofa_map
from driver.setup import setup_driver
import re
Expand All @@ -12,6 +12,8 @@
from updates.proxy import get_proxy, get_proxy_next
from requests_custom.utils import get_source_requests, close_session
from collections import defaultdict
import pickle
import threading

timeout = 10

Expand All @@ -20,7 +22,11 @@ def get_fofa_urls_from_region_list():
"""
Get the FOFA url from region
"""
region_list = config.get("Settings", "hotel_region_list").split(",")
region_list = [
region.strip()
for region in config.get("Settings", "hotel_region_list").split(",")
if region.strip()
]
urls = []
region_url = getattr(fofa_map, "region_url")
if "all" in region_list or "ALL" in region_list or "全部" in region_list:
Expand All @@ -32,6 +38,30 @@ def get_fofa_urls_from_region_list():
return urls


def update_fofa_region_result_tmp(result, multicast=False):
"""
Update fofa region result tmp
"""
with open(
resource_path(
f"updates/fofa/fofa_{'multicast' if multicast else 'hotel'}_region_result.pkl"
),
"wb",
) as file:
pickle.dump(result, file)


def get_fofa_region_result_tmp(multicast: False):
with open(
resource_path(
f"updates/fofa/fofa_{'multicast' if multicast else 'hotel'}_region_result.pkl"
),
"rb",
) as file:
result = pickle.load(file)
return result


async def get_channels_by_fofa(urls=None, multicast=False, callback=None):
"""
Get the channel by FOFA
Expand All @@ -57,9 +87,12 @@ async def get_channels_by_fofa(urls=None, multicast=False, callback=None):
if open_proxy:
test_url = fofa_urls[0][0] if multicast else fofa_urls[0]
proxy = await get_proxy(test_url, best=True, with_test=True)
cancel_event = threading.Event()

def process_fofa_channels(fofa_info):
nonlocal proxy, fofa_urls_len, open_driver, open_sort
nonlocal proxy, fofa_urls_len, open_driver, open_sort, cancel_event
if cancel_event.is_set():
return {}
fofa_url = fofa_info[0] if multicast else fofa_info
results = defaultdict(lambda: defaultdict(list))
try:
Expand All @@ -79,6 +112,9 @@ def process_fofa_channels(fofa_info):
page_source = retry_func(
lambda: get_source_requests(fofa_url), name=fofa_url
)
if "资源访问每天限制" in page_source:
cancel_event.set()
raise ValueError("Limited access to fofa page")
fofa_source = re.sub(r"<!--.*?-->", "", page_source, flags=re.DOTALL)
urls = set(re.findall(r"https?://[\w\.-]+:\d+", fofa_source))
if multicast:
Expand All @@ -94,6 +130,9 @@ def process_fofa_channels(fofa_info):
]
for future in futures:
results = merge_objects(results, future.result())
return results
except ValueError as e:
raise e
except Exception as e:
print(e)
finally:
Expand All @@ -107,15 +146,32 @@ def process_fofa_channels(fofa_info):
f"正在获取Fofa{mode_name}源, 剩余{remain}个查询地址待获取, 预计剩余时间: {get_pbar_remaining(n=pbar.n, total=pbar.total, start_time=start_time)}",
int((pbar.n / fofa_urls_len) * 100),
)
return results

max_workers = 3 if open_driver else 10
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [
executor.submit(process_fofa_channels, fofa_url) for fofa_url in fofa_urls
]
for future in futures:
fofa_results = merge_objects(fofa_results, future.result())
try:
for future in as_completed(futures):
fofa_results = merge_objects(fofa_results, future.result())
except ValueError as e:
if "Limited access to fofa page" in str(e):
for future in futures:
future.cancel()
fofa_results = {}
if fofa_results:
update_fofa_region_result_tmp(fofa_results, multicast=multicast)
else:
fofa_results = get_fofa_region_result_tmp(multicast=multicast)
print(fofa_results)
pbar.n = fofa_urls_len
pbar.update(0)
if callback:
callback(
f"正在获取Fofa{mode_name}源",
100,
)
if not open_driver:
close_session()
pbar.close()
Expand Down
6 changes: 5 additions & 1 deletion updates/hotel/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ async def get_channels_by_hotel(callback=None):
open_proxy = config.getboolean("Settings", "open_proxy")
open_driver = config.getboolean("Settings", "open_driver")
page_num = config.getint("Settings", "hotel_page_num")
region_list = config.get("Settings", "hotel_region_list").split(",")
region_list = [
region.strip()
for region in config.get("Settings", "hotel_region_list").split(",")
if region.strip()
]
if "all" in region_list or "ALL" in region_list or "全部" in region_list:
fofa_region_name_list = list(getattr(fofa_map, "region_url").keys())
region_list = fofa_region_name_list
Expand Down
6 changes: 5 additions & 1 deletion updates/multicast/update_tmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def get_multicast_region_result_by_rtp_txt(callback=None):
Get multicast region result by rtp txt
"""
rtp_path = resource_path("config/rtp")
config_region_list = set(config.get("Settings", "multicast_region_list").split(","))
config_region_list = set(
region.strip()
for region in config.get("Settings", "multicast_region_list").split(",")
if region.strip()
)
rtp_file_list = [
filename.rsplit(".", 1)[0]
for filename in os.listdir(rtp_path)
Expand Down
2 changes: 1 addition & 1 deletion updates/subscribe/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def get_channels_by_subscribe_urls(
subscribe_results = {}
pattern = r"^(.*?),(?!#genre#)(.*?)$"
subscribe_urls = [
url
url.strip()
for url in config.get("Settings", "subscribe_urls").split(",")
if url.strip()
]
Expand Down
Loading

0 comments on commit 9b6e61e

Please sign in to comment.