izuemon commited on
Commit
feeaf78
·
verified ·
1 Parent(s): 9b39e23

Create qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +167 -0
turbowarp-server/qr-converter.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import scratchcommunication
2
+ import time
3
+ import requests
4
+ import qrcode
5
+
6
+ PROJECT_ID = "1293030706"
7
+
8
+ tw = scratchcommunication.TwCloudConnection(
9
+ project_id=PROJECT_ID,
10
+ username="server",
11
+ contact_info="contact"
12
+ )
13
+
14
+ # ---------------------
15
+ # 文字変換 (a-z0-9 → 00-35)
16
+ # ---------------------
17
+
18
+ chars = "abcdefghijklmnopqrstuvwxyz0123456789"
19
+
20
+ def encode_id(text):
21
+ out = ""
22
+ for c in text:
23
+ if c in chars:
24
+ out += f"{chars.index(c):02d}"
25
+ return out
26
+
27
+ # ---------------------
28
+ # QRコード → 数値変換
29
+ # ---------------------
30
+
31
+ def qr_to_numbers(url):
32
+ qr = qrcode.QRCode(
33
+ version=None,
34
+ error_correction=qrcode.constants.ERROR_CORRECT_M,
35
+ box_size=1,
36
+ border=4
37
+ )
38
+
39
+ qr.add_data(url)
40
+ qr.make(fit=True)
41
+
42
+ matrix = qr.get_matrix()
43
+
44
+ rows = []
45
+
46
+ for row in matrix:
47
+ binary = "".join(["1" if cell else "0" for cell in row])
48
+ num = int(binary, 2)
49
+ rows.append(num)
50
+
51
+ max_len = max(len(str(r)) for r in rows)
52
+
53
+ padded = [str(r).zfill(max_len) for r in rows]
54
+
55
+ return max_len, "".join(padded)
56
+
57
+ # ---------------------
58
+ # クラウド操作
59
+ # ---------------------
60
+
61
+ def get_var(name):
62
+ try:
63
+ return tw.get_variable(name=name, name_literal=False)
64
+ except:
65
+ return None
66
+
67
+ def set_var(name, value):
68
+ try:
69
+ return tw.set_variable(name=name, value=value, name_literal=False)
70
+ except:
71
+ return None
72
+
73
+ # ---------------------
74
+ # n0管理
75
+ # ---------------------
76
+
77
+ def set_busy(flag):
78
+ set_var("n0", "1" if flag else "0")
79
+
80
+ # ---------------------
81
+ # メインループ
82
+ # ---------------------
83
+
84
+ last_value = None
85
+
86
+ while True:
87
+
88
+ v = get_var("n1")
89
+
90
+ if not v or len(v) < 3:
91
+ time.sleep(0.2)
92
+ continue
93
+
94
+ # 変更検知
95
+ if v == last_value:
96
+ time.sleep(0.2)
97
+ continue
98
+
99
+ last_value = v
100
+
101
+ mode = v[0] # 0:送信, 1:レスポンス
102
+ read_flag = v[1]
103
+
104
+ # 未読のみ処理
105
+ if mode != "0" or read_flag != "0":
106
+ time.sleep(0.2)
107
+ continue
108
+
109
+ set_busy(True)
110
+
111
+ try:
112
+ # ---------------------
113
+ # ID取得
114
+ # ---------------------
115
+ recv_id = v[2:]
116
+
117
+ # 既読に変更
118
+ set_var("n1", "01" + recv_id)
119
+
120
+ # ---------------------
121
+ # APIアクセス
122
+ # ---------------------
123
+ r = requests.get(
124
+ f"https://20.rf.gd/set.php?project_id={recv_id}",
125
+ cookies={"__test": "1de7003f8e14ac154f8f26d3c3e97895"},
126
+ timeout=10
127
+ )
128
+ data = r.json()
129
+
130
+ short_id = data.get("short_id", "")
131
+
132
+ # ---------------------
133
+ # IDエンコード
134
+ # ---------------------
135
+ encoded_id = encode_id(short_id)
136
+
137
+ # 9桁固定(足りなければ0埋め)
138
+ encoded_id = encoded_id[:18].ljust(18, "0") # 2桁×9文字
139
+
140
+ # ---------------------
141
+ # QR生成
142
+ # ---------------------
143
+ url = f"https://20.rf.gd/{short_id}"
144
+
145
+ max_len, qr_data = qr_to_numbers(url)
146
+
147
+ max_len_str = str(max_len).zfill(1)
148
+
149
+ # ---------------------
150
+ # 送信データ構築
151
+ # ---------------------
152
+ # 1:レスポンス
153
+ # 1:既読
154
+ # max_len
155
+ # ID(18桁)
156
+ # QRデータ
157
+
158
+ send_data = "11" + max_len_str + encoded_id + qr_data
159
+
160
+ set_var("n1", send_data)
161
+
162
+ except Exception:
163
+ set_var("n1", "11" + "0" + "0"*18)
164
+
165
+ set_busy(False)
166
+
167
+ time.sleep(0.2)