Spaces:
Paused
Paused
Robust curl retries (--retry 4 --retry-all-errors) + aligned subprocess timeouts + download validation
Browse files
app.py
CHANGED
|
@@ -1319,12 +1319,14 @@ def _raw_get_file_path(file_id: str) -> str:
|
|
| 1319 |
import subprocess, json as _json
|
| 1320 |
result = subprocess.run(
|
| 1321 |
[
|
| 1322 |
-
"curl", "--ipv4", "-s", "-S", "--max-time", "15", "--connect-timeout", "10",
|
| 1323 |
"-d", f"file_id={file_id}",
|
| 1324 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getFile",
|
| 1325 |
],
|
| 1326 |
-
capture_output=True, text=True, timeout=
|
| 1327 |
)
|
|
|
|
|
|
|
| 1328 |
return _json.loads(result.stdout)["result"]["file_path"]
|
| 1329 |
|
| 1330 |
|
|
@@ -1354,14 +1356,16 @@ async def download_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|
| 1354 |
with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f:
|
| 1355 |
tmp = f.name
|
| 1356 |
try:
|
| 1357 |
-
subprocess.run(
|
| 1358 |
-
["curl", "--ipv4", "-s", "-S", "--max-time", "30", "--connect-timeout", "10",
|
| 1359 |
"-o", tmp, url],
|
| 1360 |
-
capture_output=True, text=True, timeout=
|
| 1361 |
)
|
| 1362 |
with open(tmp, "rb") as fh:
|
| 1363 |
data = fh.read()
|
| 1364 |
logger.info(f"Downloaded {len(data)} bytes, magic={data[:4]!r}")
|
|
|
|
|
|
|
| 1365 |
img = Image.open(io.BytesIO(data))
|
| 1366 |
img.load()
|
| 1367 |
return img.convert("RGB")
|
|
@@ -2108,12 +2112,12 @@ def _raw_send_message(chat_id: int, text: str):
|
|
| 2108 |
try:
|
| 2109 |
result = subprocess.run(
|
| 2110 |
[
|
| 2111 |
-
"curl", "--ipv4", "-s", "-S", "--max-time", "20", "--connect-timeout", "10",
|
| 2112 |
"-d", f"chat_id={chat_id}",
|
| 2113 |
"--data-urlencode", f"text={text}",
|
| 2114 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
|
| 2115 |
],
|
| 2116 |
-
capture_output=True, text=True, timeout=
|
| 2117 |
)
|
| 2118 |
ok = '"ok":true' in result.stdout
|
| 2119 |
if not ok:
|
|
@@ -2143,13 +2147,13 @@ def _raw_send_photo(chat_id: int, img_bytes: bytes, caption: str = ""):
|
|
| 2143 |
tmp = f.name
|
| 2144 |
result = subprocess.run(
|
| 2145 |
[
|
| 2146 |
-
"curl", "--ipv4", "-s", "-S", "--max-time", "60", "--connect-timeout", "10",
|
| 2147 |
"-F", f"chat_id={chat_id}",
|
| 2148 |
"-F", f"caption={caption}",
|
| 2149 |
"-F", f"photo=@{tmp};type=image/jpeg",
|
| 2150 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto",
|
| 2151 |
],
|
| 2152 |
-
capture_output=True, text=True, timeout=
|
| 2153 |
)
|
| 2154 |
ok = '"ok":true' in result.stdout
|
| 2155 |
if not ok:
|
|
@@ -2215,13 +2219,13 @@ def _archive_photo(img_bytes: bytes, caption: str = ""):
|
|
| 2215 |
tmp = f.name
|
| 2216 |
result = subprocess.run(
|
| 2217 |
[
|
| 2218 |
-
"curl", "--ipv4", "-s", "-S", "--max-time", "30", "--connect-timeout", "10",
|
| 2219 |
"-F", f"chat_id={ARCHIVE_CHANNEL_ID}",
|
| 2220 |
"-F", f"caption={caption[:1024]}",
|
| 2221 |
"-F", f"photo=@{tmp};type=image/jpeg",
|
| 2222 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto",
|
| 2223 |
],
|
| 2224 |
-
capture_output=True, text=True, timeout=
|
| 2225 |
)
|
| 2226 |
if '"ok":true' not in result.stdout:
|
| 2227 |
logger.warning(f"Archive photo failed: {result.stdout[:120]}")
|
|
@@ -3093,7 +3097,7 @@ class CurlCffiRequest(BaseRequest):
|
|
| 3093 |
body_path = bf.name
|
| 3094 |
tmp_files.append(body_path)
|
| 3095 |
try:
|
| 3096 |
-
cmd = ["curl", "--ipv4", "-s", "-S", "--max-time", str(mt),
|
| 3097 |
"--connect-timeout", "10", "-o", body_path,
|
| 3098 |
"-w", "%{http_code}", "-X", method]
|
| 3099 |
if request_data is not None:
|
|
@@ -3119,7 +3123,7 @@ class CurlCffiRequest(BaseRequest):
|
|
| 3119 |
for k, v in params.items():
|
| 3120 |
cmd += ["--data-urlencode", f"{k}={v}"]
|
| 3121 |
cmd.append(url)
|
| 3122 |
-
r = subprocess.run(cmd, capture_output=True, text=True, timeout=mt +
|
| 3123 |
code = int((r.stdout or "0").strip() or "0")
|
| 3124 |
with open(body_path, "rb") as fh:
|
| 3125 |
body = fh.read()
|
|
|
|
| 1319 |
import subprocess, json as _json
|
| 1320 |
result = subprocess.run(
|
| 1321 |
[
|
| 1322 |
+
"curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", "15", "--connect-timeout", "10",
|
| 1323 |
"-d", f"file_id={file_id}",
|
| 1324 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getFile",
|
| 1325 |
],
|
| 1326 |
+
capture_output=True, text=True, timeout=75,
|
| 1327 |
)
|
| 1328 |
+
if not result.stdout.strip():
|
| 1329 |
+
raise RuntimeError(f"getFile empty (curl rc={result.returncode}): {result.stderr[:150]}")
|
| 1330 |
return _json.loads(result.stdout)["result"]["file_path"]
|
| 1331 |
|
| 1332 |
|
|
|
|
| 1356 |
with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f:
|
| 1357 |
tmp = f.name
|
| 1358 |
try:
|
| 1359 |
+
r = subprocess.run(
|
| 1360 |
+
["curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", "30", "--connect-timeout", "10",
|
| 1361 |
"-o", tmp, url],
|
| 1362 |
+
capture_output=True, text=True, timeout=160,
|
| 1363 |
)
|
| 1364 |
with open(tmp, "rb") as fh:
|
| 1365 |
data = fh.read()
|
| 1366 |
logger.info(f"Downloaded {len(data)} bytes, magic={data[:4]!r}")
|
| 1367 |
+
if not data:
|
| 1368 |
+
raise RuntimeError(f"empty download (curl rc={r.returncode}): {r.stderr[:150]}")
|
| 1369 |
img = Image.open(io.BytesIO(data))
|
| 1370 |
img.load()
|
| 1371 |
return img.convert("RGB")
|
|
|
|
| 2112 |
try:
|
| 2113 |
result = subprocess.run(
|
| 2114 |
[
|
| 2115 |
+
"curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", "20", "--connect-timeout", "10",
|
| 2116 |
"-d", f"chat_id={chat_id}",
|
| 2117 |
"--data-urlencode", f"text={text}",
|
| 2118 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
|
| 2119 |
],
|
| 2120 |
+
capture_output=True, text=True, timeout=90,
|
| 2121 |
)
|
| 2122 |
ok = '"ok":true' in result.stdout
|
| 2123 |
if not ok:
|
|
|
|
| 2147 |
tmp = f.name
|
| 2148 |
result = subprocess.run(
|
| 2149 |
[
|
| 2150 |
+
"curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", "60", "--connect-timeout", "10",
|
| 2151 |
"-F", f"chat_id={chat_id}",
|
| 2152 |
"-F", f"caption={caption}",
|
| 2153 |
"-F", f"photo=@{tmp};type=image/jpeg",
|
| 2154 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto",
|
| 2155 |
],
|
| 2156 |
+
capture_output=True, text=True, timeout=140,
|
| 2157 |
)
|
| 2158 |
ok = '"ok":true' in result.stdout
|
| 2159 |
if not ok:
|
|
|
|
| 2219 |
tmp = f.name
|
| 2220 |
result = subprocess.run(
|
| 2221 |
[
|
| 2222 |
+
"curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", "30", "--connect-timeout", "10",
|
| 2223 |
"-F", f"chat_id={ARCHIVE_CHANNEL_ID}",
|
| 2224 |
"-F", f"caption={caption[:1024]}",
|
| 2225 |
"-F", f"photo=@{tmp};type=image/jpeg",
|
| 2226 |
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto",
|
| 2227 |
],
|
| 2228 |
+
capture_output=True, text=True, timeout=100,
|
| 2229 |
)
|
| 2230 |
if '"ok":true' not in result.stdout:
|
| 2231 |
logger.warning(f"Archive photo failed: {result.stdout[:120]}")
|
|
|
|
| 3097 |
body_path = bf.name
|
| 3098 |
tmp_files.append(body_path)
|
| 3099 |
try:
|
| 3100 |
+
cmd = ["curl", "--ipv4", "-s", "-S", "--retry", "4", "--retry-delay", "2", "--retry-all-errors", "--max-time", str(mt),
|
| 3101 |
"--connect-timeout", "10", "-o", body_path,
|
| 3102 |
"-w", "%{http_code}", "-X", method]
|
| 3103 |
if request_data is not None:
|
|
|
|
| 3123 |
for k, v in params.items():
|
| 3124 |
cmd += ["--data-urlencode", f"{k}={v}"]
|
| 3125 |
cmd.append(url)
|
| 3126 |
+
r = subprocess.run(cmd, capture_output=True, text=True, timeout=mt * 4 + 20)
|
| 3127 |
code = int((r.stdout or "0").strip() or "0")
|
| 3128 |
with open(body_path, "rb") as fh:
|
| 3129 |
body = fh.read()
|