aioamazondevices 0.7.0__py3-none-any.whl → 0.7.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  """aioamazondevices library."""
2
2
 
3
- __version__ = "0.7.0"
3
+ __version__ = "0.7.1"
4
4
 
5
5
 
6
6
  from .api import AmazonDevice, AmazonEchoApi
aioamazondevices/api.py CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  import base64
4
4
  import hashlib
5
+ import mimetypes
5
6
  import secrets
6
7
  import uuid
7
8
  from dataclasses import dataclass
@@ -24,8 +25,11 @@ from .const import (
24
25
  AMAZON_CLIENT_OS,
25
26
  AMAZON_DEVICE_SOFTWARE_VERSION,
26
27
  AMAZON_DEVICE_TYPE,
28
+ DEFAULT_ASSOC_HANDLE,
27
29
  DEFAULT_HEADERS,
28
30
  DOMAIN_BY_COUNTRY,
31
+ HTML_EXTENSION,
32
+ JSON_EXTENSION,
29
33
  URI_QUERIES,
30
34
  )
31
35
  from .exceptions import CannotAuthenticate, CannotRegisterDevice
@@ -61,9 +65,10 @@ class AmazonEchoApi:
61
65
  locale = DOMAIN_BY_COUNTRY.get(country_code)
62
66
  domain = locale["domain"] if locale else country_code
63
67
 
64
- assoc_handle = "amzn_dp_project_dee_ios"
65
- if not locale:
66
- assoc_handle += f"_{country_code}"
68
+ if locale and (assoc := locale.get("openid.assoc_handle")):
69
+ assoc_handle = assoc
70
+ else:
71
+ assoc_handle = f"{DEFAULT_ASSOC_HANDLE}_{country_code}"
67
72
  self._assoc_handle = assoc_handle
68
73
 
69
74
  self._login_email = login_email
@@ -195,31 +200,22 @@ class AmazonEchoApi:
195
200
  url,
196
201
  data=input_data,
197
202
  )
198
- content_type = resp.headers.get("Content-Type", "")
203
+ content_type: str = resp.headers.get("Content-Type", "")
199
204
  _LOGGER.debug("Response content type: %s", content_type)
200
205
 
201
- if "text/html" in content_type:
202
- await self._save_to_file(
203
- resp.text,
204
- url,
205
- )
206
- elif content_type == "application/json":
207
- await self._save_to_file(
208
- orjson.dumps(
209
- orjson.loads(resp.text),
210
- option=orjson.OPT_INDENT_2,
211
- ).decode("utf-8"),
212
- url,
213
- extension="json",
214
- )
206
+ await self._save_to_file(
207
+ resp.text,
208
+ url,
209
+ mimetypes.guess_extension(content_type.split(";")[0]) or ".raw",
210
+ )
215
211
 
216
212
  return BeautifulSoup(resp.content, "html.parser"), resp
217
213
 
218
214
  async def _save_to_file(
219
215
  self,
220
- html_code: str,
216
+ raw_data: str | dict,
221
217
  url: str,
222
- extension: str = "html",
218
+ extension: str = HTML_EXTENSION,
223
219
  output_path: str = "out",
224
220
  ) -> None:
225
221
  """Save response data to disk."""
@@ -229,10 +225,33 @@ class AmazonEchoApi:
229
225
  output_dir = Path(output_path)
230
226
  output_dir.mkdir(parents=True, exist_ok=True)
231
227
 
232
- url_split = url.split("/")
233
- filename = f"{url_split[3]}-{url_split[4].split('?')[0]}.{extension}"
234
- with Path.open(output_dir / filename, "w+") as file:
235
- file.write(html_code)
228
+ if url.startswith("http"):
229
+ url_split = url.split("/")
230
+ base_filename = f"{url_split[3]}-{url_split[4].split('?')[0]}"
231
+ else:
232
+ base_filename = url
233
+ fullpath = Path(output_dir, base_filename + extension)
234
+
235
+ if type(raw_data) is dict:
236
+ data = orjson.dumps(raw_data, option=orjson.OPT_INDENT_2).decode("utf-8")
237
+ elif extension == HTML_EXTENSION:
238
+ data = raw_data
239
+ else:
240
+ data = orjson.dumps(
241
+ orjson.loads(raw_data),
242
+ option=orjson.OPT_INDENT_2,
243
+ ).decode("utf-8")
244
+
245
+ i = 2
246
+ while fullpath.exists():
247
+ filename = f"{base_filename}_{i!s}{extension}"
248
+ fullpath = Path(output_dir, filename)
249
+ i += 1
250
+
251
+ _LOGGER.warning("Saving data to %s", fullpath)
252
+
253
+ with Path.open(fullpath, "w+") as file:
254
+ file.write(data)
236
255
  file.write("\n")
237
256
 
238
257
  async def _register_device(
@@ -277,9 +296,9 @@ class AmazonEchoApi:
277
296
 
278
297
  headers = {"Content-Type": "application/json"}
279
298
 
280
- _LOGGER.warning("_register_device: [data=%s],[headers=%s]", body, headers)
299
+ register_url = f"https://api.amazon.{self._domain}/auth/register"
281
300
  resp = await self.session.post(
282
- f"https://api.amazon.{self._domain}/auth/register",
301
+ register_url,
283
302
  json=body,
284
303
  headers=headers,
285
304
  )
@@ -293,6 +312,11 @@ class AmazonEchoApi:
293
312
  )
294
313
  raise CannotRegisterDevice(resp_json)
295
314
 
315
+ await self._save_to_file(
316
+ resp.text,
317
+ url=register_url,
318
+ extension=JSON_EXTENSION,
319
+ )
296
320
  success_response = resp_json["response"]["success"]
297
321
 
298
322
  tokens = success_response["tokens"]
@@ -312,7 +336,7 @@ class AmazonEchoApi:
312
336
  for cookie in tokens["website_cookies"]:
313
337
  website_cookies[cookie["Name"]] = cookie["Value"].replace(r'"', r"")
314
338
 
315
- return {
339
+ login_data = {
316
340
  "adp_token": adp_token,
317
341
  "device_private_key": device_private_key,
318
342
  "access_token": access_token,
@@ -323,6 +347,8 @@ class AmazonEchoApi:
323
347
  "device_info": device_info,
324
348
  "customer_info": customer_info,
325
349
  }
350
+ await self._save_to_file(login_data, "login_data", JSON_EXTENSION)
351
+ return login_data
326
352
 
327
353
  async def login(self, otp_code: str) -> dict[str, Any]:
328
354
  """Login to Amazon."""
@@ -386,7 +412,7 @@ class AmazonEchoApi:
386
412
 
387
413
  register_device = await self._register_device(device_login_data)
388
414
 
389
- _LOGGER.warning("Register device: %s", register_device)
415
+ _LOGGER.info("Register device: %s", register_device)
390
416
  return register_device
391
417
 
392
418
  async def close(self) -> None:
aioamazondevices/const.py CHANGED
@@ -4,10 +4,12 @@ import logging
4
4
 
5
5
  _LOGGER = logging.getLogger(__package__)
6
6
 
7
+ DEFAULT_ASSOC_HANDLE = "amzn_dp_project_dee_ios"
8
+
7
9
  DOMAIN_BY_COUNTRY = {
8
10
  "us": {
9
11
  "domain": "com",
10
- "openid.assoc_handle": "amzn_dp_project_dee_ios",
12
+ "openid.assoc_handle": DEFAULT_ASSOC_HANDLE,
11
13
  },
12
14
  "uk": {
13
15
  "domain": "co.uk",
@@ -17,6 +19,7 @@ DOMAIN_BY_COUNTRY = {
17
19
  },
18
20
  "jp": {
19
21
  "domain": "co.jp",
22
+ "openid.assoc_handle": "jpflex",
20
23
  },
21
24
  "br": {
22
25
  "domain": "com.br",
@@ -48,3 +51,7 @@ AMAZON_APP_VERSION = "2.2.556530.0"
48
51
  AMAZON_DEVICE_SOFTWARE_VERSION = "35602678"
49
52
  AMAZON_DEVICE_TYPE = "A2IVLV5VM2W81"
50
53
  AMAZON_CLIENT_OS = "16.6"
54
+
55
+ # File extensions
56
+ HTML_EXTENSION = ".html"
57
+ JSON_EXTENSION = ".json"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aioamazondevices
3
- Version: 0.7.0
3
+ Version: 0.7.1
4
4
  Summary: Python library to control Amazon devices
5
5
  Home-page: https://github.com/chemelli74/aioamazondevices
6
6
  License: Apache Software License 2.0
@@ -15,8 +15,10 @@ Classifier: Operating System :: OS Independent
15
15
  Classifier: Programming Language :: Python :: 3
16
16
  Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
18
19
  Classifier: Topic :: Software Development :: Libraries
19
20
  Requires-Dist: beautifulsoup4
21
+ Requires-Dist: colorlog
20
22
  Requires-Dist: httpx
21
23
  Requires-Dist: orjson
22
24
  Project-URL: Bug Tracker, https://github.com/chemelli74/aioamazondevices/issues
@@ -0,0 +1,9 @@
1
+ aioamazondevices/__init__.py,sha256=IxGu-Lv8_pSEPCQnjhVi-_VdJGmh9WI0NOELkRSSVjQ,276
2
+ aioamazondevices/api.py,sha256=3Gshun5XSEbD47aDs-hyQkZ_zVYuQ3OMwXIaZA13lmo,15902
3
+ aioamazondevices/const.py,sha256=CQFuM-4gFrzveQcBMsH6noI8PN69J4ogV-P46uYhcTQ,1330
4
+ aioamazondevices/exceptions.py,sha256=yQ9nL4UwBdHNXvdRj8TRemed6PXBmExP8lbHaAp04vY,546
5
+ aioamazondevices/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ aioamazondevices-0.7.1.dist-info/LICENSE,sha256=sS48k5sp9bFV-NSHDfAJuTZZ_-AP9ZDqUzQ9sffGlsg,11346
7
+ aioamazondevices-0.7.1.dist-info/METADATA,sha256=Eq-0torKLJNk79WSTqq2I4pnPqAM-VsQ3u8ibrVytUo,4755
8
+ aioamazondevices-0.7.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
9
+ aioamazondevices-0.7.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.0
2
+ Generator: poetry-core 1.9.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,9 +0,0 @@
1
- aioamazondevices/__init__.py,sha256=fEz-KfjtZ3198YaYkTtbCjFrFy7tPcsxD2FCXiZmemQ,276
2
- aioamazondevices/api.py,sha256=BBk4pW2IwmPvZFYeBl2ZtWumIflVwiosaKktBDGM_I0,15124
3
- aioamazondevices/const.py,sha256=GIJWFWQ3v5K-Bh2_eEVilPKdfe761j0QloRbYwSRHic,1175
4
- aioamazondevices/exceptions.py,sha256=yQ9nL4UwBdHNXvdRj8TRemed6PXBmExP8lbHaAp04vY,546
5
- aioamazondevices/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- aioamazondevices-0.7.0.dist-info/LICENSE,sha256=sS48k5sp9bFV-NSHDfAJuTZZ_-AP9ZDqUzQ9sffGlsg,11346
7
- aioamazondevices-0.7.0.dist-info/METADATA,sha256=cFCuQinFK3QiOv0RqJXEJB8R1atXT9_W-f5UdoF2nw4,4680
8
- aioamazondevices-0.7.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
9
- aioamazondevices-0.7.0.dist-info/RECORD,,