import json
import time
from requests import get, post
from keys import *
from pprint import pprint

def getTotal():
    text = []
    headers = {
        # Request headers
        'Content-Type': 'application/pdf',
        'Ocp-Apim-Subscription-Key': apim_key,
    }
    with open(source, "rb") as f:
        data_bytes = f.read()
    try:
        resp = post(url = post_url, data = data_bytes, headers = headers)
        if resp.status_code != 202:
            print("POST analyze failed:\n%s" % resp.text)
            quit()
    #print("POST analyze succeeded:\n%s" % resp.headers)
        get_url = resp.headers["operation-location"]
    except Exception as e:
        print("POST analyze failed:\n%s" % str(e))
        quit()

    n_tries = 10
    n_try = 0
    wait_sec = 6
    while n_try < n_tries:
        try:
            resp = get(url = get_url, headers = {"Ocp-Apim-Subscription-Key": apim_key})
            resp_json = json.loads(resp.text)
            if resp.status_code != 200:
            #    pprint("GET Layout results failed:\n%s" % resp_json)
                quit()
            status = resp_json["status"]
            if status == "succeeded":
                for response in (resp_json['analyzeResult']['pageResults'][0]['tables'][0]['cells']):
                    text.append(response['text'])

                #pprint("Layout Analysis succeeded:\n%s" % resp_json)
                quit()
            if status == "failed":
            #    pprint("Layout Analysis failed:\n%s" % resp_json)
                quit()
            # Analysis still running. Wait and retry.
            time.sleep(wait_sec)
            n_try += 1
        except Exception as e:
            msg = "GET analyze results failed:\n%s" % str(e)
            print(msg)
            quit()
    return text


text = getTotal()
print(text)
