示例#1
0
    def scan(self): 
        try:
            if sys.platform == 'win32':
                ZBARPATH = list(which('zbarcam'))
                if not ZBARPATH:
                    ZBARPATH = list(which('zbarcam', path=[r"C:\Program Files\ZBar\bin", r"C:\Program Files (x86)\ZBar\bin"]))[0]
            else:
                ZBARPATH = list(which('zbarcam'))[0]
        except:
            ZBARPATH = None
        if not ZBARPATH:
            QtGui.QMessageBox.information(None, "Aranduka - Error", "Can't find zbarcam. Get it at http://zbarcam.sf.net")
            return
        p=os.popen(ZBARPATH,'r')
        p = subprocess.Popen([ZBARPATH], stdout=subprocess.PIPE).communicate()[0]
        # p = "DEMO:0345400445"
        guesser = manager.getPluginsOfCategory('Guesser')[0]
        for code in p.splitlines():
            print("scanning")
            if code:
                print(('Got barcode:', code))
                isbn = code.split(':')[1]
                # QtGui.QDesktopServices.openUrl(QtCore.QUrl('http://www.goodreads.com/search/search?q=%s'%isbn))
                i = Identifier(key='ISBN', value=isbn)
                # Create empty book
                b = Book(identifiers = [i])
                # We are supposed to have a ISBN, so assume we are getting it right.
                dlg = GuessDialog(b)
                dlg.isbn.setChecked(True)
                dlg.on_guessButton_clicked()

                r = dlg.exec_()
                
                # FIXME this is copied from book_editor. 
                # The Book class probably needs an "update from metadata" method.
                md = None
                if not r == dlg.Accepted:
                    md = None
                    b.delete()
                elif dlg.currentMD:
                    md =  dlg.currentMD

                    if md is None:
                        return
                    else:
                        # A candidate was chosen, update data
                        print(md)
                        b.title = md.title
                        if md.identifiers is not None:
                            for k,v in md.identifiers:
                                i = Identifier(
                                        key=k.upper(), 
                                        value=v,
                                        book=b)
                        b.authors = []
                        for a in md.authors:
                            author = Author.get_by(name = a)
                            if not author:
                                print(("Creating author:", a))
                                author = Author(name = a)
                            b.authors.append(author)
                        Author.sanitize()
                        # FIXME: it seems Qt can't parse alibris cover images?
                        # b.fetch_cover(md.thumbnail)
                        b.fetch_cover()
                    session.commit()