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

Remove forcedSearchQueueItem, in favor of using backlogQueueItem for single ep searches #6718

Merged
merged 16 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
13 changes: 6 additions & 7 deletions medusa/search/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from medusa.helper.common import enabled_providers, pretty_file_size
from medusa.logger.adapters.style import BraceAdapter
from medusa.network_timezones import app_timezone
from medusa.search.queue import FORCED_SEARCH_HISTORY, ForcedSearchQueueItem
from medusa.search.queue import ManualSearchQueueItem, SEARCH_HISTORY
from medusa.show.naming import contains_at_least_one_word, filter_bad_releases
from medusa.show.show import Show

Expand Down Expand Up @@ -116,11 +116,11 @@ def update_finished_search_queue_item(snatch_queue_item):
"""
# Finished Searches

for search_thread in FORCED_SEARCH_HISTORY:
for search_thread in SEARCH_HISTORY:
if snatch_queue_item.show and not search_thread.show.indexerid == snatch_queue_item.show.indexerid:
continue

if isinstance(search_thread, ForcedSearchQueueItem):
if isinstance(search_thread, ManualSearchQueueItem):
if not isinstance(search_thread.segment, list):
search_thread.segment = [search_thread.segment]

Expand Down Expand Up @@ -158,11 +158,11 @@ def collect_episodes_from_search_thread(series_obj):

# Finished Searches
searchstatus = SEARCH_STATUS_FINISHED
for search_thread in FORCED_SEARCH_HISTORY:
for search_thread in SEARCH_HISTORY:
if series_obj and not search_thread.show.identifier == series_obj.identifier:
continue

if isinstance(search_thread, ForcedSearchQueueItem):
if isinstance(search_thread, ManualSearchQueueItem):
if not [x for x in episodes if x['episodeindexerid'] in [search.indexerid for search in search_thread.segment]]:
episodes += get_episodes(search_thread, searchstatus)
else:
Expand All @@ -176,7 +176,6 @@ def collect_episodes_from_search_thread(series_obj):
def get_provider_cache_results(series_obj, show_all_results=None, perform_search=None,
season=None, episode=None, manual_search_type=None, **search_show):
"""Check all provider cache tables for search results."""
down_cur_quality = 0
preferred_words = series_obj.show_words().preferred_words
undesired_words = series_obj.show_words().undesired_words
ignored_words = series_obj.show_words().ignored_words
Expand Down Expand Up @@ -281,7 +280,7 @@ def get_provider_cache_results(series_obj, show_all_results=None, perform_search
and episode: {1}x{2}'.format(series_obj.name, season, episode)

# make a queue item for it and put it on the queue
ep_queue_item = ForcedSearchQueueItem(ep_obj.series, [ep_obj], bool(int(down_cur_quality)), True, manual_search_type) # pylint: disable=maybe-no-member
ep_queue_item = ManualSearchQueueItem(ep_obj.series, [ep_obj], manual_search_type) # pylint: disable=maybe-no-member

app.forced_search_queue_scheduler.action.add_item(ep_queue_item)

Expand Down
110 changes: 33 additions & 77 deletions medusa/search/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from medusa import app, common, failed_history, generic_queue, history, ui
from medusa.helpers import pretty_file_size
from medusa.logger.adapters.style import BraceAdapter
from medusa.search import BACKLOG_SEARCH, DAILY_SEARCH, FAILED_SEARCH, FORCED_SEARCH, SNATCH_RESULT, SearchType
from medusa.search import BACKLOG_SEARCH, DAILY_SEARCH, FAILED_SEARCH, MANUAL_SEARCH, SNATCH_RESULT, SearchType
from medusa.search.core import (
search_for_needed_episodes,
search_providers,
Expand All @@ -25,8 +25,8 @@

search_queue_lock = threading.Lock()

FORCED_SEARCH_HISTORY = []
FORCED_SEARCH_HISTORY_SIZE = 100
SEARCH_HISTORY = []
SEARCH_HISTORY_SIZE = 100


class SearchQueue(generic_queue.GenericQueue):
Expand All @@ -42,7 +42,7 @@ def is_in_queue(self, show, segment):
"""Check if item is in queue."""
for cur_item in self.queue:
if isinstance(cur_item, (BacklogQueueItem, FailedQueueItem,
ForcedSearchQueueItem, SnatchQueueItem)) \
SnatchQueueItem, ManualSearchQueueItem)) \
and cur_item.show == show and cur_item.segment == segment:
return True
return False
Expand Down Expand Up @@ -90,7 +90,7 @@ def add_item(self, item):
# daily searches
generic_queue.GenericQueue.add_item(self, item)
elif isinstance(item, (BacklogQueueItem, FailedQueueItem,
SnatchQueueItem, ForcedSearchQueueItem)) \
SnatchQueueItem, ManualSearchQueueItem)) \
and not self.is_in_queue(item.show, item.segment):
generic_queue.GenericQueue.add_item(self, item)
else:
Expand All @@ -105,12 +105,12 @@ def force_daily(self):


class ForcedSearchQueue(generic_queue.GenericQueue):
"""Search Queueu used for Forced Search, Failed Search."""
"""Search Queueu used for Manual, Failed Search."""

def __init__(self):
"""Initialize ForcedSearch Queue."""
generic_queue.GenericQueue.__init__(self)
self.queue_name = 'SEARCHQUEUE'
self.queue_name = 'FORCEDSEARCHQUEUE'

def is_in_queue(self, show, segment):
"""Verify if the show and segment (episode or number of episodes) are scheduled."""
Expand All @@ -122,14 +122,14 @@ def is_in_queue(self, show, segment):
def is_ep_in_queue(self, segment):
"""Verify if the show and segment (episode or number of episodes) are scheduled."""
for cur_item in self.queue:
if isinstance(cur_item, (ForcedSearchQueueItem, FailedQueueItem)) and cur_item.segment == segment:
if isinstance(cur_item, (BacklogQueueItem, FailedQueueItem, ManualSearchQueueItem)) and cur_item.segment == segment:
return True
return False

def is_show_in_queue(self, show):
"""Verify if the show is queued in this queue as a ForcedSearchQueueItem or FailedQueueItem."""
"""Verify if the show is queued in this queue as a BacklogQueueItem, ManualSearchQueueItem or FailedQueueItem."""
for cur_item in self.queue:
if isinstance(cur_item, (ForcedSearchQueueItem, FailedQueueItem)) and cur_item.show.indexerid == show:
if isinstance(cur_item, (BacklogQueueItem, FailedQueueItem, ManualSearchQueueItem)) and cur_item.show.indexerid == show:
return True
return False

Expand All @@ -139,11 +139,11 @@ def get_all_ep_from_queue(self, series_obj):

@param series_obj: Series object.

@return: A list of ForcedSearchQueueItem or FailedQueueItem items
@return: A list of BacklogQueueItem, FailedQueueItem or FailedQueueItem items
"""
ep_obj_list = []
for cur_item in self.queue:
if isinstance(cur_item, (ForcedSearchQueueItem, FailedQueueItem)):
if isinstance(cur_item, (BacklogQueueItem, FailedQueueItem, ManualSearchQueueItem)):
if series_obj and cur_item.show.identifier != series_obj.identifier:
continue
ep_obj_list.append(cur_item)
Expand All @@ -159,8 +159,11 @@ def is_backlog_paused(self):
return self.min_priority >= generic_queue.QueuePriorities.NORMAL

def is_forced_search_in_progress(self):
"""Test of a forced search is currently running, it doesn't check what's in queue."""
if isinstance(self.currentItem, (ForcedSearchQueueItem, FailedQueueItem)):
"""Test of a forced search is currently running (can be backlog, manual or failed search).

it doesn't check what's in queue.
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be capitalized for consistency.

"""
if isinstance(self.currentItem, (BacklogQueueItem, ManualSearchQueueItem, FailedQueueItem)):
return True
return False

Expand All @@ -170,15 +173,15 @@ def queue_length(self):
for cur_item in self.queue:
if isinstance(cur_item, FailedQueueItem):
length['failed'] += 1
elif isinstance(cur_item, ForcedSearchQueueItem) and not cur_item.manual_search:
length['forced_search'] += 1
elif isinstance(cur_item, ForcedSearchQueueItem) and cur_item.manual_search:
elif isinstance(cur_item, BacklogQueueItem) and not cur_item.manual_search:
length['backlog_search'] += 1
elif isinstance(cur_item, ManualSearchQueueItem):
length['manual_search'] += 1
return length

def add_item(self, item):
"""Add a new ForcedSearchQueueItem or FailedQueueItem to the ForcedSearchQueue."""
if isinstance(item, (ForcedSearchQueueItem, FailedQueueItem)) and not self.is_ep_in_queue(item.segment):
"""Add a new ManualSearchQueueItem or FailedQueueItem to the ForcedSearchQueue."""
if isinstance(item, (ManualSearchQueueItem, FailedQueueItem, BacklogQueueItem)) and not self.is_ep_in_queue(item.segment):
# manual, snatch and failed searches
generic_queue.GenericQueue.add_item(self, item)
else:
Expand Down Expand Up @@ -315,26 +318,22 @@ def run(self):
self.finish()


class ForcedSearchQueueItem(generic_queue.QueueItem):
"""Forced search queue item class."""
class ManualSearchQueueItem(generic_queue.QueueItem):
"""Manual search queue item class."""

def __init__(self, show, segment, down_cur_quality=False, manual_search=False, manual_search_type='episode'):
def __init__(self, show, segment, manual_search_type='episode'):
"""
Initialize class of a QueueItem used to queue forced and manual searches.

:param show: A show object
:param segment: A list of episode objects.
:param down_cur_quality: Not sure what it's used for. Maybe legacy.
:param manual_search: Passed as True (bool) when the search should be performed without snatching a result
:param manual_search_type: Used to switch between episode and season search. Options are 'episode' or 'season'.
:return: The run() method searches and snatches the episode(s) if possible or it only searches and saves results to cache tables.
"""
generic_queue.QueueItem.__init__(self, u'Forced Search', FORCED_SEARCH)
generic_queue.QueueItem.__init__(self, u'Manual Search', MANUAL_SEARCH)
self.priority = generic_queue.QueuePriorities.HIGH
# SEARCHQUEUE-MANUAL-12345
# SEARCHQUEUE-FORCED-12345
self.name = '{search_type}-{indexerid}'.format(
search_type=('FORCED', 'MANUAL')[bool(manual_search)],
search_type='MANUAL',
indexerid=show.indexerid
)

Expand All @@ -344,8 +343,6 @@ def __init__(self, show, segment, down_cur_quality=False, manual_search=False, m

self.show = show
self.segment = segment
self.down_cur_quality = down_cur_quality
self.manual_search = manual_search
self.manual_search_type = manual_search_type

def run(self):
Expand All @@ -356,57 +353,16 @@ def run(self):
try:
log.info(
'Beginning {search_type} {season_pack}search for: {ep}', {
'search_type': ('forced', 'manual')[bool(self.manual_search)],
'search_type': 'manual',
'season_pack': ('', 'season pack ')[bool(self.manual_search_type == 'season')],
'ep': self.segment[0].pretty_name()
}
)

search_result = search_providers(self.show, self.segment, True, self.down_cur_quality,
self.manual_search, self.manual_search_type)

if not self.manual_search and search_result:
for result in search_result:
# Just use the first result for now
if result.seeders not in (-1, None) and result.leechers not in (-1, None):
log.info(
'Downloading {name} with {seeders} seeders and {leechers} leechers '
'and size {size} from {provider}', {
'name': result.name,
'seeders': result.seeders,
'leechers': result.leechers,
'size': pretty_file_size(result.size),
'provider': result.provider.name,
}
)
else:
log.info(
'Downloading {name} with size: {size} from {provider}', {
'name': result.name,
'size': pretty_file_size(result.size),
'provider': result.provider.name,
}
)

# Set the search_type for the result.
result.search_type = SearchType.FORCED_SEARCH
search_result = search_providers(self.show, self.segment, True, True,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to use named arguments here, especially for the Trues. It makes it much more readable.

True, self.manual_search_type)

# Create the queue item
snatch_queue_item = SnatchQueueItem(result.series, result.episodes, result)

# Add the queue item to the queue
app.manual_snatch_scheduler.action.add_item(snatch_queue_item)

self.success = False
while snatch_queue_item.success is False:
if snatch_queue_item.started and snatch_queue_item.success:
self.success = True
time.sleep(1)

# Give the CPU a break
time.sleep(common.cpu_presets[app.CPU_PRESET])

elif self.manual_search and search_result:
if search_result:
self.results = search_result
self.success = True

Expand Down Expand Up @@ -435,7 +391,7 @@ def run(self):
log.debug(traceback.format_exc())

# Keep a list with the 100 last executed searches
fifo(FORCED_SEARCH_HISTORY, self, FORCED_SEARCH_HISTORY_SIZE)
fifo(SEARCH_HISTORY, self, SEARCH_HISTORY_SIZE)

if self.success is None:
self.success = False
Expand Down Expand Up @@ -693,7 +649,7 @@ def run(self):
log.info(traceback.format_exc())

# ## Keep a list with the 100 last executed searches
fifo(FORCED_SEARCH_HISTORY, self, FORCED_SEARCH_HISTORY_SIZE)
fifo(SEARCH_HISTORY, self, SEARCH_HISTORY_SIZE)

if self.success is None:
self.success = False
Expand Down
4 changes: 2 additions & 2 deletions medusa/server/api/v1/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
from medusa.media.fan_art import ShowFanArt
from medusa.media.network_logo import ShowNetworkLogo
from medusa.media.poster import ShowPoster
from medusa.search.queue import BacklogQueueItem, ForcedSearchQueueItem
from medusa.search.queue import BacklogQueueItem
from medusa.show.coming_episodes import ComingEpisodes
from medusa.show.history import History
from medusa.show.show import Show
Expand Down Expand Up @@ -818,7 +818,7 @@ def run(self):
return _responds(RESULT_FAILURE, msg='Episode not found')

# make a queue item for it and put it on the queue
ep_queue_item = ForcedSearchQueueItem(show_obj, [ep_obj])
ep_queue_item = BacklogQueueItem(show_obj, [ep_obj])
app.forced_search_queue_scheduler.action.add_item(ep_queue_item) # @UndefinedVariable

# wait until the queue item tells us whether it worked or not
Expand Down
14 changes: 4 additions & 10 deletions medusa/server/api/v2/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from medusa import app
from medusa.search.manual import collect_episodes_from_search_thread
from medusa.search.queue import (
BacklogQueueItem,
FailedQueueItem,
ForcedSearchQueueItem,
ManualSearchQueueItem
)
from medusa.server.api.v2.base import BaseRequestHandler
from medusa.tv.episode import Episode, EpisodeNumber
Expand Down Expand Up @@ -127,9 +128,7 @@ def _search_backlog(self, data=None):
.format(show=series.name))

for segment in itervalues(episode_segments):
# Adding the ForcedSearchQueueItem to the forced_search_queue_scheduler. This is all going to change
# when we've refactored out the ForcedSearchQueueItem.
cur_backlog_queue_item = ForcedSearchQueueItem(series, segment)
cur_backlog_queue_item = BacklogQueueItem(series, segment)
app.forced_search_queue_scheduler.action.add_item(cur_backlog_queue_item)

return self._accepted('Backlog search for {0} started'.format(data['showSlug']))
Expand Down Expand Up @@ -216,12 +215,7 @@ def _search_manual(self, data):
for segments in ({'segment': episode_segments, 'manual_search_type': 'episode'},
{'segment': season_segments, 'manual_search_type': 'season'}):
for segment in itervalues(segments['segment']):
# Adding the ForcedSearchQueueItem to the forced_search_queue_scheduler. This is all going to change
# when we've refactored out the ForcedSearchQueueItem.
cur_manual_search_queue_item = ForcedSearchQueueItem(series, segment,
down_cur_quality=False,
manual_search=True,
manual_search_type=segments['manual_search_type'])
cur_manual_search_queue_item = ManualSearchQueueItem(series, segment, manual_search_type=segments['manual_search_type'])
app.forced_search_queue_scheduler.action.add_item(cur_manual_search_queue_item)

if not episode_segments and not season_segments:
Expand Down
9 changes: 3 additions & 6 deletions medusa/server/web/home/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
from medusa.search.queue import (
BacklogQueueItem,
FailedQueueItem,
ForcedSearchQueueItem,
SnatchQueueItem,
)
from medusa.server.web.core import (
Expand Down Expand Up @@ -1710,10 +1709,8 @@ def doRename(self, indexername=None, seriesid=None, eps=None):

return self.redirect('/home/displayShow?indexername={series_obj.indexer_name}&seriesid={series_obj.series_id}'.format(series_obj=series_obj))

def searchEpisode(self, indexername=None, seriesid=None, season=None, episode=None, manual_search=None):
"""Search a ForcedSearch single episode using providers which are backlog enabled."""
down_cur_quality = 0

def searchEpisode(self, indexername=None, seriesid=None, season=None, episode=None):
"""Search for a single episode using a Backlog Search using providers that are backlog enabled."""
# retrieve the episode object and fail if we can't get one
series_obj = Show.find_by_id(app.showList, indexer_name_to_id(indexername), seriesid)
ep_obj = series_obj.get_episode(season, episode)
Expand All @@ -1723,7 +1720,7 @@ def searchEpisode(self, indexername=None, seriesid=None, season=None, episode=No
})

# make a queue item for it and put it on the queue
ep_queue_item = ForcedSearchQueueItem(ep_obj.series, [ep_obj], bool(int(down_cur_quality)), bool(manual_search))
ep_queue_item = BacklogQueueItem(ep_obj.series, [ep_obj])

app.forced_search_queue_scheduler.action.add_item(ep_queue_item)

Expand Down