Created
August 4, 2014 18:01
-
-
Save cmkpl/381aa2c7673b8bf63705 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib2 | |
import MySQLdb | |
print "Connecting to database" | |
conn = MySQLdb.connect(host="localhost", user="user", passwd="passwd", db="db", use_unicode=True, charset="utf8") | |
cursor = conn.cursor() | |
print "Connected" | |
while True: | |
isbn = input('ISBN: ') | |
title = "" | |
author = "" | |
publisher = "" | |
pubdate = "" | |
pageNo = 0 | |
try: | |
resp = urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:' + str(isbn)) | |
page = resp.read() | |
jsonResult = json.loads(page, 'utf-8') | |
if jsonResult[u'totalItems'] == 0: | |
#print 'not found' | |
pass | |
else: | |
try: | |
title = jsonResult[u'items'][0][u'volumeInfo'][u'title'].encode('utf-8') | |
except: | |
pass | |
try: | |
author = ', '.join(jsonResult[u'items'][0][u'volumeInfo'][u'authors']).encode('utf-8') | |
except: | |
pass | |
try: | |
publisher = jsonResult[u'items'][0][u'volumeInfo'][u'publisher'].encode('utf-8') | |
except: | |
pass | |
try: | |
pubdate = jsonResult[u'items'][0][u'volumeInfo'][u'publishedDate'].encode('utf-8') | |
except: | |
pass | |
try: | |
pageNo = int(jsonResult[u'items'][0][u'volumeInfo'][u'pageCount']) | |
except: | |
pass | |
except Exception as e: | |
pass | |
try: | |
resp = urllib2.urlopen('https://api.douban.com/v2/book/isbn/' + str(isbn)) | |
page = resp.read() | |
jsonResult = json.loads(page, 'utf-8') | |
if title == "": | |
try: | |
title = jsonResult[u'title'].encode('utf-8') | |
except: | |
pass | |
if author == "": | |
try: | |
author = ', '.join(jsonResult[u'author']).encode('utf-8') | |
except: | |
pass | |
if publisher == "": | |
try: | |
publisher = jsonResult[u'publisher'].encode('utf-8') | |
except: | |
pass | |
if pubdate == "": | |
try: | |
pubdate = jsonResult[u'pubdate'].encode('utf-8') | |
except: | |
pass | |
if pageNo == 0: | |
try: | |
pageNo = int(jsonResult[u'pages'].encode('utf-8')) | |
except: | |
pass | |
except Exception as e: | |
pass | |
print "Title: " + title | |
print "Authur: " + author | |
print "Publisher: " + publisher | |
print "Pub Date: " + pubdate | |
print "Page No.: " + str(pageNo) | |
if title != "": | |
print "Inserting to database" | |
try: | |
cursor.execute("INSERT INTO book (isbn, title, author, publisher, pubdate, pageNo) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE title = %s, author = %s, publisher = %s, pubdate = %s, pageNo = %s; ",(isbn, title, author, publisher, pubdate, pageNo, title, author, publisher, pubdate, pageNo)) | |
print "Inserted" | |
except Exception as e: | |
print e | |
print "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment