wvp.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from .lib import *
  2. from time import time
  3. from threading import Lock
  4. from requests import session
  5. __all__ = ["WvpProxy"]
  6. class WvpProxy:
  7. def __init__(self):
  8. self.__sess = session()
  9. self.__nextExpire, self.__expireLock = time(), Lock()
  10. self.__login()
  11. def __isExpired(self) -> "bool":
  12. with self.__expireLock:
  13. return time() < self.__nextExpire
  14. def __login(self):
  15. resp = self.__sess.get(f"{WVP_API}/user/login?username={USER}&password={PWD_MD5}", headers=HEADER)
  16. assert resp.status_code == 200, f"Login Fail: status code is <{resp.status_code}>"
  17. assert (body := resp.json()) and body["code"] == 0, f"Login Fail: body code is <{body['code']}>"
  18. if resp.cookies:
  19. self.__sess.cookies = resp.cookies
  20. with self.__expireLock:
  21. self.__nextExpire = time() + ALIVE_TIME
  22. def start(self, device: "str", channel: "str") -> "str":
  23. if self.__isExpired():
  24. self.__login()
  25. resp = self.__sess.get(f"{WVP_API}/play/start/{device}/{channel}", headers=HEADER)
  26. assert resp.status_code == 200, f"Start Fail: status code is <{resp.status_code}>"
  27. body = resp.json()
  28. assert body["code"] == 0, f"Start Fail: body code is <{body['code']}>"
  29. with self.__expireLock:
  30. self.__nextExpire = time() + ALIVE_TIME
  31. return f"http://139.9.167.178:180/rtp/{device}_{channel}.live.mp4" # body["data"]["fmp4"]
  32. def stop(self, device: "str", channel: "str") -> "None":
  33. if self.__isExpired():
  34. self.__login()
  35. resp = self.__sess.get(f"{WVP_API}/play/stop/{device}/{channel}", headers=HEADER)
  36. assert resp.status_code == 200, f"Stop Fail: status code is <{resp.status_code}>"
  37. with self.__expireLock:
  38. self.__nextExpire = time() + ALIVE_TIME