translated.py 1.4 KB

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