deepl.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # e code included in this file was originally inspired by https://github.com/KeKsBoTer/deepl-cli/blob/master/deepl.py
  2. # Sadly I could find no license file attached to this
  3. import requests
  4. import time
  5. import json
  6. from datetime import datetime
  7. from translatepy.translators.deepl import DeeplTranslate
  8. class DeeplEngine:
  9. name = "deepl"
  10. display_name = "DeepL (Testing)"
  11. def __init__(self):
  12. self.session = requests.Session()
  13. self.delay_begin = None
  14. self.deepl = DeeplTranslate()
  15. async def get_supported_source_languages(self):
  16. return {"Autodetect": "auto", **await self.get_supported_target_languages()}
  17. async def get_supported_target_languages(self):
  18. return {
  19. "Bulgarian": "BG",
  20. "Chinese": "ZH",
  21. "Czech": "CS",
  22. "Danish": "DA",
  23. "Dutch": "NL",
  24. "English": "EN",
  25. "Estonian": "ET",
  26. "Finnish": "FI",
  27. "French": "FR",
  28. "German": "DE",
  29. "Greek": "EL",
  30. "Hungarian": "HU",
  31. "Italion": "IT",
  32. "Japanese": "JA",
  33. "Latvian": "LV",
  34. "Lithuanian": "LT",
  35. "Polish": "PL",
  36. "Portugese": "PT",
  37. "Romanian": "RO",
  38. "Russian": "RU",
  39. "Slovak": "SK",
  40. "Slovenian": "SL",
  41. "Spanish": "ES",
  42. "Swedish": "SV",
  43. }
  44. async def detect_language(self, text: str):
  45. return None
  46. async def get_tts(self, text: str, language: str):
  47. return None
  48. async def translate(self, text: str, to_language: str, from_language: str="auto"):
  49. # NOTE: this takes insanely long to process
  50. try:
  51. translation = self.deepl.translate(text, to_language, from_language)
  52. except Exception as e:
  53. translation = "Failed to translate! Sorry!"
  54. return {
  55. "translated-text": translation,
  56. "source_language": from_language
  57. }
  58. async def test():
  59. e = DeeplEngine()
  60. print(
  61. await asyncio.gather(
  62. e.translate("Hallo", "en", "de"),
  63. e.translate("Bonjour", "en", "fr"),
  64. e.translate("Hola", "en", "de"),
  65. )
  66. )
  67. if __name__ == "__main__":
  68. import asyncio
  69. loop = asyncio.get_event_loop()
  70. loop.run_until_complete(test())