izuemon commited on
Commit
3a3498d
·
verified ·
1 Parent(s): e0681cc

Update turbowarp-server/qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +33 -18
turbowarp-server/qr-converter.py CHANGED
@@ -4,51 +4,63 @@ import scratchcommunication
4
 
5
  PROJECT_ID = 1293030706
6
 
 
7
  tw = scratchcommunication.TwCloudConnection(
8
  project_id=PROJECT_ID,
9
  username="server",
10
  contact_info="contact",
11
  reconnect=True,
12
- receive_from_websocket=True
 
13
  )
14
 
 
15
  def encode_short_id(short_id):
16
  chars = "abcdefghijklmnopqrstuvwxyz0123456789"
17
  result = ""
18
- for c in short_id:
19
- index = chars.index(c)
20
- result += f"{index:02d}"
 
 
21
  return result
22
 
23
  last_n1 = None
24
 
25
  while True:
26
  try:
27
- n1_raw = tw.get_variable(name="n1")
28
- n1 = str(n1_var.value) # '☁ n1' 対策
 
 
 
 
29
 
30
  if n1 != last_n1:
31
  last_n1 = n1
32
 
33
  if len(n1) < 3:
34
- continue
35
 
36
- mode = n1[0]
37
- read_flag = n1[1]
 
38
 
 
 
 
39
  if mode == "0" and read_flag == "0":
 
40
  tw.set_variable(name="n0", value=1)
 
41
 
42
- recv_id = n1[2:]
43
- print("受信ID:", recv_id)
44
-
45
- # 既読にする
46
- tw.set_variable(name="n1", value="0" + "1" + recv_id)
47
 
48
  # APIアクセス
49
- url = f"https://20.rf.gd/set.php?project_id={recv_id}"
50
  try:
51
- res = requests.get(url, cookies={"__test": "872039d283902860e895efb6018d2632"}, timeout=5)
52
  res.raise_for_status()
53
  data = res.json()
54
  short_id = data.get("short_id", "000")
@@ -63,12 +75,15 @@ while True:
63
 
64
  encoded = encode_short_id(short_id)
65
 
66
- # レスポンス書き込み
67
  tw.set_variable(name="n1", value="1" + "0" + encoded)
68
 
 
 
 
69
  elif mode == "1" and read_flag == "1":
70
  tw.set_variable(name="n0", value=0)
71
- print("完了")
72
 
73
  time.sleep(0.2)
74
 
 
4
 
5
  PROJECT_ID = 1293030706
6
 
7
+ # Scratchクラウド接続
8
  tw = scratchcommunication.TwCloudConnection(
9
  project_id=PROJECT_ID,
10
  username="server",
11
  contact_info="contact",
12
  reconnect=True,
13
+ receive_from_websocket=True,
14
+ accept_strs=True # 文字列も取得可能
15
  )
16
 
17
+ # short_id を a-z0-9 → 00~35 に変換
18
  def encode_short_id(short_id):
19
  chars = "abcdefghijklmnopqrstuvwxyz0123456789"
20
  result = ""
21
+ for c in short_id.lower(): # 念のため小文字化
22
+ if c in chars:
23
+ result += f"{chars.index(c):02d}"
24
+ else:
25
+ result += "00" # 不明文字は00
26
  return result
27
 
28
  last_n1 = None
29
 
30
  while True:
31
  try:
32
+ # クラウド変数 n1 の取得
33
+ n1 = tw.get_variable(name="n1", name_literal=False)
34
+ if n1 is None:
35
+ time.sleep(0.2)
36
+ continue
37
+ n1 = str(n1).strip()
38
 
39
  if n1 != last_n1:
40
  last_n1 = n1
41
 
42
  if len(n1) < 3:
43
+ continue # 3桁以上ない場合はスキップ
44
 
45
+ mode = n1[0] # 0: 受信/送信待ち, 1: レスポンス返却
46
+ read_flag = n1[1] # 0: 未読, 1: 既読
47
+ content = n1[2:] # 3桁目以降 = ID
48
 
49
+ # -----------------------------
50
+ # Scratch → サーバー
51
+ # -----------------------------
52
  if mode == "0" and read_flag == "0":
53
+ # 使用中フラグ
54
  tw.set_variable(name="n0", value=1)
55
+ print("受信ID:", content)
56
 
57
+ # 既読に変更
58
+ tw.set_variable(name="n1", value="0" + "1" + content)
 
 
 
59
 
60
  # APIアクセス
61
+ url = f"https://20.rf.gd/set.php?project_id={content}"
62
  try:
63
+ res = requests.get(url, timeout=5)
64
  res.raise_for_status()
65
  data = res.json()
66
  short_id = data.get("short_id", "000")
 
75
 
76
  encoded = encode_short_id(short_id)
77
 
78
+ # サーバー → Scratch にレスポンス書き込み
79
  tw.set_variable(name="n1", value="1" + "0" + encoded)
80
 
81
+ # -----------------------------
82
+ # Scratch が既読にしたら n0 を解放
83
+ # -----------------------------
84
  elif mode == "1" and read_flag == "1":
85
  tw.set_variable(name="n0", value=0)
86
+ print("通信完了。n0を0に戻しました。")
87
 
88
  time.sleep(0.2)
89