Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: KeyError: 'backlog_search'

### INFO
**Python Version**: `3.7.5 (default, Nov  1 2019, 02:16:23) [Clang 11.0.0 (clang-1100.0.33.8)]`
**Operating System**: `Darwin-19.0.0-x86_64-i386-64bit`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b
**Link to Log**: https://gist.github.com/eb1b2912f593d5e8574e1b984384246e
### ERROR
<pre>
2019-12-05 17:41:37 ERROR    Thread_19 :: [fcb5a19] Exception generated: 'backlog_search'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/server/web/core/base.py#L251">medusa/server/web/core/base.py</a>", line 251, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/server/web/manage/searches.py#L26">medusa/server/web/manage/searches.py</a>", line 26, in index
    forcedSearchQueueLength=app.forced_search_queue_scheduler.action.queue_length(),
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/search/queue.py#L179">medusa/search/queue.py</a>", line 179, in queue_length
    length['backlog_search'] += 1
KeyError: 'backlog_search'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:

### INFO
**Python Version**: `3.7.5 (default, Nov  1 2019, 02:16:23) [Clang 11.0.0 (clang-1100.0.33.8)]`
**Operating System**: `Darwin-19.0.0-x86_64-i386-64bit`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b
**Link to Log**: https://gist.github.com/0fd924dec748a93ef3584e18e4b6627b
### ERROR
<pre>
2019-12-05 17:43:41 ERROR    Thread_24 :: [fcb5a19] Exception generated: 'backlog_search'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/server/web/core/base.py#L251">medusa/server/web/core/base.py</a>", line 251, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/server/web/manage/searches.py#L26">medusa/server/web/manage/searches.py</a>", line 26, in index
    forcedSearchQueueLength=app.forced_search_queue_scheduler.action.queue_length(),
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/fcb5a1969dcd4ea0b6fb4a9debbf60179b0bbf4b/medusa/search/queue.py#L179">medusa/search/queue.py</a>", line 179, in queue_length
    length['backlog_search'] += 1
KeyError: 'backlog_search'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

this happens when i try to go to Manage Searches page when there was something in the backlog.. disappointing that this is broken on master.

this breaks as soon as there is 2 or more items in the backlog.

### INFO
**Python Version**: `3.7.5 (default, Nov  1 2019, 02:16:23) [Clang 11.0.0 (clang-1100.0.33.8)]`
**Operating System**: `Darwin-19.0.0-x86_64-i386-64bit`
**Locale**: `UTF-8`
**Branch**: [develop](../tree/develop)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@6456664611d1fe304d7e3118104dcac4cd607a25
**Link to Log**: https://gist.github.com/b1954bb9315bc610b131017c85de575d
### ERROR
<pre>
2019-12-05 17:49:48 ERROR    Thread_0 :: [6456664] Exception generated: 'backlog_search'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/6456664611d1fe304d7e3118104dcac4cd607a25/medusa/server/web/core/base.py#L251">medusa/server/web/core/base.py</a>", line 251, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/6456664611d1fe304d7e3118104dcac4cd607a25/medusa/server/web/manage/searches.py#L26">medusa/server/web/manage/searches.py</a>", line 26, in index
    forcedSearchQueueLength=app.forced_search_queue_scheduler.action.queue_length(),
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/6456664611d1fe304d7e3118104dcac4cd607a25/medusa/search/queue.py#L179">medusa/search/queue.py</a>", line 179, in queue_length
    length['backlog_search'] += 1
KeyError: 'backlog_search'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

seems to be broken on develop branch as well..

To Reproduce:
have 2 episodes from different shows marked as snatched but never downloaded
then retry download, select no when the popup shows.
do this on 2 or more episodes to queue up in backlog, then go to Manage Searches, white page will only display & error in log.
Report once and wait for the fix. Avoid useless comments that waste our time.
tnx @XxUnkn0wnxX i'll look into it. Pls standby for testing out a fix when ready. Don't know when, as I have to also look into the show updater issues

Resolving patch:
diff --git a/medusa/classes.py b/medusa/classes.py
--- a/medusa/classes.py
+++ b/medusa/classes.py
@@ -220,8 +220,14 @@ def _create_episode_objects(self):
         if self.actual_season is not None and self.series:
             if self.actual_episodes:
                 self.episodes = [self.series.get_episode(self.actual_season, ep) for ep in self.actual_episodes]
+                if len(self.actual_episodes) == 1:
+                    self.episode_number = self.actual_episodes[0]
+                else:
+                    self.episode_number = MULTI_EP_RESULT
             else:
                 self.episodes = self.series.get_all_episodes(self.actual_season)
+                self.actual_episodes = [ep.episode for ep in self.episodes]
+                self.episode_number = SEASON_RESULT
 
         return self.episodes
 
@@ -245,12 +251,6 @@ def _episodes_from_cache(self):
         # Season result
         if not sql_episodes:
             ep_objs = series_obj.get_all_episodes(actual_season)
-            if not ep_objs:
-                # We couldn't get any episodes for this season, which is odd, skip the result.
-                log.debug("We couldn't get any episodes for season {0} of {1}, skipping",
-                          actual_season, cached_data['name'])
-                return
-
             self.actual_episodes = [ep.episode for ep in ep_objs]
             self.episode_number = SEASON_RESULT
 
diff --git a/medusa/providers/generic_provider.py b/medusa/providers/generic_provider.py
--- a/medusa/providers/generic_provider.py
+++ b/medusa/providers/generic_provider.py
@@ -424,23 +424,20 @@ def find_search_results(self, series, episodes, search_mode, forced_search=False
 
             search_result.update_search_result()
 
-            if not search_result.actual_episodes:
-                episode_number = SEASON_RESULT
+            if search_result.episode_number == SEASON_RESULT:
                 log.debug('Found season pack result {0} at {1}', search_result.name, search_result.url)
-            elif len(search_result.actual_episodes) == 1:
-                episode_number = search_result.actual_episode
-                log.debug('Found single episode result {0} at {1}', search_result.name, search_result.url)
-            else:
-                episode_number = MULTI_EP_RESULT
+            elif search_result.episode_number == MULTI_EP_RESULT:
                 log.debug('Found multi-episode ({0}) result {1} at {2}',
                           ', '.join(map(str, search_result.parsed_result.episode_numbers)),
                           search_result.name,
                           search_result.url)
+            else:
+                log.debug('Found single episode result {0} at {1}', search_result.name, search_result.url)
 
-            if episode_number not in final_results:
-                final_results[episode_number] = [search_result]
+            if search_result.episode_number not in final_results:
+                final_results[search_result.episode_number] = [search_result]
             else:
-                final_results[episode_number].append(search_result)
+                final_results[search_result.episode_number].append(search_result)
 
         if cl:
             # Access to a protected member of a client class
diff --git a/medusa/tv/cache.py b/medusa/tv/cache.py
--- a/medusa/tv/cache.py
+++ b/medusa/tv/cache.py
@@ -609,11 +609,10 @@ def find_episodes(self, episodes):
                 continue
 
             search_result = self.provider.get_result(series=series_obj, cache=cur_result)
-            if search_result.episode_number is not None:
-                if search_result in cache_results[search_result.episode_number]:
-                    continue
-                # add it to the list
-                cache_results[search_result.episode_number].append(search_result)
+            if search_result in cache_results[search_result.episode_number]:
+                continue
+            # add it to the list
+            cache_results[search_result.episode_number].append(search_result)
 
         # datetime stamp this search so cache gets cleared
         self.searched = time()


Test patch: