Skip to content

Commit

Permalink
🍎 Update 1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideGalilei authored Feb 4, 2023
2 parents ffec836 + 2194bef commit c0f59a3
Show file tree
Hide file tree
Showing 26 changed files with 113 additions and 100 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ jobs:

strategy:
matrix:
version: [ "3.6", "3.7", "3.8", "3.9" ]
version: [ "3.7", "3.8", "3.9", "3.10", "3.11" ]

steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
with:
python-version: "${{ matrix.version }}"
- name: "Install Gpytranslate"
run: python setup.py install
run: python -m pip install .
- name: "Install pytest"
run: python -m pip install pytest-asyncio
- name: "Run tests"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/venv/
/.idea/
/gpytranslate.egg-info/
**/__pycache__
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A Python3 library for translating text using Google Translate API.

### Installation
Requirements:
- Python 3.6 or higher.
- Python 3.7 or higher.


```bash
Expand Down Expand Up @@ -87,6 +87,7 @@ with open("test.mp3", "wb") as file:
## Useful Resources
https://danpetrov.xyz/programming/2021/12/30/telegram-google-translate.html
https://vielhuber.de/en/blog/google-translation-api-hacking/
https://github.com/OwlGramDev/OwlGram/blob/b9bb8a247758adbf7be7aaf3eb150f680bec1269/TMessagesProj/src/main/java/it/owlgram/android/translator/GoogleAppTranslator.java

----
## Development
Expand Down
2 changes: 1 addition & 1 deletion examples/async/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/async/https_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/async/socks5_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/async/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/sync/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/sync/https_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/sync/socks5_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion gpytranslate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .types import TranslatedObject
from .exceptions import GpytranslateException, TranslationError

__version__ = "1.4.0"
__version__ = "1.5.1"
__all__ = [
"Translator",
"SyncTranslator",
Expand Down
14 changes: 10 additions & 4 deletions gpytranslate/gpytranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,15 +31,21 @@
from aiofiles.threadpool import AsyncBufferedIOBase

from .exceptions import TranslationError
from .types import TranslatedObject, BaseTranslator, get_base_headers
from .types import (
TranslatedObject,
BaseTranslator,
get_base_headers,
DEFAULT_TRANSLATION_ENDPOINT,
DEFAULT_TTS_ENDPOINT,
)


class Translator(BaseTranslator):
def __init__(
self,
proxies: Dict[str, str] = None,
url: str = "https://translate.googleapis.com/translate_a/single",
tts_url: str = "https://translate.google.com/translate_tts",
url: str = DEFAULT_TRANSLATION_ENDPOINT,
tts_url: str = DEFAULT_TTS_ENDPOINT,
headers: Union[dict, callable] = ...,
**options
):
Expand Down
14 changes: 10 additions & 4 deletions gpytranslate/sync/sync_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -29,15 +29,21 @@
import httpx

from ..exceptions import TranslationError
from ..types import TranslatedObject, BaseTranslator, get_base_headers
from ..types import (
TranslatedObject,
BaseTranslator,
get_base_headers,
DEFAULT_TRANSLATION_ENDPOINT,
DEFAULT_TTS_ENDPOINT,
)


class SyncTranslator(BaseTranslator):
def __init__(
self,
proxies: Dict[str, str] = None,
url: str = "https://translate.googleapis.com/translate_a/single",
tts_url: str = "https://translate.google.com/translate_tts",
url: str = DEFAULT_TRANSLATION_ENDPOINT,
tts_url: str = DEFAULT_TTS_ENDPOINT,
headers: Union[dict, callable] = ...,
**options
):
Expand Down
50 changes: 37 additions & 13 deletions gpytranslate/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,44 @@
from .translated_object import TranslatedObject
from .base_translator import BaseTranslator

USER_AGENTS: tuple = (
"GoogleTranslate/6.6.1.RC09.302039986 (Linux; U; Android 9; Redmi Note 8)",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36",
)
DEFAULT_TRANSLATION_ENDPOINT: str = "https://translate.google.com/translate_a/single"
DEFAULT_TTS_ENDPOINT: str = "https://translate.google.com/translate_tts"


class Device:
DEVICES: tuple = (
"Linux; U; Android 10; Pixel 4",
"Linux; U; Android 10; Pixel 4 XL",
"Linux; U; Android 10; Pixel 4a",
"Linux; U; Android 10; Pixel 4a XL",
"Linux; U; Android 11; Pixel 4",
"Linux; U; Android 11; Pixel 4 XL",
"Linux; U; Android 11; Pixel 4a",
"Linux; U; Android 11; Pixel 4a XL",
"Linux; U; Android 11; Pixel 5",
"Linux; U; Android 11; Pixel 5a",
"Linux; U; Android 12; Pixel 4",
"Linux; U; Android 12; Pixel 4 XL",
"Linux; U; Android 12; Pixel 4a",
"Linux; U; Android 12; Pixel 4a XL",
"Linux; U; Android 12; Pixel 5",
"Linux; U; Android 12; Pixel 5a",
"Linux; U; Android 12; Pixel 6",
"Linux; U; Android 12; Pixel 6 Pro",
)

__i = 0

@classmethod
def shift(cls) -> str:
cls.__i += 1
cls.__i %= len(cls.DEVICES)
return cls.DEVICES[cls.__i]


def get_base_headers() -> dict:
BASE_HEADERS: dict = {
"User-Agent": random.choice(USER_AGENTS)
return {
"User-Agent": "GoogleTranslate/6.28.0.05.421483610 ({device})".format(
device=Device.shift(),
)
}

return BASE_HEADERS
2 changes: 1 addition & 1 deletion gpytranslate/types/base_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion gpytranslate/types/translated_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "gpytranslate"
version = "1.5.1"
dependencies = [
"httpx",
"aiofiles",
]

authors = [
{ name="Davide Galilei", email="davidegalilei2018@gmail.com" },
]
maintainers = [
{ name="Davide Galilei", email="davidegalilei2018@gmail.com" },
]

description = "A Python3 library for translating text using Google Translate API."
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/DavideGalilei/gpytranslate"
"Bug Tracker" = "https://github.com/DavideGalilei/gpytranslate/issues"
56 changes: 0 additions & 56 deletions setup.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/async/test_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion tests/async/test_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -46,7 +46,7 @@ async def test_translate_source():
"Ciao.", sourcelang="it", targetlang="en"
)

assert translation.text == "Hello.", "Translations are not equal."
assert translation.text in ("Hello.", "HI."), "Translations are not equal."


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/async/test_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2022 Davide Galilei
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit c0f59a3

Please sign in to comment.