integrate_proxy_nogroup.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import yaml
  4. # 无分组版本, 需要
  5. # 读取的 yaml 的文件名
  6. bitz_file_name = 'Bitz Net.yaml'
  7. xfltd_file_name = 'XFLTD.yaml'
  8. partiot_file_name = 'index.php.yaml'
  9. file_name_list = ['Bitz Net.yaml', 'XFLTD.yaml', 'index.php.yaml']
  10. # 需要的代理地区
  11. need_list = [
  12. 'HK', '香港', 'TW', '台湾', '新加坡', '日本', '马来西亚', '菲律宾'
  13. ]
  14. # 创建 config.yaml 模版, 读出来的 proxies 和 proxy-groups 存到对应的地方, rules 不要了
  15. config_template = {
  16. "mixed-port": 7890,
  17. "allow-lan": True,
  18. "bind-address": "*",
  19. "mode": "rule",
  20. "log-level": "info",
  21. "external-controller": "127.0.0.1:9090",
  22. "secret": "",
  23. "dns": {
  24. "enable": False,
  25. "ipv6": False,
  26. "nameserver": [],
  27. "fallback": []
  28. },
  29. "proxies": [],
  30. "proxy-groups": [],
  31. }
  32. def process_yaml_file(is_other):
  33. temp_data = []
  34. for file_name in file_name_list:
  35. if not os.path.exists(file_name):
  36. return
  37. data = None
  38. with open(file_name, 'r', encoding='utf-8') as file:
  39. data = yaml.safe_load(file)
  40. if not data:
  41. return
  42. proxies = data['proxies']
  43. for proxy in proxies:
  44. for keyword in need_list:
  45. if is_other:
  46. if keyword not in proxy['name'] and proxy not in temp_data:
  47. temp_data.append(proxy)
  48. else:
  49. if keyword in proxy['name'] and proxy not in temp_data:
  50. temp_data.append(proxy)
  51. sorted_proxies = sorted(temp_data, key=lambda x: x['name'] if 'name' in x else '')
  52. if temp_data:
  53. config_template['proxies'] = sorted_proxies
  54. if __name__ == '__main__':
  55. for i in range(2):
  56. process_yaml_file(i)
  57. with open(f'config{i + 1}.yaml', 'w', encoding='utf-8') as file:
  58. yaml.dump(config_template, file, allow_unicode=True, default_flow_style=False)
  59. print('done!')