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

list shows in json format #662

Open
wants to merge 10 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
56 changes: 54 additions & 2 deletions trackma/ui/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import subprocess
import sys
import textwrap
import json
from operator import itemgetter # Used for sorting list

from trackma import messenger
Expand Down Expand Up @@ -75,6 +76,7 @@ class Trackma_cmd(command.Cmd):
'mediatype': (0, 1),
'info': 1,
'search': 1,
'list_json': (0, 1),
'add': 1,
'del': 1,
'delete': 1,
Expand Down Expand Up @@ -362,6 +364,24 @@ def do_list(self, args):
# Show the list in memory
self._make_list(self.sortedlist)

def do_list_json(self, args):
"""
Print list of shows in json format.
Copy link
Collaborator

@FichteFoll FichteFoll Jan 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should mention that each entry is printed as a JSON line on its own (instead of an array). At least right now.

If we were to consider things like the scoring format, it may be useful to also report that in a meta object on its own, in which case the question follows whether the entries should instead be wrapped in an array to build an object as follows:

{ "account": "string", "api": "string", "has_progress": false, "scoring_format": "enum", "entries": [{}] }

I don't now how the scoring format options in anilist are implemented currently in various parts of the code or whether it makes more sense to instead specify a max_score to be of either 3, 5, or 10 (and use the latter for 10, 20, and 100-point schemas).

And that it is intended to be used by third-party tools.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my particular usage, each entry being on its own line is better. In case another shell program wants to use this, I think it might be better to leave it like this.

The scoring format is something I was simply wondering about, but I think is a good idea to investigate.

Copy link
Collaborator

@FichteFoll FichteFoll Jan 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this unresolved for @z411 to read (also the description hasn't been updated).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind reminder.
@z411

Use the command `filter` without arguments to see the available statuses.

:name list_json
:usage list_json <status>
"""
if args:
try:
self.filter_num = self._guess_status(args[0].lower())
self._load_list()
except KeyError:
print("Invalid filter.")
return

self._make_list_json(self.sortedlist)

def do_info(self, args):
"""
Gets detailed information about a local show.
Expand Down Expand Up @@ -945,6 +965,38 @@ def _make_list(self, showlist):
print('%d results' % len(showlist))
print()

def _make_list_json(self, showlist):
"""
helper function for printing a json formatted show list
"""

# list shows
for index, show in showlist:
if self.engine.mediainfo['has_progress']:
episode_str_current = "{0}".format(show['my_progress'] or '0')
episode_str_last = "{0}".format(show['total'] or '?')

# ensure score is string; anilist can have string scores such as stars or smiles
score = "{0}".format(show['my_score'])

# json dictionary
j = {
"title": show['title'],
"current_episode": episode_str_current,
"total_episodes": episode_str_last,
"score": score,
"status": show['status'].value
}

# Check if show is airing and get estimated aired episode
if show['status'] == utils.Status.AIRING:
estimate = utils.estimate_aired_episodes(show)
j["estimated_aired_episode"] = "{0}".format(estimate)

# cover image url
j["image"] = show['image']

print(json.dumps(j))

class Trackma_accounts(AccountManager):
def _get_id(self, index):
Expand Down Expand Up @@ -983,12 +1035,12 @@ def select_account(self, bypass):
password = getpass.getpass('Enter password (no echo): ')
elif selected_api[2] in [utils.Login.OAUTH, utils.Login.OAUTH_PKCE]:
username = input('Enter account name: ')

auth_url = selected_api[3]
if selected_api[2] == utils.Login.OAUTH_PKCE:
extra['code_verifier'] = utils.oauth_generate_pkce()
auth_url = auth_url % extra['code_verifier']

print('OAuth Authentication')
print('--------------------')
print('This website requires OAuth authentication.')
Expand Down