Skip to content

Commit b5436fe

Browse files
mkb79bottydim
andauthored
V0.8.0dev (#89)
* fully support pre-Amazon accounts The `Authenticator` now holds the `with_username` attribute and other stuff for pre-Amazon accounts. Auth data saved with v0.8 or later can not be loaded with versions less than v0.8! If an auth file for an pre-Amazon account (with_username=True) was created with v0.7.1 or v0.7.2 set `auth.with_username` to `True` and save the data again. After this, deregistration, refreshing access tokens and requesting cookies for another domain will work for pre-Amazon accounts. * setdefault `with_username` to `False` when instantiate an Authenticator * rework setdefault `with_username` * Update client.py - `Client` and `AsynClient` now accepts session kwargs which are bypassed to the underlying httpx Client - rename (and rework) `Client._split_kwargs` to `Client._prepare_params` * Update CHANGELOG.md * Add `response_callback` to `Client` and `AsyncClient` This allows to set a custom response callback like: ```python def custom_response_callback(resp): return resp ``` This returns the unprepared `httpx.Response` object for further purposes (e.g. accessing respopnse headers). * remove trailing newlines * Update activation_bytes.py * remove whitspaces * work on docs and docstring * Update CHANGELOG.md * Create example to get and analyse listening statistics Include an example that authenticates using the browser and goes on to extract aggregate per month listening time since account creation (currently hardcoded to 2013) to the current month. Then the script uses time-series analysis to find trends and seasonalities in the listening pattern. * Create download_bookmarks.py Create an example to download all bookmarks for all books in the library and store them in a json file 'books.json'. * rework `_prepare_api_path` method of client classes - absolute url paths are passed through without preparation - query params for a given url will be keep intact * Update CHANGELOG.md Co-authored-by: Botty Dimanov <bottydim@users.noreply.github.com>
1 parent bdcf3ed commit b5436fe

23 files changed

+578
-117
lines changed

.github/workflows/pypi-publish-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
build-n-publish:
88
name: Build and publish Audible to TestPyPI
99
runs-on: ubuntu-18.04
10-
10+
1111
steps:
1212
- uses: actions/checkout@master
1313
- name: Set up Python 3.9

.github/workflows/pypi-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
build-n-publish:
88
name: Build and publish Audible to PyPI
99
runs-on: ubuntu-18.04
10-
10+
1111
steps:
1212
- uses: actions/checkout@master
1313
- name: Set up Python 3.9

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased
88

9-
-
9+
### Added
10+
11+
- full support of pre-Amazon accounts (e.g. refresh access token, deregister device)
12+
- `Client` and `AsynClient` now accepts session kwargs which are bypassed to the underlying httpx Client
13+
- a `respone_callback` can now be set to `Client` and `AsyncClient` class to allow custom preparation of response output
14+
- An absolut url (e.g. https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/sidecar) can now be passed to a client `get`, `post`, `delete` and `put` method as the `path` arg. So in most cases the client `raw_request` method is not needed anymore.
15+
16+
### Changed
17+
18+
- rename (and rework) `Client._split_kwargs` to `Client._prepare_params`
1019

1120
## [0.7.2] - 2022-03-27
1221

docs/source/auth/authentication.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ Sign request method
2525
With the sign request method you gain unrestricted access to the Audible API.
2626
To use this method, you need the RSA private key and the adp_token from a
2727
*device registration*. This method is used by the Audible apps for iOS and
28-
Android too.
28+
Android too. A device registration is done automatically with
29+
:meth:`audible.Authenticator.from_login` or
30+
:meth:`audible.Authenticator.from_login_external`
2931

3032
Request signing is fairly straight-forward and uses a signed SHA256 digest.
3133
Headers look like::
@@ -37,8 +39,8 @@ Headers look like::
3739
Bearer method
3840
-------------
3941

40-
API requests with the bearer method are restricted. Some API call like the
41-
:http:post:`/1.0/content/(string:asin)/licenserequest` doesn't work. To use
42+
API requests with the bearer method have some restrictions. Some API call, like
43+
the :http:post:`/1.0/content/(string:asin)/licenserequest`, doesn't work. To use
4244
the bearer method you need an access token and a client id. You receive the
4345
token after a device registration. Which values are valid for the client-id
4446
is unknown but 0 does work. An access token expires after 60 minutes. It

docs/source/auth/authorization.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ To handle the login with a external browser or program logic you can do the foll
148148
auth = audible.Authenticator.from_login_external(locale=COUNTRY_CODE)
149149

150150
By default, this code print out the login url for the selected country code. Now you have
151-
to copy and paste this code into a webbrowser (or a custom program) and authorize yourself.
151+
to copy and paste this code into a web browser (or a custom program) and authorize yourself.
152152
You have to enter your credentials two times (because of missing init cookies).
153153
On first time, the password can be a random one.
154154
On second time, you have to solve a captcha before you can submit the login form with your
@@ -158,6 +158,11 @@ Please copy the url from the address bar from your browser and paste the url to
158158
field of the python code. This url looks like
159159
"https://www.amazon.{domain}/ap/maplanding?...&openid.oa2.authorization_code=..."
160160

161+
.. note::
162+
If you have `playwright <https://pypi.org/project/playwright/>`_ installed and
163+
use the default ``login_url_callback``, a new browser is opened, where you can
164+
authorize to your account,
165+
161166
.. note::
162167

163168
If you are using MacOS and have trouble insert the login result url, simply import the

docs/source/auth/register.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ To authorize and register a new device you can do::
1717
with_username=False,
1818
)
1919

20+
This will authorize you to your account and register an Audible device.
21+
2022
.. important::
2123

2224
Every device registration will be shown on the Amazon devices list. So only

docs/source/intro/getting_started.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ First Audible device
1313
====================
1414

1515
Before you can communicate with the non-publicly Audible Api, you need to
16-
authorize (login) yourself to Audible with your Audible username or Amazon
17-
account and register a new "virtual" Audible device. Please make sure to
18-
select the correct Audible marketplace. An overview about all known Audible
19-
marketplaces and associated country codes be found at :ref:`country_codes`.
16+
authorize (login) yourself to Amazon (or Audible) and register a new "virtual"
17+
Audible device. Please make sure to select the correct Audible marketplace.
18+
An overview about all known Audible marketplaces and associated country codes
19+
be found at :ref:`country_codes`.
2020

2121
.. code-block::
2222
@@ -30,7 +30,7 @@ marketplaces and associated country codes be found at :ref:`country_codes`.
3030
with_username=False
3131
)
3232
33-
# Save credendtials to file
33+
# Save credentials to file
3434
auth.to_file(FILENAME)
3535
3636
.. important::
@@ -42,11 +42,12 @@ marketplaces and associated country codes be found at :ref:`country_codes`.
4242
.. note::
4343

4444
If you have activated 2-factor-authentication for your Amazon account, you
45-
can append the current OTP to the password.
45+
can append the current OTP to your password. This eliminates the need for a
46+
new OTP prompt.
4647

4748
.. note::
4849

49-
Set `with_username=True` to login with a pre-Amazon account (for US, UK or
50+
Set `with_username=True` to login with your pre-Amazon account (for US, UK or
5051
DE marketplace only).
5152

5253
.. note::
@@ -76,8 +77,8 @@ your first API call. To fetch and print out all books from your Audible library
7677

7778
.. note::
7879

79-
The information provided by the API depends on the requested `response_groups`.
80-
The response from the example above are very minimized. Please take a look at
80+
The information returned by the API depends on the requested `response_groups`.
81+
The response for the example above are very minimized. Please take a look at
8182
:http:get:`/1.0/library` for all known `response_groups` and other parameter
8283
for the library endpoint.
8384

docs/source/intro/install.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ development version::
3030
cd Audible
3131
pip install .
3232

33-
Alternatively, install directly from the GitHub repository::
33+
Alternatively, install it directly from the GitHub repository::
3434

3535
pip install git+https://github.com/mkb79/audible.git
3636

docs/source/misc/advanced.rst

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@ Client classes
77

88
Here are some information about the ``Client`` and the ``AsyncClient`` classes.
99

10-
Both client classes have the following methods to send requests
10+
Instantiate a client
11+
--------------------
12+
13+
A client needs at least an :class:`audible.Authenticator` at instantiation. The
14+
following args and kwargs can be passed to the client instantiation:
15+
16+
* country_code (overrides the country code set in :class:`audible.Authenticator`)
17+
* headers (will be bypassed to the underlying httpx client)
18+
* timeout (will be bypassed to the underlying httpx client)
19+
* response_callback (custom response preparation - read more below)
20+
* all other kwargs (will be bypassed to the underlying httpx client)
21+
22+
Make API requests
23+
-----------------
24+
25+
Both client classes have the following methods to send requests
1126
to the external API:
1227

1328
- get
1429
- post
1530
- delete
16-
- patch
17-
18-
Make API requests
19-
-----------------
31+
- put
2032

2133
The external Audible API offers currently two API versions, `0.0` and
2234
`1.0`. The Client use the `1.0` by default. So both terms are equal::
2335

2436
resp = client.get("library")
2537
resp = client.get("1.0/library")
2638

27-
Each query parameter can be written as a separat keyword argument or you can
39+
Each query parameter can be written as a separate keyword argument or you can
2840
merge them as a dict to the `params` keyword. So both terms are equal::
2941

3042
resp = client.get("library", response_groups="...", num_results=20)
@@ -52,6 +64,27 @@ and output them as a Python dict.
5264

5365
For all known API endpoints take a look at :ref:`api_endpoints`.
5466

67+
Client responses
68+
----------------
69+
70+
.. versionadded:: v0.8.0
71+
72+
The ``response_callback`` kwarg to client __init__, get, post, delete and put methods.
73+
74+
By default requesting the API with the client get, post, delete and put methods
75+
will call :func:`audible.client.raise_for_status` and try to convert
76+
the response with :func:`audible.client.convert_response_content` to a Python dict,
77+
which is finally returned.
78+
79+
If you want to implement your own response preparation, you can do::
80+
81+
def own_response_callback(resp):
82+
return resp
83+
84+
client = audible.Client(auth=..., response_callback=own_response_callback)
85+
86+
This will return the unprepared response (include headers).
87+
5588
Show/Change Marketplace
5689
-----------------------
5790

docs/source/misc/load_save.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ The following data will be stored:
8080
- device_info data
8181
- customer_info data
8282
- activation_bytes
83+
- with_username (``True`` for pre-Amazon accounts else ``False``)
8384

8485
.. versionadded:: 0.5.1
8586

8687
Stores ``activation_bytes`` to file (if they where fetched before).
8788

89+
.. versionadded:: v0.8.0
90+
91+
The ``with_username`` value
92+
8893
Advanced use of encryption/decryption
8994
=====================================
9095

@@ -135,3 +140,4 @@ unencrypted. If the `Authenticator` can't load your data, you can try::
135140
decrypted_file=FILENAME,
136141
password=PASSWORD_FOR_ENCRYPTED_FILE
137142
)
143+

0 commit comments

Comments
 (0)