43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
import json
|
|
import logging
|
|
from datetime import datetime
|
|
|
|
import httpx
|
|
|
|
from yuxi.channel.extensions.alipay.types import AlipayAccount
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ALIPAY_FILE_GATEWAY_PRODUCTION = "https://openfile.alipay.com/chat/multimedia.do"
|
|
ALIPAY_FILE_GATEWAY_SANDBOX = "https://openfile.alipaydev.com/chat/multimedia.do"
|
|
|
|
|
|
class AlipayMedia:
|
|
def __init__(self, gateway=None):
|
|
self._gateway = gateway
|
|
|
|
async def download(self, account: AlipayAccount, media_id: str) -> bytes | None:
|
|
gateway_url = (
|
|
ALIPAY_FILE_GATEWAY_SANDBOX if "sandbox" in account.gateway_url else ALIPAY_FILE_GATEWAY_PRODUCTION
|
|
)
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(15.0)) as client:
|
|
resp = await client.get(
|
|
gateway_url,
|
|
params={
|
|
"app_id": account.app_id,
|
|
"method": "alipay.mobile.public.multimedia.download",
|
|
"format": "JSON",
|
|
"charset": "utf-8",
|
|
"sign_type": "RSA2",
|
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
"version": "1.0",
|
|
"biz_content": json.dumps({"media_id": media_id}),
|
|
},
|
|
)
|
|
resp.raise_for_status()
|
|
return resp.content
|
|
except Exception:
|
|
logger.exception("Failed to download media %s", media_id)
|
|
return None
|