Spaces:
Running
Running
File size: 1,938 Bytes
25fd285 edb8a16 25fd285 32cc9d5 edb8a16 5a06a76 edb8a16 e8d51b6 edb8a16 5a06a76 edb8a16 25fd285 edb8a16 25fd285 e8d51b6 25fd285 5a06a76 25fd285 5a06a76 32cc9d5 e8d51b6 edb8a16 5a06a76 edb8a16 5a06a76 25fd285 5a06a76 edb8a16 32cc9d5 25fd285 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import requests
import base64
import os
def fetch_and_decode_base64(url):
print(f"正在获取 {url}")
try:
response = requests.get(url, verify=False)
response.raise_for_status()
decoded_content = base64.b64decode(response.text)
return decoded_content.decode('utf-8')
except requests.RequestException as e:
print(f"获取 {url} 时出错: {e}")
return None
def upload_to_gist(content, gist_id, github_token):
url = f"https://api.github.com/gists/{gist_id}"
headers = {
'Authorization': f'token {github_token}',
'Accept': 'application/vnd.github.v3+json'
}
data = {
"files": {
"configsub.yaml": {
"content": content
}
}
}
try:
response = requests.patch(url, headers=headers, json=data)
response.raise_for_status()
print(f"成功更新 Gist: {gist_id}")
except requests.RequestException as e:
print(f"更新 Gist 时出错: {e}")
def main():
file_path = '/app/aggregator/data/subscribes.txt'
with open(file_path, 'r') as file:
urls = file.read().strip().split('\n')
all_decoded_texts = []
for url in urls:
decoded_content = fetch_and_decode_base64(url)
if decoded_content:
all_decoded_texts.append(decoded_content)
merged_content = "\n".join(all_decoded_texts)
encoded_merged_content = base64.b64encode(merged_content.encode('utf-8')).decode('utf-8')
merged_file_path = '/app/merged.txt'
with open(merged_file_path, 'w') as file:
file.write(encoded_merged_content)
print(f"已将编码后的合并内容写入 {merged_file_path}")
# 上传合并内容到 Gist
github_token = os.getenv('GITHUB_TOKEN')
gist_id = os.getenv('GITHUB_GIST_ID')
upload_to_gist(encoded_merged_content, gist_id, github_token)
if __name__ == "__main__":
main() |