Skip to content

Commit

Permalink
Merge pull request #11 from ppizarror/fix-goslate-ssl
Browse files Browse the repository at this point in the history
Fix goslate ssl
  • Loading branch information
ppizarror authored Feb 13, 2022
2 parents ba4b253 + 1ea9ac2 commit 700a228
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 12 deletions.
14 changes: 8 additions & 6 deletions PyMultiDictionary/_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
]

import re
import goslate
import ssl
import urllib.error
import PyMultiDictionary._goslate as goslate
import PyMultiDictionary._utils as ut

from bs4 import BeautifulSoup
Expand Down Expand Up @@ -130,17 +131,18 @@ def _bsoup(self, link: str, encoding: str = 'utf-8') -> Optional['BeautifulSoup'
if link in bs_keys:
return _CACHED_SOUPS[link]
if link in self._test_cached_file.keys():
f = open(self._test_cached_file[link], 'r')
f = open(self._test_cached_file[link], 'r', encoding='utf8')
data = ''.join(f.readlines())
f.close()
else:
try:
data = str(urlopen(link).read().decode(encoding))
data = str(urlopen(link, context=ssl.SSLContext()).read().decode(encoding))
except (urllib.error.HTTPError, ValueError):
return None
bs = BeautifulSoup(data, 'html.parser')
_CACHED_SOUPS[link] = bs
if len(bs_keys) >= self._max_cached_websites:
# noinspection PyTypeChecker
del _CACHED_SOUPS[bs[0]]
return bs

Expand All @@ -154,7 +156,7 @@ def _save_bsoup(self, link: str, filename: str, encoding: str = 'utf-8') -> None
"""
bs = self._bsoup(link, encoding)
html = str(bs.prettify())
with open(filename, 'w') as out:
with open(filename, 'w', encoding='utf8') as out:
out.write(html)

def _check_defined_lang(self) -> None:
Expand Down Expand Up @@ -417,8 +419,8 @@ def translate(self, lang: str, word: str, to: str = '', dictionary: str = DICT_E
gs = goslate.Goslate()
try:
return [(to, gs.translate(word, to, lang))]
except (urllib.error.HTTPError, IndexError):
warn(f'{word} cannot be translated to {to} as Google API is not available')
except (urllib.error.HTTPError, IndexError) as e:
warn(f'{word} cannot be translated to {to}-language as Google API is not available. Error: {e}')

if lang not in self._langs.keys() or not self._langs[lang][2]:
raise InvalidLangCode(f'{lang} code is not supported for translation')
Expand Down
Loading

0 comments on commit 700a228

Please sign in to comment.