import json
import csv
import requests 
import pprint
from urllib.request import urlopen
from multiprocessing.pool import ThreadPool
from time import time as timer

import pandas as pd
import matplotlib.pyplot as plot
from IPython.display import Image

# Load Azure Custom Vision API endpoint URL and API key
with open('azurecredentials.json') as f:
    data = json.load(f)
    PREDICTION_KEY = data['predictionkey']
    AZURE_CV_URL   = data['azure_cv_url']
    
# Set POST request header to JSON and our secret key
headers = {'Content-Type' : 'application/json', 'Prediction-Key' : PREDICTION_KEY }

metapibaseurl  = 'https://collectionapi.metmuseum.org/public/collection/v1/objects/{}'

threshold = 0.6

content = None
contentdict = None

# Take image URL, feed it to Azure Custom Vision instance
# Return dataframe of results
def image2predictiondf(url):
    try:
        payload = '{"url":"' + url.strip() + '"}'
        # Send POST request to our Azure Custom Vision API
        r = requests.post(AZURE_CV_URL, headers=headers, data=payload).json()
        df = pd.DataFrame(r['predictions'],columns=['tagName','probability'])
        return df
    except Exception as e:
        return None, e

def metid2image(id):
    url = metapibaseurl.format(id)   # Create request string

    try:
        response = urlopen(url)
        item = json.loads(response.read())
        imageurl = item['primaryImageSmall']
        return imageurl
    except Exception as e:
        return None, e
    
def metid2predictiondf(id):
    return image2predictiondf(metid2image(id))

def metid2predictionlist(id):
    df = image2predictiondf(metid2image(id))
    print (id, df[df['probability']>threshold]['tagName'].tolist())
    return None
metid2image(185860)
df=metid2predictiondf(185860)
taglist = df[df['probability']>threshold]['tagName'].tolist()
taglist
['Men', 'Inscriptions', 'Flowers', 'Leaves', 'Women']
metobjectsfile = 'head20-met-new-objects-0205.txt'
    
from tqdm import tqdm
with open(metobjectsfile) as f:
    content = f.readlines()
    content = [x.strip() for x in content]   # remove whitespace

    contentdict =  { i : None for i in content } # create dict from list
# Parallel threads, undocumented mostly
# https://stackoverflow.com/questions/16181121/a-very-simple-multithreading-parallel-url-fetching-without-queue
start = timer()
results = ThreadPool(20).imap_unordered(metid2predictionlist, content)

#    metid2image ()



#    pp = pprint.PrettyPrinter(indent=4)
#    pp.pprint(item)
#        item = json.loads(response)
#        if item.primaryImageSmall:
#            print (item.primaryImageSmall)
212348 ['Flowers', 'TextileFragments', 'Lace', 'Leaves']
212307 ['Flowers', 'TextileFragments']
212319 ['Flowers', 'TextileFragments']
61902 ['Vases', 'UtilitarianObjects', 'Bottles']
212331 ['Flowers']
212332 ['TextileFragments']
185870 ['Dishes']
185884 ['Flowers', 'Jewelry']
185887 ['UtilitarianObjects', 'DecorativeElements', 'Pendants', 'Flowers', 'Jewelry']
468759 ['Men', 'MythicalCreatures', 'Animals', 'HumanFigures']
print ("foobar")