kimi_balance_monitor.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. @author: luojunhui
  3. """
  4. import requests
  5. import traceback
  6. from applications import bot
  7. from applications.utils.decorator import retryOnTimeout
  8. BALANCE_LIMIT_THRESHOLD = 200.0
  9. @retryOnTimeout(retries=5, delay=5)
  10. def check_kimi_balance():
  11. """
  12. 校验kimi余额
  13. :return:
  14. """
  15. url = "https://api.moonshot.cn/v1/users/me/balance"
  16. payload = {}
  17. headers = {
  18. 'Authorization': 'Bearer sk-5DqYCa88kche6nwIWjLE1p4oMm8nXrR9kQMKbBolNAWERu7q'
  19. }
  20. response = requests.request("GET", url, headers=headers, data=payload, timeout=10)
  21. if response.status_code == 200:
  22. response_json = response.json()
  23. print(response_json)
  24. try:
  25. balance = response_json['data']['available_balance']
  26. if balance < BALANCE_LIMIT_THRESHOLD:
  27. bot(
  28. title="kimi余额小于 {} 块".format(BALANCE_LIMIT_THRESHOLD),
  29. detail={
  30. "balance": balance
  31. }
  32. )
  33. except Exception as e:
  34. error_stack = traceback.format_exc()
  35. bot(
  36. title="kimi余额接口处理失败,数据结构异常",
  37. detail={
  38. "error": str(e),
  39. "error_msg": error_stack
  40. }
  41. )
  42. else:
  43. bot(
  44. title="kimi余额接口调用失败",
  45. detail={
  46. "response": response.text
  47. }
  48. )
  49. if __name__ == '__main__':
  50. check_kimi_balance()