From 66850b94b99f6ece853512b82a8fd14daac1b5a9 Mon Sep 17 00:00:00 2001 From: Moonrise <70655051+PetitPotiron@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:37:52 +0200 Subject: [PATCH] Delete convertio directory --- convertio/__init__.py | 1 - convertio/__version__.py | 7 -- convertio/client.py | 204 --------------------------------------- convertio/conversion.py | 17 ---- convertio/options.py | 96 ------------------ 5 files changed, 325 deletions(-) delete mode 100644 convertio/__init__.py delete mode 100644 convertio/__version__.py delete mode 100644 convertio/client.py delete mode 100644 convertio/conversion.py delete mode 100644 convertio/options.py diff --git a/convertio/__init__.py b/convertio/__init__.py deleted file mode 100644 index 421945b..0000000 --- a/convertio/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .client import * diff --git a/convertio/__version__.py b/convertio/__version__.py deleted file mode 100644 index 1de8f17..0000000 --- a/convertio/__version__.py +++ /dev/null @@ -1,7 +0,0 @@ -__title__ = 'convertio' -__description__ = 'An API wrapper for convertio.co' -__url__ = 'https:/github.com/PetitPotiron/python-convertio/' -__version__ = '1.0.0' -__author__ = 'PetitPotiron' -__license__ = 'GNU General Public License v3.0' -__copyright__ = 'Copyright (c) 2021 PetitPotiron' diff --git a/convertio/client.py b/convertio/client.py deleted file mode 100644 index d7189a5..0000000 --- a/convertio/client.py +++ /dev/null @@ -1,204 +0,0 @@ -from os import PathLike -import base64 -import urllib.request as request -import json -import time -import os.path as path -from .conversion import * -from .options import * -import urllib.error as weberror - - -class Client: - - def __init__(self, token) -> None: - self.token = token - - def convert_by_filename(self, fp: PathLike, output_format: str, output_filename: str = None, options: Options = None): - """Converts the file found in the path provided. - - Args: - filename (str): The file's path - output_format (str): The file format you want the file to be converted to - options (Options, optional): OCR Defaults to None. - """ - if output_filename is None: - filename = "".join(path.splitext(path.basename(fp))) - else: - filename = output_filename - if options is not None: - _options = { - 'ocr_enabled': True, - 'ocr_settings': { - 'langs': options.langs, - 'page_nums': options.page_nums - } - } - else: - _options = None - with open(fp, 'r') as file: - data = { - 'apikey': self.token, - 'input': 'base64', - 'file': base64.b64encode(file.read().encode('utf8')).decode('utf8'), - 'filename': filename, - 'outputformat': output_format, - 'options': _options - } - if data['options'] is None: - del data['options'] - else: - if data['options']['ocr_settings']['page_nums'] is None: - del data['options']['ocr_settings']['page_nums'] - - if data['options']['ocr_enabled'] is False: - del data['options'] - r = request.Request( - url='https://api.convertio.co/convert', - data=json.dumps(data).encode("utf8"), - method='POST', - ) - r.add_header('Content-Type', 'application/json') - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - return data['data']['id'] - except weberror.HTTPError as e: - if e.code == 401: - raise ValueError( - 'This API Key is invalid or there is no convertion minutes left') - elif e.code == 400: - raise ValueError("Field `options.langs` required to be non-empty array") - elif e.code == 422: - raise ValueError( - 'CONVERTER: The Type of Output File is not supported yet') - - def convert_by_url(self, url: str, output_format: str, options: Options = None): - """Converts the file found in the given url. - - Args: - url (str): The url where the file is - output_format (str): The file format you want the file to be converted to - options (Options, optional): OCR Options Defaults to None. - """ - data = { - 'apikey': self.token, - 'input': 'url', - 'file': url, - 'outputformat': output_format, - 'options': { - 'ocr_enabled': True if options else False, - 'ocr_settings': { - 'langs': options.langs, - 'page_nums': options.page_nums - } if options else None, - } if options else None - } - if data['options'] is None: - del data['options'] - else: - if data['options']['ocr_settings']['page_nums'] is None: - del data['options']['ocr_settings']['page_nums'] - - if data['options']['ocr_enabled'] is False: - del data['options'] - r = request.Request( - url='https://api.convertio.co/convert', - data=json.dumps(data).encode("utf8"), - method='POST', - ) - r.add_header('Content-Type', 'application/json') - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - return data['data']['id'] - except weberror.HTTPError as e: - if e.code == 401: - raise ValueError( - 'This API Key is invalid or there is no convertion minutes left') - elif e.code == 400: - raise ValueError( - "Field `options.langs` required to be non-empty array") - elif e.code == 422: - raise ValueError( - 'CONVERTER: The Type of Output File is not supported yet') - - def check_conversion(self, id: str): - """Checks the step of a conversion - - Args: - id (str) : The id of the conversion - - Returns: - Conversion: The status of the conversion - """ - r = request.Request(f'https://api.convertio.co/convert/{id}/status') - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - return Conversion(data) - except weberror.HTTPError as e: - if e.code == 404: - raise ValueError("The system is unable to find the requested action (the conversion isn't found)") - elif e.code == 422: - raise ValueError('Input file appears to be corrupted') - - def download(self, id: str, fp: PathLike): - """Writes the file content to a path.""" - r = request.Request(f'https://api.convertio.co/convert/{id}/status') - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - if data['status'] == 'error': - raise ValueError(data['error']) - data = Conversion(data) - r = request.Request(data.output_url) - r = request.urlopen(r) - - with open(fp, "wb") as file: - file.write(r.read()) - except weberror.HTTPError as e: - if e.code == 404: - raise ValueError("The system is unable to find the requested action (the conversion isn't found)") - elif e.code == 422: - raise ValueError('Input file appears to be corrupted') - except ValueError as e: - raise ValueError("The url isn't available yet") - - def delete(self, id: str): - """Deletes or cancels a conversion""" - r = request.Request( - url='https://api.convertio.co/convert/' + id, - method='DELETE', - ) - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - except weberror.HTTPError as e: - if e.code == 404: - raise ValueError("File not found") - - def cancel(self, id: str): - """Cancels a conversion""" - self.delete(id) - - def conversions_list(self, status='all', count=1): - """Gets the list of the client's conversions (count=-1 for all the conversions).""" - data = { - 'apikey': self.token, - 'status': status, - 'count': count - } - - r = request.Request(url=f'https://api.convertio.co/convert/list', - method='POST', - data=json.dumps(data).encode('utf8')) - try: - r = request.urlopen(r) - data = json.loads(r.read().decode('utf8')) - return data['data'] - except weberror.HTTPError as e: - if e.code == 400: - raise ValueError('Incorrect status value') - elif e.code == 401: - raise ValueError('This API Key is invalid') diff --git a/convertio/conversion.py b/convertio/conversion.py deleted file mode 100644 index daedd11..0000000 --- a/convertio/conversion.py +++ /dev/null @@ -1,17 +0,0 @@ -class Conversion: - def __init__(self, data) -> None: - self.code = data['code'] - self.status = data['status'] - self.id = data['data']['id'] - self.step = data['data']['step'] - self.step_percent = data['data']['step_percent'] - self.minutes = data['data']['minutes'] - try: - self.output_url = data['data']['output']['url'] - self.output_size = data['data']['output']['size'] - except: - self.output_url = "Url not available yet" - self.output_size = None - - def __repr__(self): - return f' {self.status} | {self.step} - {self.step_percent} % \n {self.output_url}' diff --git a/convertio/options.py b/convertio/options.py deleted file mode 100644 index 096f9ee..0000000 --- a/convertio/options.py +++ /dev/null @@ -1,96 +0,0 @@ -def lang_code(language): - lang_codes = { - 'afrikaans': 'afr', - 'albanian': 'sqi', - 'arabic': 'ara', - 'armenian-eastern': 'arm_east', - 'armenian-western': 'arm_west', - 'azeri-cyrillic': 'aze_cyrl', - 'azeri-latin': 'aze', - 'basque': 'eus', - 'belarusian': 'bel', - 'bulgarian': 'bul', - 'catalan': 'cat', - 'cebuano': 'ceb', - 'chinese-simplified': 'chi_sim', - 'chinese-traditional': 'chi_tra', - 'croatian': 'hrv', - 'czech': 'ces', - 'danish': 'dan', - 'dutch': 'dut', - 'dutch-belgian': 'nld', - 'english': 'eng', - 'esperanto': 'epo', - 'estonian': 'est', - 'fijian': 'fij', - 'finnish': 'fin', - 'french': 'fra', - 'galician': 'glg', - 'german': 'deu', - 'greek': 'grk', - 'hawaiian': 'haw', - 'hebrew': 'heb', - 'hungarian': 'hun', - 'icelandic': 'isl', - 'indonesian': 'ind', - 'irish': 'gle', - 'italian': 'ita', - 'japanese': 'jpn', - 'kazakh': 'kaz', - 'kirghiz': 'kir', - 'kongo': 'jon', - 'korean': 'kor', - 'kurdish': 'kur', - 'latin': 'lat', - 'latvian': 'lav', - 'lithuanian': 'lit', - 'macedonian': 'mkd', - 'malay-malaysian': 'mal', - 'maltese': 'mlt', - 'norwegian-bokmal': 'nor', - 'polish': 'pol', - 'portuguese': ' por', - 'portuguese-brazilian': 'bra', - 'romanian': ' ron', - 'russian': 'rus', - 'scottish': 'sco', - 'serbian-cyrillic': 'srp', - 'serbian-latin': 'srp_latn', - 'slovak': 'slk', - 'slovenian': 'slv', - 'somali': 'som', - 'spanish': 'spa', - 'swahili': 'swa', - 'swedish': 'swe', - 'tagalog': 'tgl', - 'tahitian': 'tah', - 'tajik': 'tgk', - 'tatar': 'tat', - 'thai': 'tha', - 'turkish': 'tur', - 'turkmen': 'turk', - 'uighur-cyrillic': 'uig_cyr', - 'uighur-latin': 'uig', - 'ukrainian': 'ukr', - 'uzbek-cyrillic': 'uzb_cyrl', - 'uzbek-latin': 'uzb', - 'vietnamese': 'vie', - 'welsh': 'cym' - } - if not language.lower() in lang_codes.values(): - return lang_codes[language.lower()] - else: - return language - - -class Lang: - def __init__(self, language: str): - self.code = lang_code(language) - - -class Options: - def __init__(self, langs: list, page_nums: str = None) -> None: - self.langs = [] - for lang in langs: - self.langs.append(lang.code) - self.page_nums = page_nums