def load_potter():
    from sixdegrees import SixDegrees
    s = SixDegrees()
    s.load_potter_isbns(books)
from sixdegrees import SixDegrees

import simplejson

s = SixDegrees()

def get_isbn(openlib_row):
    isbn = openlib_row.get("isbn_10")
    if isbn:
        #clean up and return isbn(s)
        # Might be a list
        cleaned = []
        for i_string in isbn:
            # remove '-'s
            c_i = "".join(i_string.split("-"))
            cleaned.append(c_i)
        return cleaned
    else:
        isbn = openlib_row.get("isbn_13")
        if isbn:
            # Clean up and return isbn(s) (13)
            return isbn
    print "Row didn't have isbns"
    print openlib_row

with open("query.json", "r") as q_json:
    jdata = simplejson.loads(q_json.read())
    for record in jdata:
        isbns = get_isbn(record)
        if isbns:
            for i in isbns:
from sixdegrees import SixDegrees

s = SixDegrees()

# Go through each level and get titles and subjects for as many of the ISBNs as possible using the Open Library API

for level in [1,2,3,4,5,6,7]:
    isbns = s.get_related(level)
    for isbn in isbns:
        results = s.ol_lookup_isbn_10(isbn)
        # Lazy - assume first result is most relevant
        if results:
            result = results[0]
            s.r.incr("tally:%s" % level)
            s.r.set("isbn:%s" % isbn, result.get("title") )
            print result.get("title")
            subjs = result.get("subjects", [])
            if subjs:
                for subj in subjs:
                    print subj
                    s.r.sadd("subj:%s" % level, subj)
        else:
            s.r.incr("misses:%s" % level)
for level in [1,2,3,4,5,6,7]:
    print "Level %s" % level
    print "Sample Titles:"
    for isbn in s.get_related(level):
        if s.r.get("isbn:%s" % isbn):
            print s.r.get("isbn:%s" % isbn)
    print "Subject areas:"
    print "Number of subjects: %s" % s.r.scard("subj:%s" % level)