deepl.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Deepl translation engine"""
  3. about = {
  4. "website": 'https://deepl.com',
  5. "wikidata_id": 'Q43968444',
  6. "official_api_documentation": 'https://www.deepl.com/docs-api',
  7. "use_official_api": True,
  8. "require_api_key": True,
  9. "results": 'JSON',
  10. }
  11. engine_type = 'online_dictionary'
  12. categories = ['general', 'translate']
  13. url = 'https://api-free.deepl.com/v2/translate'
  14. api_key = None
  15. def request(_query, params):
  16. '''pre-request callback
  17. params<dict>:
  18. - ``method`` : POST/GET
  19. - ``headers``: {}
  20. - ``data``: {} # if method == POST
  21. - ``url``: ''
  22. - ``category``: 'search category'
  23. - ``pageno``: 1 # number of the requested page
  24. '''
  25. params['url'] = url
  26. params['method'] = 'POST'
  27. params['data'] = {'auth_key': api_key, 'text': params['query'], 'target_lang': params['to_lang'][1]}
  28. return params
  29. def response(resp):
  30. results = []
  31. result = resp.json()
  32. if not result.get('translations'):
  33. return results
  34. translations = [{'text': translation['text']} for translation in result['translations']]
  35. results.append({'answer': translations[0]['text'], 'answer_type': 'translations', 'translations': translations})
  36. return results