Update ACL4SSR_Online_Full_WithIcon.yaml #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Convert Icon URL to Base64 | |
on: | |
push: | |
paths: | |
- yaml/ACL4SSR_Online_Full_WithIcon.yaml | |
jobs: | |
convert-icons: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
- name: Check if the YAML file exists | |
id: check_file | |
run: | | |
if [ ! -f "yaml/ACL4SSR_Online_Full_WithIcon.yaml" ]; then | |
echo "File does not exist." | |
exit 0 | |
fi | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install pyyaml requests | |
- name: Convert icon URLs to Base64 and modify YAML | |
run: | | |
python - <<EOF | |
import yaml | |
import requests | |
import base64 | |
# 输入和输出文件路径 | |
input_file = 'yaml/ACL4SSR_Online_Full_WithIcon.yaml' | |
output_file = 'yaml/ACL4SSR_Online_Full_WithBase64Icon.yaml' | |
# 加载 YAML 文件 | |
with open(input_file, 'r', encoding='utf-8') as file: | |
data = yaml.safe_load(file) | |
# 函数:将图像 URL 转换为 Base64 编码,并包裹单引号 | |
def url_to_base64(url): | |
response = requests.get(url) | |
if response.status_code == 200: | |
img_type = url.split('.')[-1] # 从 URL 中推断图像格式 | |
base64_str = base64.b64encode(response.content).decode('utf-8') | |
# 不显式添加单引号,避免重复包裹 | |
return f"data:image/{img_type};base64,{base64_str}" | |
else: | |
return url # 如果下载失败,保留原始 URL | |
# 遍历数据并替换 icon URL 为 Base64(无显式单引号) | |
for group in data.get('proxy-groups', []): | |
if 'icon' in group: | |
group['icon'] = url_to_base64(group['icon']) | |
# 将修改后的数据写入新的 YAML 文件,并显式指定样式 | |
with open(output_file, 'w', encoding='utf-8') as file: | |
yaml.dump(data, file, allow_unicode=True, default_style="'") | |
print(f"Conversion completed: {output_file}") | |
EOF | |
- name: Commit and Push changes | |
run: | | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
git add yaml/ACL4SSR_Online_Full_WithBase64Icon.yaml | |
git commit -m "Converted image URL to base64 in ACL4SSR_Online_Full_WithBase64Icon.yaml" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |