コード例 #1
0
ファイル: book.py プロジェクト: dtn1712/Swapleaf
def search_book_online_by_isbn(isbn):
	if len(isbn) == 10:
		books = Book.objects.filter(isbn10=isbn)
		if len(books) != 0:
			book = books[0]
			for i in range(1,len(books)):
				books[i].delete()
			return book
	else:
		books = Book.objects.filter(isbn13=isbn)
		if len(books) != 0:
			book = books[0]
			for i in range(1,len(books)):
				books[i].delete()
			return book
	isbn = isbn.replace(" ","")
	link = "http://openlibrary.org/api/books?bibkeys=ISBN:" + isbn + "&jscmd=details&format=json"
	print link
	data = urllib2.urlopen(link)
	a = data.read()
	json_data = json.loads(a)
	if len(json_data) != 0:
		new_book = Book()
		if 'table_of_contents' in json_data['ISBN:' + isbn]['details']:
			new_book.title = json_data['ISBN:' + isbn]['details']['table_of_contents'][0]['title']
		else:
			new_book.title = json_data['ISBN:' + isbn]['details']['title']
		if len(isbn) == 10:
			new_book.isbn10 = isbn
			if "isbn_13" in json_data['ISBN:' + isbn]['details']:
				new_book.isbn13 = json_data['ISBN:' + isbn]['details']['isbn_13'][0]
		else:
			new_book.isbn13 = isbn
			if "isbn_10" in json_data['ISBN:' + isbn]['details']:
				new_book.isbn10 = json_data['ISBN:' + isbn]['details']['isbn_10'][0]
		if "authors" in json_data['ISBN:' + isbn]['details']:
			for i in range(0,len(json_data['ISBN:' + isbn]['details']['authors'])):
				if i == 0:
					new_book.author = new_book.author + json_data['ISBN:' + isbn]['details']['authors'][i]['name']
				else:
					new_book.author = new_book.author + ", " + json_data['ISBN:' + isbn]['details']['authors'][i]['name']
		if "revision" in json_data['ISBN:' + isbn]['details']:
			new_book.edition = json_data['ISBN:' + isbn]['details']['revision']
		new_book.save()
		return new_book
	else:
		return None
コード例 #2
0
ファイル: load_book_data1.py プロジェクト: dtn1712/Swapleaf
	print name
	if isbn not in hash_isbn:
		hash_isbn[isbn] = True
		link = "http://openlibrary.org/api/books?bibkeys=ISBN:" + isbn + "&jscmd=details&format=json"
		data = urllib2.urlopen(link)
		a = data.read()
		json_data = json.loads(a)
		isbn_check = Book.objects.filter(isbn10=isbn)
		print isbn
		if len(isbn_check) == 0:
			book = Book()
			if len(json_data) != 0:
				book.title = json_data['ISBN:' + isbn]['details']['title']
				book.isbn10 = isbn
				if "isbn_13" in json_data['ISBN:' + isbn]['details']:
					book.isbn13 = json_data['ISBN:' + isbn]['details']['isbn_13'][0]
				if "authors" in json_data['ISBN:' + isbn]['details']:
					for i in range(0,len(json_data['ISBN:' + isbn]['details']['authors'])):
						if i == 0:
							book.author = book.author + json_data['ISBN:' + isbn]['details']['authors'][i]['name']
						else:
							 book.author = book.author + ", " + json_data['ISBN:' + isbn]['details']['authors'][i]['name']
				if "revision" in json_data['ISBN:' + isbn]['details']:
					book.edition = json_data['ISBN:' + isbn]['details']['revision']	
			else:
				book.title = name
				book.isbn10 = isbn
			book.save()

books = Book.objects.all()
count = 0