Skip to content

Commit

Permalink
Merge pull request #7 from bapehnkk/main
Browse files Browse the repository at this point in the history
Changed the reading of the downloaded file to binary.
  • Loading branch information
PetitPotiron authored Dec 3, 2023
2 parents 8b459c2 + 963c590 commit 53345e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ client.delete(conversion_id)

## Notes

Version `1.1.2`
Version `1.2.0`

This module is neither created nor managed by convertio.co. It represents services faithful to the API and documentation from their website. This API is not unofficial, and it can be payable if you exceed a certain usage. Notice that you must respect convertio.co's [terms of use](https://convertio.co/terms).

Expand All @@ -40,4 +40,5 @@ This module is neither created nor managed by convertio.co. It represents servic
- [CLI for convertio.co, by themselves](https://developers.convertio.co/cli)

### Changelog
- [Fix issue #1 (typing does not work on 3.8 and lower)](https://github.com/PetitPotiron/python-convertio/commit/3faddfac11d2d0e055c5b181041fd9d0587c3f46)
- Fixed error when trying to upload a file of a different format than .txt. It used to be impossible. Added usage example
- [Fix issue #1 (typing does not work on 3.8 and lower)](https://github.com/PetitPotiron/python-convertio/commit/3faddfac11d2d0e055c5b181041fd9d0587c3f46)
5 changes: 2 additions & 3 deletions convertio/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
__title__ = 'convertio'
__description__ = 'An API wrapper for convertio.co'
__url__ = 'https:/github.com/PetitPotiron/python-convertio/'
__version__ = '1.1.2'
__version__ = '1.2.0'
__author__ = 'PetitPotiron'
__license__ = 'GNU General Public License v3.0'
__copyright__ = 'Copyright (c) 2021 PetitPotiron'
__license__ = 'GNU General Public License v3.0'
13 changes: 7 additions & 6 deletions convertio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def convert_by_filename(self,
else:
_options = None

with open(fp, 'r', encoding='utf-8') as file:
with open(fp, 'rb') as file:
data: ConversionPostDict = {
'apikey': self.token,
'input': 'base64',
'file': base64.b64encode(file.read().encode('utf8')).decode('utf8'),
'filename': filename,
'file': base64.b64encode(file.read()).decode(),
'filename': filename,
'outputformat': output_format,
'options': _options
}
Expand All @@ -64,7 +64,8 @@ def convert_by_filename(self,

r = request.Request(
url='https://api.convertio.co/convert',
data=json.dumps(data).encode("utf8"),
data=json.dumps({k: base64.b64encode(v).decode('utf-8') if isinstance(v, bytes) else v for k, v in
data.items()}).encode("utf8"),
method='POST',
)
r.add_header('Content-Type', 'application/json')
Expand Down Expand Up @@ -98,8 +99,8 @@ def convert_by_url(self, url: str, output_format: str, options: Optional[Options
'options': {
'ocr_enabled': True if options else False,
'ocr_settings': {
'langs': options.langs,
'page_nums': options.page_nums
'langs': options.langs,
'page_nums': options.page_nums
},
} if options else None
}
Expand Down
18 changes: 18 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import convertio
import time

Expand All @@ -7,3 +8,20 @@
time.sleep(1)
client.download(conversion_id, 'result.pdf')
client.delete(conversion_id)


def convert_with_convertio(input_file: str, output_file: str, api_key: str):
client = convertio.Client(token=api_key)
conversion_id = client.convert_by_filename(fp=input_file,
output_format=get_file_extension(output_file))
while client.check_conversion(conversion_id).step != 'finish':
time.sleep(1)
client.download(conversion_id, os.path.abspath(output_file))


convert_with_convertio('source.txt', 'result.pdf', "INSERT_YOUR_TOKEN_HERE")


def get_file_extension(file: str):
root, extension = os.path.splitext(file)
return extension[1:]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = fh.read()

setup(name="convertio",
version="1.1.2",
version="1.2.0",
description="An API wrapper for convertio.co",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 53345e7

Please sign in to comment.