diff --git a/src/change_url/change_url.py b/src/change_url/change_url.py index 070b1eb..cd2d113 100644 --- a/src/change_url/change_url.py +++ b/src/change_url/change_url.py @@ -1,7 +1,6 @@ import re import requests -from sqlalchemy import create_engine -from sqlalchemy import text as sql_text +from sqlalchemy import create_engine, text as sql_text try: engine = create_engine("mysql+mysqlconnector://ky_tools:HJQY35seXen8patn@1panel.stupidpz.com:3306/ky_tools") @@ -31,12 +30,6 @@ def write_file(filename, content): raise -def replace_urls(js_content, url_mapping): - for placeholder, url in url_mapping.items(): - js_content = js_content.replace(placeholder, url) - return js_content - - def get_url_mapping(urls_with_context, template_type): categories = {"WEB": [], "H5": [], "全站": [], "体育": [], "棋牌": [], "电子": [], "web": [], "h5": [], "全站APP": [], "体育APP": []} @@ -46,39 +39,35 @@ def get_url_mapping(urls_with_context, template_type): if key in categories: categories[key].append(url) - # 构建映射字典,直接返回结果 + # 构建映射字典,直接返回结果 if template_type == 'hth': url_mapping = { - "{{hthApp}}": categories.get("全站", [""])[0] if categories.get("全站", [""]) else + "hthApp": categories.get("全站", [""])[0] if categories.get("全站", [""]) else categories.get("全站APP", [""])[0], - "{{hthtyApp}}": categories.get("体育", [""])[0] if categories.get("体育", [""]) else + "hthtyApp": categories.get("体育", [""])[0] if categories.get("体育", [""]) else categories.get("体育APP", [""])[0], - "{{hthPc}}": categories.get("WEB", [""])[0] if categories.get("WEB", [""]) else categories.get("web", [""])[ + "hthWeb": categories.get("WEB", [""])[0] if categories.get("WEB", [""]) else categories.get("web", [""])[ 0], - "{{hthH5}}": categories.get("H5", [""])[0] if categories.get("H5", [""]) else categories.get("h5", [""])[0] + "hthH5": categories.get("H5", [""])[0] if categories.get("H5", [""]) else categories.get("h5", [""])[0] } elif template_type == 'ky': # ky模板 url_mapping = { - "{{kyPc}}": categories.get("WEB", [""])[0], - "{{kyPc2}}": categories.get("WEB", ["", ""])[1], - "{{kyH51}}": categories.get("H5", [""])[0], - "{{kyH52}}": categories.get("H5", ["", ""])[1], - "{{kyApp1}}": categories.get("全站", [""])[0], - "{{kyApp2}}": categories.get("体育", [""])[0], - # "{{kyApp2}}": categories.get("全站", ["", ""])[1], - "{{kyTy1}}": categories.get("体育", [""])[0], - "{{kyTy2}}": categories.get("全站", [""])[0], - # "{{kyTy2}}": categories.get("体育", ["", ""])[1] + "kyWeb": categories.get("WEB", [""])[0], + "kyFljWeb": categories.get("WEB", ["", ""])[1], + "kyH5": categories.get("H5", [""])[0], + "kyFljH5": categories.get("H5", ["", ""])[1], + "kyApp": categories.get("全站", [""])[0], + "kyTyApp": categories.get("体育", [""])[0], } elif template_type == 'jy': # jy模板 url_mapping = { - "{{jyPc}}": categories.get("WEB", [""])[0], - "{{jyH5}}": categories.get("H5", [""])[0], - "{{jyApp}}": categories.get("全站", [""])[0], - "{{jyQp}}": categories.get("棋牌", [""])[0], - "{{jyDz}}": categories.get("电子", [""])[0], - "{{jyFljPc}}": categories.get("WEB", [""])[1] if len(categories.get("WEB", [""])) == 2 else '', - "{{jyFljH5}}": categories.get("H5", [""])[1] if len(categories.get("H5", [""])) == 2 else '', + "jyWeb": categories.get("WEB", [""])[0], + "jyH5": categories.get("H5", [""])[0], + "jyApp": categories.get("全站", [""])[0], + "jyQp": categories.get("棋牌", [""])[0], + "jyDz": categories.get("电子", [""])[0], + "jyFljPc": categories.get("WEB", ["", ""])[1], + "jyFljH5": categories.get("H5", ["", ""])[1] } else: print('未知模板') @@ -88,8 +77,6 @@ def get_url_mapping(urls_with_context, template_type): def extract_urls(text): - # seo_part_index = text.find("SEO 防拦截域名") - # relevant_text = text[:seo_part_index] if seo_part_index != -1 else text pattern = r'(?:\n|^)\s*([^\n]*?)\s*(https?://[^\s]+)' # 查找到所有的url url_type = 'unknown' @@ -99,7 +86,7 @@ def extract_urls(text): try: response = requests.get(url, headers={ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}) - if response.status_code == 200: + if response.status_code == 200 or "开云" in text: if "开云" in response.text or ky_base64 in response.text or "开云" in text: url_type = 'ky' elif '华体会' in response.text: @@ -113,10 +100,7 @@ def extract_urls(text): update_agent_url(url_list, url_type) break except requests.RequestException: - print(f"URL '{url}' 不可用,跳过。") - - else: - print(f"未找到匹配的关键词。") + print(f"URL '{url}' 不可用,跳过。") return [(context.strip(), url) for context, url in url_list], url_type, is_agent @@ -124,48 +108,109 @@ def update_agent_url(url_list, url_type): for context, url in url_list: if "代理web" in context.replace(" ", "").lower(): try: - update_stmt = sql_text(f"UPDATE ky_tools.ky_account SET url = :url WHERE type = :url_type") - connection.execute(update_stmt, {"url": url, "url_type": url_type}) - connection.commit() + with engine.connect() as connection: + update_stmt = sql_text("UPDATE ky_tools.ky_account SET url = :url WHERE type = :url_type") + connection.execute(update_stmt, {"url": url, "url_type": url_type}) + connection.commit() except Exception as e: - print(f"执行SQL语句出错:{e}") - finally: - connection.close() + print(f"执行SQL语句出错: {e}") break +def update_js_content(url_mapping, url_type): + js_file = 'template/link.js' + + # 读取现有的 link.js 文件内容 + try: + with open(js_file, 'r') as file: + existing_content = file.read() + except FileNotFoundError: + # 如果文件不存在,则创建一个新的 JavaScript 文件 + existing_content = """ +const codes = { + 'hth': '3016341', + 'ky': '97238304', + 'jy': '90413847' +}; + +const links = { + 'hth': { + 'hthWeb': 'https://www.3wmnr0.vip:8005/register?i_code='+codes['hth'], + 'hthH5': 'https://www.95b8ns.vip:8002/entry/register?i_code='+codes['hth'], + 'hthApp': 'https://www.jv4n7s1.com:8000/?i_code='+codes['hth'], + 'hthtyApp': 'https://www.cmqd0v.vip:9515/?i_code='+codes['hth'], + }, + 'ky': { + 'kyWeb': 'https://www.2ezmig.com:9501/register?i_code='+codes['ky'], + 'kyH5': 'https://www.u3hnhg.com:9970/entry/register?i_code='+codes['ky'], + 'kyApp': 'https://www.1wpeq.vip:8663/?i_code='+codes['ky'], + 'kyTyApp': 'https://www.3djjf.vip:9512/?i_code='+codes['ky'], + 'kyFljWeb': 'https://www.qpiao.vip:9006/register?i_code='+codes['ky'], + 'kyFljH5': 'https://www.7ig4jf.com:8443/entry/register?i_code='+codes['ky'], + +}, + 'jy': { + 'jyWeb': 'https://www.1wv3jq.com:9008/register/?i_code='+codes['jy'], + 'jyH5': 'https://www.1j5voy.vip:9113/entry/register?i_code='+codes['jy'], + 'jyApp': 'https://www.shbb1.vip:9511/?i_code='+codes['jy'], + 'jyQp': 'https://www.3a10en.vip:9077/?i_code='+codes['jy'], + 'jyDz': 'https://www.3z9tsr.vip:9964/?i_code='+codes['jy'], + 'jyFljPc': 'https://www.az8z32.vip:8002/register/?i_code='+codes['jy'], + 'jyFljH5': 'https://www.c053i2.vip:6004/entry/register?i_code='+codes['jy'], + } +}; +function visit(platform, key) { + window.open(links[platform][key]); +} + +""" + + # 使用正则表达式匹配和替换链接的域名部分 + for key, value in url_mapping.items(): + print(f"key: {key}, value: {value}") + pattern = re.compile(r"'{}':?\s*'(https?://[^/]+)(/[^']*)'".format(key)) + existing_content = pattern.sub(lambda match: "'{0}': '{1}{2}'".format(key, value, match.group(2)), + existing_content) + + + # 将更新后的内容写回到 combined.js 文件中 + with open(js_file, 'w') as file: + file.write(existing_content) + + def change_url(text): try: urls_with_context, url_type, is_agent = extract_urls(text) - expected_filename = None if url_type == 'unknown': return False, 'unknown.js' if not is_agent: - template_path = f'/www/wwwroot/change_url/{url_type}.js.template' - js_content = read_file(template_path) url_mapping = get_url_mapping(urls_with_context, url_type) - updated_content = replace_urls(js_content, url_mapping) - expected_filename = template_path.replace('.template', '') - write_file(expected_filename, updated_content) - - return True, expected_filename + update_js_content(url_mapping, url_type) + return True, url_type return True, '代理更新' except Exception as e: print(f"An error occurred: {e}") - return False, expected_filename if expected_filename else 'unknown.js' + return False, 'unknown.js' +# 主程序入口 if __name__ == '__main__': - text = '''代理 web https://www.krb01.com:8002   -代理 web https://www.k7hhk.com:9015    -代理 web https://www.ku1fs.com:9966     -代理H5 https://www.lwa5x.com:8004 -代理H5 https://www.libsz.com:9009   -代理H5   https://www.lqqgl.com:9152''' - # res = requests.get("https://www.e5vj65s.com:9961") - # print(res) - # print("华体会" in res.text) - # print("开云" in res.text) - # print("九游" in res.text) + try: + text = ''' +九游娱乐 +SEO 专用域名 + +WEB https://www.1wv3jq.com:9008 【新】 +H5 https://www.1j5voy.vip:9113 【新】 +全站 https://www.shbb1.vip:9511   【新】 +棋牌 https://www.3a10en.vip:9077 【新】 +电子 https://www.3z9tsr.vip:9964 【新】 + +九游娱乐【SEO防拦截域名】 +WEB https://www.az8z32.vip:8002 【新】 +H5 https://www.c053i2.vip:6004 【新】 +''' - change_url(text) + change_url(text) + except Exception as e: + print(f"An error occurred in the main program: {e}") diff --git a/src/change_url/hth.js b/src/change_url/hth.js deleted file mode 100644 index dcf6d77..0000000 --- a/src/change_url/hth.js +++ /dev/null @@ -1,17 +0,0 @@ -const hthCode = '3016341' - -var hth_link = { - hthApp: '{{hthApp}}/?i_code='+hthCode, - hthtyApp: '{{hthtyApp}}/?i_code='+hthCode, - hthPc: '{{hthPc}}/register/?i_code='+hthCode, - hthH5: '{{hthH5}}/entry/register?i_code='+hthCode, -} - -function visit_hth(key) { - window['open'](hth_link[key]); - try { - LA.track(key); - } catch (error) { - console.error('An error occurred while tracking:', error); - } -} \ No newline at end of file diff --git a/src/change_url/ky.js b/src/change_url/ky.js deleted file mode 100644 index e6a5358..0000000 --- a/src/change_url/ky.js +++ /dev/null @@ -1,20 +0,0 @@ -const kyCode = '97238304' - -var ky_link = { - kyApp1: '{{kyApp1}}/?i_code='+kyCode, - kyApp2: '{{kyApp2}}/?i_code='+kyCode, - kyPc1: '{{kyPc1}}/register/?i_code='+kyCode, - kyPc2: '{{kyPc2}}/register?i_code='+kyCode, - kyH51: '{{kyH51}}/entry/register?i_code='+kyCode, - kyH52: '{{kyH52}}/entry/register?i_code='+kyCode -} - - -function visit_ky(key) { - window['open'](ky_link[key]); - try { - LA.track(key); - } catch (error) { - console.error('An error occurred while tracking:', error); - } -} \ No newline at end of file diff --git a/src/change_url/template/hth.js.template b/src/change_url/template/hth.js.template deleted file mode 100644 index 8223739..0000000 --- a/src/change_url/template/hth.js.template +++ /dev/null @@ -1,12 +0,0 @@ -const hthCode = '3016341' - -var hth_link = { - hthApp: '{{hthApp}}/?i_code='+hthCode, - hthtyApp: '{{hthtyApp}}/?i_code='+hthCode, - hthPc: '{{hthPc}}/register/?i_code='+hthCode, - hthH5: '{{hthH5}}/entry/register?i_code='+hthCode, -} - -function visit_hth(key) { - window['open'](hth_link[key]); -} \ No newline at end of file diff --git a/src/change_url/template/jy.js.template b/src/change_url/template/jy.js.template deleted file mode 100644 index ee3668d..0000000 --- a/src/change_url/template/jy.js.template +++ /dev/null @@ -1,16 +0,0 @@ -const jyCode = '90413847' - -var jy_link = { - jyPc: '{{jyPc}}/register/?i_code='+jyCode, - jyH5: '{{jyH5}}/entry/register?i_code='+jyCode, - jyApp: '{{jyApp}}/?i_code='+jyCode, - jyQp:'{{jyQp}}/?i_code='+jyCode, - jyDz:'{{jyDz}}/?i_code='+jyCode, - jyFljPc:'{{jyFljPc}}/register/?i_code='+jyCode, - jyFljH5:'{{jyFljH5}}/entry/register?i_code='+jyCode -} - - -function visit_jy(key) { - window['open'](jy_link[key]); -} \ No newline at end of file diff --git a/src/change_url/template/ky.js.template b/src/change_url/template/ky.js.template deleted file mode 100644 index f93c38a..0000000 --- a/src/change_url/template/ky.js.template +++ /dev/null @@ -1,15 +0,0 @@ -const kyCode = '97238304' - -var ky_link = { - kyApp1: '{{kyApp1}}/?i_code='+kyCode, - kyApp2: '{{kyApp2}}/?i_code='+kyCode, - kyPc1: '{{kyPc1}}/register/?i_code='+kyCode, - kyPc2: '{{kyPc2}}/register?i_code='+kyCode, - kyH51: '{{kyH51}}/entry/register?i_code='+kyCode, - kyH52: '{{kyH52}}/entry/register?i_code='+kyCode -} - - -function visit_ky(key) { - window['open'](ky_link[key]); -} \ No newline at end of file