diff --git a/README.md b/README.md index d4c406c7ebec984bcf4f13cbaf23d401e4be48f2..1f76b2a49531de92d530ff02e51a3bf22f3d8273 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Simple translator tool. Options: -h, --help Display this message + -u, --url Server URL of an instance -s, --source Source Language to translate -t, --target Target Language to translate -txt, --text Text to translate @@ -31,7 +32,10 @@ Options: -o, --output Output file translation result $ translate -s auto -t id -txt "How are You ?" - Apa kabar ? + Apa kabarmu ? + +$ translate -s auto -t id -txt "How are You ?" -u https://lingva.ml + Apa kabarmu ? $ translate -s auto -t id -txt "How are You ?" -o output.txt Translation result saved in output.txt diff --git a/pylingva/cli.py b/pylingva/cli.py index 3361adc54303c01dac4dd213a2fe6f1171fc9447..c74a94b359c90842b31c8e8a97ba0857fb7f5a7c 100644 --- a/pylingva/cli.py +++ b/pylingva/cli.py @@ -15,6 +15,7 @@ Simple translator tool. def translate(): arg = ArgumentParser(description=desc, allow_abbrev=False, add_help=False) arg.add_argument("-h", "--help", action="help", help="Display this message") + arg.add_argument("-u", "--url", type=str, help="Server URL of an instance") 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") @@ -22,9 +23,9 @@ def translate(): arg.add_argument("-f", "--file", help="Path file .txt to translate") arg.add_argument("-o", "--output", help="Output file translation result") args = arg.parse_args() - - translate = pylingva() - + + translate = pylingva(args.url) + if args.list_languages: lang = translate.languages() print("{:<25} {:<25}".format('Name', 'Code')) diff --git a/pylingva/pylingva.py b/pylingva/pylingva.py index 3aaacb42faa6151ba9bf017008a890c6d75c3ec8..bd259f09b10b148730481511bd11367631aa435e 100644 --- a/pylingva/pylingva.py +++ b/pylingva/pylingva.py @@ -1,11 +1,15 @@ #! /usr/bin/env python +import urllib.parse import requests as req class pylingva: - def __init__(self): + def __init__(self, server_url = None): url = "https://lingva.ml" + if server_url: + url = server_url + try: check_url = req.get(url) check_url.raise_for_status() @@ -32,6 +36,7 @@ class pylingva: return lang def translate(self, source, target, text): + text = urllib.parse.quote(text, safe='') url = f"{self.url}/{source}/{target}/{text}" url = url.replace("?", "%3F") r = req.get(url) diff --git a/setup.py b/setup.py index 3d47c1c59ffa28f12473c58b67d4d247615dbc9c..78d0ff9b9d537560c55d11d02be6641435be1250 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as des: setup( name='pylingva', packages=find_packages(), - version='0.1.2', + version='0.1.2.2', entry_points={'console_scripts': ['translate = pylingva.cli:translate']}, description='Simple translator using Lingva Translate API', url='https://gitlab.com/nesstero/pylingva',