def _restore_from_session_file(self): # Try to restore session from file if possible try: auth = audible.FileAuthenticator(filename=self._session_file, locale=self._locale) self._client = audible.AudibleAPI(auth) except Exception as msg: msg = f"Can't log into Audible using session file ({self._session_file}): {msg}" raise Exception(msg)
filename = pathlib.Path.cwd() / "audiobooks" / title with open(filename, 'wb') as f: for chunk in r.iter_bytes(): f.write(chunk) print(f"File downloaded in {r.elapsed}") return filename except KeyError: return "Nothing downloaded" if __name__ == "__main__": password = input("Password for file: ") auth = audible.FileAuthenticator(filename="FILENAME", encryption="json", password=password) client = audible.Client(auth) books = client.get(path="library", params={ "response_groups": "product_attrs", "num_results": "999" }) asins = [book["asin"] for book in books["items"]] for asin in asins: dl_link = _get_download_link(auth, asin) if dl_link:
print(book["item"]) print("\n", 40 * "-", "\n") if __name__ == "__main__": # authenticate with login # don't stores any credentials on your system auth = audible.LoginAuthenticator("USERNAME", "PASSWORD", locale="us", register=False) loop = asyncio.get_event_loop() loop.run_until_complete(main(auth)) # store credentials to file auth.to_file(filename="FILENAME", encryption="json", password="******") # register device auth.register_device() # save again auth.to_file() # load credentials from file auth = audible.FileAuthenticator(filename="FILENAME", password="******") loop = asyncio.get_event_loop() loop.run_until_complete(main(auth)) # deregister device auth.deregister_device()
async def main(): #Name of the encrypted file with Amazon credentials Auth_file = "Credentials" #If it doesn't exists, it will be created if (not os.path.exists(Auth_file)): auth = audible.LoginAuthenticator( input("Username: "******"Password: "******"Enter one of the following country" + "\ncode 'de', 'us', 'ca', 'uk', 'au', 'fr', 'jp', 'it', 'in' \nEnter: " ), register=True) print("This information will be stored in an encrypted" + "\nfile so you don't have to insert them anymore.") config.credentials_pass = input( "Enter a password for the encryption: ") auth.to_file(filename=Auth_file, password=config.credentials_pass, encryption="bytes") else: auth = audible.FileAuthenticator(filename=Auth_file, password=config.credentials_pass) async with audible.AsyncClient(auth=auth) as client: activation_bytes = auth.get_activation_bytes() #Checking download folder if (not os.path.exists(config.download_folder)): os.mkdir(config.download_folder) #Retrieving audiobooks library = await client.get("library", num_results=1000) books = {book["title"]: book["asin"] for book in library["items"]} titles = [title for title in books.keys()] #Printing list of audiobooks for index, title in enumerate(titles): print(str(index + 1) + ") " + title) #Input of the book(s) that will be downloaded print("\nEnter the number of the book you want to download" + "\nIf you want to download multiple book enter the" + "\nnumber of the first and the last book separated" + "\nby a dash and without spaces between (i.e. 0-10)\n") book_range = input("Enter: ") book_range = book_range.split("-") if (len(book_range) == 2): first_book, last_book = book_range first_book = int(first_book) - 1 last_book = int(last_book) elif (len(book_range) == 1): first_book = int(book_range[0]) - 1 last_book = first_book + 1 else: raise Exception("Invalid input!") #Downloading and converting books for index in range(first_book, last_book): asin = books[titles[index]] dl_link = await _get_download_link(auth, asin) if dl_link: print(f"Downloading now: {titles[index]}") status = await download_file(dl_link) print(f"Downloaded file: {status}") print("Now converting") status = await ConvertToMp3( status, activation_bytes, os.path.join(config.download_folder, titles[index].replace(" ", ""))) print(f"Converted file: {status}")