Python Examples

From Pangeanic
Jump to navigation Jump to search

Translate Function

This Python snippet can be used to translate a texts array texts from src language code to tgt language code using the engine with id engine and using an apiKey

def relay(texts, src, tgt, engine, apiKey):
	url = 'https://prod.pangeamt.com:8443/NexRelay/v1/translate'
	data = {
			"src":src,
			"tgt":tgt,
			"apikey":apiKey,
			"engine": engine,
			"text": texts
			}
	headers = {'Content-type': 'application/json'}
	print(data)
	r = requests.post(url, data=json.dumps(data), headers=headers)
	ans=r.text
	try:
		translationresponse = json.loads(ans, strict=False)
	except:
		print(translationresponse)
	return translationresponse


Batch translation of documents

The following example script can be used to request the translation of documents contained in a folder/directory structure.

import requests
import os
import json
from glob import glob
import time
from shutil import copyfile

username='your-username'
apikey='your-apikey'

engine = 0 # The id of the engine you want to use for process 

srcLang="es"
tgtLang="en"

# top level directories to get files for process and leave processed and failed files
srcdir="C:\\Users\\Usuario\\PycharmProjects\\sendfile\\srcfiles"
tgtdir="C:\\Users\\Usuario\\PycharmProjects\\sendfile\\tgtfiles"
faildir="C:\\Users\\Usuario\\PycharmProjects\\sendfile\\failedfiles"



sent={}
status={}
failed=[]
finished={}
url_base = 'https://prod.pangeamt.com:8443/PGFile/v1'

#sending files to processs
def dosend(path, engine, src, tgt, apikey, username):
    url = url_base + '/sendfile'
    files = {'file': open(path, 'rb')}
    filename = os.path.basename(path)
    values = {'title':filename, 'engine':engine, 'src':src, 'tgt':tgt, 'apikey': apikey, 'processname':'translate', 'username':username, 'notiflink':'testlink', 'processoption': '1'}
    
    r = requests.post(url, files=files, data=values)
    rc=None
    ret = json.loads(r.text)
    if 'error' in ret:
        print("error found processing", path, ret['error_message'])
    else:
        #print("no error, id:", ret['fileId'])
        rc=ret['fileId']
    return (rc)


# getting processed files
def doget(username, apikey):
    url = url_base + '/checkfile?apikey='+apikey+'&username='+username
    r = requests.get(url)
    return (json.loads(r.text))


def dodownload(fileid, apikey, newpath):
    url = url_base + '/download?apikey='+str(apikey)+'&fileid='+str(fileid)
    #print("Calling", url)
    r = requests.get(url)
    local_filename = url.split('/')[-1]
    totalbits = 0
    if r.status_code == 200:
        with open(newpath, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    totalbits += 1024
                    #print("Downloaded", totalbits)
                    f.write(chunk)
    return ((r.status_code, totalbits))




#scan srcdir
tosend=[]
srcpaths = glob(srcdir + '\\**\\*.*', recursive=True)
nfound=0
nvalid=0
for path in srcpaths:
    nfound = nfound +1
    file=os.path.basename(path)
    fName, ext =os.path.splitext(file)
    #print (path, file, fName, ext)
    if ext in ['.txt','.docx','.pdf','.xlsx','.pptx','.PDF', '.DOCX']:
        nvalid=nvalid+1
        tosend.append(path)
print("SRC found", nfound, ", files to send", nvalid, tosend)
initTime=time.time()
#scan & create tgtdir
try:
    os.makedirs(tgtdir)
except OSError as e:
    print ("error creating tgtdir")
    received = []
    tgtpaths = glob(tgtdir + '\\**\\*.*', recursive=True)
    nfound = 0
    for path in tgtpaths:
        nfound = nfound + 1
        file = os.path.basename(path)
        fName, ext = os.path.splitext(file)
        received.append(path)
    print("TGT found received", nfound,  received)

#dosend
for path in tosend:
    fileId = dosend(path, engine, srcLang, tgtLang, apikey, username)
    if fileId!=None:
        sent[fileId]=path
    else:
        failed.append(path)
print ("All files sent")

dochek=True
while dochek:
    time.sleep(5)
    status={}
    rcstatus=doget(username, apikey)
    for st in rcstatus:
        #print("Status of ", st['fileId'], st['status'])
        status[st['fileId']]=st
        if st['status'] == -10:
            try:
                if sent[st['fileId']] in failed:
                    pass
                else:
                    failed.append(sent[st['fileId']])
                    srcPath = sent[st['fileId']]
                    srcDir = os.path.dirname(srcPath)
                    srcName = os.path.basename(srcPath)
                    # calc failsubdir
                    relPath = srcDir[len(srcdir):]
                    tgtDir = faildir + relPath
                    print(relPath+'\\'+srcName, "FAILED")
                    # create tgtsundir if needed
                    try:
                        os.makedirs(tgtDir)
                    except OSError as e:
                        pass
                    #print("Copying", sent[st['fileId']],tgtDir+'\\'+srcName)
                    copyfile(sent[st['fileId']], tgtDir+'\\'+srcName)
            except:
                pass
        if st['status']==100:
            try:
                srcPath=sent[st['fileId']]
                srcDir=os.path.dirname(srcPath)

                #calc tgtsubdir
                relPath=srcDir[len(srcdir):]
                tgtDir=tgtdir+relPath
                print(relPath+'\\'+st['translatedName'], "can be downloaded")
                #create tgtsundir if needed
                try:
                    os.makedirs(tgtDir)
                except OSError as e:
                    pass

                #calc tgtpath
                translatedPath=tgtDir+'\\'+st['translatedName']
                #print("TranslatedPath", translatedPath)
                (rc,bits) = dodownload(st['fileId'],apikey,translatedPath)
                print("Download of", translatedPath, "rc:",rc,"bits:",bits)
                if rc==200 and bits>0:
                    finished[st['fileId']]=translatedPath
            except Exception as ee:
                print(ee)
                pass

    print("Sent", len(sent))
    print("Finished", len(finished))
    print("Failed", len(failed))
    if len(finished)+len(failed)==len(sent):
        dochek=False
        print("Job finished!")
        endTime = time.time()
        print("Started at", initTime, "ended at", endTime, " delay:", (endTime-initTime))
    else:
        print("In Process", len(sent)-len(finished)-len(failed))
    print("=============================================================")