translated.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """MyMemory Translated
  3. """
  4. # about
  5. about = {
  6. "website": 'https://mymemory.translated.net/',
  7. "wikidata_id": None,
  8. "official_api_documentation": 'https://mymemory.translated.net/doc/spec.php',
  9. "use_official_api": True,
  10. "require_api_key": False,
  11. "results": 'JSON',
  12. }
  13. engine_type = 'online_dictionary'
  14. categories = ['general', 'translate']
  15. url = 'https://api.mymemory.translated.net/get?q={query}&langpair={from_lang}|{to_lang}{key}'
  16. web_url = 'https://mymemory.translated.net/en/{from_lang}/{to_lang}/{query}'
  17. weight = 100
  18. https_support = True
  19. api_key = ''
  20. def request(query, params): # pylint: disable=unused-argument
  21. if api_key:
  22. key_form = '&key=' + api_key
  23. else:
  24. key_form = ''
  25. params['url'] = url.format(
  26. from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query'], key=key_form
  27. )
  28. return params
  29. def response(resp):
  30. results = []
  31. results.append(
  32. {
  33. 'url': web_url.format(
  34. from_lang=resp.search_params['from_lang'][2],
  35. to_lang=resp.search_params['to_lang'][2],
  36. query=resp.search_params['query'],
  37. ),
  38. 'title': '[{0}-{1}] {2}'.format(
  39. resp.search_params['from_lang'][1], resp.search_params['to_lang'][1], resp.search_params['query']
  40. ),
  41. 'content': resp.json()['responseData']['translatedText'],
  42. }
  43. )
  44. return results