translated.py 1.5 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_dictionnary'
  15. categories = ['general']
  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(from_lang=params['from_lang'][1],
  27. to_lang=params['to_lang'][1],
  28. query=params['query'],
  29. key=key_form)
  30. return params
  31. def response(resp):
  32. results = []
  33. results.append({
  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. 'title': '[{0}-{1}] {2}'.format(
  39. resp.search_params['from_lang'][1],
  40. resp.search_params['to_lang'][1],
  41. resp.search_params['query']),
  42. 'content': resp.json()['responseData']['translatedText']
  43. })
  44. return results