Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 234adbad rédigé par Kubat's avatar Kubat
Parcourir les fichiers

KAGARI: Add an option to display query results as json or not

parent d60704a3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #2159 réussi avec des avertissements
......@@ -10,6 +10,7 @@ import argparse
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.request import urlopen
from textwrap import wrap
class CustomFormatter(
......@@ -105,6 +106,13 @@ def create_parser():
+ "in the response.\n ",
)
parser.add_argument(
"-j",
"--json",
action="store_true",
help='In "search" mode, output the result as a json.'
)
return parser
......@@ -241,10 +249,27 @@ def search_mode(args):
print(emsg)
sys.exit(1)
# Displaying the received JSON, or writing it to a file
if args.json:
pretty_output = json.dumps(kara_json, indent=4, ensure_ascii=False)
else:
pretty_output = ""
for res in kara_json:
new_kara = (" NEW!" if res['is_new'] == "1" else "")
output_string = build_kara_name(res)
output_string = f"[{res['id']}] {output_string[0]}\n" + \
f"{output_string[1]} in {res['author_year']}{new_kara}\n" + \
f" Size: {res['size']} Kio\n"
pretty_output += output_string
if res['popularity']:
pretty_output += " Popularity: " + res['popularity'] + "\n"
if res['upload_comment']:
for comment_line in wrap(res['upload_comment'], fix_sentence_endings=True):
pretty_output += " " + comment_line + "\n"
pretty_output += "\n"
# Displaying the received JSON, or writing it to a file
if not args.path:
print(pretty_output)
print(pretty_output, end='')
else:
try:
with open(args.path, "w") as output_file:
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter