megatrump commited on
Commit
3d91208
·
1 Parent(s): 28832ec

auto mixin

Browse files
Files changed (2) hide show
  1. app.py +64 -0
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
 
2
  import sys
 
3
  import requests
4
  from flask import Flask, Response, request
5
  from urllib.parse import urlencode
@@ -11,6 +13,67 @@ app = Flask(__name__)
11
  API_KEY = os.environ.get('API_KEY')
12
  SUBSCRIBE_URLS = os.environ.get('SUBSCRIBE_URLS') # 存储真实URL的变量
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  @app.get("/getsub")
15
  def read_subscribe():
16
  # 验证API Key
@@ -36,6 +99,7 @@ def read_subscribe():
36
  resp = requests.get(target_url)
37
  resp.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
38
  data = resp.text
 
39
  return Response(data, mimetype='text/yaml')
40
  except requests.exceptions.RequestException as e:
41
  return {"error": str(e)}, 500 # Handle request errors and return an error response
 
1
  import os
2
+ import re
3
  import sys
4
+ import yaml
5
  import requests
6
  from flask import Flask, Response, request
7
  from urllib.parse import urlencode
 
13
  API_KEY = os.environ.get('API_KEY')
14
  SUBSCRIBE_URLS = os.environ.get('SUBSCRIBE_URLS') # 存储真实URL的变量
15
 
16
+ def subscribe_mixin(content: str) -> str:
17
+ """输入 YAML 字符串,输出转换后的 YAML 字符串
18
+ """
19
+ try:
20
+ d = yaml.safe_load(content)
21
+
22
+ my_auto_group_name = "AI Unrestrict"
23
+ regex_list = [
24
+ re.compile(r"美国", re.IGNORECASE), # 美国 (不区分大小写)
25
+ re.compile(r"america", re.IGNORECASE), # america (不区分大小写)
26
+ re.compile(r"us", re.IGNORECASE), # us (不区分大小写)
27
+ re.compile(r"新加坡", re.IGNORECASE), # 新加坡 (不区分大小写)
28
+ re.compile(r"singapore", re.IGNORECASE), # singapore (不区分大小写)
29
+ re.compile(r"sg", re.IGNORECASE), # sg (不区分大小写)
30
+ re.compile(r"加拿大", re.IGNORECASE), # 加拿大 (不区分大小写)
31
+ re.compile(r"canada", re.IGNORECASE), # canada (不区分大小写)
32
+ re.compile(r"ca", re.IGNORECASE), # ca (不区分大小写)
33
+ ]
34
+ matching_proxies = [] # 用于存储匹配的代理名称
35
+
36
+ # 1. 查找并保存符合正则表达式的 proxy name
37
+ if "proxies" in d and isinstance(d["proxies"], list):
38
+ for proxy in d["proxies"]:
39
+ if "name" in proxy:
40
+ for regex in regex_list:
41
+ if regex.search(proxy["name"]): # 使用 re.search
42
+ matching_proxies.append(proxy["name"])
43
+ break # 匹配到一个 regex 就跳出循环,避免重复添加
44
+
45
+ # 2. 创建新的 proxy-group 对象
46
+ new_proxy_group = {
47
+ "name": my_auto_group_name,
48
+ "type": "url-test",
49
+ "proxies": matching_proxies,
50
+ "url": "http://www.gstatic.com/generate_204",
51
+ "interval": 60 # interval 是整数
52
+ }
53
+
54
+ # 3. 将新的 proxy-group 添加到 proxy-groups 数组
55
+ if "proxy-groups" in d and isinstance(d["proxy-groups"], list):
56
+ d["proxy-groups"].append(new_proxy_group)
57
+
58
+ # 4. 将 myAutoGroupName 添加到第一个 proxy-group 的 "proxies" 列表的最前面
59
+ if d["proxy-groups"] and len(d["proxy-groups"]) > 0 and \
60
+ "proxies" in d["proxy-groups"][0] and isinstance(d["proxy-groups"][0]["proxies"], list):
61
+ d["proxy-groups"][0]["proxies"].insert(0, my_auto_group_name) # 使用 insert(0, ...)
62
+ else:
63
+ d["proxy-groups"] = [new_proxy_group] # 如果 proxy-groups 不存在,则创建
64
+
65
+ # 将修改后的字典转换回 YAML 字符串
66
+ modified_yaml = yaml.dump(d, allow_unicode=True, indent=2) # 使用 yaml.dump
67
+
68
+ return modified_yaml
69
+
70
+ except yaml.YAMLError as e:
71
+ print(f"YAML 解析错误:{e}")
72
+ return "" # 或者抛出异常,根据你的需求
73
+ except Exception as e:
74
+ print(f"其他错误:{e}")
75
+ return ""
76
+
77
  @app.get("/getsub")
78
  def read_subscribe():
79
  # 验证API Key
 
99
  resp = requests.get(target_url)
100
  resp.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
101
  data = resp.text
102
+ data = subscribe_mixin(data)
103
  return Response(data, mimetype='text/yaml')
104
  except requests.exceptions.RequestException as e:
105
  return {"error": str(e)}, 500 # Handle request errors and return an error response
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
 
2
  flask
3
- requests
4
  asgiref
 
5
  uvicorn[standard]==0.17.*
 
1
 
2
  flask
3
+ pyyaml
4
  asgiref
5
+ requests
6
  uvicorn[standard]==0.17.*