Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Guovin committed Jul 4, 2024
1 parent 2dda283 commit 7c1a6f1
Show file tree
Hide file tree
Showing 20 changed files with 1,026 additions and 986 deletions.
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,6 @@ Fork 本项目并开启工作流更新

[更新日志](./CHANGELOG.md)

## 免责声明

本项目是为了提供编程学习和研究的资源。项目中收集的数据来源于网络,开发者不对数据的准确性、完整性或可靠性做任何保证。

开发者不对任何可能因使用这些代码或数据而产生的任何直接或间接损失负责。使用者应自行判断其使用的合法性和风险。

本项目的代码和数据仅供学习和研究使用,不得用于任何商业用途。任何人或组织在使用时,应遵守相关法律法规,尊重并保护开发者的权益。

如果您使用了本项目的代码或数据,即表示您已了解并同意此免责声明。如果您不同意此免责声明,您应立即停止使用本项目的代码和数据。

此外,本项目的代码和数据可能会不定期进行更新,但不保证更新的及时性和准确性,也不保证代码的稳定性和功能性。

在任何情况下,因使用或无法使用本项目的代码或数据所产生的任何损害或其他责任,开发者和任何贡献者都不承担任何责任。

使用本项目的代码或数据即表示您已经了解并接受这些条款。

## 许可证

[MIT](./LICENSE) License © 2024-PRESENT [Govin](https://github.com/guovin)
Expand Down
16 changes: 0 additions & 16 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,6 @@ If you don't want to bother, and my configuration just meets your needs, you can

[Changelog](./CHANGELOG.md)

## Disclaimer

This project is provided for programming learning and research resources. The data collected in the project comes from the network, and the developer does not make any guarantees about the accuracy, completeness, or reliability of the data.

The developer is not responsible for any direct or indirect losses that may be caused by the use of these codes or data. Users should judge the legality and risk of their use by themselves.

The code and data of this project are only for learning and research use, and must not be used for any commercial purposes. Anyone or organization should abide by relevant laws and regulations when using it, respect and protect the rights and interests of developers.

If you use the code or data of this project, it means that you have understood and agreed to this disclaimer. If you do not agree with this disclaimer, you should stop using the code and data of this project immediately.

In addition, the code and data of this project may be updated irregularly, but there is no guarantee of the timeliness and accuracy of the update, nor the stability and functionality of the code.

In any case, the developer and any contributor do not assume any responsibility for any damage or other liability caused by the use or inability to use the code or data of this project.

Using the code or data of this project means that you have understood and accepted these terms.

## License

[MIT](./LICENSE) License © 2024-PRESENT [Govin](https://github.com/guovin)
Expand Down
Empty file added driver/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions driver/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from selenium import webdriver


def setup_driver(proxy=None):
"""
Setup the driver for selenium
"""
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("blink-settings=imagesEnabled=false")
options.add_argument("--log-level=3")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--allow-running-insecure-content")
options.add_argument("blink-settings=imagesEnabled=false")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
if proxy:
options.add_argument("--proxy-server=%s" % proxy)
driver = webdriver.Chrome(options=options)
return driver
Empty file added fofa/__init__.py
Empty file.
116 changes: 116 additions & 0 deletions fofa/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from utils.config import get_config
from tqdm.asyncio import tqdm_asyncio
from time import time
from asyncio import Queue, get_running_loop
from request import get
from concurrent.futures import ThreadPoolExecutor
import fofa_map
from driver.setup import setup_driver
import re
from utils.retry import retry_func
from utils.channel import format_channel_name
from utils.utils import merge_objects, get_pbar_remaining

config = get_config()
timeout = 10


def get_fofa_urls_from_region_list():
"""
Get the FOFA url from region
"""
region_list = getattr(config, "region_list", [])
urls = []
region_url = getattr(fofa_map, "region_url")
if "all" in region_list:
urls = [url for url in region_url.values() if url]
else:
for region in region_list:
if region in region_url:
urls.append(region_url[region])
return urls


async def get_channels_by_fofa(callback):
"""
Get the channel by FOFA
"""
fofa_urls = get_fofa_urls_from_region_list()
fofa_urls_len = len(fofa_urls)
pbar = tqdm_asyncio(total=fofa_urls_len)
start_time = time()
fofa_results = {}
pbar.set_description(f"Processing multicast, {fofa_urls_len} regions remaining")
callback(f"正在获取组播源更新, 共{fofa_urls_len}个地区", 0)
fofa_queue = Queue()
for fofa_url in fofa_urls:
await fofa_queue.put(fofa_url)

def process_fofa_channels(fofa_url, fofa_urls_len, callback):
driver = setup_driver()
try:
retry_func(lambda: driver.get(fofa_url), name=fofa_url)
driver.get(fofa_url)
fofa_source = re.sub(r"<!--.*?-->", "", driver.page_source, flags=re.DOTALL)
urls = set(re.findall(r"https?://[\w\.-]+:\d+", fofa_source))
channels = {}
for url in urls:
try:
final_url = url + "/iptv/live/1000.json?key=txiptv"
# response = retry_func(
# lambda: get(final_url, timeout=timeout),
# name=final_url,
# )
response = get(final_url, timeout=timeout)
try:
json_data = response.json()
if json_data["code"] == 0:
try:
for item in json_data["data"]:
if isinstance(item, dict):
item_name = format_channel_name(
item.get("name")
)
item_url = item.get("url").strip()
if item_name and item_url:
total_url = url + item_url
if item_name not in channels:
channels[item_name] = [total_url]
else:
channels[item_name].append(total_url)
except Exception as e:
# print(f"Error on fofa: {e}")
continue
except Exception as e:
# print(f"{url}: {e}")
continue
except Exception as e:
# print(f"{url}: {e}")
continue
merge_objects(fofa_results, channels)
except Exception as e:
# print(e)
pass
finally:
fofa_queue.task_done()
pbar.update()
remain = fofa_urls_len - pbar.n
pbar.set_description(f"Processing multicast, {remain} regions remaining")
callback(
f"正在获取组播源更新, 剩余{remain}个地区待获取, 预计剩余时间: {get_pbar_remaining(pbar, start_time)}",
int((pbar.n / fofa_urls_len) * 100),
)
if config.open_online_search and pbar.n / fofa_urls_len == 1:
callback("正在获取在线搜索结果, 请耐心等待", 0)
driver.quit()

with ThreadPoolExecutor(max_workers=10) as pool:
while not fofa_queue.empty():
loop = get_running_loop()
fofa_url = await fofa_queue.get()
loop.run_in_executor(
pool, process_fofa_channels, fofa_url, fofa_urls_len, callback
)
print("Finish processing fofa url")
pbar.close()
return fofa_results
26 changes: 10 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import asyncio
from utils import (
from utils.config import get_config
from utils.channel import (
get_channel_items,
update_channel_urls_txt,
format_channel_name,
)
from utils.utils import (
update_file,
sort_urls_by_speed_and_resolution,
get_total_urls_from_info_list,
get_channels_by_subscribe_urls,
check_url_by_patterns,
get_channels_by_fofa,
get_channels_by_online_search,
format_channel_name,
resource_path,
load_external_config,
get_pbar_remaining,
get_ip_address,
)
from utils.speed import sort_urls_by_speed_and_resolution, get_total_urls_from_info_list
from subscribe import get_channels_by_subscribe_urls
from fofa import get_channels_by_fofa
from online_search import get_channels_by_online_search
import logging
from logging.handlers import RotatingFileHandler
import os
Expand All @@ -24,13 +24,7 @@
from flask import Flask, render_template_string
import sys

config_path = resource_path("user_config.py")
default_config_path = resource_path("config.py")
config = (
load_external_config("user_config.py")
if os.path.exists(config_path)
else load_external_config("config.py")
)
config = get_config()

app = Flask(__name__)

Expand Down
Empty file added online_search/__init__.py
Empty file.
Loading

0 comments on commit 7c1a6f1

Please sign in to comment.