asemxin commited on
Commit
b31c8c5
·
1 Parent(s): 2902034

feat: 添加 telegraph/file.io 备用图床,调整优先级

Browse files
Files changed (1) hide show
  1. image_daemon.py +53 -13
image_daemon.py CHANGED
@@ -123,36 +123,76 @@ def download_image(token, message_id, file_key):
123
  return None
124
 
125
  def upload_image(data):
126
- # catbox.moe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  try:
128
  resp = requests.post("https://catbox.moe/user/api.php",
129
  data={"reqtype": "fileupload"},
130
  files={"filedata": ("img.jpg", data, "image/jpeg")}, timeout=30)
131
  if resp.status_code == 200 and resp.text.startswith("http"):
 
132
  return resp.text.strip()
133
  log(f"⚠️ catbox 失败: HTTP {resp.status_code}")
134
  except Exception as e:
135
  log(f"⚠️ catbox 异常: {e}")
136
- # 0x0.st
 
137
  try:
138
  resp = requests.post("https://0x0.st",
139
  files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
140
  if resp.status_code == 200 and resp.text.startswith("http"):
 
141
  return resp.text.strip()
142
  log(f"⚠️ 0x0 失败: HTTP {resp.status_code}")
143
  except Exception as e:
144
  log(f"⚠️ 0x0 异常: {e}")
145
- # tmpfiles
146
- try:
147
- resp = requests.post("https://tmpfiles.org/api/v1/upload",
148
- files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
149
- if resp.status_code == 200:
150
- url = resp.json().get("data", {}).get("url", "")
151
- if url:
152
- return url.replace("tmpfiles.org/", "tmpfiles.org/dl/")
153
- log(f"⚠️ tmpfiles 失败: HTTP {resp.status_code}")
154
- except Exception as e:
155
- log(f"⚠️ tmpfiles 异常: {e}")
156
  return None
157
 
158
  def reply_text(token, chat_id, msg_id, text):
 
123
  return None
124
 
125
  def upload_image(data):
126
+ """上传图片到图床,按可用性排序,多重 fallback"""
127
+ # 1. tmpfiles.org — 当前最稳定
128
+ try:
129
+ resp = requests.post("https://tmpfiles.org/api/v1/upload",
130
+ files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
131
+ if resp.status_code == 200:
132
+ url = resp.json().get("data", {}).get("url", "")
133
+ if url:
134
+ result = url.replace("tmpfiles.org/", "tmpfiles.org/dl/")
135
+ log(f"📤 tmpfiles 成功")
136
+ return result
137
+ log(f"⚠️ tmpfiles 失败: HTTP {resp.status_code}")
138
+ except Exception as e:
139
+ log(f"⚠️ tmpfiles 异常: {e}")
140
+
141
+ # 2. Telegraph (Telegra.ph) — 无需 API key
142
+ try:
143
+ import base64
144
+ # Telegraph 的图片上传接口
145
+ resp = requests.post("https://telegra.ph/upload",
146
+ files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
147
+ if resp.status_code == 200:
148
+ result = resp.json()
149
+ if isinstance(result, list) and len(result) > 0:
150
+ src = result[0].get("src", "")
151
+ if src:
152
+ url = f"https://telegra.ph{src}"
153
+ log(f"📤 telegraph 成功")
154
+ return url
155
+ log(f"⚠️ telegraph 失败: HTTP {resp.status_code}")
156
+ except Exception as e:
157
+ log(f"⚠️ telegraph 异常: {e}")
158
+
159
+ # 3. file.io — 一次性链接但可用性高
160
+ try:
161
+ resp = requests.post("https://file.io",
162
+ files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
163
+ if resp.status_code == 200:
164
+ url = resp.json().get("link", "")
165
+ if url:
166
+ log(f"📤 file.io 成功 (注意: 链接仅可下载一次)")
167
+ return url
168
+ log(f"⚠️ file.io 失败: HTTP {resp.status_code}")
169
+ except Exception as e:
170
+ log(f"⚠️ file.io 异常: {e}")
171
+
172
+ # 4. catbox.moe — 近期返回 412,保留作为后备
173
  try:
174
  resp = requests.post("https://catbox.moe/user/api.php",
175
  data={"reqtype": "fileupload"},
176
  files={"filedata": ("img.jpg", data, "image/jpeg")}, timeout=30)
177
  if resp.status_code == 200 and resp.text.startswith("http"):
178
+ log(f"📤 catbox 成功")
179
  return resp.text.strip()
180
  log(f"⚠️ catbox 失败: HTTP {resp.status_code}")
181
  except Exception as e:
182
  log(f"⚠️ catbox 异常: {e}")
183
+
184
+ # 5. 0x0.st — 近期返回 403,保留作为最后手段
185
  try:
186
  resp = requests.post("https://0x0.st",
187
  files={"file": ("img.jpg", data, "image/jpeg")}, timeout=30)
188
  if resp.status_code == 200 and resp.text.startswith("http"):
189
+ log(f"📤 0x0 成功")
190
  return resp.text.strip()
191
  log(f"⚠️ 0x0 失败: HTTP {resp.status_code}")
192
  except Exception as e:
193
  log(f"⚠️ 0x0 异常: {e}")
194
+
195
+
 
 
 
 
 
 
 
 
 
196
  return None
197
 
198
  def reply_text(token, chat_id, msg_id, text):