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

Vueify/new show #4267

Merged
merged 18 commits into from
Jun 13, 2018
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
4 changes: 2 additions & 2 deletions medusa/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def __init__(self, config, log=None):
self.log = log

def select_series(self, all_series):
from medusa.helper.common import dateTimeFormat
from medusa.helper.common import dateFormat

search_results = []
series_names = []
Expand All @@ -270,7 +270,7 @@ def select_series(self, all_series):
if search_term.lower() in name.lower():
if 'firstaired' not in cur_show:
default_date = parser.parse('1900-01-01').date()
cur_show['firstaired'] = default_date.strftime(dateTimeFormat)
cur_show['firstaired'] = default_date.strftime(dateFormat)

if cur_show not in search_results:
search_results += [cur_show]
Expand Down
110 changes: 107 additions & 3 deletions medusa/server/api/v2/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import os
import re

from medusa import app, db
from medusa.helper.common import try_int
from medusa import app, classes, db
from medusa.helper.common import sanitize_filename, try_int
from medusa.indexers.indexer_api import indexerApi
from medusa.indexers.indexer_exceptions import IndexerException, IndexerUnavailable
from medusa.indexers.utils import reverse_mappings
from medusa.logger.adapters.style import BraceAdapter
from medusa.server.api.v2.base import BaseRequestHandler
from medusa.tv.series import Series, SeriesIdentifier

from six import itervalues
from six import iteritems, itervalues

log = BraceAdapter(logging.getLogger(__name__))

Expand Down Expand Up @@ -136,3 +139,104 @@ def resource_existing_series(self):
dir_list.append(cur_dir)

return self._ok(data=dir_list)

# searchIndexersForShowName
def resource_search_indexers_for_show_name(self):
"""
Search indexers for show name.

Query parameters:
:param query: Search term
:param indexerId: Indexer to search, defined by ID. '0' for all indexers.
:param language: 2-letter language code to search the indexer(s) in
"""
query = self.get_argument('query', '').strip()
indexer_id = self.get_argument('indexerId', '0').strip()
language = self.get_argument('language', '').strip()

if not query:
return self._bad_request('No search term provided.')

enabled_indexers = indexerApi().indexers
indexer_id = try_int(indexer_id)
if indexer_id > 0 and indexer_id not in enabled_indexers:
return self._bad_request('Invalid indexer id.')

if not language or language == 'null':
language = app.INDEXER_DEFAULT_LANGUAGE

search_terms = [query]

# If search term ends with what looks like a year, enclose it in ()
matches = re.match(r'^(.+ |)([12][0-9]{3})$', query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably add this as another PR but we also need to do vs and vs. as I found with Red vs. Blue.

if matches:
search_terms.append('{0}({1})'.format(matches.group(1), matches.group(2)))

for searchTerm in search_terms:
# If search term begins with an article, let's also search for it without
matches = re.match(r'^(?:a|an|the) (.+)$', searchTerm, re.I)
if matches:
search_terms.append(matches.group(1))

results = {}
final_results = []

# Query indexers for each search term and build the list of results
for indexer in enabled_indexers if indexer_id == 0 else [indexer_id]:
indexer_instance = indexerApi(indexer)
custom_api_params = indexer_instance.api_params.copy()
custom_api_params['language'] = language
custom_api_params['custom_ui'] = classes.AllShowsListUI

try:
indexer_api = indexer_instance.indexer(**custom_api_params)
except IndexerUnavailable as msg:
log.info('Could not initialize indexer {indexer}: {error}',
{'indexer': indexer_instance.name, 'error': msg})
continue

log.debug('Searching for show with search term(s): {terms} on indexer: {indexer}',
{'terms': search_terms, 'indexer': indexer_api.name})
for search_term in search_terms:
try:
indexer_results = indexer_api[search_term]
# add search results
results.setdefault(indexer, []).extend(indexer_results)
except IndexerException as error:
log.info('Error searching for show: {error}', {'error': error})

# Get all possible show ids
all_show_ids = {}
for show in app.showList:
for external in show.externals:
indexer_id = reverse_mappings.get(external)
show_id = show.externals[external]
all_show_ids.setdefault(indexer_id, []).append(show_id)

for indexer, shows in iteritems(results):
indexer_api = indexerApi(indexer)
indexer_results_set = set()
for show in shows:
show_id = int(show['id'])
indexer_results_set.add(
(
indexer_api.name,
indexer,
indexer_api.config['show_url'],
show_id,
show['seriesname'].encode('utf-8'),
show['firstaired'] or 'N/A',
show.get('network', '').encode('utf-8') or 'N/A',
sanitize_filename(show['seriesname']).encode('utf-8'),
show_id in all_show_ids.get(indexer, []) and (indexer, show_id)
)
)

final_results.extend(indexer_results_set)

language_id = indexerApi().config['langabbv_to_id'][language]
data = {
'results': final_results,
'languageId': language_id
}
return self._ok(data=data)
Loading