Repository: pymedusa/Medusa

Issue:
Subtitle error
Im getting this quite some often:
```
2022-05-02 14:12:26 WARNING  FINDSUBTITLES :: [e4870f8] Unexpected error. Provider addic7ed
Traceback (most recent call last):
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 168, in reverse
    return self.from_symbol[code]
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 51, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'french (canadian)'
```
During handling of the above exception, another exception occurred:
```
Traceback (most recent call last):
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/subliminal/core.py", line 115, in list_subtitles_provider
    return self[provider].list_subtitles(video, provider_languages)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/medusa/subtitle_providers/addic7ed.py", line 285, in list_subtitles
    subtitles = [s for s in self.query(show_id, title, video.season, video.year)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/medusa/subtitle_providers/addic7ed.py", line 257, in query
    language = Language.fromaddic7ed(cells[3].text)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/language.py", line 110, in fromcode
    return cls(*language_converters[converter].reverse(code))
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/subliminal/converters/addic7ed.py", line 32, in reverse
    return self.name_converter.reverse(addic7ed)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 170, in reverse
    raise LanguageReverseError(code)
babelfish.exceptions.LanguageReverseError: 'French (Canadian)'
```
I dont even use/search for french ?


Discussion:
Any way i can reproduce this?
A specific show this error is thrown?
Any debug log?
Also please use the template to create the issue.
I don't know, I get it often, always from addic7ed.
```
2022-05-04 07:18:58 WARNING  FINDSUBTITLES :: [e4870f8] Unexpected error. Provider addic7ed'
```
This is my latest.

where is the debug log located?

Which template is that, I just pushed new issue and wrote there ?

Enable debug logging in config - general - Logs
Then restart medusa.
if you click on New Issue - bug report. You'll get a template that you need to fill in.
![image](https://user-images.githubusercontent.com/1331394/166666885-f9bb971d-a8a4-46db-94dc-1798d1c03e1f.png)

Already enabled it, but where do i find it :)

Medusa has a Logs folder in the program folder.
There you'll find application.log as older retentioned application.log.x files.
Cool, found this in log for the error:
``` 
2022-05-04 07:18:37 INFO     FINDSUBTITLES :: [e4870f8] Getting the page of show id 1175, season 11
2022-05-04 07:18:39 DEBUG    EPISODEUPDATER :: [e4870f8] Finished updating network timezones
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/1/168305/0' [en]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Ignoring subtitle with status 23.96% Completed
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/1/168305/1' [en]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/10/168305/2' [pt-BR]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/16/168305/3' [tr]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/7/168305/4' [it]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/17/168305/5' [nl]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/8/168305/6' [fr]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/8/168305/7' [fr]>
2022-05-04 07:18:58 DEBUG    FINDSUBTITLES :: [e4870f8] Found subtitle <Addic7edSubtitle 'updated/8/168305/8' [fr]>
2022-05-04 07:18:58 WARNING  FINDSUBTITLES :: [e4870f8] Unexpected error. Provider addic7ed
Traceback (most recent call last):
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 168, in reverse
    return self.from_symbol[code]
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 51, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'french (canadian)'
```
During handling of the above exception, another exception occurred:
```
Traceback (most recent call last):
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/subliminal/core.py", line 115, in list_subtitles_provider
    return self[provider].list_subtitles(video, provider_languages)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/medusa/subtitle_providers/addic7ed.py", line 285, in list_subtitles
    subtitles = [s for s in self.query(show_id, title, video.season, video.year)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/medusa/subtitle_providers/addic7ed.py", line 257, in query
    language = Language.fromaddic7ed(cells[3].text)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/language.py", line 110, in fromcode
    return cls(*language_converters[converter].reverse(code))
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/subliminal/converters/addic7ed.py", line 32, in reverse
    return self.name_converter.reverse(addic7ed)
  File "/share/CACHEDEV1_DATA/.qpkg/OMedusa/OMedusa/ext/babelfish/converters/__init__.py", line 170, in reverse
    raise LanguageReverseError(code)
babelfish.exceptions.LanguageReverseError: 'French (Canadian)'
2022-05-04 07:18:59 DEBUG    FINDSUBTITLES :: [e4870f8] Discarding provider addic7ed
2022-05-04 07:18:59 DEBUG    FINDSUBTITLES :: [e4870f8] Could not search in addic7ed provider. Discarding for now
2022-05-04 07:18:59 DEBUG    FINDSUBTITLES :: [e4870f8] Could not search in tvsubtitles provider. Discarding for now
```
I suspect this is for the show `the walking dead`.
I'll try to reproduce it. which languages do you have enabled?
Danish and English.
I'm not going to spend a lot of time on this. Yesterday I tried to reproduce. But I only got timeouts from addic7ed. We added a custom addic7ed provider, cause someone found a workaround for the logon. But it's really a hit and miss solution. Like when it works, nice. 
I suggest that this is a dupe of https://github.com/pymedusa/Medusa/issues/10344 which was just re-opened with a suggestion on how to improve the situation.

Resolving patch:
diff --git a/medusa/subtitle_providers/addic7ed.py b/medusa/subtitle_providers/addic7ed.py
--- a/medusa/subtitle_providers/addic7ed.py
+++ b/medusa/subtitle_providers/addic7ed.py
@@ -5,7 +5,8 @@
 import logging
 import re
 
-from babelfish import Language
+from babelfish import Language, language_converters
+from babelfish.exceptions import LanguageReverseError
 
 from guessit import guessit
 
@@ -21,7 +22,7 @@
 
 logger = logging.getLogger(__name__)
 
-# language_converters.register('addic7ed = subliminal.converters.addic7ed:Addic7edConverter')
+language_converters.register('addic7ed = medusa.subtitle_providers.converters.addic7ed:Addic7edConverter')
 
 # Series cell matching regex
 show_cells_re = re.compile(b'<td class="vr">.*?</td>', re.DOTALL)
@@ -131,6 +132,10 @@ def _get_show_ids(self):
         r = self.session.get(self.server_url + 'shows.php', timeout=20, cookies=self.cookies)
         r.raise_for_status()
 
+        if 'Log in' in r.text:
+            logger.warning('Failed to login, check your userid, password')
+            return {}
+
         # LXML parser seems to fail when parsing Addic7ed.com HTML markup.
         # Last known version to work properly is 3.6.4 (next version, 3.7.0, fails)
         # Assuming the site's markup is bad, and stripping it down to only contain what's needed.
@@ -200,6 +205,9 @@ def get_show_id(self, series, year=None, country_code=None):
         """
         series_sanitized = sanitize(series).lower()
         show_ids = self._get_show_ids()
+        if not show_ids:
+            show_ids = self._get_show_ids.invalidate()
+
         show_id = None
 
         # attempt with country
@@ -254,7 +262,12 @@ def query(self, show_id, series, season, year=None, country=None):
                 continue
 
             # read the item
-            language = Language.fromaddic7ed(cells[3].text)
+            try:
+                language = Language.fromaddic7ed(cells[3].text)
+            except LanguageReverseError as error:
+                logger.debug('Error trying to reverse map the language %s with error %r', cells[3].text, error)
+                continue
+
             hearing_impaired = bool(cells[6].text)
             page_link = self.server_url + cells[2].a['href'][1:]
             season = int(cells[0].text)
diff --git a/medusa/subtitle_providers/converters/addic7ed.py b/medusa/subtitle_providers/converters/addic7ed.py
new file mode 100644
--- /dev/null
+++ b/medusa/subtitle_providers/converters/addic7ed.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+"""Custom language converter for addic7ed."""
+from babelfish import LanguageReverseConverter, language_converters
+
+
+class Addic7edConverter(LanguageReverseConverter):
+    """Addic7ed Converter class."""
+
+    def __init__(self):
+        """Converter constructor."""
+        self.name_converter = language_converters['name']
+        self.from_addic7ed = {
+            'Català': ('cat',),
+            'Chinese (Simplified)': ('zho',),
+            'Chinese (Traditional)': ('zho',),
+            'Euskera': ('eus',),
+            'Galego': ('glg',),
+            'Greek': ('ell',),
+            'Malay': ('msa',),
+            'Portuguese (Brazilian)': ('por', 'BR'),
+            'Serbian (Cyrillic)': ('srp', None, 'Cyrl'),
+            'Serbian (Latin)': ('srp',),
+            'Spanish (Latin America)': ('spa',),
+            'Spanish (Spain)': ('spa',),
+            'French (Canadian)': ('fra', 'CA')
+        }
+        self.to_addic7ed = {
+            ('cat',): 'Català',
+            ('zho',): 'Chinese (Simplified)',
+            ('eus',): 'Euskera',
+            ('glg',): 'Galego',
+            ('ell',): 'Greek',
+            ('msa',): 'Malay',
+            ('por', 'BR'): 'Portuguese (Brazilian)',
+            ('srp', None, 'Cyrl'): 'Serbian (Cyrillic)',
+            ('fra', 'CA'): 'French (Canadian)'
+        }
+        self.codes = self.name_converter.codes | set(self.from_addic7ed.keys())
+
+    def convert(self, alpha3, country=None, script=None):
+        """Convert method."""
+        if (alpha3, country, script) in self.to_addic7ed:
+            return self.to_addic7ed[(alpha3, country, script)]
+        if (alpha3, country) in self.to_addic7ed:
+            return self.to_addic7ed[(alpha3, country)]
+        if (alpha3,) in self.to_addic7ed:
+            return self.to_addic7ed[(alpha3,)]
+
+        return self.name_converter.convert(alpha3, country, script)
+
+    def reverse(self, addic7ed):
+        """Reverse convert method."""
+        if addic7ed in self.from_addic7ed:
+            return self.from_addic7ed[addic7ed]
+
+        return self.name_converter.reverse(addic7ed)


Test patch: