Spaces:
Sleeping
Sleeping
Dink0 commited on
Commit ·
04c44bc
1
Parent(s): a70c3aa
Temporarily verify MAX public post API access
Browse files
app.py
CHANGED
|
@@ -739,6 +739,66 @@ def health_check():
|
|
| 739 |
return {"status": "ok"}
|
| 740 |
|
| 741 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
@app.post("/parse", response_model=ParseResponse)
|
| 743 |
def parse_links(payload: ParseRequest):
|
| 744 |
deadline = time.monotonic() + TOTAL_TIME_BUDGET
|
|
|
|
| 739 |
return {"status": "ok"}
|
| 740 |
|
| 741 |
|
| 742 |
+
@app.get("/_max_public_probe")
|
| 743 |
+
def max_public_probe():
|
| 744 |
+
import base64
|
| 745 |
+
import hashlib
|
| 746 |
+
import json
|
| 747 |
+
import ssl
|
| 748 |
+
import urllib.error
|
| 749 |
+
import urllib.request
|
| 750 |
+
|
| 751 |
+
token = os.getenv("MAX_ACCESS_TOKEN", "").strip()
|
| 752 |
+
if not token:
|
| 753 |
+
return {"configured": False}
|
| 754 |
+
try:
|
| 755 |
+
cert_response = requests.get(
|
| 756 |
+
"https://gu-st.ru/content/Other/doc/russian_trusted_root_ca.cer",
|
| 757 |
+
timeout=(5, 10),
|
| 758 |
+
)
|
| 759 |
+
cert_response.raise_for_status()
|
| 760 |
+
pem = cert_response.text
|
| 761 |
+
fingerprint = hashlib.sha256(ssl.PEM_cert_to_DER_cert(pem)).hexdigest()
|
| 762 |
+
if fingerprint != "d26d2d0231b7c39f92cc738512ba54103519e4405d68b5bd703e9788ca8ecf31":
|
| 763 |
+
return {"configured": True, "certificate": "unexpected_fingerprint"}
|
| 764 |
+
context = ssl.create_default_context(cadata=pem)
|
| 765 |
+
except Exception as exc:
|
| 766 |
+
return {"configured": True, "certificate_error": type(exc).__name__}
|
| 767 |
+
|
| 768 |
+
def check(path):
|
| 769 |
+
request = urllib.request.Request(
|
| 770 |
+
"https://platform-api2.max.ru" + path,
|
| 771 |
+
headers={"Authorization": token},
|
| 772 |
+
)
|
| 773 |
+
try:
|
| 774 |
+
with urllib.request.urlopen(request, context=context, timeout=8) as response:
|
| 775 |
+
data = json.load(response)
|
| 776 |
+
stat = data.get("stat") or {}
|
| 777 |
+
return {"http": response.status, "code": data.get("code"),
|
| 778 |
+
"views": stat.get("views"),
|
| 779 |
+
"message_count": len(data.get("messages", [])),
|
| 780 |
+
"is_bot": data.get("is_bot")}
|
| 781 |
+
except urllib.error.HTTPError as exc:
|
| 782 |
+
try:
|
| 783 |
+
data = json.load(exc)
|
| 784 |
+
except Exception:
|
| 785 |
+
data = {}
|
| 786 |
+
return {"http": exc.code, "code": data.get("code")}
|
| 787 |
+
except Exception as exc:
|
| 788 |
+
return {"error": type(exc).__name__}
|
| 789 |
+
|
| 790 |
+
post_id = "AaE-viFyB3E"
|
| 791 |
+
post_hex = base64.urlsafe_b64decode(post_id + "=").hex()
|
| 792 |
+
channel_id = 68067500157571
|
| 793 |
+
negative_hex = ((-channel_id) & ((1 << 64) - 1)).to_bytes(8, "big").hex()
|
| 794 |
+
positive_hex = channel_id.to_bytes(8, "big").hex()
|
| 795 |
+
return {"configured": True, "auth": check("/me"),
|
| 796 |
+
"channel": check(f"/messages?chat_id={-channel_id}&count=1"),
|
| 797 |
+
"negative_mid": check(f"/messages/mid.{negative_hex}{post_hex}"),
|
| 798 |
+
"positive_mid": check(f"/messages/mid.{positive_hex}{post_hex}"),
|
| 799 |
+
"short_id": check(f"/messages/{post_id}")}
|
| 800 |
+
|
| 801 |
+
|
| 802 |
@app.post("/parse", response_model=ParseResponse)
|
| 803 |
def parse_links(payload: ParseRequest):
|
| 804 |
deadline = time.monotonic() + TOTAL_TIME_BUDGET
|