Skip to content

Commit

Permalink
Update main.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
lakevilladom authored Oct 12, 2024
1 parent 39515a5 commit 4305392
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,31 @@ jobs:
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:
content = file.read()
# 匹配 icon 的 URL (假设 icon URL 的形式如 "icon: http://...")
url_pattern = re.compile(r"(icon:\s*)(http[s]?:\/\/[^\s]+)", re.IGNORECASE)
# 函数:将图像 URL 转换为 Base64 编码,并返回新的格式
def url_to_base64(url):
try:
response = requests.get(url)
if response.status_code == 200:
img_type = url.split('.')[-1] # 从 URL 中推断图像格式
img_type = url.split('.')[-1]
base64_str = base64.b64encode(response.content).decode('utf-8')
return f"data:image/{img_type};base64,{base64_str}"
else:
return url # 如果下载失败,保留原始 URL
return url
except Exception as e:
print(f"Failed to convert {url}: {str(e)}")
return url # 如果请求失败,保留原始 URL
return url
# 替换 YAML 文件中所有匹配的 icon URL 为 Base64
def replace_icon(match):
original_url = match.group(2)
base64_data = url_to_base64(original_url)
return f"{match.group(1)}'{base64_data}'" # 添加单引号包裹 Base64 数据
# 使用正则表达式替换所有匹配的 icon URL
return f"{match.group(1)}'{base64_data}'"
updated_content = url_pattern.sub(replace_icon, content)
# 将修改后的内容写回新的 YAML 文件
with open(output_file, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"Icon URLs converted to Base64 and saved to {output_file}")
EOF
Expand Down

0 comments on commit 4305392

Please sign in to comment.