Skip to content

Commit

Permalink
Add --trusted flag to only search for trusted uploads (Closes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvictorfs committed May 24, 2020
1 parent 2ca33f5 commit 790b180
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

---

## 0.1.9
## 0.2.0

- Only show torrent alert messages in debug mode (`--debug`) (Closes Issue [`#25`](https://github.com/johnvictorfs/nyaa-cli/issues/25))

- Add argument to search for only trusted torrents `-t` (`--trusted`) (Closes Issue [`#26`](https://github.com/johnvictorfs/nyaa-cli/issues/26))

- Add argument alias for `--debug` (`-d`)

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

---

## 0.1.8
Expand Down
7 changes: 4 additions & 3 deletions nyaacli/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def entries_autocomplete(ctx, args: List[str], incomplete: str):
@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('--debug/--no-debug', default=False, help=green('Debug Mode'))
def main(anime: str, episode: int, output: str, debug: bool = False, number: int = 10):
@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):
"""
Search for Anime on https://nyaa.si and downloads it
Expand All @@ -64,7 +65,7 @@ def main(anime: str, episode: int, output: str, debug: bool = False, number: int
logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)

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

if torrent_search:
torrent_path, result_name = torrent_search
Expand Down
33 changes: 24 additions & 9 deletions nyaacli/nyaa_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from nyaacli.colors import red, green, prompt_style

from urllib import request
from typing import Optional, List, Tuple
from urllib import parse, request
from typing import Optional, List, Tuple, Dict
from dataclasses import dataclass
from datetime import datetime
from time import mktime
Expand Down Expand Up @@ -58,28 +58,43 @@ def __str__(self):
return f'Entry(title={repr(self.title)}, episode={repr(self.episode)})'


def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False, number: int = 10) -> Optional[Tuple[str, str]]:
def search_torrent(
search: str,
episode: Optional[int] = None,
dub: bool = False,
number: int = 10,
trusted_only: bool = False
) -> Optional[Tuple[str, str]]:
"""
Results a tuple with (Path to .torrent file, Result name of video file)
Nyaa.si rss search flags
https://github.com/nyaadevs/nyaa/blob/a38e5d5b53805ecb1d94853d849826f948f07aad/nyaa/views/main.py#L65
"""

search_query = f'{search}'.strip().replace(' ', '%20')
search_query = f'{search}'.strip()
if episode:
search_query += f' {episode}'.replace(' ', '%20')
search_query += f' {episode}'

logger.debug(f'Searching nyaa for query: \'{search_query}\'')

search_url = f'https://nyaa.si/rss?c=1_2&q={search_query}&s=seeders&o=desc'
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

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

# Parse Nyaa.si rss feed search
feed: feedparser.FeedParserDict = feedparser.parse(search_url)
logger.debug(f'Getting feed parse from: \'{search_url}\'')

if not feed.entries and feed.bozo_exception:
# Malformatted feed
print(red(f"[Error] {str(feed.bozo_exception)}"))
if not feed.entries:
print(red('[Error] No entries found for search'))
sys.exit(1)

entries: List[Entry] = []
Expand Down

0 comments on commit 790b180

Please sign in to comment.