Skip to content

Commit

Permalink
Changed from using python-inquirer to PyInquirer (#12)
Browse files Browse the repository at this point in the history
- Closes #10 
- Closes #7 
- Closes #4
  • Loading branch information
johnvictorfs committed Nov 18, 2019
1 parent 6d8fe2d commit b609d2f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
3 changes: 3 additions & 0 deletions nyaacli/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

from nyaacli.nyaa_search import search_torrent
from nyaacli.colors import red, green
from colorama import init

init()


@click.command()
Expand Down
45 changes: 17 additions & 28 deletions nyaacli/colors.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
from inquirer.themes import Theme
from blessings import Terminal
from colorama import init
from PyInquirer import Token, style_from_dict
from colorama import init, Fore, Style

init()


def red(text: str) -> str:
return f'\033[31m{text}\033[0m'
return Fore.RED + text + Style.RESET_ALL


def green(text: str) -> str:
return f'\33[92m{text}\033[0m'
return Fore.GREEN + text + Style.RESET_ALL


def yellow(text: str) -> str:
return f'\33[33m{text}\033[0m'
return Fore.YELLOW + text + Style.RESET_ALL


def blue(text: str) -> str:
return f'\33[34m{text}\033[0m'


class PromptTheme(Theme):
def __init__(self):
super(PromptTheme, self).__init__()

term = Terminal()

self.Question.mark_color = term.yellow
self.Question.brackets_color = term.normal
self.Question.default_color = term.normal
self.Editor.opening_prompt_color = term.bright_black
self.Checkbox.selection_color = term.blue
self.Checkbox.selection_icon = '❯'
self.Checkbox.selected_icon = '◉'
self.Checkbox.selected_color = term.yellow + term.bold
self.Checkbox.unselected_color = term.normal
self.Checkbox.unselected_icon = '◯'
self.List.selection_color = term.blue
self.List.selection_cursor = '❯'
self.List.unselected_color = term.normal
return Fore.BLUE + text + Style.RESET_ALL


prompt_style = style_from_dict({
Token.Separator: '#6C6C6C',
Token.QuestionMark: '#5F819D',
Token.Selected: '#48b5b5 bold',
Token.Pointer: '#48b5b5 bold',
Token.Instruction: '#77a371',
Token.Answer: '#48b5b5 bold',
Token.Question: '#289c64 bold',
})
43 changes: 25 additions & 18 deletions nyaacli/nyaa_search.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import sys
from urllib import request
from typing import Optional, List, Tuple
from dataclasses import dataclass
import os
import feedparser

from guessit import guessit
import inquirer
from PyInquirer import prompt

from nyaacli.colors import red, yellow, green, PromptTheme
from nyaacli.utils import clear_screen
from nyaacli.colors import red, green, prompt_style


def get_file_extension(path: str) -> str:
Expand Down Expand Up @@ -129,39 +129,46 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False
if entry.seeders:
seeders = f'{entry.seeders} Seeders'

if int(entry.seeders) > 40:
entry_title += f" - {green(seeders)}"
elif 40 > int(entry.seeders) > 20:
entry_title += f" - {yellow(seeders)}"
else:
entry_title += f" - {red(seeders)}"
# Colors on options are not working very well with PyInquirer
# if int(entry.seeders) > 40:
# entry_title += f" - {green(seeders)}"
# elif 40 > int(entry.seeders) > 20:
# entry_title += f" - {yellow(seeders)}"
# else:
# entry_title += f" - {red(seeders)}"
entry_title += f" - {seeders}"

if entry.size:
entry_title += f" - {entry.size}"

entry.display_title = entry_title

choices = [{'name': entry.display_title, 'value': index} for index, entry in enumerate(entries[:5])]

questions = [
inquirer.List(
'entry',
message=green("Select one of the entries below"),
choices=[(str(entry.display_title), index) for index, entry in enumerate(entries[:5])],
),
{
'type': 'list',
'choices': choices,
'name': 'selection',
'message': 'Select one of the entries below',
}
]

answer = inquirer.prompt(questions, theme=PromptTheme())
answer = prompt(questions, style=prompt_style)

clear_screen()
if not answer:
# Cancelled with Ctrl + C
sys.exit(0)

index_choice = answer['entry'] - 1
index_choice = answer['selection'] - 1

entry_choice = entries[index_choice]

final_path = entry_choice.full_title

torrent_path = f'/tmp/{final_path}.torrent'

print(f"{green('[Downloading]')} '{torrent_path}'")
print(f"{green('[Downloading Torrent File]')} '{torrent_path}'")

request.urlretrieve(entry_choice.link, torrent_path)

Expand Down

0 comments on commit b609d2f

Please sign in to comment.