所以我正在测试Zamzar API,一切正常,直到我决定更改手动执行流程,而不是运行单个脚本,如获取文件ID并手动粘贴ID到下载脚本并单独运行,所有脚本都应该在一个脚本中运行。
问题是,脚本工作正常,但下载的文件最终被破坏,但如果我手动完成所有操作似乎工作正常,下载的文件永远不会被破坏。
以下是代码:
import json
import requests
from requests.auth import HTTPBasicAuth
try:
from Tkinter import *
except ImportError:
from tkinter import *
from tkinter import filedialog
api_key = 'xxxxf7ed51289f62acc556e1026299xxxxxxxxxx'
## Test inspection
endpoint = "https://sandbox.zamzar.com/v1/formats/docx"
resp1 = requests.get(endpoint, auth=HTTPBasicAuth(api_key, ''))
json_str1 = json.dumps(resp1.json())
print (json.loads(json_str1))
## Start a conversion job
endpointC = "https://sandbox.zamzar.com/v1/jobs"
Tk().withdraw()
source_file = filedialog.askopenfilename()
target_format = "pdf"
file_content = {'source_file': open(source_file, 'rb')}
data_content = {'target_format': target_format}
res = requests.post(endpointC, data=data_content, files=file_content, auth=HTTPBasicAuth(api_key, ''))
json_str2 = json.dumps(res.json())
resp2 = json.loads(json_str2)
## Check the completed job
job_id = (resp2['id'])
endpointJ = "https://sandbox.zamzar.com/v1/jobs/{}".format(job_id)
respo = requests.get(endpointJ, auth=HTTPBasicAuth(api_key, ''))
json_str3 = json.dumps(respo.json())
resp3 = json.loads(json_str3)
## Download the converted file
file_id = (resp3['source_file']['id'])
local_filename = 'tmp/why.pdf'
endpointD = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id)
respon = requests.get(endpointD, stream=True, auth=HTTPBasicAuth(api_key, ''))
try:
with open(local_filename, 'wb') as f:
for chunk in respon.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
print ("File downloaded")
except IOError:
print ("Error")
现在我想知道它与json到python dict对话有什么关系吗? 或者有什么我做错了吗?
答案 0 :(得分:0)
我是Zamzar API的主要开发人员之一,所以应该可以在这里提供帮助。
这里的问题似乎是您在设置代码以下载转换后的文件时将ID读取到 source_file ,因此您实际上只是再次下载输入文件并保存作为PDF(这就是为什么它似乎是"已损坏"):
## Download the converted file
file_id = (resp3['source_file']['id'])
关于转化作业的retrieving details,转换的文件ID可以在JSON响应的 target_files 数组中找到:
{
"id" : 15,
"key" : "XXXX",
"status" : "successful",
"sandbox" : true,
"created_at" : "2013-10-27T13:41:00Z",
"finished_at" : "2013-10-27T13:41:13Z",
"source_file" : {"id":2,"name":"portrait.gif","size":90571},
"target_files" : [{"id":3,"name":"portrait.png","size":15311}],
"target_format" : "png",
"credit_cost" : 1
}
因此,您应修改代码以从此字段中获取文件ID,如下所示:
## Download the converted file
file_id = (resp3['target_files'][0]['id'])
但是,您需要确保作业已完成首次转换,因为仅在作业完成时填充此字段。因此,您应该首先扩展代码以在循环中检查这一点 - 例如:
def check_job(id):
print ("Checking if job is finished")
endpoint = "https://sandbox.zamzar.com/v1/jobs/{}".format(job_id)
response = requests.get(endpoint, auth=HTTPBasicAuth(api_key, ''))
return json.loads(json.dumps(response.json()))
## Check the completed job
job_id = (resp2['id'])
job_status = ""
job_details = ""
while job_status != "successful" :
job_details = check_job(job_id)
job_status = (job_details['status'])
print (job_status)
time.sleep(2)
if job_status == "successful" :
break
然后您可以继续下载转换后的文件:
file_id = (job_details['target_files'][0]['id'])
local_filename = '/tmp/why.pdf'
endpointD = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id)
另外,我们建议不要公开发布您的API密钥 - 任何阅读此问题的人都可以看到并滥用它。一般来说,如果您要发布此类示例代码,只需将密钥哈希,这是一种很好的做法。
为防止滥用行为,我们为您重新生成了一个新密钥,您可以在https://developers.zamzar.com/user
的API信息中心找到该密钥。