-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeep_alive.py
More file actions
219 lines (175 loc) · 7.05 KB
/
keep_alive.py
File metadata and controls
219 lines (175 loc) · 7.05 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from flask import *
from threading import Thread
from datetime import datetime
from io import BytesIO
from flask_cors import CORS
import os, base64, requests, zipfile
from projects import register_routes
from editor import register
from users import register_login
from the_scratch_channel import register_articles
app = Flask(__name__)
CORS(app)
register_routes(app)
register(app)
register_login(app)
register_articles(app)
app.secret_key = 'a9f3d7c0e1b24f6d9e8a5c3b7f1d2e0c'
GITHUB_USER = 'MyScratchBlocks'
REPO_NAME = 'Project-DB'
GITHUB_API_BASE = f'https://api.github.com/repos/{GITHUB_USER}/{REPO_NAME}/contents'
GITHUB_TOKEN_PRIMARY = os.getenv('GH_KEY')
GITHUB_TOKEN_FALLBACK = os.getenv('GH_TOKEN_V')
if not GITHUB_TOKEN_PRIMARY:
raise EnvironmentError("Missing GH_KEY environment variable.")
current_token = GITHUB_TOKEN_PRIMARY
upload_logs = [] # ✅ log list
def log_upload(message, level="info"):
timestamp = datetime.utcnow().isoformat()
upload_logs.append({"timestamp": timestamp, "level": level, "message": message})
print(f"[{level.upper()}] {timestamp} - {message}")
def switch_token():
global current_token
if current_token == GITHUB_TOKEN_PRIMARY and GITHUB_TOKEN_FALLBACK:
current_token = GITHUB_TOKEN_FALLBACK
else:
current_token = GITHUB_TOKEN_PRIMARY
def gh_request(method, url, **kwargs):
global current_token
headers = kwargs.get('headers', {}).copy()
headers['Authorization'] = f'token {current_token}'
headers['Accept'] = 'application/vnd.github+json'
kwargs['headers'] = headers
response = requests.request(method, url, **kwargs)
if response.status_code != 403:
return response
switch_token()
headers['Authorization'] = f'token {current_token}'
kwargs['headers'] = headers
return requests.request(method, url, **kwargs)
def upload_file_to_github(file_storage, tag="general"):
filename = file_storage.filename
file_contents = file_storage.read()
encoded = base64.b64encode(file_contents).decode('utf-8')
api_url = f"{GITHUB_API_BASE}/{filename}"
existing = gh_request('get', api_url)
sha = existing.json().get('sha') if existing.status_code == 200 else None
payload = {
"message": f"Upload {filename} at {datetime.utcnow().isoformat()}",
"content": encoded,
"branch": "main"
}
if sha:
payload['sha'] = sha
response = gh_request('put', api_url, json=payload)
if response.status_code in [200, 201]:
log_upload(f"[{tag}] Uploaded {filename} successfully.")
return True
else:
log_upload(f"[{tag}] Failed to upload {filename}: {response.text}", level="error")
return False
@app.route('/upload/compiler', methods=['POST'])
def upload_files():
if 'file' not in request.files:
return jsonify({"error": "No files in request"}), 400
files = request.files.getlist('file')
if not files:
return jsonify({"error": "No files uploaded"}), 400
results = []
for item in files:
if not item or item.filename == '':
results.append({"filename": None, "status": "skipped - no filename"})
continue
ext = os.path.splitext(item.filename)[1].lower()
tag = "sb3" if ext == '.sb3' else "asset"
success = upload_file_to_github(item, tag=tag)
results.append({
"filename": item.filename,
"status": "uploaded" if success else "failed",
"type": tag
})
return jsonify({"results": results})
@app.route('/uploads/files', methods=['GET'])
def download_zipped_uploads():
memory_file = BytesIO()
try:
github_list_resp = gh_request('get', GITHUB_API_BASE)
github_list = github_list_resp.json()
if isinstance(github_list, dict) and github_list.get("message"):
return jsonify({"error": github_list["message"]}), 500
with zipfile.ZipFile(memory_file, 'w') as zipf:
for file_info in github_list:
name = file_info.get('name', '')
if name and file_info.get('url'):
file_api_url = file_info['url']
file_data_resp = gh_request('get', file_api_url)
file_data = file_data_resp.json()
encoding = file_data.get('encoding')
content = file_data.get('content')
if encoding == 'base64' and content:
decoded = base64.b64decode(content)
zipf.writestr(name, decoded)
else:
log_upload(f"Skipped {name}: encoding={encoding}", level="warn")
memory_file.seek(0)
return send_file(
memory_file,
download_name='uploads.zip',
as_attachment=True,
mimetype='application/zip'
)
except Exception as e:
log_upload(f"Error creating zip: {str(e)}", level="error")
return jsonify({"error": str(e)}), 500
@app.route('/contents', methods=['GET'])
def list_sb3_files_in_zip():
try:
# Fetch all files from the GitHub repo
github_list_resp = gh_request('get', GITHUB_API_BASE)
github_list = github_list_resp.json()
if isinstance(github_list, dict) and github_list.get("message"):
return jsonify({"error": github_list["message"]}), 500
sb3_filenames = []
for file_info in github_list:
name = file_info.get('name', '')
if not name.lower().endswith('.zip'):
continue # only examine ZIP files
file_api_url = file_info['url']
file_data_resp = gh_request('get', file_api_url)
file_data = file_data_resp.json()
encoding = file_data.get('encoding')
content = file_data.get('content')
if encoding != 'base64' or not content:
continue
decoded = base64.b64decode(content)
with zipfile.ZipFile(BytesIO(decoded), 'r') as zipf:
for zip_info in zipf.infolist():
if zip_info.filename.lower().endswith('.sb3'):
sb3_filenames.append(zip_info.filename)
if not sb3_filenames:
return jsonify({"message": "No .sb3 files found in recent ZIPs."}), 404
return jsonify({"sb3_files": sb3_filenames})
except Exception as e:
log_upload(f"Error listing .sb3 files: {str(e)}", level="error")
return jsonify({"error": str(e)}), 500
@app.route('/status', methods=['GET'])
def status():
return jsonify({"logs": upload_logs})
@app.route('/<path:path>')
def serve_index(path):
# Sanitize and serve the file directly from the current directory
if not path.endswith('.html'):
path += '.html'
return send_from_directory(os.getcwd(), path)
@app.errorhandler(404)
def notfound(e):
return send_from_directory(os.getcwd(), '404.html')
# Optional: add a root route
@app.route('/')
def index():
return send_from_directory(os.getcwd(), 'index.html')
def run():
app.run(host='0.0.0.0')
def keep_alive():
t = Thread(target=run)
t.start()