Repository: pymedusa/Medusa

Issue:
Guessit issues with parsing releases from animetorrents
### Before submitting your issue:

Enable debug logging in Medusa settings, reproduce the error (be sure to disable after the bug is fixed)

**Branch/Commit: 7083ed5028ecfa0388d5229ab75fa5a9d86831d5**
**OS: windows 7**
**What you did: Searched for show Naruto**
**What happened: Provider is giving back results, but the releases are not parsed correctly.**
**What you expected: Parsed titles.**
**Logs:**
```
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=Naruto+Shippuuden%25476&searchin=filedisc&cat=2&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Data returned from provider does not contain any torrents
2016-12-07 22:48:11 INFO     SEARCHQUEUE-DAILY-SEARCH :: [ad18671] Using daily search providers
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [AnimeTorrents] :: [ad18671] Last update was too soon, using old cache: 2016-12-07 22:40:18. Updated less then 10 minutes ago
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [AnimeTorrents] :: [ad18671] Transaction with 5 queries executed
2016-12-07 22:48:11 INFO     SEARCHQUEUE-DAILY-SEARCH :: [ad18671] No needed episodes found
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=Naruto+Shippuuden%25476&searchin=filedisc&cat=7&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Found result: Naruto Shippuuden - 476-479 (2007) [HorribleSubs][WEBRip]  [HD 720p] with 12 seeders and 2 leechers
2016-12-07 22:48:11 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Search string: NARUTO 疾風伝%476
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=NARUTO+%E7%96%BE%E9%A2%A8%E4%BC%9D%25476&searchin=filedisc&cat=2&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Data returned from provider does not contain any torrents
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=NARUTO+%E7%96%BE%E9%A2%A8%E4%BC%9D%25476&searchin=filedisc&cat=7&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Data returned from provider does not contain any torrents
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Search string: Naruto Shippuden%476
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=Naruto+Shippuden%25476&searchin=filedisc&cat=2&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:12 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Data returned from provider does not contain any torrents
2016-12-07 22:48:13 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] GET URL: http://animetorrents.me/ajax/torrents_data.php?search=Naruto+Shippuden%25476&searchin=filedisc&cat=7&SearchSubmit=&total=1&page=1 [Status: 200]
2016-12-07 22:48:13 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Data returned from provider does not contain any torrents
2016-12-07 22:58:07 WARNING  DAILYSEARCHER :: [ad18671] Manual search is running. Can't start Daily search
2016-12-07 22:59:43 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [AnimeTorrents] :: [ad18671] Unable to match Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p] to a show in your database. Parser result: title: Naruto Shippuuden, alias: Naruto Shippuuden - 476-479, alternative_title: 476-479, year: 2007, release_group: HorribleSubs, format: WEBRip, other: HD, screen_size: 720p, type: episode, parsing_time: 0.214999914169, total_time: 682.592000008, absolute_episode: [], quality: 720p WEB-DL, episode: [], season: None
2016-12-07 22:59:43 DEBUG    SEARCHQUEUE-MANUAL-79824 :: [ad18671] 79824: Formatting pattern: %SN - S%0SE%0E - %EN -> Naruto Shippuden - S22E18 - The Final Battle
2016-12-07 22:59:43 INFO     SEARCHQUEUE-MANUAL-79824 :: [ad18671] Unable to find manual results for: [Naruto Shippuden - S22E18 - The Final Battle]
```
'Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]'

Report guessit:
```
D:\medusa\tests>python report_guessit.py 'Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]'
# guessit: 2.1.1.dev0  rebulk: 0.8.3.dev0
? 'Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]'
: title: 'Naruto Shippuuden
  alias: 'Naruto Shippuuden - 476-479
  alternative_title: 476-479
  year: 2007
  format: WEBRip
  other: HD
  screen_size: 720p
  type: episode
  parsing_time: 0.0829999446869
```

What i expect:
```
D:\medusa\tests>python report_guessit.py 'Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]'
# guessit: 2.1.1.dev0  rebulk: 0.8.3.dev0
? 'Naruto.Shippuuden.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]'
: title: 'Naruto Shippuuden
  alias: ''
  alternative_title: ''
  year: 2007
  format: WEBRip
  other: HD
  screen_size: 720p
  type: episode
  absolute_season: 1
  absolute_episode: [476, 476, 478, 479]
  parsing_time: 0.0829999446869
```

Not totally sure if guessit should report it like that.
Those eps are actually absolute episodes, but don't know if guessit can guess that.


Discussion:
A log with some more parse results.
Allot of them are not parsed correct:
https://gist.github.com/p0psicles/f5350eba0b78c2b8c085ab6a24b07346

And then i'm mostly interested in the titles with episode like:
```Joker.Game.-.07.(2016).[RH].[English.Dubbed][WEBRip]..[HD.1080p] to a show in your database. Parser result: title: Joker Game, alias: Joker Game - 07, alternative_title: 07, year: 2016, release_group: RH, language: en, format: WEBRip, other: HD, screen_size: 1080p, type: episode, parsing_time: 0.191999912262, total_time: 0.464999914169, absolute_episode: [], quality: 1080p WEB-DL, episode: [], season: None```
```
? 'Joker.Game.-.07.(2016).[RH].[English.Dubbed][WEBRip]..[HD.1080p]'
: title: 'Joker Game
  alternative_title: 07
  year: 2016
  language: en
  format: WEBRip
  other: HD
  screen_size: 1080p
  type: episode
  parsing_time: 0.0600001811981
```
Should be:
```
  title: 'Joker Game'
  alternative_title: ''
  year: 2016
  language: en
  format: WEBRip
  other: HD
  screen_size: 1080p
  type: episode
  absolute_season: 01
  absolute_episode: 07
  release_group: RH
  parsing_time: 0.0600001811981
```

```? 'Hibike!.Euphonium.2.-.10.(2016).[HorribleSubs][WEBRip]..[HD.720p]'
: title: 'Hibike! Euphonium 2
  alias: 'Hibike! Euphonium 2 - 10
  alternative_title: 10
  year: 2016
  format: WEBRip
  other: HD
  screen_size: 720p
  type: episode
  parsing_time: 0.0599999427795
```

Should be: 
```
? 'Hibike!.Euphonium.2.-.10.(2016).[HorribleSubs][WEBRip]..[HD.720p]'
: title: 'Hibike! Euphonium 2
  alias: ''
  alternative_title: ''
  year: 2016
  format: WEBRip
  other: HD
  screen_size: 720p
  type: episode
  release_group: HorribleSubs
  absolute_season: 01
  absolute_episode: 10
  parsing_time: 0.0599999427795```

Resolving patch:
diff --git a/medusa/name_parser/rules/rules.py b/medusa/name_parser/rules/rules.py
--- a/medusa/name_parser/rules/rules.py
+++ b/medusa/name_parser/rules/rules.py
@@ -602,7 +602,7 @@ class FixInvalidTitleOrAlternativeTitle(Rule):
 
     priority = POST_PROCESS
     consequence = [RemoveMatch, AppendMatch]
-    absolute_re = re.compile(r'([\W|_]*)(?P<absolute_episode_start>\d{3,4})\-(?P<absolute_episode_end>\d{3,4})\W*$')
+    absolute_re = re.compile(r'([\W|_]*)(?P<absolute_episode_start>\d{2,4})(?:-(?P<absolute_episode_end>\d{3,4}))?\W*$')
     properties = ('title', 'alternative_title', 'episode_title')
 
     def when(self, matches, context):
@@ -614,10 +614,6 @@ def when(self, matches, context):
         :type context: dict
         :return:
         """
-        episodes = matches.named('episode')
-        if not episodes:
-            return
-
         fileparts = matches.markers.named('path')
         for filepart in marker_sorted(fileparts, matches):
             # retrieve all problematic titles
@@ -626,6 +622,7 @@ def when(self, matches, context):
 
             to_remove = []
             to_append = []
+            episodes = matches.named('episode')
 
             for title in problematic_titles:
                 m = self.absolute_re.search(title.raw)
@@ -647,9 +644,12 @@ def when(self, matches, context):
                 # and add the absolute episode range
                 g = m.groupdict()
                 absolute_episode_start = int(g['absolute_episode_start'])
-                absolute_episode_end = int(g['absolute_episode_end'])
+                if not g['absolute_episode_end'] and title.name != 'alternative_title':
+                    continue
+
+                absolute_episode_end = int(g['absolute_episode_end'] or g['absolute_episode_start'])
                 for i in range(absolute_episode_start, absolute_episode_end + 1):
-                    episode = copy.copy(episodes[0])
+                    episode = copy.copy(title)
                     episode.name = 'absolute_episode'
                     episode.value = i
                     if i == absolute_episode_start:
@@ -661,8 +661,14 @@ def when(self, matches, context):
                     else:
                         episode.start = title.start + m.start('absolute_episode_end')
                         episode.end = title.start + m.end('absolute_episode_end')
+
                     to_append.append(episode)
 
+                    if not episodes:
+                        ep = copy.copy(episode)
+                        ep.name = 'episode'
+                        to_append.append(ep)
+
                 return to_remove, to_append
 
 


Test patch:
diff --git a/tests/test_guessit.py b/tests/test_guessit.py
--- a/tests/test_guessit.py
+++ b/tests/test_guessit.py
@@ -38,6 +38,7 @@ def show_list(create_tvshow):
         create_tvshow(indexerid=16, name='3 Show på (abc2)'),  # unicode characters, numbers and parenthesis
         create_tvshow(indexerid=16, name="Show '70s Name"),
         create_tvshow(indexerid=17, name='An Anime Show 100', anime=1),
+        create_tvshow(indexerid=17, name='Show! Name 2', anime=1),
         create_tvshow(indexerid=18, name='24'),  # expected titles shouldn't contain numbers
     ]
 
diff --git a/tests/test_guessit.yml b/tests/test_guessit.yml
--- a/tests/test_guessit.yml
+++ b/tests/test_guessit.yml
@@ -4022,3 +4022,37 @@
   container: mkv
   mimetype: video/x-matroska
   type: episode
+
+? Show.Name.-.476-479.(2007).[HorribleSubs][WEBRip]..[HD.720p]
+: title: Show Name
+  absolute_episode: [476, 477, 478, 479]
+  episode: [476, 477, 478, 479]
+  year: 2007
+  release_group: HorribleSubs
+  format: WEBRip
+  other: HD
+  screen_size: 720p
+  type: episode
+
+? Show.Name.-.07.(2016).[RH].[English.Dubbed][WEBRip]..[HD.1080p]
+: title: Show Name
+  absolute_episode: 7
+  episode: 7
+  year: 2016
+  release_group: RH
+  language: en
+  format: WEBRip
+  other: HD
+  screen_size: 1080p
+  type: episode
+
+? Show!.Name.2.-.10.(2016).[HorribleSubs][WEBRip]..[HD.720p]
+: title: Show! Name 2
+  absolute_episode: 10
+  episode: 10
+  year: 2016
+  release_group: HorribleSubs
+  format: WEBRip
+  other: HD
+  screen_size: 720p
+  type: episode