kimi_balance_monitor.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. @author: luojunhui
  3. """
  4. import requests
  5. import traceback
  6. from applications import bot
  7. from applications.decoratorApi 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. try:
  24. balance = response_json['data']['available_balance']
  25. if balance < BALANCE_LIMIT_THRESHOLD:
  26. bot(
  27. title="kimi余额小于 {} 块".format(BALANCE_LIMIT_THRESHOLD),
  28. detail={
  29. "balance": balance
  30. }
  31. )
  32. except Exception as e:
  33. error_stack = traceback.format_exc()
  34. bot(
  35. title="kimi余额接口处理失败,数据结构异常",
  36. detail={
  37. "error": str(e),
  38. "error_msg": error_stack
  39. }
  40. )
  41. else:
  42. bot(
  43. title="kimi余额接口调用失败",
  44. detail={
  45. "response": response.text
  46. }
  47. )
  48. if __name__ == '__main__':
  49. check_kimi_balance()