def to_xml(self, library=None): d = self.to_dict() res = "<book>\n" for x in d: if x == "authors": if len(d["authors"]) > 0: res = res + " <authors>\n"+ \ " <name>" + ("</name>\n <name>".join([Utilities.escape_xml(y) for y in d["authors"]])) + "</name>\n"+ \ " </authors>\n" elif x == "categories" and library != None: if len(d["categories"]) > 0: res = res + " <categories>\n" col = library.categories.collection for i in d["categories"]: res = res + \ " <item>\n" + \ " " + "".join(["<n color='" + col[y].color + "'>" + Utilities.escape_xml(col[y].name) + "</n>" for y in library.categories.get_full_category_ids(i)]) + "\n" +\ " </item>\n" res = res + " </categories>\n" elif x == "isbn10": res = res + " <isbn10>" + ISBN.to_string(d["isbn10"], set_hyphen=False) + "</isbn10>\n" elif x == "isbn13": res = res + " <isbn13>" + ISBN.to_string(d["isbn13"], set_hyphen=False) + "</isbn13>\n" elif d[x] != None: res = res + " <" + x + ">" + Utilities.escape_xml(str(d[x])) + "</" + x + ">\n" return res + "</book>\n"
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")
def mine_isbn(self): obuf = StringIO() rsrcmgr = PDFResourceManager() laparams = LAParams() device = TextConverter(rsrcmgr, outfp=obuf, laparams=laparams) interpreter = PDFPageInterpreter(rsrcmgr, device) if True: for page in self.document.get_pages(): interpreter.process_page(page) curpg = obuf.getvalue() part = curpg.partition("ISBN") if part[1] == 'ISBN': isbn = [] part = part[2].partition(" ") for c in part[2]: try: isbn.append(int(c)) except: pass if c == '\n': break if isbn[:3] == [9,7,8]: if len(isbn) == 12: return isbn + [ISBN.checksum_isbn13(isbn)] elif len(isbn) == 9: return isbn + [ISBN.checksum_isbn10(isbn)] print("ISBN " + ISBN.to_string(isbn) + " incomplete! continue...") #except Exception: #raise RuntimeError("ExtractionError") raise RuntimeError("ExtractionError")
def add_book(self, b, localCover = True): if b in self.data: return False if localCover: if b.cover[:7] == 'http://' or b.cover[:8] == 'https://': localPath = self.baseDir + "/covers/" + ISBN.to_string(b.isbn13) + "-" + os.path.basename(urlparse(b.cover).path) up = urlopen(b.cover) fp = open(localPath, "wb") fp.write(up.read()) fp.close() up.close() b.cover = "file://" + localPath if self.default_db == "xml": self.data.append(b) elif self.default_db == "sqlite": c = self.connection.cursor() c.execute("SELECT id FROM book WHERE isbn13=?", [ISBN.to_string(b.isbn13, False)]) res = c.fetchone() if res: print("[!] BOOK ALREADY IN COLLECTION: " + str(res)) return False else: c.execute("INSERT INTO book(isbn13) VALUES (?)", [ISBN.to_string(b.isbn13, False)]) lid = c.lastrowid for key,keyid in sql.sqlbase_attribute_names.items(): attr_val = b.get_attribute_by_name(key) if attr_val: if isinstance(attr_val, list): for c in attr_val: c.execute("INSERT INTO attribute (id_book, id_attributename, attr_value) VALUES (?, ?, ?)", [lid, str(keyid), str(c)]) else: c.execute("INSERT INTO attribute (id_book, id_attributename, attr_value) VALUES (?, ?, ?)", [lid, str(keyid), str(attr_val)]) print("[!] INSERTED BOOK: id=" + str(c.lastrowid)) self.connection.commit() return True
def add_book_item(self, book, library = None): row = Gtk.ListBoxRow() row.ebola_labels = {} self.connect("row-selected", self.on_row_selected) #row.connect("focus-in-event", self.on_focus_in) hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10) vimgbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) vimgbox.pack_start(Gtk.Alignment(), False, False, 0) if book.cover == None: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size("unknown_cover.svg", 105, 74) missingImg = Gtk.Image.new_from_pixbuf(pixbuf) vimgbox.pack_start(missingImg, False, False, 0) else: if book.cover[:7] == "file://": pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(book.cover[7:], 105, 74) missingImg = Gtk.Image.new_from_pixbuf(pixbuf) vimgbox.pack_start(missingImg, False, False, 0) hbox.pack_start(vimgbox, False, False, 0) vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5) lbl = Gtk.Label(xalign=0) lbl.set_markup("<big><b>" + Utilities.escape_xml(str(book.title)) + "</b></big>") lbl.set_justify(Gtk.Justification.LEFT) lbl.set_margin_right(5) row.ebola_labels["title"] = lbl hbox2 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) hbox2.pack_start(lbl, True, True, 0) vbox.pack_start(hbox2, True, True, 0) lbl = Gtk.Label(xalign=0) lbl.set_markup("<b><i>" + Utilities.escape_xml(", ".join(book.authors)) + "</i></b>") lbl.set_justify(Gtk.Justification.LEFT) vbox.pack_start(lbl, True, True, 0) row.ebola_labels["authors"] = lbl cats = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) for cid in book.categories: cats.pack_start(self.category_selector_widget(cid, library), False, False, 0) vbox.pack_start(cats, False, True, 0) lbl = Gtk.Label(xalign=0) edition = "" if book.edition != None: edition = str(book.edition) + ". Edition, " lbl.set_markup("<b>Publisher:</b> " + Utilities.escape_xml(str(book.publisher)) + ", " + Utilities.escape_xml(edition + str(book.publication_date))) lbl.set_justify(Gtk.Justification.LEFT) vbox.pack_start(lbl, True, True, 0) row.ebola_labels["pubInfo"] = lbl lbl = Gtk.Label(xalign=0) lbl.set_markup("<i>ISBN " + ISBN.to_string(book.isbn13) + "</i>") lbl.set_justify(Gtk.Justification.LEFT) vbox.pack_start(lbl, True, True, 0) row.ebola_labels["isbn"] = lbl hbox.pack_start(vbox, True, True, 0) row.add(hbox) self.add(row) row.show_all() row.ebola_button_show_in_folder = Gtk.LinkButton(book.uri, "Show in folder") row.ebola_button_show_in_folder.set_margin_top(5) row.ebola_button_show_in_folder.set_margin_bottom(5) row.ebola_button_show_in_folder.set_margin_right(5) row.ebola_button_show_in_folder.connect("activate-link", self.on_show_in_folder) hbox2.pack_end(row.ebola_button_show_in_folder, False, True, 0) row.ebola_button_open = Gtk.LinkButton(book.uri, "Open") row.ebola_button_open.set_margin_top(5) row.ebola_button_open.set_margin_bottom(5) row.ebola_button_open.set_margin_right(5) hbox2.pack_end(row.ebola_button_open, False, True, 0) row.ebola_book_description = None if book.description != None and book.description != "": lbl = Gtk.Label(xalign=0) lbl.set_markup("\n<b>Description:</b> " + Utilities.escape_xml(str(book.description))) lbl.set_justify(Gtk.Justification.LEFT) lbl.set_line_wrap(True) lbl.set_selectable(True) lbl.set_lines(3) revealer = Gtk.Revealer() revealer.set_reveal_child(False) revealer.add(lbl) lbl = Gtk.Label() lbl.set_markup("<a href='#'>Show Description</a>") lbl.ebola_revealer = revealer lbl.connect("activate-link", self.toggle_description) row.ebola_book_description = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) row.ebola_book_description.pack_end(lbl, True, True, 0) row.ebola_book_description.pack_end(revealer, True, True, 0) vbox.pack_start(row.ebola_book_description, True, True, 0) self.rows.append(row)
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