|
1 | | -import io |
| 1 | +import requests |
2 | 2 | import random |
3 | 3 | import string |
4 | | -from urllib import request |
5 | | - |
| 4 | +from urllib import parse |
| 5 | +from io import BytesIO |
6 | 6 | from PIL import Image |
7 | | - |
8 | 7 | from RtspClient import RtspClient |
9 | | -from resthandle import Request |
10 | 8 |
|
11 | 9 |
|
12 | 10 | class RecordingAPIMixin: |
@@ -49,19 +47,19 @@ def get_snap(self, timeout: int = 3) -> Image or None: |
49 | 47 | :param timeout: Request timeout to camera in seconds |
50 | 48 | :return: Image or None |
51 | 49 | """ |
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 | + |
57 | 58 | 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) |
65 | 63 | return None |
66 | 64 |
|
67 | 65 | except Exception as e: |
|
0 commit comments