Skip to content

Commit

Permalink
rework download examples
Browse files Browse the repository at this point in the history
See issue #3
  • Loading branch information
mkb79 committed Aug 5, 2019
1 parent 7fbacb0 commit 27b9dfa
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
72 changes: 72 additions & 0 deletions examples/download_books_aax.py
Original file line number Diff line number Diff line change
@@ -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}")
12 changes: 8 additions & 4 deletions examples/download_books.py → examples/download_books_aaxc.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 27b9dfa

Please sign in to comment.