Skip to content

Commit ad08a8c

Browse files
committed
Rewrite get_snap to use requests lib as the old method did not work.
1 parent b4f95d7 commit ad08a8c

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

api/recording.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import io
1+
import requests
22
import random
33
import string
4-
from urllib import request
5-
4+
from urllib import parse
5+
from io import BytesIO
66
from PIL import Image
7-
87
from RtspClient import RtspClient
9-
from resthandle import Request
108

119

1210
class RecordingAPIMixin:
@@ -49,19 +47,19 @@ def get_snap(self, timeout: int = 3) -> Image or None:
4947
:param timeout: Request timeout to camera in seconds
5048
:return: Image or None
5149
"""
52-
randomstr = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
53-
snap = self.url + "?cmd=Snap&channel=0&rs=" \
54-
+ randomstr \
55-
+ "&user=" + self.username \
56-
+ "&password=" + self.password
50+
data = {}
51+
data['cmd'] = 'Snap'
52+
data['channel'] = 0
53+
data['rs'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
54+
data['user'] = self.username
55+
data['password'] = self.password
56+
parms = parse.urlencode(data).encode("utf-8")
57+
5758
try:
58-
req = request.Request(snap)
59-
req.set_proxy(Request.proxies, 'http')
60-
reader = request.urlopen(req, timeout)
61-
if reader.status == 200:
62-
b = bytearray(reader.read())
63-
return Image.open(io.BytesIO(b))
64-
print("Could not retrieve data from camera successfully. Status:", reader.status)
59+
response = requests.get(self.url, params=parms, timeout=timeout)
60+
if response.status_code == 200:
61+
return Image.open(BytesIO(response.content))
62+
print("Could not retrieve data from camera successfully. Status:", response.stats_code)
6563
return None
6664

6765
except Exception as e:

0 commit comments

Comments
 (0)