From 27b9dfade99b85a563b1318324b64eb4407a391c Mon Sep 17 00:00:00 2001 From: mkb79 Date: Mon, 5 Aug 2019 10:09:43 +0200 Subject: [PATCH] rework download examples See issue https://github.com/mkb79/Audible/issues/3 --- examples/download_books_aax.py | 72 +++++++++++++++++++ ...wnload_books.py => download_books_aaxc.py} | 12 ++-- 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 examples/download_books_aax.py rename examples/{download_books.py => download_books_aaxc.py} (84%) diff --git a/examples/download_books_aax.py b/examples/download_books_aax.py new file mode 100644 index 00000000..d526dd0e --- /dev/null +++ b/examples/download_books_aax.py @@ -0,0 +1,72 @@ +import pathlib +import shutil + +import audible +import requests + + +# get download link(s) for book +def _get_download_link(asin, codec="LC_128_44100_stereo"): + try: + content_url = f"https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/FSDownloadContent" + params = { + 'type': 'AUDI', + 'currentTransportMethod': 'WIFI', + 'key': asin, + 'codec': codec + } + + signed_headers = client._sign_request('GET', content_url, params, {}) + headers = client.headers.copy() + for item in signed_headers: + headers[item] = signed_headers[item] + + r = client.session.request('GET', content_url, headers=headers, params=params, json={}, allow_redirects=False) + link = r.headers['Location'] + + # prepare link + # see https://github.com/mkb79/Audible/issues/3#issuecomment-518099852 + tld = auth.locale.audible_api.split("api.audible.")[1] + new_link = link.replace("cds.audible.com", f"cds.audible.{tld}") + return new_link + + except Exception as e: + print(f"Error: {e}") + return + + +def download_file(url): + r = requests.get(url, stream=True) + + title = r.headers["Content-Disposition"].split("filename=")[1] + filename = pathlib.Path.cwd() / "audiobooks" / title + + with open(filename, 'wb') as f: + shutil.copyfileobj(r.raw, f) + return filename + + +if __name__ == "__main__": + password = input("Password for file: ") + + auth = audible.FileAuthenticator( + filename="FILENAME", + encryption="json", + password=password + ) + client = audible.AudibleAPI(auth) + + books, _ = client.get( + "library", + response_groups="product_attrs", + num_results="999" + ) + + for book in books["items"]: + asin = book["asin"] + dl_link = _get_download_link(asin) + + if dl_link: + print(f"download link now: {dl_link}") + status = download_file(dl_link) + print(f"downloaded file: {status}") diff --git a/examples/download_books.py b/examples/download_books_aaxc.py similarity index 84% rename from examples/download_books.py rename to examples/download_books_aaxc.py index 0ce37c6a..a5df0fee 100644 --- a/examples/download_books.py +++ b/examples/download_books_aaxc.py @@ -1,8 +1,14 @@ import pathlib -import requests import shutil import audible +import requests + + +# files downloaded via this script can't be converted at this moment +# audible uses a new format (aaxc instead of aax) +# more informations and workaround here: +# https://github.com/mkb79/Audible/issues/3 # get download link(s) for book @@ -21,8 +27,6 @@ def _get_download_link(asin, quality): print(f"Error: {e}") return - return response['content_license']['content_metadata']['content_url']['offline_url'] - def download_file(url, filename): r = requests.get(url, stream=True) @@ -49,7 +53,7 @@ def download_file(url, filename): for book in books: asin = book['asin'] - title = book['title'] + f" ({asin})" + ".aax" + title = book['title'] + f" ({asin})" + ".aaxc" dl_link = _get_download_link(asin, quality="Extreme") if dl_link: filename = pathlib.Path.cwd() / "audiobooks" / title