Spaces:
Running
Running
Update turbowarp-server/qr-converter.py
Browse files- turbowarp-server/qr-converter.py +43 -53
turbowarp-server/qr-converter.py
CHANGED
|
@@ -6,45 +6,29 @@ from io import BytesIO
|
|
| 6 |
import scratchcommunication
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
-
# ---
|
| 10 |
PROJECT_ID = 1293416663
|
| 11 |
tw = scratchcommunication.TwCloudConnection(
|
| 12 |
project_id=PROJECT_ID,
|
| 13 |
username="server",
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return tw.get_variable(name=name, name_literal=False)
|
| 20 |
-
except scratchcommunication.exceptions.NotSupported as e:
|
| 21 |
-
print(f"[WARN] NotSupported error while getting '{name}': {e}")
|
| 22 |
-
return None
|
| 23 |
-
except KeyError as e:
|
| 24 |
-
print(f"[ERROR] KeyError while getting '{name}': {e}")
|
| 25 |
-
return None
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"[ERROR] Unexpected error while getting '{name}': {e}")
|
| 28 |
-
traceback.print_exc()
|
| 29 |
-
return None
|
| 30 |
-
|
| 31 |
-
def set_var(name, value):
|
| 32 |
-
try:
|
| 33 |
-
return tw.set_variable(name=name, value=value, name_literal=False)
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f"[ERROR] Failed to set '{name}' to '{value}': {e}")
|
| 36 |
-
traceback.print_exc()
|
| 37 |
-
return None
|
| 38 |
|
| 39 |
# --- n-chars.txt の読み込み ---
|
| 40 |
try:
|
| 41 |
with open("turbowarp-server/n-chars.txt", "r", encoding="utf-8") as f:
|
| 42 |
n_chars = [line.strip() for line in f]
|
| 43 |
except Exception as e:
|
| 44 |
-
print(f"[FATAL]
|
| 45 |
traceback.print_exc()
|
| 46 |
n_chars = []
|
| 47 |
|
|
|
|
| 48 |
def decode_prompt(encoded_str):
|
| 49 |
chars = []
|
| 50 |
for i in range(0, len(encoded_str), 2):
|
|
@@ -53,7 +37,7 @@ def decode_prompt(encoded_str):
|
|
| 53 |
if idx < len(n_chars):
|
| 54 |
chars.append(n_chars[idx])
|
| 55 |
except Exception as e:
|
| 56 |
-
print(f"[WARN]
|
| 57 |
return "".join(chars)
|
| 58 |
|
| 59 |
def rgb_to_scratch_number(rgb):
|
|
@@ -81,7 +65,7 @@ def generate_image(prompt):
|
|
| 81 |
resp.raise_for_status()
|
| 82 |
return Image.open(BytesIO(resp.content))
|
| 83 |
except Exception as e:
|
| 84 |
-
print(f"[ERROR]
|
| 85 |
traceback.print_exc()
|
| 86 |
return None
|
| 87 |
|
|
@@ -97,39 +81,45 @@ def split_packets(data, max_length=9998):
|
|
| 97 |
packets = []
|
| 98 |
idx = 0
|
| 99 |
while idx < len(data):
|
| 100 |
-
|
| 101 |
-
packets.append(chunk)
|
| 102 |
idx += max_length
|
| 103 |
return packets
|
| 104 |
|
| 105 |
# --- メインループ ---
|
| 106 |
while True:
|
| 107 |
try:
|
| 108 |
-
n1 =
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
-
print(f"[CRITICAL]
|
| 134 |
traceback.print_exc()
|
| 135 |
time.sleep(0.2)
|
|
|
|
| 6 |
import scratchcommunication
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
+
# --- Turbowarpクラウド接続 ---
|
| 10 |
PROJECT_ID = 1293416663
|
| 11 |
tw = scratchcommunication.TwCloudConnection(
|
| 12 |
project_id=PROJECT_ID,
|
| 13 |
username="server",
|
| 14 |
+
cloud_host="wss://clouddata.turbowarp.org/",
|
| 15 |
+
accept_strs=False, # 数値を扱いやすくする
|
| 16 |
+
reconnect=True
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Quickaccess有効化(辞書のように扱える)
|
| 20 |
+
tw.enable_quickaccess()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# --- n-chars.txt の読み込み ---
|
| 23 |
try:
|
| 24 |
with open("turbowarp-server/n-chars.txt", "r", encoding="utf-8") as f:
|
| 25 |
n_chars = [line.strip() for line in f]
|
| 26 |
except Exception as e:
|
| 27 |
+
print(f"[FATAL] n-chars.txt 読み込み失敗: {e}")
|
| 28 |
traceback.print_exc()
|
| 29 |
n_chars = []
|
| 30 |
|
| 31 |
+
# --- ユーティリティ関数 ---
|
| 32 |
def decode_prompt(encoded_str):
|
| 33 |
chars = []
|
| 34 |
for i in range(0, len(encoded_str), 2):
|
|
|
|
| 37 |
if idx < len(n_chars):
|
| 38 |
chars.append(n_chars[idx])
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"[WARN] decode失敗: {encoded_str[i:i+2]} -> {e}")
|
| 41 |
return "".join(chars)
|
| 42 |
|
| 43 |
def rgb_to_scratch_number(rgb):
|
|
|
|
| 65 |
resp.raise_for_status()
|
| 66 |
return Image.open(BytesIO(resp.content))
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"[ERROR] 画像生成失敗: '{prompt}' -> {e}")
|
| 69 |
traceback.print_exc()
|
| 70 |
return None
|
| 71 |
|
|
|
|
| 81 |
packets = []
|
| 82 |
idx = 0
|
| 83 |
while idx < len(data):
|
| 84 |
+
packets.append(data[idx:idx + max_length])
|
|
|
|
| 85 |
idx += max_length
|
| 86 |
return packets
|
| 87 |
|
| 88 |
# --- メインループ ---
|
| 89 |
while True:
|
| 90 |
try:
|
| 91 |
+
n1 = tw.get("n1", None)
|
| 92 |
+
n0 = tw.get("n0", None)
|
| 93 |
+
|
| 94 |
+
if n1 and len(n1) >= 3 and n1[0] == "0" and n0 == 0:
|
| 95 |
+
tw["n0"] = 1 # 処理中フラグ
|
| 96 |
+
|
| 97 |
+
user_id = n1[1:3]
|
| 98 |
+
encoded_prompt = n1[3:]
|
| 99 |
+
prompt = decode_prompt(encoded_prompt)
|
| 100 |
+
|
| 101 |
+
# 画像生成
|
| 102 |
+
img = generate_image(prompt)
|
| 103 |
+
if img is None:
|
| 104 |
+
tw["n0"] = 0
|
| 105 |
+
time.sleep(1)
|
| 106 |
+
continue
|
| 107 |
+
|
| 108 |
+
scratch_data = resize_and_encode(img)
|
| 109 |
+
packets = split_packets(scratch_data)
|
| 110 |
+
|
| 111 |
+
# パケット送信
|
| 112 |
+
for pkt in packets:
|
| 113 |
+
tw["n1"] = pkt
|
| 114 |
+
time.sleep(0.2)
|
| 115 |
+
# 次パケット送信待ち
|
| 116 |
+
while tw.get("n1") != 11:
|
| 117 |
+
time.sleep(0.1)
|
| 118 |
+
|
| 119 |
+
# 完了
|
| 120 |
+
tw["n0"] = 0
|
| 121 |
+
|
| 122 |
except Exception as e:
|
| 123 |
+
print(f"[CRITICAL] メインループ例外: {e}")
|
| 124 |
traceback.print_exc()
|
| 125 |
time.sleep(0.2)
|