Skip to content

Commit

Permalink
create convert.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Leev1s committed Jan 2, 2024
1 parent c1944ca commit 423528d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/convert.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Convert Conf Files

on:
schedule:
- cron: "0 1 * * *" # every day
workflow_dispatch:

jobs:
convert_conf_files:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: pip3 install glob2

- name: Convert Conf Files
env:
DNS_URL: ${{ secrets.DNS_URL }}
run: python convert.py

- name: Commit changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
current_time=$(date +'%Y-%m-%d %H:%M:%S')
git add .
git commit -m "Convert conf files to txt - $current_time"
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master
Empty file added .gitignore
Empty file.
32 changes: 32 additions & 0 deletions convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import glob

def convert_conf_to_txt(conf_file, txt_file, dns_url):
with open(conf_file, 'r') as conf:
with open(txt_file, 'w') as txt:
for line in conf:
parts = line.strip().split('=')
if len(parts) != 2:
print(f"Invalid line: {line.strip()}")
continue
domain = parts[1].split('/')[1]
txt.write(f"[/{domain}/]" + dns_url + "\n")

def main():
current_directory = os.getcwd() # 获取当前目录
# 使用glob匹配以.conf结尾的文件
conf_files = glob.glob(os.path.join(current_directory, '*china.conf'))
# 逐个读取文件内容
for thefile in conf_files:
if os.path.basename(thefile) == 'bogus-nxdomain.china.conf':
continue
convert_conf_to_txt(thefile, thefile + ".txt", dns_url)

# 从环境变量中获取 DNS URL
dns_url = os.environ.get('DNS_URL')

# 检查 DNS URL 是否为空
if dns_url is None:
print("DNS_URL 环境变量未设置")
else:
main()

0 comments on commit 423528d

Please sign in to comment.