Commit fda6889d authored by Mattia Bondanza's avatar Mattia Bondanza

Sono un gonfio.

parent 312cdd6d
import cStringIO
import sys, os
import re
import commands
import json
import xmltodict as xmlparser
def isValidOpenLibraryAnsware( metadata ):
metadata = json.loads( metadata )
if metadata['status'] == 'ok' :
return True
return False
return False
def metadataFromISBN( isbn ):
mtd = {}
mtd['title'] =''
if len( isbn ) == 10:
query = ''+isbn+"&works="
elif len( isbn ) == 13:
query = ''+isbn+"&works="
return mtd
#print "query:", query
body = commands.getoutput("curl \'"+query+"\' -s")
#print "result:", body
json_query = json.loads( body )
if len( json_query ) == 0:
return mtd
object_id = json_query[0]['key'].strip('\/books\/')
query = ''+object_id+'&prettyprint;=true'
body = commands.getoutput("curl \'"+query+"\' -s")
#print body
metadata = json.loads( body)
#print metadata
if isValidOpenLibraryAnsware( body ):
metadata = json.loads( body )
if metadata['result'].has_key('title'):
mtd['title'] = metadata['result']['title']
if metadata['result'].has_key('authors') and type(metadata['result']['authors']) == list :
for author in metadata['result']['authors']:
query = ''+author['key'].strip('/authors/')+'&prettyprint;=true'
body = commands.getoutput("curl \'"+query+"\' -s")
if isValidOpenLibraryAnsware(body):
auth = json.loads(body)['result']['name']
mtd['author']+=auth+", "
if len( mtd['author'] ) > 0:
return mtd
def metadataISBNGetAuthor( mdt ):
if 'author' in mdt['list'][0]:
return mdt['list'][0]['author']
return None
def metadataISBNGetTitle( mdt ):
if 'title' in mdt['list'][0]:
return mdt['list'][0]['title']
return None
def printMetadataForISBN( isbn ):
mdt = metadataFromISBN( isbn )
if mdt['author'] is not '' and mdt['title'] is not '':
print "( S:OpenLibrary A:\x1b[33m", mdt['author'], "\x1b[0mT:\x1b[32m", mdt['title'], "\x1b[0m)",
print "\x1b[31m( S:OpenLibrary No metadata found.)\x1b[0m",
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment