Skip to content

Commit d63d7f6

Browse files
committed
package version 1.0.0
1 parent ba1a590 commit d63d7f6

11 files changed

Lines changed: 261 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__/
2+
*.py[cod]
3+
build/
4+
dist/
5+
*.egg-info/

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [Conversion Tools](https://conversiontools.io) API Python Client
2+
3+
[Conversion Tools](https://conversiontools.io) is an online service that offers a fast and easy way to convert documents between different formats, like XML, Excel, PDF, Word, Text, CSV and others.
4+
5+
This Client allows to integrate the conversion of the files into Python applications.
6+
7+
To convert the files Python Client uses the public [Conversion Tools REST API](https://conversiontools.io/api-documentation).
8+
9+
## Installation
10+
11+
```bash
12+
pip install --upgrade conversiontools
13+
```
14+
15+
or when building from the sources:
16+
17+
```bash
18+
python setup.py install
19+
```
20+
21+
## Examples
22+
23+
To use REST API - get API Token from the Profile page at https://conversiontools.io/profile.
24+
25+
See example `test.py` in the `./examples/` folder.
26+
27+
```python
28+
from conversiontools import ConversionClient
29+
30+
# put token here from your Profile page at https://conversiontools.io/profile
31+
token = ''
32+
33+
# files
34+
fileInput = 'test.xml'
35+
fileOutput = 'test.csv'
36+
37+
client = ConversionClient(token)
38+
try:
39+
client.convert('convert.xml_to_csv', fileInput, fileOutput, { 'delimiter': 'tabulation' })
40+
except Exception as error:
41+
print(error)
42+
```
43+
44+
## API
45+
46+
### Create `ConversionClient` instance with a token.
47+
```python
48+
from conversiontools import ConversionClient
49+
client = ConversionClient('<token>')
50+
```
51+
52+
Where `<token>` is API token from the account's Profile page https://conversiontools.io/profile.
53+
54+
### Convert input file and download the result
55+
```python
56+
try:
57+
client.convert('<conversion type>', fileInput, fileOutput, '<options>')
58+
except Exception as error:
59+
print(error)
60+
```
61+
62+
Where
63+
- `<conversion type>` is a specific type of conversion, from [API Documentation](https://conversiontools.io/api-documentation).
64+
- `<options>` is a Python dict with options for a corresponding converter, for example:
65+
```python
66+
options = { 'delimiter': 'tabulation' }
67+
```
68+
69+
## Documentation
70+
71+
List of available Conversion Types and corresponding conversion options can be found on the [Conversion Tools API Documentation](https://conversiontools.io/api-documentation) page.
72+
73+
## License
74+
75+
Licensed under [MIT](./LICENSE).
76+
77+
Copyright (c) 2020 [Conversion Tools](https://conversiontools.io)

conversiontools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__version__ = "1.0.0"
2+
from .client import *

conversiontools/api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from .request import requestAPI, uploadAPI, downloadAPI
2+
3+
base_url = 'https://api.conversiontools.io/v1'
4+
5+
def uploadFile(token, name):
6+
fd = open(name, 'rb')
7+
file = { 'file': fd }
8+
result = uploadAPI(base_url + '/files', token, file)
9+
if result['error'] != None:
10+
raise Exception(result['error'])
11+
return result['file_id']
12+
13+
def createTask(token, type, file_id, options=None):
14+
data = {
15+
'type': type,
16+
'options': {
17+
'file_id': file_id
18+
}
19+
}
20+
data['options'].update(options)
21+
22+
result = requestAPI('POST', base_url + '/tasks', token, data)
23+
if result['error'] != None:
24+
raise Exception(result['error'])
25+
return result['task_id']
26+
27+
def getTaskStatus(token, task_id):
28+
result = requestAPI('GET', base_url + '/tasks/' + task_id, token)
29+
errorMessage = result['error']
30+
status = result['status']
31+
file_id = result['file_id']
32+
if errorMessage != None:
33+
raise Exception(errorMessage)
34+
return { "status": status, "file_id": file_id }
35+
36+
def downloadFile(token, file_id, filename):
37+
result = downloadAPI(base_url + '/files/' + file_id, token)
38+
fd = open(filename, 'wb')
39+
fd.write(result)
40+
fd.close()

conversiontools/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
from .api import *
3+
4+
class ConversionClient:
5+
6+
def __init__(self, token):
7+
self.token = token
8+
9+
def convert(self, type, input, output, options=None):
10+
token = self.token
11+
file_id = uploadFile(token, input)
12+
task_id = createTask(token, type, file_id, options)
13+
while True:
14+
taskStatus = getTaskStatus(token, task_id)
15+
status = taskStatus['status']
16+
file_id = taskStatus['file_id']
17+
if status == 'SUCCESS':
18+
downloadFile(token, file_id, output)
19+
break
20+
if status == 'ERROR':
21+
break
22+
time.sleep(5)

conversiontools/request.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import conversiontools
2+
import requests
3+
4+
user_agent = 'conversiontools-python/' + conversiontools.__version__
5+
6+
def prepareHeaders(token):
7+
headers = {
8+
'Authorization': 'Bearer ' + token,
9+
'User-Agent': user_agent
10+
}
11+
return headers
12+
13+
def requestAPI(method, url, token, data=None):
14+
headers = prepareHeaders(token)
15+
response = requests.request(method, url, headers=headers, json=data)
16+
data = response.json()
17+
return data
18+
19+
def uploadAPI(url, token, files):
20+
headers = prepareHeaders(token)
21+
response = requests.request('POST', url, headers=headers, files=files)
22+
data = response.json()
23+
return data
24+
25+
def downloadAPI(url, token):
26+
headers = prepareHeaders(token)
27+
response = requests.request('GET', url, headers=headers)
28+
return response.content

examples/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from conversiontools import ConversionClient
2+
3+
# put token here from your Profile page at https://conversiontools.io/profile
4+
token = ''
5+
6+
# files
7+
fileInput = 'test.xml'
8+
fileOutput = 'test.csv'
9+
10+
client = ConversionClient(token)
11+
try:
12+
client.convert('convert.xml_to_csv', fileInput, fileOutput, { 'delimiter': 'tabulation' })
13+
except Exception as error:
14+
print(error)

examples/test.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<breakfast_menu>
3+
<food>
4+
<name>Belgian Waffles</name>
5+
<price>$5.95</price>
6+
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
7+
<calories>650</calories>
8+
</food>
9+
<food>
10+
<name>Strawberry Belgian Waffles</name>
11+
<price>$7.95</price>
12+
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
13+
<calories>900</calories>
14+
</food>
15+
<food>
16+
<name>Berry-Berry Belgian Waffles</name>
17+
<price>$8.95</price>
18+
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
19+
<calories>900</calories>
20+
</food>
21+
<food>
22+
<name>French Toast</name>
23+
<price>$4.50</price>
24+
<description>Thick slices made from our homemade sourdough bread</description>
25+
<calories>600</calories>
26+
</food>
27+
<food>
28+
<name>Homestyle Breakfast</name>
29+
<price>$6.95</price>
30+
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
31+
<calories>950</calories>
32+
</food>
33+
</breakfast_menu>

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests>=2.4.2

setup.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[bdist_wheel]
2+
universal = 1
3+
4+
[easy_install]
5+
6+
[metadata]
7+
description-file = README.md

0 commit comments

Comments
 (0)