Created
April 24, 2016 08:09
-
-
Save gr8Adakron/41520ea52d132ba45bd0b1e1d5abdba9 to your computer and use it in GitHub Desktop.
Python script to convert anything, from any format to desire format using zamzar api (example : convert image.jpeg to image.png / data.odt to data.doc / data.pdf to data.txt etc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from requests.auth import HTTPBasicAuth | |
#--------------------------------------------------------------------------# | |
api_key = 'Put_Your_API_KEY' #your Api_key from developer.zamzar.com (Create Signup id and get api_key) | |
source_file = "tmp/armash.pdf" #source_file_path | |
target_file = "results/armash.txt" #target_file_path_and_name | |
target_format = "txt" #targeted Format. | |
#-------------------------------------------------------------------------# | |
def check(job_id,api_key): | |
check_endpoint = "https://sandbox.zamzar.com/v1/jobs/{}".format(job_id) | |
response = requests.get(check_endpoint, auth=HTTPBasicAuth(api_key, '')) | |
#print(response.json()) | |
#print(response.json()) | |
checked_data=response.json() | |
value_list=checked_data['target_files'] | |
#print(value_list[0]['id']) | |
return value_list[0]['id'] | |
def download(file_id,api_key,local_filename): | |
downlaod_endpoint = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id) | |
download_response = requests.get(downlaod_endpoint, stream=True, auth=HTTPBasicAuth(api_key, '')) | |
try: | |
with open(local_filename, 'wb') as f: | |
for chunk in download_response.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
f.flush() | |
print("File downloaded") | |
except IOError: | |
print("Error") | |
endpoint = "https://sandbox.zamzar.com/v1/jobs" | |
file_content = {'source_file': open(source_file, 'rb')} | |
data_content = {'target_format': target_format} | |
res = requests.post(endpoint, data=data_content, files=file_content, auth=HTTPBasicAuth(api_key, '')) | |
print(res.json()) | |
data=res.json() | |
#print(data) | |
print("=========== Job ID ============\n\n") | |
print(data['id']) | |
target_id=check(data['id'],api_key) | |
print("\n================= target_id ===========\n\n") | |
print(target_id) | |
download(target_id,api_key,target_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment