Skip to content

Commit

Permalink
Add option to search by different values other than seeders (Closes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvictorfs committed May 25, 2020
1 parent 790b180 commit ef73121
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- Removed off-switch for `--debug` flag (used to be `--no-debug`). It's already off by default so it was an useless argument

- Add option to sort by different values other than seeders with `--sort-by` (`-s`). (Closes Issue [`#27`](https://github.com/johnvictorfs/nyaa-cli/issues/27)). See linked issue to see options available.

---

## 0.1.8
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Usage: nyaa [OPTIONS] ANIME [EPISODE]
Options:
-o, --output PATH Output Folder [default: ~/Videos/Anime]
-n, --number INTEGER Number of entries [default: 10]
--debug / --no-debug Debug Mode
-s, --sort-by TEXT Sort by [default: seeders]
-t, --trusted Only search trusted uploads
-d, --debug Debug Mode
--help Show this message and exit.
```

Expand Down
30 changes: 27 additions & 3 deletions nyaacli/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,46 @@ def entries_autocomplete(ctx, args: List[str], incomplete: str):

entries = [
('5', f'5 {text}'),
('10', f'10 {text}'),
('10', f'10 {text} (default)'),
('15', f'15 {text}'),
('20', f'20 {text}')
]

return [c for c in entries if incomplete in c[0]]


def sort_mode_autocomplete(ctx, args: List[str], incomplete: str):
"""
Auto-complete choices for --sort-by / -s argument
"""

entries = [
('seeders', 'Sort by number of torrent seeders (default)'),
('date', 'Sort by upload date'),
('size', 'Sort by file size'),
('comments', 'Sort by number of comments')
]

return [c for c in entries if incomplete in c[0]]


@click.command()
@click.argument('anime')
@click.argument('episode', type=int, default=None, required=False)
@click.option('--output', '-o', default='~/Videos/Anime', help=green('Output Folder'), type=click.Path(), show_default=True)
@click.option('--number', '-n', default=10, help=green('Number of entries'), show_default=True, autocompletion=entries_autocomplete)
@click.option('--sort-by', '-s', default='seeders', help=green('Sort by'), show_default=True, autocompletion=sort_mode_autocomplete)
@click.option('--trusted', '-t', default=False, help=green('Only search trusted uploads'), is_flag=True)
@click.option('--debug', '-d', default=False, help=green('Debug Mode'), is_flag=True)
def main(anime: str, episode: int, output: str, debug: bool = False, trusted: bool = False, number: int = 10):
def main(
anime: str,
episode: int,
output: str,
debug: bool = False,
trusted: bool = False,
number: int = 10,
sort_by: str = 'seeders',
):
"""
Search for Anime on https://nyaa.si and downloads it
Expand All @@ -65,7 +89,7 @@ def main(anime: str, episode: int, output: str, debug: bool = False, trusted: bo
logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)

torrent_search = search_torrent(anime, episode, number=number, trusted_only=trusted)
torrent_search = search_torrent(anime, episode, number=number, trusted_only=trusted, sort_by=sort_by)

if torrent_search:
torrent_path, result_name = torrent_search
Expand Down
17 changes: 14 additions & 3 deletions nyaacli/nyaa_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def search_torrent(
episode: Optional[int] = None,
dub: bool = False,
number: int = 10,
trusted_only: bool = False
trusted_only: bool = False,
sort_by: str = 'seeders'
) -> Optional[Tuple[str, str]]:
"""
Results a tuple with (Path to .torrent file, Result name of video file)
Expand All @@ -80,13 +81,23 @@ def search_torrent(
url_arguments: Dict[str, str] = {
'c': '1_2', # Language (English)
'q': search_query, # Search Query
's': 'seeders', # Sort by seeders
'o': 'desc', # Sort order
}

if trusted_only:
url_arguments['f'] = '2' # Trusted uploaders only

# What to sort torrents by
if sort_by == 'seeders':
url_arguments['s'] = 'seeders'
elif sort_by == 'date':
url_arguments['s'] = 'id'
elif sort_by == 'size':
url_arguments['s'] = 'size'
elif sort_by == 'comments':
url_arguments['s'] = 'comments'

logger.debug(f'URL Arguments: {url_arguments}')

search_url = 'https://nyaa.si/rss?' + parse.urlencode(url_arguments)

# Parse Nyaa.si rss feed search
Expand Down

0 comments on commit ef73121

Please sign in to comment.