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 3 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
82 changes: 80 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 @@ -74,7 +75,9 @@ class Trackma_cmd(command.Cmd):
'sort': 1,
'mediatype': (0, 1),
'info': 1,
'name': 1,
FichteFoll marked this conversation as resolved.
Show resolved Hide resolved
'search': 1,
'list_json': (0, 1),
'add': 1,
'del': 1,
'delete': 1,
Expand Down Expand Up @@ -362,6 +365,39 @@ 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_name(self, args):
"""
Print title name for given index.

:param show Show index.
:usage name <index>
"""
try:
show = self._get_show(args[0])
except utils.TrackmaError as e:
self.display_error(e)
return

print(show['title'])

def do_info(self, args):
"""
Gets detailed information about a local show.
Expand Down Expand Up @@ -945,6 +981,48 @@ 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
"""
altnames = self.engine.altnames()

episode_str_current = ""
episode_str_last = ""
# 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 '?')

# get title (and alt. title) and if need be, truncate it
title_str = show['title']
if altnames.get(show['id']):
title_str += " [{}]".format(altnames.get(show['id']))
FichteFoll marked this conversation as resolved.
Show resolved Hide resolved

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

# json dictionary
j = {
"title": title_str,
"current_episode": episode_str_current,
"final_episode": episode_str_last,
RaitaroH marked this conversation as resolved.
Show resolved Hide resolved
"score": score
}

# Color title according to status
if show['status'] == utils.Status.AIRING:
estimate = utils.estimate_aired_episodes(show)
if estimate and show['my_progress'] < estimate:
# User is behind the (estimated) aired episode
j["color"] = _COLOR_BEHIND
else:
j["color"] = _COLOR_AIRING
else:
j["color"] = _COLOR_RESET

print(json.dumps(j))

class Trackma_accounts(AccountManager):
def _get_id(self, index):
Expand Down Expand Up @@ -983,12 +1061,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