def readJsonFile(self, dirName=None): # print 'readJsonFile----->', os.path.join(Workspace().libraryPath, dirName , 'book.json') try: if os.path.exists( os.path.join(Workspace().libraryPath, dirName, 'book.json')): bookJsonFile = open( os.path.join(Workspace().libraryPath, dirName, 'book.json'), 'r') else: os.removedirs(os.path.join(Workspace().libraryPath, dirName)) except Exception as e: print e print os.path.join(Workspace().libraryPath, dirName) rep = '' for line in bookJsonFile: rep = rep + line bookJsonFile.close b = None try: b = json.loads(rep) except: traceback.print_exc() # print rep return b
def __init__(self, parent, log=None): self.log = log wx.Panel.__init__(self, parent, -1) self.newPath = Workspace().path vBox = wx.BoxSizer(wx.VERTICAL) hBox1 = wx.BoxSizer(wx.HORIZONTAL) hBox2 = wx.BoxSizer(wx.HORIZONTAL) hBox3 = wx.BoxSizer(wx.HORIZONTAL) hBox4 = wx.BoxSizer(wx.HORIZONTAL) page = ''' <html> <body> <h4>Select a workspace</h4> Opal stores your books in a folder called workspace. \n Choose a workspace folder to use for this session. </body> </html> ''' ctrl = wx.html.HtmlWindow(self, -1, wx.DefaultPosition, size=(550, 100)) if "gtk2" in wx.PlatformInfo or "gtk3" in wx.PlatformInfo: ctrl.SetStandardFonts() ctrl.SetPage(page) self.info = wx.InfoBar(self) # TODO self.dbb = filebrowse.DirBrowseButton( self, -1, size=(450, -1), changeCallback=self.dbbCallback, startDirectory=Workspace().path ) print '----otherWorkspace------', Workspace().path if Workspace().path: self.dbb.textControl.SetValue(Workspace().path) okButton = wx.Button(self, -1, "OK") cancelButton = wx.Button(self, -1, "Cancel") self.Bind(wx.EVT_BUTTON, self.okBtnClicked, okButton) self.Bind(wx.EVT_BUTTON, self.cancelBtnClicked, cancelButton) hBox2.Add(ctrl, 0, wx.ALL | wx.EXPAND, 5) vBox.Add(self.info, 0, wx.EXPAND) hBox4.Add(self.dbb, 0, wx.ALL | wx.EXPAND, 5) hBox3.Add(okButton, 0, wx.ALL | wx.RIGHT, 5) hBox3.Add(cancelButton, 0, wx.ALL | wx.RIGHT, 5) vBox.Add(hBox2, 0, wx.EXPAND) vBox.Add(hBox1, 0, wx.EXPAND) vBox.Add(hBox4, 0, wx.EXPAND) vBox.Add(hBox3, 0, wx.ALL | wx.RIGHT, 5) self.SetSizer(vBox) vBox.Fit(self)
def __init__(self): ''' Creating database for library. ''' self.engine = create_engine(onlineDatabaseUrl, echo=True) Session = sessionmaker(autoflush=True, autocommit=False, bind=self.engine) self.session = OnlineSingletonSession().session if not os.path.exists(Workspace().searchedPath): os.mkdir(Workspace().searched) os.chdir(Workspace().searchedPath)
def creatingDatabase(self): if not os.path.exists(Workspace().libraryPath): os.mkdir(Workspace().libraryPath) os.chdir(Workspace().libraryPath) listOfDir = os.listdir(Workspace().libraryPath) isDatabase = False for sName in listOfDir: if ("_opal.sqlite" in str(sName)) and (os.stat(Workspace().libraryPath + os.sep + '_opal.sqlite').st_size != 0): print sName isDatabase = True if not isDatabase: self.createDatabase .addingData()
def createSession(self): engine = create_engine(onlineDatabaseUrl, echo=True) Session = sessionmaker(autoflush=False, autocommit=False, bind=engine) self.session = Session() database_fileName = os.path.join(Workspace().searchedPath , databaseFileName) if not os.path.exists(database_fileName) or os.path.getsize(database_fileName) == 0: if not os.path.exists(Workspace().searchedPath): os.mkdir(Workspace().searchedPath) os.chdir(Workspace().searchedPath) # print '---------------------------',os.path.getsize(database_fileName) # self.creatingDatabase() print Base.metadata.drop_all(engine) print Base.metadata.create_all(engine)
def __init__(self, table): """ Image Renderer Test. This just places an image in a cell based on the row index. There are N choices and the choice is made by choice[row%N] """ Grid.PyGridCellRenderer.__init__(self) self.table = table self._choices = { 'pdf': wx.Bitmap(os.path.join(Workspace().appPath, "images", "pdf.png")), 'chm': wx.Bitmap(os.path.join(Workspace().appPath, "images", "chm.png")), 'mobi': wx.Bitmap(os.path.join(Workspace().appPath, "images", "mobi.png")), 'epub': wx.Bitmap(os.path.join(Workspace().appPath, "images", "epub.png")), 'djvu': wx.Bitmap(os.path.join(Workspace().appPath, "images", "djvu.png")), 'doc': wx.Bitmap(os.path.join(Workspace().appPath, "images", "doc.png")), 'azw': wx.Bitmap(os.path.join(Workspace().appPath, "images", "azw.png")), 'cbr': wx.Bitmap(os.path.join(Workspace().appPath, "images", "cbr.png")) } self.colSize = None self.rowSize = None
def __init__(self, parent): title = "Opal" size = wx.DefaultSize style = wx.DEFAULT_FRAME_STYLE | wx.MAXIMIZE | wx.SUNKEN_BORDER # wx.Frame.__init__(self, parent, wx.ID_ANY, title, pos, size, style) wx.Frame.__init__(self, parent, wx.ID_ANY, title=title, style=style) print '1----------------------->' image = wx.Image(os.path.join(Workspace().appPath, "images", "Library-icon.png"), wx.BITMAP_TYPE_PNG).ConvertToBitmap() icon = wx.EmptyIcon() icon.CopyFromBitmap(image) # set frame icon self.SetIcon(icon) if not os.path.exists(Workspace().libraryPath): self.createWizard() self.createDatabase = CreateDatabase() # self.creatingDatabase() self.books = list() self.thumbnail = None self.fileDropTarget = FileDropTarget(self) # self.grid = wx.grid.Grid(self, -1, wx.Point(0, 0), wx.Size(150, 250),wx.NO_BORDER | wx.WANTS_CHARS) self._mgr = aui.AuiManager() # tell AuiManager to manage this frame self._mgr.SetManagedWindow(self) # set up default notebook style self._notebook_style = aui.AUI_NB_DEFAULT_STYLE | aui.AUI_NB_TAB_EXTERNAL_MOVE | wx.NO_BORDER self._notebook_theme = 0 # Attributes self._textCount = 1 self._transparency = 255 self._snapped = False self._custom_pane_buttons = False self._custom_tab_buttons = False self._pane_icons = False self._veto_tree = self._veto_text = False print '1----------------------->', os.getcwd() os.chdir(os.path.dirname(os.path.abspath(__file__))) self.BuildPanes() self.CreateMenuBar() self.BindEvents() self.buildStatusBar()
def resetWorkspace(self): os.chdir(Workspace().path) print '---resetWorkspace---->', os.getcwd() listOfDir = os.listdir(Workspace().path) if len(listOfDir) > 0: # print len(listOfDir) isDatabase = False for sName in listOfDir: if ".sqlite" in str(sName): print sName isDatabase = True if not isDatabase: createDatabase = CreateDatabase() session = createDatabase.creatingDatabase() createDatabase.addingData()
def __init__(self): ''' Creating database for library. ''' self.engine = create_engine('sqlite:///' + Workspace().libraryPath + os.sep + '_opal.sqlite', echo=False) Session = sessionmaker(autoflush=True, autocommit=False, bind=self.engine) self.session = SingletonSession().session if not os.path.exists(Workspace().libraryPath): os.mkdir(Workspace().libraryPath) os.chdir(Workspace().libraryPath)
def addSingleBookData(self, dirName): ''' Using this method you can update single book info into database. ''' try: single = {} duplicate = {} libraryPath = Workspace().libraryPath os.chdir(libraryPath) duplicateBooks = list() addDatabase = True b = self.readJsonFile(dirName=dirName) book = self.createBookFromJson(bookJson=b) book.bookPath = os.path.join(libraryPath, dirName) if book.isbn_13: if not single.has_key(book.isbn_13): single[book.isbn_13] = book else: duplicate[book.isbn_13] = book addDatabase = False duplicateBooks.append(duplicate) if addDatabase: self.session.add(book) self.session.commit() except: traceback.print_exc() self.session.rollback()
def addingBookToWorkspace(self, sourcePath=None): ''' This function will be creating a new dir. Get the max of id in Book table. Create the folder name with max of id plus one. @param sourcePath: This is the path of selected book. -1. Check if database present in workspace. There is possibility of a new workspace. 0. Check if book already present in workspace. 1. Create a folder with max_book_id+1 . 2. Copy the book file in the directory. 3. Create metadata i.e. (book.json) 4. Make an entry in database. ''' if sourcePath: maxBookId = self.createDatabase.getMaxBookID() if maxBookId == None: maxBookId = 0 workspacePath = Workspace().libraryPath self.book.bookPath = os.path.join(workspacePath, str(maxBookId + 1)) head, tail = os.path.split(sourcePath) self.book.bookFileName = tail self.book.inLanguage = 'English' self.book.hasCover = 'Y' splited_name = tail.split(".") self.book.bookFormat = splited_name[-1:][0] splited_name.remove(self.book.bookFormat) book_file_name = '.'.join(splited_name) self.book.bookName = book_file_name self.book.wishListed = 'No' if not self.findingSameBook(): self.book.bookPath = os.path.join(workspacePath, str(maxBookId + 1)) if not os.path.exists(self.book.bookPath): os.makedirs(self.book.bookPath) dest = os.path.join(self.book.bookPath, tail) if sourcePath != dest: shutil.copy (sourcePath, dest) if 'pdf' == self.book.bookFormat : self.getPdfMetadata(sourcePath) if 'epub' == self.book.bookFormat: self.getEpubMetadata(sourcePath) pass os.chdir(self.book.bookPath) self.book.bookImgName = book_file_name + '.jpg' BookImage().getBookImage(self.book.bookPath, book_file_name, self.book.bookFormat) book_copy1 = copy.deepcopy(self.book) self.writeBookJson(self.book.bookPath, book_copy1) self.addingBookInfoInDatabase(self.book)
def findAllBooks(self, pageSize=None): ''' This method will give all the books list in book library. ''' books = list() os.chdir(Workspace().libraryPath) books = self.createDatabase.findAllBook(pageSize=pageSize) return books
def __init__(self, baseUrl=None): ''' Constructor ''' self.baseUrl = baseUrl self.directory_name = Workspace().libraryPath self.createDatabase = CreateDatabase() pass
def main(): # global books, frame # session = CreateDatabase().creatingDatabase() # # CreateDatabase().addingData(session) # books = CreateDatabase().findAllBook(session) # bookName = 'head' # books = CreateDatabase().findByBookName(session, bookName) if Workspace().libraryPath + os.sep + '_opal.sqlite': if os.stat(Workspace().libraryPath + os.sep + '_opal.sqlite').st_size == 0: c = CreateDatabase() c.creatingDatabase() c.addingData() print 'data loaded' app = wx.App(0) frame = MainWindow(None, "My Calibre") app.MainLoop()
def createSession(self): engine = create_engine('sqlite:///' + Workspace().libraryPath + os.sep + '_opal.sqlite', echo=True) Session = sessionmaker(autoflush=False, autocommit=False, bind=engine) self.session = Session() database_fileName = os.path.join(Workspace().libraryPath, '_opal.sqlite') if not os.path.exists(database_fileName) or os.path.getsize( database_fileName) == 0: if not os.path.exists(Workspace().libraryPath): os.mkdir(Workspace().libraryPath) os.chdir(Workspace().libraryPath) # print '---------------------------',os.path.getsize(database_fileName) # self.creatingDatabase() print Base.metadata.drop_all(engine) print Base.metadata.create_all(engine)
def __init__(self, baseUrl=None): self.baseUrl = baseUrl self.directory_name = Workspace().libraryPath self.createDatabase = CreateDatabase() self.header_info = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0'} # book image url self.imageUrl = None self.bookUrl = None pass
def doGoogleSearch(self, searchText=None): r = requests.get('https://www.googleapis.com/books/v1/volumes?q=' + searchText) json_data = r.json() items = json_data['items'] listOfBooks = [] for x in items: # print x.keys() b = Book(x) volumeInfo = VolumeInfo(b['volumeInfo']) b.bookName = volumeInfo.title # volumeInfo.imageLinks['thumbnail'] if volumeInfo.has_key('imageLinks'): url = str(volumeInfo.imageLinks['thumbnail']) else: url = "https://books.google.co.in/googlebooks/images/no_cover_thumb.gif" path = Workspace().imagePath # os.mkdir(tmp_path) # path = os.path.dirname(__file__) + os.sep + 'tmp' print path if not os.path.exists(path): os.mkdir(path) b.localImagePath = path # + os.sep + b.id + '.jpeg' b.imageFileName = b.id + '.jpeg' if not os.path.exists( os.path.join(b.localImagePath, b.imageFileName)): os.chdir(path) print 'writing file' self.downloadUrl(imageUrl=url, bookImgName=b.id + '.jpeg', destinationPath=Workspace().imagePath) # with open(path + os.sep + b.id + '.jpeg', 'wb') as f: # f.write(urllib2.urlopen(url).read()) b.volumeInfo = volumeInfo b.bookPath = None b.source = 'googleapis.com' listOfBooks.append(b) return listOfBooks
def okBtnClicked(self, event): print 'okBtnClicked' if not os.path.exists(self.newPath): os.makedirs(self.newPath) if os.path.exists(self.newPath): os.chdir(self.newPath) print ("CWD: %s\n" % os.getcwd()), Workspace().path = os.getcwd() self.resetWorkspace() self.Parent.Destroy()
def createWizard(self): # Create the wizard and the pages wizard = Wizard(self, -1, "Opal welcome wizard", wx.EmptyBitmap(200, 200)) page1 = TitledPage(wizard, "Welcome to Opal") page2 = TitledPage(wizard, "Page 2") page3 = TitledPage(wizard, "Page 3") page4 = TitledPage(wizard, "Page 4") self.page1 = page1 self.page1 = page1 vbox = wx.BoxSizer(wx.HORIZONTAL) lable = wx.StaticText(page1, -1, "Choose your language:") choice = wx.Choice(page1, -1, (0, 0), choices=['English']) choice.SetSelection(0) vbox.Add(lable, 0, wx.ALIGN_CENTRE | wx.ALL, 5) vbox.Add(choice, 0, wx.ALIGN_CENTRE | wx.ALL, 5) page1.sizer.Add(vbox) # vbox = wx.BoxSizer(wx.HORIZONTAL) # t1 = wx.TextCtrl(page1, -1, "Test it out and see", size=(125, -1)) # vbox.Add(t1, 0, wx.ALIGN_CENTRE | wx.ALL, 5) # page1.sizer.Add(vbox) page1.sizer.Add( wx.StaticText( page1, -1, """ Choose a location of your workspace. When you add books to Opal, they will be copied here. Use an empty folder for a new Opal workspace."""), 0, wx.ALIGN_LEFT | wx.ALL, 1) dbb = DirBrowseButton(page1, -1, size=(450, -1), changeCallback=self.dbbCallback) dbb.SetFocus() dbb.SetLabel("Book Library Location") dbb.SetHelpText('Please set your default workspace location.') dbb.textControl.SetValue(Workspace().path) page1.sizer.Add(dbb, 0, wx.ALIGN_CENTRE | wx.ALL, 5) wizard.FitToPage(page1) # Use the convenience Chain function to connect the pages WizardPageSimple.Chain(page1, page2) # WizardPageSimple.Chain(page2, page3) # WizardPageSimple.Chain(page3, page4) wizard.GetPageAreaSizer().Add(page1) if wizard.RunWizard(page1): pass
def searchingBook(self, searchText=None, exactSearchFlag=False): ''' This method return list of books matching with search text. @param searchText: may be a book name ''' books = list() if searchText != None and searchText != '': os.chdir(Workspace().libraryPath) if exactSearchFlag: books = self.createDatabase.findByBookName(searchText) else: books = self.createDatabase.findBySimlarBookName(searchText) else: books = self.findAllBooks() return books
def downloadUrl(self, imageUrl=None, bookImgName=None, destinationPath=Workspace().imagePath): ''' This function will download url. ''' if not os.path.exists(destinationPath): os.mkdir(destinationPath) os.chdir(destinationPath) try: with open(os.path.join(destinationPath, bookImgName), 'wb') as f: f.write(urllib2.urlopen(imageUrl).read()) except: print 'unable to donwload url: ', imageUrl traceback.print_exc()
def PopulateList(self): if 0: # for normal, simple columns, you can add them like this: self.list.InsertColumn(0, "Command") self.list.InsertColumn(1, "Binding", wx.LIST_FORMAT_RIGHT) self.list.InsertColumn(2, "Category") else: # but since we want images on the column header we have to do it the hard way: info = wx.ListItem() info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT info.m_image = 1 info.m_format = 0 info.m_text = "Command" self.list.InsertColumnInfo(0, info) info.m_format = wx.LIST_FORMAT_RIGHT info.m_text = "Binding" self.list.InsertColumnInfo(1, info) info.m_format = 0 info.m_text = "Category" self.list.InsertColumnInfo(2, info) items = musicdata.items() for key, data in items: image = wx.Image(os.path.join(Workspace().appPath, "images", data[3]), wx.BITMAP_TYPE_PNG).ConvertToBitmap() index = self.list.InsertImageStringItem(sys.maxint, data[0], self.il.Add(image)) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetItemData(index, key) self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(2, 100) # show how to select an item self.list.SetItemState(5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED) # show how to change the colour of a couple items item = self.list.GetItem(1) item.SetTextColour(wx.BLUE) self.list.SetItem(item) item = self.list.GetItem(4) item.SetTextColour(wx.RED) self.list.SetItem(item) self.currentItem = 0
def getMaxBookID(self, book=None): ''' This method will find the book in database . It will return true.If book present. ''' books = None # maxBookId = self.session.query(func.max(Book.id)).one() length = len(Workspace().libraryPath) + 2 print length sql = 'select max(substr(book_path,' + str( length) + '), id) from book order by id desc' print 'getMaxBookID----sql: > ', sql maxBookId = self.session.execute(sql).first() if maxBookId == None: maxBookId = [0] print int(maxBookId[0]) return int(maxBookId[0])
def addingData(self): directory_name = Workspace().libraryPath os.chdir(directory_name) listOfDir = list() # listOfDir = [ name for name in os.listdir(directory_name) if os.path.isdir(os.path.join(directory_name, name)) ] for name in os.listdir(directory_name): if os.path.isdir(os.path.join(directory_name, name)): try: if int(name): listOfDir.append(name) except Exception as e: pass if listOfDir: listOfDir.sort(key=int) one = '' try: single = {} duplicate = {} self.duplicateBooks = list() for dirName in listOfDir: addDatabase = True b = self.readJsonFile(dirName=dirName) book = self.createBookFromJson(bookJson=b) book.bookPath = os.path.join(directory_name, dirName) if book.isbn_13: if not single.has_key(book.isbn_13): single[book.isbn_13] = book else: duplicate[book.isbn_13] = book addDatabase = False self.duplicateBooks.append(duplicate) # print single if addDatabase: self.session.add(book) self.session.commit() print self.duplicateBooks except: # print duplicate traceback.print_exc() self.session.rollback() print 'data loaded'
def readJsonFromDir(self, dirName=None): ''' This method read the json file from workspace and return book object. @param dirName: this is directory name. ''' bookJsonFile = open(os.path.join(Workspace().libraryPath, dirName , 'book.json'), 'r') rep = '' for line in bookJsonFile: rep = rep + line bookJsonFile.close b=None try: b = json.loads(rep) except: traceback.print_exc() print rep return b
def missingNumbers(self): directory_name = Workspace().libraryPath os.chdir(directory_name) listOfDir = [ name for name in os.listdir(directory_name) if os.path.isdir(os.path.join(directory_name, name)) ] if listOfDir: listOfDir.sort(key=int) # for l in listOfDir: # print type(l) for l in range(7103, 7035, -1): if str(l) not in listOfDir: self.missingListing.append(l) return self.missingListing
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None): ''' Constructor, setting location of downloaded book. ''' super(DownloadItEbook, self).__init__(group=group, target=target, name=name, verbose=verbose) self.args = args self.kwargs = kwargs self.directory_name = Workspace().libraryPath self.createDatabase = CreateDatabase() pass
def getAmazonSingleBookInfo(self, bookImgName, bookUrl=None): ''' e.g. urls are given below. 'http://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730', 'http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994', ''' # payload = {'Host':"www.amazon.com", # 'User-Agent':"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0", # 'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", # 'Accept-Language':"en-US,en;q=0.5", # 'Accept-Encoding':"gzip, deflate", # 'Referer':"http://www.amazon.com/", # 'Connection':"keep-alive"} # searchUrl = '' if self.requestHeader: self.payload = self.generatePayload() print bookUrl, bookImgName # with requests.Session() as c:. r = self.session.get(bookUrl, params=self.payload) # print(r.url) print r.status_code if r.status_code == 200: content = r.text soup = BeautifulSoup(content, "html.parser") print r.url b = self.populateBookObj(htmlContent=soup, bookImgName=bookImgName) # print content el = soup.find(id="imgBlkFront", class_="a-dynamic-image") imgUrl = el['src'] if not os.path.exists(os.path.join(b.localImagePath, b.bookImgName)): self.downloadUrl(imageUrl=imgUrl, bookImgName=bookImgName, destinationPath=Workspace().imagePath) else: # print r.text self.getAmazonSingleBookInfo(bookImgName, bookUrl)
def findFolderWithoutBook(self): ''' this method will find all the folder without book. ''' directory_name = Workspace().libraryPath os.chdir(directory_name) listOfDir = [ name for name in os.listdir(directory_name) if os.path.isdir(os.path.join(directory_name, name)) ] if listOfDir: listOfDir.sort(key=int) defaulterList = list() for dir in listOfDir: lst = list() levelOne = os.path.join(directory_name, dir) for sName in os.listdir(levelOne): if os.path.isfile(os.path.join(levelOne, sName)): print sName lst.append(sName.split('.')[-1:][0]) # if 'pdf' not in lst: # defaulterList.append(levelOne) if len(lst) < 3: defaulterList.append(levelOne) print defaulterList
def updateBooksMetadata(self): miss = Missing() listOfDir = miss.availableNumbers() listOfDir = listOfDir[1391:] baseUrl = 'http://it-ebooks.info' for number in listOfDir: print '------------------->', number # url = self.getUrl(baseUrl, number) # a = urllib2.urlopen(url) # strig = a.geturl() # if '404' != strig[-4:-1]: # number=7102 # genUrl=self.downloadItEbook.getUrl(baseUrl, number) try: book = self.findBookDetail(baseUrl, number) book.itEbookUrlNumber = number subUrl = book.bookImgName imageFileName = subUrl.split('/')[-1:][0] book.bookImgName = imageFileName bookPath = os.path.join(Workspace().libraryPath, number) self.writeJsonToDir(bookPath, book) except: traceback.print_exc()