Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when trying to get book download URL #16

Closed
lanouettea opened this issue Jul 24, 2020 · 12 comments
Closed

Error when trying to get book download URL #16

lanouettea opened this issue Jul 24, 2020 · 12 comments

Comments

@lanouettea
Copy link

I'm trying to follow the different examples provided to get a book download URL.

I'm successfully authenticating and fetching my library. However, I'm getting a 500 error when trying to get the book download URL as follow:

 license = client.post(
            f"content/{child_asin}/licenserequest",
            body={
                "drm_type": "Adrm",
                "consumption_type": "Download",
                "quality": "Extreme"
            }
        )
content_url = license['content_license']['content_metadata']['content_url']['offline_url']

The error obtained is as follow:
audible.exceptions.UnexpectedError: Internal Server Error (500): Unexpected error while servicing request for ASIN: B07231CR59 and ACR: null.

I tried using the book asin directly and using its child asin. Can you point me in the correct direction?

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

Hi,

I try to find some informations about the asin B07231CR59 but google found nothing. What is this for a product and which content type? Is it a serie, a SinglePartBook, a MultiPartBook or something other?

Hint:
If you download a book with the licenserequest method, it downloads the book in aaxc format. This format can’t be encrypted at this moment (I think so)! Please read #3 for more informations how to download books in aax format.

Regards

@lanouettea
Copy link
Author

Thanks for the fast response! It is much appreciated!

The specific asin I provided is the asin of a child to the book. I also attempted with the book asin ('B07148GH11') and other books and always obtained the same error.

How do I determine if this is a single part book or a multipart book? Ideally, I would like 1 large file for the whole book instead of downloading it in several parts.

I also tried to follow the instructions in the post you shared and found that the method 'client._sign_request' does not exist. I haven't found where it might have been moved to.

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

Which version of my audible app you use?

Can you try my download example at https://github.com/mkb79/Audible/blob/master/examples/download_books_aax.py

@lanouettea
Copy link
Author

lanouettea commented Jul 24, 2020

I tried with the example you provided and I am receiving a 500 error on each calls to get the download link. I did make sure I specify a codec as available in the list of available codecs for each given book.

I'm using Audible Canada, could this be an issue?

I'm using the latest version published on Pypi

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

Hmm, that sounds strange.

Can you make the following api call:

book = client.get(
    "library/B07148GH11",
    response_groups="product_desc, series, relationships"
)

print(book)

and send me the response. You can send me a mail to [email protected] instead to post the response content here.

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

Can you try this code please (please set the correct settings to your auth file):

import pathlib
import shutil

import audible
import httpx


# get download link(s) for book
def _get_download_link(auth, asin, codec="LC_128_44100_stereo"):
    # need at least v0.4.0dev
    try:
        content_url = (f"https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/"
                       f"FSDownloadContent")
        params = {
            'type': 'AUDI',
            'currentTransportMethod': 'WIFI',
            'key': asin,
            'codec': codec
        }            
        r = httpx.get(
            url=content_url,
            params=params,
            allow_redirects=False,
            auth=auth
        )

        # prepare link
        # see https://github.com/mkb79/Audible/issues/3#issuecomment-518099852
        link = r.headers['Location']
        tld = auth.locale.domain
        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 = httpx.get(url)

    try:
        title = r.headers["Content-Disposition"].split("filename=")[1]
        filename = pathlib.Path.cwd() / "audiobooks" / title
    
        with open(filename, 'wb') as f:
            shutil.copyfileobj(r.iter_raw, f)
        return filename
    except KeyError:
        return "Nothing downloaded"

if __name__ == "__main__":
    auth = audible.FileAuthenticator(
        ...
    )
    client = audible.Client(auth)

    asin = "B07148GH11"
    dl_link = _get_download_link(auth, asin)
    print(dl_link)

    if dl_link:
        print(f"download link now: {dl_link}")
        status = download_file(dl_link)
        print(f"downloaded file: {status}")

@lanouettea
Copy link
Author

I still receive a 500 error with no details on the request

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

Have you register a device and use sign request method or you use access token authentication? You can test this with print(auth.adp_token). If you get a not None response you use sign request method.

Can you try this code and send me the response via mail please?:

import audible
import httpx


auth = audible.FileAuthenticator(
    ...
)
client = audible.Client(auth)
asin = "B07148GH11"
codec="LC_128_44100_stereo"
content_url = (f"https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/"
               f"FSDownloadContent")
params = {
    'type': 'AUDI',
    'currentTransportMethod': 'WIFI',
    'key': asin,
    'codec': codec
}

r = httpx.get(
    url=content_url,
    params=params,
    allow_redirects=False,
    auth=auth
)
print(r.headers)

@lanouettea
Copy link
Author

print(auth.adp_token) returns None. So I guess I am using the sign request method.

The code above returns the following:
Headers({'x-adp-host': 'H3K246P9RFUS2E', 'content-length': '87', 'date': 'Fri, 24 Jul 2020 15:30:13 GMT', 'connection': 'close', 'server': 'Amazon Web Server'})

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

If you have no adp token in your auth file then you use access token auth mode. Access token is only valid for 60 minutes. So you have to authenticate again after that time.

Access token auth mode have more limitations if you make api calls. Maybe this is the problem. Can you use LoginAuthenticator with register option and save your auth data to file. Then try again the last code snippet.

@lanouettea
Copy link
Author

Well damn! That did the trick! I now have the "location" value in the returned header.

Thanks for the great support, I'm going to be closing this issue.

@mkb79
Copy link
Owner

mkb79 commented Jul 24, 2020

That‘s great to hear!

I will adjust my download example to make sure adp_token is present in auth file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants