integrate_proxy_nogroup.py 2.4 KB

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