Skip to content

Instantly share code, notes, and snippets.

@albertofwb
Last active December 25, 2024 05:04
Show Gist options
  • Save albertofwb/bad7a78fd4efb74d7c6082bb5d82b49f to your computer and use it in GitHub Desktop.
Save albertofwb/bad7a78fd4efb74d7c6082bb5d82b49f to your computer and use it in GitHub Desktop.
get okx sell usdt to cny rate 获取 usdt 汇率
import time
import requests
def get_okx_usdt_cny_sell_price() -> float:
"""Get Okx C2C quick trade USDT sell real-time rate"""
url = f"https://www.okx.com/v4/c2c/express/price?crypto=USDT&fiat=CNY&side=sell&t={int(time.time())}"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers, timeout=5)
response.raise_for_status()
result = response.json()
if int(result.get("error_code", -1)) != 0:
raise ValueError(f"json parse error: {result.get('error_message', 'Unknown error')}")
price = result.get("data", {}).get("price")
if price and float(price) > 0:
return float(price)
raise ValueError("okx resp json data.price not found or <= 0")
def main():
usdt_rate = get_okx_usdt_cny_sell_price()
print(usdt_rate)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment