-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
382 lines (299 loc) · 10.2 KB
/
app.py
File metadata and controls
382 lines (299 loc) · 10.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import scratchattach as sa
import requests
import urllib.request
from PIL import Image
import json, os, random, math, time
from keep_alive import keep_alive
def log(*args):
print(f"[{round(time.time())}]", *args)
def convertToNumber(s):
return int.from_bytes(s.encode(), 'little')
def convertFromNumber(n):
return n.to_bytes(math.ceil(n.bit_length() / 8), 'little').decode()
def get_follower_count(username):
url = f"https://api.scratch.mit.edu/users/{username}/followers"
offset = 0
limit = 40
count = 0
while True:
res = requests.get(f"{url}?offset={offset}&limit={limit}")
data = res.json()
if not data:
break
count += len(data)
offset += limit
return count
session_ps = os.environ.get("SCRATCH_PS")
session = sa.login('Dev-Server', session_ps)
cloud1 = session.connect_cloud(1186198180)
client1 = cloud1.requests()
# --- Shared logic for get_pfp and gif ---
def handle_get_pfp(username):
try:
data = requests.get(f"https://api.scratch.mit.edu/users/{username}").json()
user_id = data["id"]
except:
return "User Not Found"
img_url = f"https://uploads.scratch.mit.edu/get_image/user/{user_id}_1000x1000.png"
image_name = f"pfp{convertToNumber(username)}.png"
image_path = f"/tmp/{image_name}"
try:
urllib.request.urlretrieve(img_url, image_path)
img = Image.open(image_path)
if getattr(img, "is_animated", False) and getattr(img, "n_frames", 1) > 1:
log("Animated GIF detected inside PNG for", username)
os.remove(image_path)
return "GIF"
except Exception as e:
log("Failed to process profile picture:", e)
return "Error processing image"
log(f"Image stored in {image_path}")
return image_name
def handle_gif(username, quality):
try:
data = requests.get(f"https://api.scratch.mit.edu/users/{username}").json()
user_id = data["id"]
except:
return "User Not Found"
img_url = f"https://uploads.scratch.mit.edu/get_image/user/{user_id}_1000x1000.png"
image_name = f"pfp{convertToNumber(username)}.png"
path = f"/tmp/{image_name}"
try:
urllib.request.urlretrieve(img_url, path)
except:
return "Failed to download profile picture"
try:
img = Image.open(path)
if not (getattr(img, "is_animated", False) and getattr(img, "n_frames", 1) > 1):
return "Not a GIF"
img.seek(0) # First frame
frame = img.convert("RGBA")
except:
return "Failed to open image"
try:
size = int(quality)
frame = frame.resize((size, size))
except:
return "Invalid quality"
pixels = frame.load()
width, height = frame.size
colors = []
for y in range(height):
for x in range(width):
r, g, b, a = pixels[x, y]
color = a * 16777216 + r * 65536 + g * 256 + b
colors.append(color)
log(f"Returning {len(colors)} colors from GIF for", username)
return colors
# --- Shared get_image_piece and done ---
def handle_image_piece(img_id, y_offset, img_size, username):
img_id = img_id.replace("/", "").replace("\\", "")
try:
img = Image.open(f"/tmp/{img_id}").convert("RGBA")
except:
log("Failed to get image data from", img_id, "by", username)
return "Error getting image data"
img = img.resize((int(img_size), int(img_size)))
width, height = img.size
pixels = img.load()
amount = 10
colors = []
for y in range(int(y_offset), int(y_offset) + int(amount)):
for x in range(width):
r, g, b, a = pixels[x, y]
color = a * 16777216 + r * 65536 + g * 256 + b
colors.append(color)
log(username, 'requested image piece for image "' + img_id + '" with y offset', y_offset)
return colors
def handle_done(img_id):
try:
os.remove(f'/tmp/{str(img_id)}')
log("Removing file", img_id)
return "Done"
except:
return "Error deleting file"
# === CLIENT 1 ===
@client1.request
def ping(username):
log("Ping request received")
return "pong"
@client1.request
def get_pfp(username):
return handle_get_pfp(username)
@client1.request
def gif(username, quality):
return handle_gif(username, quality)
@client1.request
def count(user):
return sa.get_user(user.replace(" ", "")).follower_count()
@client1.request
def get_image_piece(img_id, y_offset, img_size, username):
return handle_image_piece(img_id, y_offset, img_size, username)
@client1.request
def done(img_id):
return handle_done(img_id)
@client1.event
def on_ready():
log("Request handler is running for client1")
# === CLIENT 2 ===
cloud2 = sa.get_tw_cloud(1186198180)
client2 = cloud2.requests()
@client2.request
def ping(username):
log("Ping request received")
return "pong"
@client2.request
def get_pfp(username):
return handle_get_pfp(username)
@client2.request
def count(user):
return sa.get_user(user.replace(" ", "")).follower_count()
@client2.request
def gif(username, quality):
return handle_gif(username, quality)
@client2.request
def get_image_piece(img_id, y_offset, img_size, username):
return handle_image_piece(img_id, y_offset, img_size, username)
@client2.request
def done(img_id):
return handle_done(img_id)
@client2.event
def on_ready():
log("Request handler is running for client2")
cloud3 = session.connect_cloud(1186838073)
client3 = cloud3.requests()
@client3.request
def count(user, something):
return sa.get_user(user.replace(" ", "")).follower_count()
@client3.request
def pfp(user):
try:
data = requests.get(f"https://api.scratch.mit.edu/users/{user}").json()
user_id = data["id"]
except:
return "User Not Found"
img_url = f"https://uploads.scratch.mit.edu/get_image/user/{user_id}_1000x1000.png"
image_path = f"/tmp/pfp_{user}.png"
try:
urllib.request.urlretrieve(img_url, image_path)
img = Image.open(image_path).convert("RGBA")
img = img.resize((100, 100))
except Exception as e:
log("Error downloading or processing image:", e)
return "Error"
pixels = img.load()
colors = []
for y in range(100):
for x in range(100):
r, g, b, a = pixels[x, y]
color = a * 16777216 + r * 65536 + g * 256 + b
colors.append(color)
log(f"Returning {len(colors)} colors for", user)
return colors
@client3.request
def ping():
return "pong"
cloud4 = session.connect_cloud(1191148134)
client4 = cloud4.requests()
@client4.request
def ping(username):
log("Ping request received")
return "pong"
@client4.request
def get_pfp(username):
return handle_get_pfp(username)
@client4.request
def gif(username, quality):
return handle_gif(username, quality)
@client4.request
def count(user):
return sa.get_user(user.replace(" ", "")).message_count()
@client4.request
def get_image_piece(img_id, y_offset, img_size, username):
return handle_image_piece(img_id, y_offset, img_size, username)
@client4.request
def done(img_id):
return handle_done(img_id)
@client4.event
def on_ready():
log("Request handler is running for client1")
cloud5 = session.connect_cloud(1192979296)
client5 = cloud5.requests()
cloud6 = session.connect_cloud(1206391475)
client6 = cloud6.requests()
# Directory to store chat histories per user
CHAT_DIR = "chat_histories"
os.makedirs(CHAT_DIR, exist_ok=True)
CHAT_LIMIT = 15
def load_chat_history(user, chat_name):
filepath = os.path.join(CHAT_DIR, f"{user}_{chat_name}.json")
if os.path.exists(filepath):
with open(filepath, "r") as file:
return json.load(file)
return []
def save_chat_history(user, chat_name, history):
filepath = os.path.join(CHAT_DIR, f"{user}_{chat_name}.json")
with open(filepath, "w") as file:
json.dump(history, file, indent=2)
def get_available_chat_name(user, base_name="chat"):
for i in range(1, 100):
name = f"{base_name}_{i}"
filepath = os.path.join(CHAT_DIR, f"{user}_{name}.json")
if not os.path.exists(filepath):
return name
with open(filepath, "r") as file:
history = json.load(file)
if len(history) < CHAT_LIMIT:
return name
return f"{base_name}_overflow"
def askAI(prompt, user, chat_name=None):
API_KEY = os.getenv("APIKEY")
API_URL = os.getenv("APIURL") + API_KEY
# Determine or create chat
if not chat_name:
chat_name = get_available_chat_name(user)
chat_history = load_chat_history(user, chat_name)
# Build full history with new prompt
chat_history.append({
"role": "user",
"parts": [{
"text": f"You are an AI for scratch.If you are asked who made you reply with, i was made by The ScratchGPT team and that you were mostly made by kRxZy_kRxZy You can only write 100 words max, cannot say anything inappropriate for children under 12, cannot use markdown, images, or canvas code. Only return plain text. You can do math or any logical question but mostly about scratch. Question: '{prompt}'"
}]
})
payload = {"contents": chat_history[-CHAT_LIMIT:]}
# Make request
try:
response = requests.post(API_URL, json=payload)
response.raise_for_status()
data = response.json()
# Parse reply
reply = data.get("candidates", [])[0].get("content", {}).get("parts", [])[0].get("text", "")
chat_history.append({
"role": "model",
"parts": [{"text": reply}]
})
save_chat_history(user, chat_name, chat_history)
return reply
except Exception as e:
return "Failed To Get Response!"
@client6.request
def ping():
return "AI: Welcome To ScratchAI, Ask Any Question You Want!"
@client6.request
def ask(prompt, user):
return f"AI: {askAI(prompt, user)}"
@client5.request
def ping():
return "AI: Welcome To ScratchGPT, Ask Any Question You Want!"
@client5.request
def ask(prompt, user):
return f"AI: {askAI(prompt, user)}"
# === Start Everything ===
keep_alive()
client1.start()
client6.start()
client2.start()
client3.start()
client4.start()
client5.start()
log("Started all clients")