integrate_proxy_nogroup.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. for keyword in need_list:
  43. if is_other:
  44. if keyword not in proxy['name'] and proxy not in temp_data:
  45. print('{}: {}'.format(file_name, proxy['name']))
  46. temp_data.append(proxy)
  47. else:
  48. if keyword in proxy['name'] and proxy not in temp_data:
  49. print('{}: {}'.format(file_name, proxy['name']))
  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!')