Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 authored Jul 5, 2024
2 parents e7c002b + 5eff195 commit cf2c434
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
16 changes: 8 additions & 8 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
flask
Flask-HTTPAuth
paho-mqtt
pydantic
python-dotenv
requests
pyyaml
xxtea # windows .whl currently only exist for Python 3.8.x
Flask==3.0.*
Flask-HTTPAuth==4.8.*
paho-mqtt== 2.1.*
pydantic==2.8.*
python-dotenv==1.0.*
requests==2.32.*
PyYAML==6.0.*
xxtea==3.2.*
47 changes: 26 additions & 21 deletions app/wyzecam/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ class AccessTokenError(Exception):


class RateLimitError(Exception):
def __init__(self, resp):
reset = resp.headers.get("X-RateLimit-Reset-By")
self.remaining = int(resp.headers.get("X-RateLimit-Remaining", 0))
self.reset_by = self.get_reset_time(reset)
super().__init__(f"{self.remaining} requests remaining until {reset}")
def __init__(self, resp: Response):
self.remaining: int = self.parse_remaining(resp)
reset_by: str = resp.headers.get("X-RateLimit-Reset-By", "")
self.reset_by: int = self.get_reset_time(reset_by)
super().__init__(f"{self.remaining} requests remaining until {reset_by}")

@staticmethod
def parse_remaining(resp: Response) -> int:
try:
return int(resp.headers.get("X-RateLimit-Remaining", 0))
except Exception:
return 0

def get_reset_time(self, reset_by: str):
@staticmethod
def get_reset_time(reset_by: str) -> int:
ts_format = "%a %b %d %H:%M:%S %Z %Y"
try:
return int(datetime.strptime(reset_by, ts_format).timestamp())
Expand Down Expand Up @@ -112,10 +120,11 @@ def refresh_token(auth_info: WyzeCredential) -> WyzeCredential:
[get_camera_list()][wyzecam.api.get_camera_list].
"""
payload = _payload(auth_info.access_token, auth_info.phone_id)
payload = _payload(auth_info)
payload["refresh_token"] = auth_info.refresh_token

resp = post(f"{WYZE_API}/user/refresh_token", json=payload, headers=_headers())

resp_json = validate_resp(resp)
resp_json["user_id"] = auth_info.user_id
resp_json["phone_id"] = auth_info.phone_id
Expand All @@ -136,9 +145,7 @@ def get_user_info(auth_info: WyzeCredential) -> WyzeAccount:
"""
resp = post(
f"{WYZE_API}/user/get_user_info",
json=_payload(auth_info.access_token, auth_info.phone_id),
headers=_headers(),
f"{WYZE_API}/user/get_user_info", json=_payload(auth_info), headers=_headers()
)

resp_json = validate_resp(resp)
Expand All @@ -151,7 +158,7 @@ def get_homepage_object_list(auth_info: WyzeCredential) -> dict[str, Any]:
"""Get all homepage objects."""
resp = post(
f"{WYZE_API}/v2/home_page/get_object_list",
json=_payload(auth_info.access_token, auth_info.phone_id),
json=_payload(auth_info),
headers=_headers(),
)

Expand Down Expand Up @@ -213,7 +220,7 @@ def get_camera_list(auth_info: WyzeCredential) -> list[WyzeCamera]:
def run_action(auth_info: WyzeCredential, camera: WyzeCamera, action: str):
"""Send run_action commands to the camera."""
payload = dict(
_payload(auth_info.access_token, auth_info.phone_id, "run_action"),
_payload(auth_info, "run_action"),
action_params={},
action_key=action,
instance_id=camera.mac,
Expand All @@ -236,7 +243,7 @@ def post_device(
headers = sign_payload(auth_info, "9319141212m2ik", payload)
resp = post(device_url, data=payload, headers=headers)
else:
params |= _payload(auth_info.access_token, auth_info.phone_id, endpoint)
params |= _payload(auth_info, endpoint)
resp = post(device_url, json=params, headers=_headers())

return validate_resp(resp)
Expand Down Expand Up @@ -284,20 +291,18 @@ def validate_resp(resp: Response) -> dict:
return resp_json.get("data", resp_json)


def _payload(
access_token: Optional[str], phone_id: Optional[str] = "", endpoint: str = "default"
) -> dict:
endpoint = endpoint if endpoint in SC_SV else "default"
def _payload(auth_info: WyzeCredential, endpoint: str = "default") -> dict:
values = SC_SV.get(endpoint, SC_SV["default"])
return {
"sc": SC_SV[endpoint]["sc"],
"sv": SC_SV[endpoint]["sv"],
"sc": values["sc"],
"sv": values["sv"],
"app_ver": f"com.hualai.WyzeCam___{APP_VERSION}",
"app_version": APP_VERSION,
"app_name": "com.hualai.WyzeCam",
"phone_system_type": 1,
"ts": int(time.time() * 1000),
"access_token": access_token,
"phone_id": phone_id,
"access_token": auth_info.access_token,
"phone_id": auth_info.phone_id,
}


Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.hwaccel
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN pip3 install --disable-pip-version-check --prefix=/build/usr/local -r /build
RUN cd /build \
&& . app/.env \
&& mkdir -p tokens img ${QSV:+usr/lib} \
&& curl -SL https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz \
&& curl -SL https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n6.1-latest-linux64-gpl-6.1.tar.xz \
| tar -Jxf - --strip-components=1 -C usr/local --wildcards '*ffmpeg' \
&& curl -SL https://github.com/bluenviron/mediamtx/releases/download/v${MTX_TAG}/mediamtx_v${MTX_TAG}_linux_amd64.tar.gz \
| tar -xzf - -C app --wildcards 'mediamtx*' \
Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile.qsv
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ARG QSV
ARG BUILD_DATE
RUN if [ -n "$QSV" ]; then echo 'deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware' >/etc/apt/sources.list.d/debian-testing.list; fi \
&& apt-get update \
&& apt-get install -y curl tar \
&& apt-get install -y curl tar xz-utils \
${QSV:+i965-va-driver intel-gpu-tools intel-media-va-driver-non-free intel-opencl-icd libmfx1 libva-drm2 libx11-6 vainfo} \
&& if [ -n "$QSV" ]; then apt-get install -y i965-va-driver-shaders; fi \
&& apt-get clean \
Expand All @@ -19,8 +19,8 @@ RUN pip3 install --disable-pip-version-check --prefix=/build/usr/local -r /build
RUN cd /build \
&& . app/.env \
&& mkdir -p tokens img ${QSV:+usr/lib} \
&& curl -SL https://github.com/homebridge/ffmpeg-for-homebridge/releases/latest/download/ffmpeg-alpine-x86_64.tar.gz \
| tar xzf - -C . \
&& curl -SL https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n6.1-latest-linux64-gpl-6.1.tar.xz \
| tar -Jxf - --strip-components=1 -C usr/local --wildcards '*ffmpeg' \
&& curl -SL https://github.com/bluenviron/mediamtx/releases/download/v${MTX_TAG}/mediamtx_v${MTX_TAG}_linux_amd64.tar.gz \
| tar -xzf - -C app --wildcards 'mediamtx*' \
&& cp app/lib/lib.amd64 usr/local/lib/libIOTCAPIs_ALL.so \
Expand Down

0 comments on commit cf2c434

Please sign in to comment.