Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed Mar 5, 2023
0 parents commit b43a784
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 Vladislav Zenkevich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.MD
include LICENSE
17 changes: 17 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1 align="center">Mintrans is a free API wrapper that utilizes Bing Translator for translation purposes.</h1>

<br>

<h1 align="center"> -How to use- </h1>

```python
from mintrans import BingTranslator

text = 'Hello World!'
from_lang = 'en'
to_lang = 'de'

translator = BingTranslator()
translation = translator.translate(text, from_lang, to_lang)
print(translation)
```
1 change: 1 addition & 0 deletions mintrans/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .mintrans import BingTranslator
43 changes: 43 additions & 0 deletions mintrans/mintrans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import requests
import re
import random

class BingTranslator:
def __init__(self):
self.session = self._get_session()

def _get_session(self):
session = requests.Session()
headers = {
'User-Agent': '',
'Referer': 'https://www.bing.com/translator'
}
session.headers.update(headers)
response = session.get('https://www.bing.com/translator')
content = response.text
params_pattern = re.compile(r'params_AbusePreventionHelper\s*=\s*(\[.*?\]);', re.DOTALL)
match = params_pattern.search(content)
if match:
params = match.group(1)
key, token, time = [p.strip('"').replace('[', '').replace(']', '') for p in params.split(',')]
session.headers.update({'key': key, 'token': token})
match = re.search(r'IG:"(\w+)"', content)
if match:
ig_value = match.group(1)
session.headers.update({'IG': ig_value})
return session

def translate(self, text, from_lang, to_lang):
url = f'https://www.bing.com/ttranslatev3?isVertical=1&&IG={self.session.headers.get("IG")}&IID=translator.{random.randint(5019, 5026)}.{random.randint(1, 3)}'
data = {'': '', 'fromLang': from_lang, 'text': text, 'to': to_lang, 'token': self.session.headers.get('token'), 'key': self.session.headers.get('key')}
response = self.session.post(url, data=data).json()
if type(response) is dict:
if 'ShowCaptcha' in response.keys():
self.session = self._get_session()
return self.translate(text, from_lang, to_lang)
elif 'statusCode' in response.keys():
if response['statusCode'] == 400:
response['errorMessage'] = f'1000 characters limit! You send {len(text)} characters.'
else:
return response[0]
return response
26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup, find_packages

with open("README.md", "r", encoding="utf-8") as f:
long_description = f.read()

setup(
name="mintrans",
version="1.0.0",
author="Maehdakvan",
author_email="visitanimation@google.com",
description="Mintrans is a free API wrapper that utilizes Bing Translator for translation purposes.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/DedInc/mintrans",
project_urls={
"Bug Tracker": "https://github.com/DedInc/mintrans/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
packages=find_packages(),
install_requires=['requests'],
python_requires='>=3.0'
)

0 comments on commit b43a784

Please sign in to comment.