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

adding downloadable flag to library list cmd #145

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/audible_cli/cmds/cmd_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def _get_library(session, client, resolve_podcasts):
"relationships, review_attrs, categories, badge_types, "
"category_ladders, claim_code_url, is_downloaded, "
"is_finished, is_returnable, origin_asin, pdf_url, "
"percent_complete, provided_review"
"percent_complete, provided_review, customer_rights"
),
bunch_size=bunch_size,
start_date=start_date,
Expand Down Expand Up @@ -152,6 +152,22 @@ def _prepare_item(item):
output_filename.write_text(data)


def _intersects(filter_authors, item_authors):
for ia in item_authors:
if ia['name'] in filter_authors:
return True
return False


def _get_authors_filter(filters):
authors = []
for f in filters:
if f.startswith('author='):
(_,author) = f.split('=',1)
authors.append(author)
return authors


@cli.command("list")
@timeout_option
@bunch_size_option
Expand All @@ -160,11 +176,16 @@ def _prepare_item(item):
is_flag=True,
help="Resolve podcasts to show all episodes"
)
@click.option(
"--filter",
multiple=True,
help="Filter library list: 'downloadable' 'not-downloadable' 'author=<Name>'"
)
@start_date_option
@end_date_option
@pass_session
@pass_client
async def list_library(session, client, resolve_podcasts):
async def list_library(session, client, resolve_podcasts, filter):
"""list titles in library"""

@wrap_async
Expand All @@ -186,9 +207,19 @@ def _prepare_item(item):
fields.append(item.title)
return ": ".join(fields)

filters = {
'downloadable': 'downloadable' in filter,
'not downloadable': 'not downloadable' in filter,
'authors': _get_authors_filter(filter),
}

library = await _get_library(session, client, resolve_podcasts)

books = await asyncio.gather(
*[_prepare_item(i) for i in library]
*[_prepare_item(i) for i in library if \
(filters['downloadable'] is False or i.is_downloadable() is True) and \
(filters['not downloadable'] is False or i.is_downloadable() is False) and \
(len(filters['authors']) == 0 or _intersects(filters['authors'], i.authors))
]
)
[echo(i) for i in sorted(books) if len(i) > 0]