Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zamzar API download failed

Tags:

python

request

Unable to download the converted file from zamzar api using python program, as specified on the https://developers.zamzar.com/docs but as i am using the code correctly along with api key. It is only showing error code : 20. Wasted 4hour behind this error, someone please.

import requests
from requests.auth import HTTPBasicAuth

file_id =291320
local_filename = 'afzal.txt'
api_key = 'my_key_of_zamzar_api'
endpoint = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id)

response = requests.get(endpoint, stream=True, auth=HTTPBasicAuth(api_key, ''))

try:
  with open(local_filename, 'wb') as f:
    for chunk in response.iter_content(chunk_size=1024):
      if chunk:
        f.write(chunk)
        f.flush()

    print("File downloaded")

except IOError:
  print("Error")

THis is the code I am using for downloading the converted file.

like image 275
The Gr8 Adakron Avatar asked Jan 20 '26 08:01

The Gr8 Adakron


1 Answers

This code easily convert files into different formats :

import requests
from requests.auth import HTTPBasicAuth
#--------------------------------------------------------------------------#
api_key = 'Put_Your_API_KEY' #your Api_key from developer.zamzar.com
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)

Hope this well somebody!.

like image 65
The Gr8 Adakron Avatar answered Jan 22 '26 21:01

The Gr8 Adakron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!