Repository: pymedusa/Medusa

Issue:
File not PostProcessed
Branch/Commit: develop
OS: Ubuntu 14.04
What you did: let auto PP ran
What happened: A file did not get post processed due to title miss match
What you expected: For it to PP
Logs:
```
2016-11-09 14:45:42 INFO     POSTPROCESSOR :: [131bad0] Processing /media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv (Torrent)
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Analyzing name: dead.set.on.life.0105.720p-yestv.mkv
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Scene numbering enabled show 'Dead Set on Life' using indexer numbering: S01E05
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Converted parsed result dead.set.on.life.0105.720p-yestv.mkv into title: dead set on life, season: 1, episode: [5], screen_size: 720p, release_group: yestv, container: mkv, mimetype: video/x-matroska, type: episode, absolute_episode: [], quality: Unknown
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Parsed 'dead.set.on.life.0105.720p-yestv.mkv' into title: dead set on life, season: 1, episode: [5], screen_size: 720p, release_group: yestv, container: mkv, mimetype: video/x-matroska, type: episode, absolute_episode: [], quality: Unknown
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Analyzing name: /media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv
2016-11-09 14:45:48 DEBUG    POSTPROCESSOR :: [131bad0] Unable to match /media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv to a show in your database. Parser result: title: Dead Set On Life S01, screen_size: 720p, format: HDTV, audio_codec: DolbyDigital, audio_channels: 5.1, video_codec: h264, release_group: YesTV, season: 1, episode: [5], container: mkv, mimetype: video/x-matroska, type: episode, absolute_episode: [], quality: 720p HDTV
2016-11-09 14:45:48 WARNING  POSTPROCESSOR :: [131bad0] Processing failed for /media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv: Not enough information to determine what episode this is
```

The important lines
```
2016-11-09 14:45:42 DEBUG    POSTPROCESSOR :: [131bad0] Parsed 'dead.set.on.life.0105.720p-yestv.mkv' into title: dead set on life, season: 1, episode: [5], screen_size: 720p, release_group: yestv, container: mkv, mimetype: video/x-matroska, type: episode, absolute_episode: [], quality: Unknown
```

```
2016-11-09 14:45:48 DEBUG    POSTPROCESSOR :: [131bad0] Unable to match /media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv to a show in your database. Parser result: title: Dead Set On Life S01, screen_size: 720p, format: HDTV, audio_codec: DolbyDigital, audio_channels: 5.1, video_codec: h264, release_group: YesTV, season: 1, episode: [5], container: mkv, mimetype: video/x-matroska, type: episode, absolute_episode: [], quality: 720p HDTV
```

One "says" : `title: dead set on life` while the other `title: Dead Set On Life S01`


Discussion:
Also happening for `Years.of.Living.Dangerously.S01.720p.HDTV.x264/Years.of.Living.Dangerously.Part.6.720p.HDTV.x264-BATV.mkv: Not enough information to determine what episode this is`
I'm not getting any packs post processed 

To make the issue here clear.

_First issue:_
**dead.set.on.life.0105.720p-yestv.mkv** doesn't have a quality, so the PP will try parsing the full path. Which results in: **/media/extssd/download/programas2/Dead.Set.On.Life.S01.720p.HDTV.DD5.1.x264-YesTV/dead.set.on.life.0105.720p-yestv.mkv**. This full path has the quality and everything else too, but guessit is confused by the **2** of the folder **programas2**, making the show title _Dead Set On Life S01_. This is a guessit issue, but we should be smarter here and avoid using the full path. **I will have this fixed in the PP code**, but _a guessit fix is also welcome_.

_Second issue:_
Similar to the first issue. **Years.of.Living.Dangerously.Part.6.720p.HDTV.x264-BATV.mkv** doesn't have a season, so it will try the full path (I expect it to be also _programas2_). The **2 in programas2** confuses guessit once again and that causes it to miss the season on the full path as well, resulting in not enough information. Also a guessit issue, but will be resolved with the fixed PP code (same as above).

Will fix this asap.

Programas2? Really? We should avoid such folder names 

It shouldn't go that high in the tree just to parse a file name.. 
At most it should use the it's top directory.. 


Resolving patch:
diff --git a/medusa/post_processor.py b/medusa/post_processor.py
--- a/medusa/post_processor.py
+++ b/medusa/post_processor.py
@@ -65,8 +65,8 @@ def __init__(self, file_path, nzb_name=None, process_method=None, is_priority=No
         # file name only
         self.file_name = os.path.basename(file_path)
 
-        # the name of the folder only
-        self.folder_name = os.path.basename(self.folder_path)
+        # relative path to the file that is being processed
+        self.rel_path = self._get_rel_path()
 
         # name of the NZB that resulted in this folder
         self.nzb_name = nzb_name
@@ -99,6 +99,24 @@ def _log(self, message, level=logger.INFO):
         logger.log(message, level)
         self.log += message + '\n'
 
+    def _get_rel_path(self):
+        """Return the relative path to the file if possible, else the parent dir.
+
+        :param file_path: path to the file
+        :type file_path: text_type
+        :return: relative path to file or parent dir to file
+        :rtype: text_type
+        """
+        if app.TV_DOWNLOAD_DIR:
+            rel_path = os.path.relpath(self.file_path, app.TV_DOWNLOAD_DIR)
+            # check if we really found the relative path
+            if not rel_path.startswith('..'):
+                return rel_path
+
+        parent_name = os.path.basename(os.path.dirname(self.file_path))
+        # return self.file_path once this bug is fixed: goo.gl/U4XNoP
+        return os.path.join(parent_name, self.file_name)
+
     def _checkForExistingFile(self, existing_file):
         """
         Check if a file exists already and if it does whether it's bigger or smaller than
@@ -524,7 +542,7 @@ def _find_info(self):
             lambda: self._analyze_name(self.file_name),
 
             # try to analyze the file path
-            lambda: self._analyze_name(self.file_path)
+            lambda: self._analyze_name(self.rel_path)
         ]
 
         # Try every possible method to get our info
@@ -742,7 +760,7 @@ def _get_quality(self, ep_obj):
                 return ep_quality
 
         # NZB name is the most reliable if it exists, followed by file name and lastly folder name
-        name_list = [self.nzb_name, self.file_name, self.folder_name]
+        name_list = [self.nzb_name, self.file_name, self.rel_path]
 
         for cur_name in name_list:
 


Test patch:
diff --git a/tests/legacy/feedparser_tests.py b/tests/legacy/feedparser_tests.py
--- a/tests/legacy/feedparser_tests.py
+++ b/tests/legacy/feedparser_tests.py
@@ -14,7 +14,6 @@ class FeedParserTests(unittest.TestCase):
     def test_womble(self):
         result = womble.cache.getRSSFeed(womble.urls['rss'], params={'sec': 'tv-sd', 'fr': 'false'})
         self.assertTrue('entries' in result)
-        self.assertTrue('feed' in result)
         for item in result['entries'] or []:
             title, url = womble._get_title_and_url(item)     # pylint: disable=protected-access
             self.assertTrue(title and url)
diff --git a/tests/legacy/pp_tests.py b/tests/legacy/pp_tests.py
--- a/tests/legacy/pp_tests.py
+++ b/tests/legacy/pp_tests.py
@@ -39,7 +39,7 @@ def test_init_file_name(self):
         self.assertEqual(self.post_processor.file_name, test.FILENAME)
 
     def test_init_folder_name(self):
-        self.assertEqual(self.post_processor.folder_name, test.SHOW_NAME)
+        self.assertEqual(self.post_processor.rel_path, test.REL_PATH)
 
 
 class PPBasicTests(test.AppTestDBCase):
diff --git a/tests/legacy/test_lib.py b/tests/legacy/test_lib.py
--- a/tests/legacy/test_lib.py
+++ b/tests/legacy/test_lib.py
@@ -50,6 +50,7 @@
 FILENAME = u"show name - s0" + str(SEASON) + "e0" + str(EPISODE) + ".mkv"
 FILE_DIR = os.path.join(TEST_DIR, SHOW_NAME)
 FILE_PATH = os.path.join(FILE_DIR, FILENAME)
+REL_PATH = os.path.join(os.path.basename(os.path.dirname(FILE_PATH)), FILENAME)
 SHOW_DIR = os.path.join(TEST_DIR, SHOW_NAME + " final")