Skip to content

Commit

Permalink
Fix issue when searching with episode numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvictorfs committed Apr 21, 2020
1 parent e980da3 commit f1ac2e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
23 changes: 20 additions & 3 deletions nyaacli/nyaa_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
from datetime import datetime
from time import mktime
import feedparser
import logging
import sys
import os

os.environ['REGEX_DISABLED'] = '1'

logger = logging.getLogger('nyaa')


def get_file_extension(path: str) -> str:
"""
Expand Down Expand Up @@ -51,6 +54,9 @@ class Entry:
episode_title: Optional[str]
date: datetime

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) -> Optional[Tuple[str, str]]:
"""
Expand All @@ -61,12 +67,15 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False

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

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'

# 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
Expand All @@ -90,7 +99,7 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False
good_size = int(title.get('screen_size').replace('p', '')) > 480

if (title.get('episode') == episode or not episode) and title.get('type') == 'episode' and good_size:
entries.append(Entry(
entry = Entry(
link=entry['link'],
size=entry['nyaa_size'],
original_title=entry['title'],
Expand All @@ -107,7 +116,9 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False
screen_size=title.get('screen_size'),
alternative_title=title.get('alternative_title'),
date=datetime.fromtimestamp(mktime(entry.get('published_parsed')))
))
)
logger.debug(f'Added entry: {entry}')
entries.append(entry)

if not entries:
print(red(f'No results found for search: \'{search_query.replace("%20", " ")}\''))
Expand Down Expand Up @@ -162,6 +173,8 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False
}
]

logger.debug(f'Download choices: {choices}')

answer = prompt(questions, style=prompt_style)

if not answer:
Expand All @@ -172,12 +185,16 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False

entry_choice = entries[index_choice]

logger.debug(f'Selected entry at index {index_choice}: {entry_choice}')

final_path = entry_choice.full_title

torrent_path = f'/tmp/{final_path}.torrent'
logger.debug(f'Downloading torrent file to \'{torrent_path}\'')

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

request.urlretrieve(entry_choice.link, torrent_path)
logger.debug('Downloaded torrent file')

return torrent_path, final_path + entry_choice.extension
7 changes: 7 additions & 0 deletions nyaacli/torrenting.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import os
import sys
import time
import logging

import libtorrent

from nyaacli.colors import red, green
from nyaacli.utils import clear_screen

logger = logging.getLogger('nyaa')


def download_torrent(filename: str, result_filename: str = None, show_progress: bool = True, base_path: str = 'Anime'):
session = libtorrent.session({'listen_interfaces': '0.0.0.0:6881'})
logger.debug('Started libtorrent session')

base_path = os.path.expanduser(base_path)
logger.debug(f'Downloading output to: \'{base_path}\'')

info = libtorrent.torrent_info(filename)

logger.debug('Started downloading torrent')
handle: libtorrent.torrent_handle = session.add_torrent({
'ti': info,
'save_path': base_path
Expand Down Expand Up @@ -59,6 +65,7 @@ def download_torrent(filename: str, result_filename: str = None, show_progress:

clear_screen()

logger.debug('Finished torrent download')
print(f'Finished download at: \'{green(new_name)}\' ')


Expand Down

0 comments on commit f1ac2e9

Please sign in to comment.