コード例 #1
0
ファイル: lookup.py プロジェクト: koe-/ebola
	def  lookup(isbn, ignore=[]):
		isbn10 = None
		isbn13 = None
		if len(isbn) == 10:
			isbn10 = isbn
			isbn13 = ISBN.to_isbn13(isbn)
		else:
			isbn13 = isbn
			isbn10 = ISBN.to_isbn10(isbn)
		
		print('Fetching: https://www.googleapis.com/books/v1/volumes?q=isbn:' + ISBN.to_string(isbn13, set_hyphen=False))
		up = urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:' + ISBN.to_string(isbn13, set_hyphen=False) + "&projection=lite")
		bytes = up.read()
		up.close()
		
		sleep(0.5)
		
		resp = json.loads(bytes.decode('utf8'))
		if resp['totalItems'] == 1:
			cur = dict()
			
			up = urlopen('https://www.googleapis.com/books/v1/volumes/' + resp["items"][0]["id"])
			try:
				bytes = up.read()
			except Exception:
				print("Unauthorized")
			up.close()
			
			resp = json.loads(bytes.decode('utf8'))

			try:
				cur["isbn10"] = isbn10
				cur["isbn13"] = isbn13
				cur["title"] = resp["volumeInfo"]["title"]
				cur["authors"] = resp["volumeInfo"]["authors"]
				cur["publisher"] = resp["volumeInfo"]["publisher"]
				cur["publicationDate"] = resp["volumeInfo"]["publishedDate"]
				cur["description"] = Utilities.remove_tags(resp["volumeInfo"]["description"])
				cur["pages"] = resp["volumeInfo"]["pageCount"]
				cur["language"] = resp["volumeInfo"]["language"]
				cur["cover"] = resp["volumeInfo"]["imageLinks"]["thumbnail"]
				cur["categories"] = [[[y.strip(),None] for y in x.split(" / ")] for x in resp["volumeInfo"]["categories"]]
			except:
				pass
			
			for x in ignore:
				if x in cur:
					del cur[x]
			
			return cur
		elif resp['totalItems'] == 0:
			raise LookupError("The given ISBN Number cannot be associated with a book")
		else:
			raise NotImplementedError("TODO: Handle multiple results")
コード例 #2
0
ファイル: lookup.py プロジェクト: koe-/ebola
	def lookup(isbn, ignore=[]):
		isbn10 = None
		isbn13 = None
		if len(isbn) == 10:
			isbn10 = isbn
			isbn13 = ISBN.to_isbn13(isbn)
		else:
			isbn13 = isbn
			isbn10 = ISBN.to_isbn10(isbn)
		
		print('Fetching: http://www.lookupbyisbn.com/Lookup/Book/' + ISBN.to_string(isbn10, set_hyphen=False) + '/' + ISBN.to_string(isbn13, set_hyphen=False) + '/1')
		up = urlopen('http://www.lookupbyisbn.com/Lookup/Book/' + ISBN.to_string(isbn10, set_hyphen=False) + '/' + ISBN.to_string(isbn13, set_hyphen=False) + '/1')
		bytes = up.read()
		up.close()
		
		resp = bytes.decode('utf8')
		
		error = resp.find("<title>Lookup by ISBN: Error!</title>")
		if error != -1:
			raise LookupError("[lookupisbn] The given ISBN Number cannot be associated with a book")
		
		cur = dict()
		
		resp = resp.partition("<h2>")[2]
		tmp = resp.partition("</h2>")
		title = tmp[0]
		resp = tmp[2]
		
		resp = resp.partition('<div class="specimage">')[2]
		resp = resp.partition('<img src="')[2]
		tmp = resp.partition('"')
		coverURL = tmp[0]
		resp = tmp[2]
		
		info = ['<span class="title">ISBN:</span>',
				'<span class="title">Author(s):</span>',
				'<span class="title">Publisher:</span>',
				'<span class="title">Publication date:</span>',
				'<span class="title">Edition:</span>',
				'<span class="title">Binding:</span>',
				'<span class="title">Volume(s):</span>',
				'<span class="title">Pages:</span>']

		extracted = []

		for i in info:
			resp = resp.partition(i)[2]
			tmp = resp.partition("\r")
			extracted.append(tmp[0])
			resp = tmp[2]
		
		resp = resp.partition("<h2>")[2]
		resp = resp.partition("</h2>")[2]
		tmp = resp.partition("</div>")
		description = Utilities.remove_tags(tmp[0])
		resp = tmp[2]
		
		cur["isbn10"] = isbn10
		cur["isbn13"] = isbn13
		cur["title"] = title
		cur["cover"] = coverURL
		cur["authors"] = [y.strip() for y in extracted[1].split(",")]
		cur["publisher"] = extracted[2]
		cur["publicationDate"] = extracted[3]
		cur["edition"] = None if extracted[4] == '--' else extracted[4]
		cur["description"] = description.strip()
		cur["binding"] = extracted[5]
		cur["volumes"] = None if extracted[6] == '--' else extracted[6]
		cur["pages"] = extracted[7]
		
		for x in ignore:
			if x in cur:
				del cur[x]
		
		return cur