xjf6b commited on
Commit
25fd285
1 Parent(s): c58d804

Create merged2upload.py

Browse files
Files changed (1) hide show
  1. merged2upload.py +63 -0
merged2upload.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import base64
3
+ import os
4
+
5
+ def fetch_and_decode_base64(url):
6
+ print(url)
7
+ try:
8
+ response = requests.get(url, verify=False)
9
+ response.raise_for_status()
10
+ decoded_content = base64.b64decode(response.text)
11
+ return decoded_content.decode('utf-8')
12
+ except requests.RequestException as e:
13
+ print(f"Error fetching {url}: {e}")
14
+ return None
15
+
16
+ def upload_to_gist(content, gist_id, github_token):
17
+ url = f"https://api.github.com/gists/{gist_id}"
18
+ headers = {
19
+ 'Authorization': f'token {github_token}',
20
+ 'Accept': 'application/vnd.github.v3+json'
21
+ }
22
+ data = {
23
+ "files": {
24
+ "configsub.yaml": {
25
+ "content": content
26
+ }
27
+ }
28
+ }
29
+ try:
30
+ response = requests.patch(url, headers=headers, json=data)
31
+ response.raise_for_status()
32
+ print(f"Successfully updated Gist: {gist_id}")
33
+ except requests.RequestException as e:
34
+ print(f"Error updating Gist: {e}")
35
+
36
+ def main():
37
+ file_path = '/app/aggregator/data/subscribes.txt'
38
+
39
+ with open(file_path, 'r') as file:
40
+ urls = file.read().strip().split('\n')
41
+
42
+ all_decoded_texts = []
43
+
44
+ for url in urls:
45
+ decoded_content = fetch_and_decode_base64(url)
46
+ if decoded_content:
47
+ all_decoded_texts.append(decoded_content)
48
+
49
+ merged_content = "\n".join(all_decoded_texts)
50
+ encoded_merged_content = base64.b64encode(merged_content.encode('utf-8')).decode('utf-8')
51
+
52
+ merged_file_path = '/app/aggregator/data/merged.txt'
53
+ with open(merged_file_path, 'w') as file:
54
+ file.write(encoded_merged_content)
55
+ print(f"Encoded merged content written to {merged_file_path}")
56
+
57
+ # Upload the merged content to the Gist
58
+ github_token = os.getenv('GITHUB_TOKEN')
59
+ gist_id = os.getenv('GITHUB_GIST_ID')
60
+ upload_to_gist(encoded_merged_content, gist_id, github_token)
61
+
62
+ if __name__ == "__main__":
63
+ main()