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

Search and filter commands #52

Merged
merged 7 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions mal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ def create_parser():
help='search an anime')
parser_search.add_argument('anime_regex',
help='regex pattern to match anime titles')
parser_search.add_argument('--extend', action='store_true', # defaults to false
help='display all available information on anime')
parser_search.set_defaults(func=commands.search)

# Parser for "filter" command
parser_filter = subparsers.add_parser('filter',
help='find anime in users list')
parser_filter.add_argument('anime_regex',
help='regex pattern to match anime titles')
parser_filter.add_argument('--extend', action='store_true',
help='display all available information on anime')
parser_filter.add_argument('--user', type=str, default=None,
help='choose which users list to filter through')
parser_filter.set_defaults(func=commands.filter)

# Parser for "increase" command
parser_increase = subparsers.add_parser('increase',
help="increase anime's watched episodes by one",
Expand Down
9 changes: 7 additions & 2 deletions mal/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

def search(mal, args):
"""Search MAL (not just the user) anime database."""
core.find(mal, args.anime_regex.lower())
core.search(mal, args.anime_regex.lower(), full=args.extend)


def filter(mal, args):
"""Search and find an anime in the users list."""
core.find(mal, args.anime_regex.lower(), extra=args.extend, user=args.user)


def increase(mal, args):
Expand All @@ -44,7 +49,7 @@ def list(mal, args):
"""Show all the animes on the users list."""
# . matches any character except line breaks
# + matches one or more occurences of the previous character
core.find(mal, '.+', args.section, args.extend, args.user)
core.find(mal, '.+', args.section, extra=args.extend, user=args.user)


def drop(mal, args):
Expand Down
35 changes: 35 additions & 0 deletions mal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# stdlib
import sys
import math
import html
from operator import itemgetter
from datetime import date

Expand Down Expand Up @@ -89,6 +90,40 @@ def progress_update(mal, regex, inc):
report_if_fails(response)


def search(mal, regex, full=False):
"""Search the MAL database for an anime."""
result = mal.search(regex)
# if no results or only one was found we treat them special
if len(result) == 0:
print(color.colorize("No matches in MAL database ᕙ(⇀‸↼‶)ᕗ", 'red'))
return
if len(result) == 1: full = True # full info if only one anime was found

lines = ["{index}: {title}", " Episodes: {episodes}\tScore: {score}", " Synopsis: {synopsis}"]
extra_lines = [" Start date: {start}\tEnd data: {end}", " Status: {status}"]

print("Found", color.colorize(str(len(result)), "cyan", "underline"), "animes:")
for i, anime in enumerate(result):
# replace tags and special html chars (like —) with actual characters
synopsis = html.unescape(str(anime["synopsis"])).replace("<br />", "")
if len(synopsis) > 70 and not full:
synopsis = synopsis[:70] + "..."

# this template/line stuff might need some refactoring
template = {
"index": str(i + 1),
"title": color.colorize(anime["title"], "red", "bold"),
"episodes": color.colorize(anime["episodes"], "white", "bold"),
"score": color.score_color(float(anime["score"])),
"synopsis": synopsis,
"start": anime["start_date"] if anime["start_date"] != "0000-00-00" else "NA",
"end": anime["end_date"] if anime["end_date"] != "0000-00-00" else "NA",
"status": anime["status"]
}
print("\n".join(line.format_map(template) for line in lines))
if full: print("\n".join(line.format_map(template) for line in extra_lines))
print("\n")

def drop(mal, regex):
"""Drop a anime based a regex expression"""
items = remove_completed(mal.find(regex))
Expand Down