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

Here's the steps I completed. #1

Closed
cjemorton opened this issue Jun 22, 2019 · 18 comments
Closed

Here's the steps I completed. #1

cjemorton opened this issue Jun 22, 2019 · 18 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@cjemorton
Copy link

cjemorton commented Jun 22, 2019

pip install audible

created a test.py file.

import audible

# for US accounts
client = audible.Client("myemail", "mypassword", local="us")

# specify a api_version on request
# default is api_version="1.0"
# get deprecated version of library
library = client.get("library/books", api_version="0.0", purchaseAfterDate="01/01/1970", sortInAscendingOrder="true")

print(library)

executed that file. with python3.6

python3.6 test.py

the results I got back are.

Answer for CAPTCHA: audible-api: unable to open X server `' @ error/display.c/DisplayImageCommand/410.

I realize I need to register, however I have no idea how to go about doing that. Is there any sample code on how to go about doing that?

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

It looks like an error with the captcha who can’t display correctly.

This codeline is important:

with urllib.request.urlopen(captcha["src"]) as url:
    f = io.BytesIO(url.read())

    img= Image.open(f)
    img.show()

Which is you use and which x-Server you have?

@mkb79 mkb79 self-assigned this Jun 22, 2019
@mkb79 mkb79 added bug Something isn't working enhancement New feature or request labels Jun 22, 2019
@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

A quick workaround:

Change the obove lines of code to:
with urllib.request.urlopen(captcha["src"]) as url:

    f = io.BytesIO(url.read())

    try:
        img= Image.open(f)
        img.show()
    execept:
        print(f)

On error audible-api now printes you the url for captcha on screen and you have to open the link with a webbrowser.

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

Sorry there is a error in my code. Correct is:

with urllib.request.urlopen(captcha["src"]) as url:

    f = io.BytesIO(url.read())

    try:
        img= Image.open(f)
        img.show()
    execept:
        print(captcha["src"])

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

Can you please update audible with pip update audible and try again.

@cjemorton
Copy link
Author

pip install --upgrade audible
Collecting audible
Downloading https://files.pythonhosted.org/packages/23/ca/0c3822c159ed9f0b1e2900fe27a6d368f1dd4904aa1a7db6743e726357a2/audible-0.1.3-py3-none-any.whl
Requirement already satisfied, skipping upgrade: beautifulsoup4 in /usr/local/lib/python3.6/site-packages (from audible) (4.7.1)
Requirement already satisfied, skipping upgrade: Pillow in /usr/local/lib/python3.6/site-packages (from audible) (6.0.0)
Requirement already satisfied, skipping upgrade: rsa in /usr/local/lib/python3.6/site-packages (from audible) (4.0)
Requirement already satisfied, skipping upgrade: requests in /usr/local/lib/python3.6/site-packages (from audible) (2.21.0)
Requirement already satisfied, skipping upgrade: soupsieve>=1.2 in /usr/local/lib/python3.6/site-packages (from beautifulsoup4->audible) (1.8)
Requirement already satisfied, skipping upgrade: pyasn1>=0.1.3 in /usr/local/lib/python3.6/site-packages (from rsa->audible) (0.4.5)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/site-packages (from requests->audible) (3.0.4)
Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /usr/local/lib/python3.6/site-packages (from requests->audible) (2.8)
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/site-packages (from requests->audible) (1.24.1)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /usr/local/lib/python3.6/site-packages (from requests->audible) (2019.3.9)
Installing collected packages: audible
Found existing installation: audible 0.1.2
Uninstalling audible-0.1.2:
Successfully uninstalled audible-0.1.2
Successfully installed audible-0.1.3

Just installed the new version from pip.

I am not running an x-server my use case needs me to get it running on a headless machine.
That is... I think..., once I've read over the errors again... I've pinpointed the problem. DOH!

Is there any ways to print out the URL so I can open and enter it manually?
Like a check or something that detects if the machine is running without a x-server if so, go strictly text based?

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

I have released version 1.4 on pypi now. Please update again.

How you execute the script? Tru a ssh session? Then you have the output in console and you can try following code:

import audible

def custom_captcha_callback(captcha_url):
    print(captcha_url)
    guess = input("Answer for CAPTCHA: ")
    guess = str(guess).strip().lower()
    return guess

client = audible.Client("EMAIL", "PASSWORD", local="us", captcha_callback=custom_captcha_callback)

Now the Captcha url will be prompted in your ssh console and you can copy&past them to a Webbrowser and insert captcha answer in console.

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

If you have success don’t forget to save session. Otherwise you can try to execute the script on a local machine and save session to file. Then you can store the session file in project folder on your headless machine and init a session with
client = audible.Client(filename="your filename", local="us")

@cjemorton
Copy link
Author

cjemorton commented Jun 22, 2019

success!

Still not sure how to save a session tho, right now I have to type in a Captcha each time I call the script.

The last snip of code you shared is for loading a saved session. How do I go about saving the session to begin with?

Once I get that figured out, I should be able to put the pieces together from there.

import audible

def custom_captcha_callback(captcha_url):
print(captcha_url)
guess = input("Answer for CAPTCHA: ")
guess = str(guess).strip().lower()
return guess

client = audible.Client("MYUSERNAME", "MYPASSWORD", local="us", captcha_callback=custom_captcha_callback)

library = client.get("library/books", api_version="0.0", purchaseAfterDate="01/01/1970", sortInAscendingOrder="true")

print(library)

I'm just executing a script through a ssh session.

Basically: python3.6 test.py

Another quick question, does the local="us" also work with the audible.ca ? the canadian version? I seemed to be getting the results from my library, so I think it is?...

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

In the README I wrote how to save session. Here are two examples:

# give filename at instantiate client
# saves session after success init and register device
client = audible.Client(..., filename="your filename or path+filename")

# saves at any moment
client = audible.Client(...)
client.to_json_file(filename or path+filename)

Hint:
You are using deprecated version of library call. This is the new one:

client.get(
    "library",
    num_results=999,
    response_groups="media, series, ..."
)

Canadian is not supported but can be implemented very easy. 

@cjemorton
Copy link
Author

! Got it working, just needed to add filename="FILENAME",
then load it with the snip of code you posted earlier.

@mkb79
Copy link
Owner

mkb79 commented Jun 22, 2019

I don’t know if you can use an session file from us store to call canadian audible api.

I have add canadian now to my code but will them upload later. In germany it‘s now 1am.

Meanwhile you can open the localization.py delivered by audible module and rewrite to:

from urllib.parse import urlparse


localization = {
    "de": {
        "AMAZON_LOGIN": urlparse("https://www.amazon.de"),
        "AMAZON_API": urlparse("https://api.amazon.de"),
        "AUDIBLE_API": urlparse("https://api.audible.de"),
        "Accept-Language": "de-DE",
        "marketPlaceId": "AN7V1F1VY261K",
        "openid_assoc_handle": "amzn_audible_ios_de",
        "oauth_lang": "de-DE",
        "auth_register_domain": ".amazon.de"
    },
    "us": {
        "AMAZON_LOGIN": urlparse("https://www.amazon.com"),
        "AMAZON_API": urlparse("https://api.amazon.com"),
        "AUDIBLE_API": urlparse("https://api.audible.com"),
        "Accept-Language": "en-US",
        "marketPlaceId": "AF2M0KC94RCEA",
        "openid_assoc_handle": "amzn_audible_ios_us",
        "oauth_lang": "en-US",
        "auth_register_domain": ".amazon.com"
    },
    "uk": {
        "AMAZON_LOGIN": urlparse("https://www.amazon.co.uk"),
        "AMAZON_API": urlparse("https://api.amazon.co.uk"),
        "AUDIBLE_API": urlparse("https://api.audible.co.uk"),
        "Accept-Language": "en-GB",
        "marketPlaceId": "A2I9A3Q2GNFNGQ",
        "openid_assoc_handle": "amzn_audible_ios_uk",
        "oauth_lang": "en-GB",
        "auth_register_domain": ".amazon.co.uk"
    },
    "fr": {
        "AMAZON_LOGIN": urlparse("https://www.amazon.fr"),
        "AMAZON_API": urlparse("https://api.amazon.fr"),
        "AUDIBLE_API": urlparse("https://api.audible.fr"),
        "Accept-Language": "fr-FR",
        "marketPlaceId": "A2728XDNODOQ8T",
        "openid_assoc_handle": "amzn_audible_ios_fr",
        "oauth_lang": "fr-FR",
        "auth_register_domain": ".amazon.fr"
    },
    "ca": {
        "AMAZON_LOGIN": urlparse("https://www.amazon.ca"),
        "AMAZON_API": urlparse("https://api.amazon.ca"),
        "AUDIBLE_API": urlparse("https://api.audible.ca"),
        "Accept-Language": "en-CA",
        "marketPlaceId": "A2CQZ5RBY40XE",
        "openid_assoc_handle": "amzn_audible_ios_ca",
        "oauth_lang": "en-CA",
        "auth_register_domain": ".amazon.ca"
    },
}

And then you can

import audible


client = audible.Client(User, Password, local="ca", filename="your filename", captcha_callback="your captcha function here")

@cjemorton
Copy link
Author

I just tried it, yes you can use a session file from the US to call the Canadian api. It worked for me.

I just tweaked my localization file manually here. I'll pull in the changes with pip later when you update. Have a good night!

cjemorton pushed a commit to cjemorton/Audible that referenced this issue Jun 23, 2019
mkb79 added a commit that referenced this issue Jun 23, 2019
Added the Canadian Localization from #1
@mkb79
Copy link
Owner

mkb79 commented Jun 23, 2019

That sounds great. Can you try follow code and tell me in what currency the prices are?

client.get(
    "library",
    num_results=999,
    response_groups="price"
)

If the prices are in CAD all is fine. But if they in USD you need a new session file from ca store.

@cjemorton
Copy link
Author

Ok, I setup a little test. Using my session-us.key (the session key generated using local="us")
Running the code snippet above against each localization:

de -> {'items': [], 'response_groups': ['always-returned', 'price']}

us -> 'currency_code': 'USD'

uk -> {'items': [], 'response_groups': ['always-returned', 'price']}

fr -> {'items': [], 'response_groups': ['always-returned', 'price']}

ca -> 'currency_code': 'CAD'

So I can conclude that my session-key works for the us local and the ca local. However for the de, uk, fr local I get those responses.

I think this might be the case because I have never bought anything from those other 3 stores, so I have no data returned.

As a side note, whats the best way to parse the data returned from each api call? is it JSON formatted?

@mkb79
Copy link
Owner

mkb79 commented Jun 23, 2019

Thanks for your help. The response type is a python dictionary.

@mkb79
Copy link
Owner

mkb79 commented Jun 23, 2019

I must correct me. The api answer is in json. But this app converted them to a python dictionary.

@cjemorton
Copy link
Author

No problem, if I can help with anything let me know.

Python Dictionary! I never would have guessed... I was trying to parse them as json and it just wasn't working... was starting to pull my hair out...

@mkb79
Copy link
Owner

mkb79 commented Jun 23, 2019

First I thought to output the response as json. But as json you can‘t work correctly with the response. So I have decided me to output the response as a 'classic' python dictionary. I would close this issue now.

@mkb79 mkb79 closed this as completed Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants