Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: TypeError: decoding Unicode is not supported

### INFO
**Python Version**: `2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]`
**Operating System**: `Linux-3.16.0-4-amd64-x86_64-with-debian-8.10`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.8`
**Commit**: pymedusa/Medusa@bd0832551d836713481dd0a93f4118372bb5d963
**Link to Log**: https://gist.github.com/a5511789ac5b75747461dada424c4231
### ERROR
<pre>
2018-02-04 19:57:35 ERROR    Thread-22 :: [bd08325] Exception generated: decoding Unicode is not supported
Traceback (most recent call last):
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/server/web/core/base.py#L280">medusa/server/web/core/base.py</a>", line 280, in async_call
    result = function(**kwargs)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/server/web/home/post_process.py#L49">medusa/server/web/home/post_process.py</a>", line 49, in processEpisode
    ignore_subs=argToBool(ignore_subs)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L146">medusa/process_tv.py</a>", line 146, in process
    ignore_subs=ignore_subs)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L335">medusa/process_tv.py</a>", line 335, in process_files
    self.process_media(path, self.video_files, force, is_priority, ignore_subs)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L536">medusa/process_tv.py</a>", line 536, in process_media
    self.result = processor.process()
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/post_processor.py#L1229">medusa/post_processor.py</a>", line 1229, in process
    cur_ep.refresh_subtitles()
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/tv/episode.py#L422">medusa/tv/episode.py</a>", line 422, in refresh_subtitles
    current_subtitles = subtitles.get_current_subtitles(self)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L590">medusa/subtitles.py</a>", line 590, in get_current_subtitles
    video = get_video(tv_episode, video_path)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L712">medusa/subtitles.py</a>", line 712, in get_video
    subtitles_dir = _encode(subtitles_dir or get_subtitles_dir(video_path))
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L760">medusa/subtitles.py</a>", line 760, in get_subtitles_dir
    return _decode(app.SUBTITLES_DIR)
  File "/srv/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L638">medusa/subtitles.py</a>", line 638, in _decode
    return text_type(value, encoding)
TypeError: decoding Unicode is not supported
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:

### INFO
**Python Version**: `2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]`
**Operating System**: `Linux-4.9.0-5-amd64-x86_64-with-debian-9.3`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.8`
**Commit**: pymedusa/Medusa@bd0832551d836713481dd0a93f4118372bb5d963
**Link to Log**: https://gist.github.com/90debebc6b0ee328685e679d9e73594b
### ERROR
<pre>
2018-02-06 19:48:49 ERROR    POSTPROCESSOR :: [bd08325] Exception generated: decoding Unicode is not supported
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/scheduler.py#L90">medusa/scheduler.py</a>", line 90, in run
    self.action.run(self.force)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/auto_post_processor.py#L57">medusa/auto_post_processor.py</a>", line 57, in run
    process_tv.ProcessResult(app.TV_DOWNLOAD_DIR, app.PROCESS_METHOD).process(force=force)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L146">medusa/process_tv.py</a>", line 146, in process
    ignore_subs=ignore_subs)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L335">medusa/process_tv.py</a>", line 335, in process_files
    self.process_media(path, self.video_files, force, is_priority, ignore_subs)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/process_tv.py#L536">medusa/process_tv.py</a>", line 536, in process_media
    self.result = processor.process()
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/post_processor.py#L1214">medusa/post_processor.py</a>", line 1214, in process
    app.MOVE_ASSOCIATED_FILES, app.USE_SUBTITLES and ep_obj.series.subtitles)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/post_processor.py#L504">medusa/post_processor.py</a>", line 504, in post_process_action
    action=action, subtitle_action=subtitle_action, subtitles=subtitles)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/post_processor.py#L439">medusa/post_processor.py</a>", line 439, in _combined_file_operation
    new_file_path = self.rename_associated_file(new_path, new_basename, cur_associated_file)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/post_processor.py#L405">medusa/post_processor.py</a>", line 405, in rename_associated_file
    new_filepath = os.path.join(get_subtitles_dir(new_filepath), new_filename)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L760">medusa/subtitles.py</a>", line 760, in get_subtitles_dir
    return _decode(app.SUBTITLES_DIR)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L638">medusa/subtitles.py</a>", line 638, in _decode
    return text_type(value, encoding)
TypeError: decoding Unicode is not supported
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]`
**Operating System**: `Linux-4.9.0-5-amd64-x86_64-with-debian-9.3`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.8`
**Commit**: pymedusa/Medusa@bd0832551d836713481dd0a93f4118372bb5d963
**Link to Log**: https://gist.github.com/1ddd8139a9d65401519ea9d27c734acb
### ERROR
<pre>
2018-02-06 20:38:10 ERROR    FINDSUBTITLES :: [bd08325] Exception generated: decoding Unicode is not supported
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/scheduler.py#L90">medusa/scheduler.py</a>", line 90, in run
    self.action.run(self.force)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L1054">medusa/subtitles.py</a>", line 1054, in run
    episode_object.download_subtitles()
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/tv/episode.py#L466">medusa/tv/episode.py</a>", line 466, in download_subtitles
    new_subtitles = subtitles.download_subtitles(self, lang=lang)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L401">medusa/subtitles.py</a>", line 401, in download_subtitles
    subtitles_dir = get_subtitles_dir(video_path)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L760">medusa/subtitles.py</a>", line 760, in get_subtitles_dir
    return _decode(app.SUBTITLES_DIR)
  File "/opt/medusa/<a href="../blob/bd0832551d836713481dd0a93f4118372bb5d963/medusa/subtitles.py#L638">medusa/subtitles.py</a>", line 638, in _decode
    return text_type(value, encoding)
TypeError: decoding Unicode is not supported
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Resolving patch:
diff --git a/medusa/subtitles.py b/medusa/subtitles.py
--- a/medusa/subtitles.py
+++ b/medusa/subtitles.py
@@ -41,7 +41,7 @@
 from medusa.show.show import Show
 from medusa.subtitle_providers.utils import hash_itasa
 
-from six import iteritems, string_types, text_type
+from six import binary_type, iteritems, string_types, text_type
 
 from subliminal import ProviderPool, compute_score, provider_manager, refine, save_subtitles, scan_video
 from subliminal.core import search_external_subtitles
@@ -608,6 +608,9 @@ def _encode(value, fallback=None):
     :return: the encoded value
     :rtype: str
     """
+    if isinstance(value, binary_type):
+        return value
+
     encoding = 'utf-8' if os.name != 'nt' else app.SYS_ENCODING
 
     try:
@@ -632,6 +635,9 @@ def _decode(value, fallback=None):
     :return: the decoded value
     :rtype: unicode
     """
+    if isinstance(value, text_type):
+        return value
+
     encoding = 'utf-8' if os.name != 'nt' else app.SYS_ENCODING
 
     try:


Test patch: