commit 22b9c7f80405271865bb1c6e2e97db503307a34e Author: Nestero Date: Wed Jan 5 06:28:35 2022 +0700 pylingva diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a5e675 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/pylingva/__pycache__/* +/build/* +/dist/* +/pylingva.egg-info/* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a317cda --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Nestero + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..17cb31d --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# Pylingva +Simple translator using [Lingva Translate](https://lingva.ml/) API + +# Install +``` +pip install pylingva +``` + +# Usage +## CLI +```shell +$ translate -h +usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll] + +options: + -h, --help show this help message and exit + -s SOURCE, --source SOURCE + Source Language to translate + -t TARGET, --target TARGET + Target Language to translate + -txt TEXT, --text TEXT + Text to translate + -ll, --list-languages + List Languages support + +$ translate -s auto -t id -txt "How are You ?" + Apa kabar ? +``` +## Python +### List languages +```python +from pylingva import pylingva +trans = pylingva() +list_languages = trans.languages() +print(list_languages) +``` +**Output:** +``` +{'Detect': 'auto', 'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu', 'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chichewa': 'ny', 'Chinese': 'zh', 'Chinese (Traditional)': 'zh_HANT', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en', 'Esperanto': 'eo', 'Estonian': 'et', 'Filipino': 'tl', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl', 'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw', 'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id', 'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km', 'Kinyarwanda': 'rw', 'Korean': 'ko', 'Kurdish (Kurmanji)': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt', 'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi', 'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Odia (Oriya)': 'or', 'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru', 'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala': 'si', 'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv', 'Tajik': 'tg', 'Tamil': 'ta', 'Tatar': 'tt', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Turkmen': 'tk', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo', 'Zulu': 'zu'} +``` + +### Translate +```python +from pylingva import pylingva +trans = pylingva() +txt = "Halo apa kabar ?" +translate = trans.translate("auto", "en", txt) +print(translate) +``` +**Output**: +``` +Hello, how are you ? +``` diff --git a/pylingva/__init__.py b/pylingva/__init__.py new file mode 100644 index 0000000..9b1e614 --- /dev/null +++ b/pylingva/__init__.py @@ -0,0 +1,2 @@ +#! /usr/bin/env python +from .pylingva import * diff --git a/pylingva/cli.py b/pylingva/cli.py new file mode 100644 index 0000000..adda6c1 --- /dev/null +++ b/pylingva/cli.py @@ -0,0 +1,24 @@ +#! /usr/bin/env python + +from pylingva import pylingva +import argparse + +def translate(): + translate = pylingva() + arg = argparse.ArgumentParser() + arg.add_argument("-s", "--source", type=str, help="Source Language to translate") + arg.add_argument("-t", "--target", type=str, help="Target Language to translate") + arg.add_argument("-txt", "--text", type=str, help="Text to translate") + arg.add_argument("-ll", "--list-languages", help="List Languages support", action="store_true") + args = arg.parse_args() + + if args.list_languages: + lang = translate.languages() + print("{:<25} {:<25}".format('Name', 'Code')) + for key, value in lang.items(): + x = key + y = value + print("{:<25} {:<25}".format(x, y)) + else: + result = translate.translate(args.source, args.target, args.text) + print(result) diff --git a/pylingva/pylingva.py b/pylingva/pylingva.py new file mode 100644 index 0000000..eb158de --- /dev/null +++ b/pylingva/pylingva.py @@ -0,0 +1,42 @@ +#! /usr/bin/env python + +import requests as req + +class pylingva: + + def __init__(self): + self.url_list = ("https://lingva.ml", "https://translate.alefvanoon.xyz", "https://translate.igna.rocks", "https://lingva.pussthecat.org") + for url in self.url_list: + check_url = req.get(url) + status = check_url.status_code + if status == 200: + self.url = url + "/api/v1/" + break + else: + self.url = 0 + + def languages(self): + if self.url == 0: + print("Server Down !") + else: + url = self.url + "languages" + all_languages = req.get(url) + all_languages = all_languages.json() + lang = {} + for x in range(0, len(all_languages['languages'])): + list_code = all_languages['languages'][x]['code'] + list_name = all_languages['languages'][x]['name'] + lang.update({list_name: list_code}) + return lang + + def translate(self, source, target, text): + if self.url == 0: + print("Server Down !") + else: + url = f"{self.url}/{source}/{target}/{text}" + url = url.replace("?", "%3F") + r = req.get(url) + r = r.json() + result = r['translation'] + return result + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8ae4e7c --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +from setuptools import find_packages, setup + +with open("README.md", "r") as des: + l_desc = des.read() + +setup( + name='pylingva', + packages=find_packages(), + version='0.1.0', + entry_points={'console_scripts': ['translate = pylingva.cli:translate']}, + description='Simple translator using Lingva Translate API', + url='https://gitlab.com/nesstero/pylingva', + license='MIT', + author='nestero', + author_email='nestero@mail.com', + long_description=l_desc, + long_description_content_type='text/markdown', +)