def title(title_id): """ Render title (grid of page thumbnails). """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: render_template("error.html", response="Volume " + title_id + " does not exist") # Get list of images for this title thumbs = Image.select( Image, Volume).join(Volume).where(Volume.id == title_id).order_by(Image.page) # Get list of tags for this title tags = Tag.select(Tag, TagRelation, Volume).join(TagRelation).join(Volume).where( Volume.id == title_id).order_by(Tag.name) # pass list of thumbnails to template return render_template("title.html", title=volume.title, id=str(title_id), thumbs=thumbs, tags=tags)
def page_image(title_id, page_num): """Get archive file corresponding to title ID and member file corresponding to page number Returns extracted image to browser""" # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get page information try: page = Image.select().join(Volume).where((Volume.id == title_id) & (Image.page == page_num)).get() except Image.DoesNotExist: abort(404) if volume.filetype == 'zip': # TODO: Error check on archive open z = zipfile.ZipFile(INPUT_PATH+volume.filename) # Get page binary data # TODO: Error checking zf = z.read(page.filename) z.close() # Return extracted image to browser return send_file(BytesIO(zf), mimetype=page.mimetype) elif volume.filetype == 'rar': # TODO: Error check on archive open rar = rarfile.RarFile(INPUT_PATH+volume.filename) # Get page binary data # TODO: Error checking rardata = rar.read(page.filename) rar.close() # Return extracted image to browser return send_file(BytesIO(rardata), mimetype=page.mimetype) # unrecognised archive type else: abort(500)
def slide(title_id): """Test template for Unslider display, displays Title title_id""" # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get list of images for this title pages = Image.select(Image, Volume).join(Volume).where( Volume.id == title_id).order_by(Image.page) # Pass volume and page details to Unslider template return render_template("slider.html", title_id=title_id, volume=volume, pages=pages)
def show_tag(tag_id): """ Show titles associated with given tag tag_id """ try: tags = Tag.select().where(Tag.id == tag_id).order_by(Tag.name) except Tag.DoesNotExist: return render_template("error.html", response='Tag ' + tag_id + ' does not exist') # Filter Volume on given Tag ID t = Volume.select().join(Tag).where(Tag.id == tag_id).order_by( Volume.title) return render_template("tag.html", tags=tags, title=t)
def title(title_id): """ Render title (grid of thumbnails). Image 0 as blurred CSS background? """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: abort(404) # Get list of images for this title thumbs = Image.select(Image, Volume).join(Volume).where(Volume.id == title_id).order_by(Image.page) # Get list of tags for this title tags = Tag.select(Tag, TagRelation, Volume).join(TagRelation).join(Volume).where(Volume.id == title_id).order_by(Tag.name) # pass list of thumbnails to template return render_template("title.html", title=volume.title, id=str(title_id), thumbs=thumbs, tags=tags)
def filter_tags(filter_string): """ Display list of titles filtered by tags filter_string is dot-separated list of tag IDs Check that each tag id exists """ filter_list = [] filters = filter_string.split = (".") for x in filters: try: tag = Tag.get(id == x) except Tag.DoesNotExist: continue filter_list.append(x) # Filter titles on list of tag IDs t = Volume.select().join(Tag).where(Tag.id.in_(filter_list)) return render_template("tags.html", tags=filter_list, title=t)
def thumb_list(): vols = Volume.select().order_by(Volume.title) return render_template("thumbs.html", vols=vols)
def scan_archive_file(archives, filetype): # Loop over zips & rars, get tags from titles # Add volume to Volume DB # Add tags to Tag DB (many-to-many mapping) # Add pages to Image DB # initialise mimetypes mimetypes.init() # Iterate over input list of archive files for f in archives: # Attempt to scan ZIP file if filetype == 'zip': try: myfile = zipfile.ZipFile(INPUT_PATH + f, 'r') except zipfile.BadZipfile as e: raise Exception( '"{}" is not a valid ZIP file! Error: {}'.format(f, e)) except: print('Unknown error: ZIP extraction failed: {}'.format(f)) raise # Scan RAR file elif filetype == 'rar': try: myfile = rarfile.RarFile(INPUT_PATH + f, 'r') except (rarfile.BadRarFile, rarfile.NotRarFile) as e: raise Exception( '"{}" is not a valid RAR file! Error: {}'.format(f, e)) except: print('Unknown error: RAR extraction failed: {}'.format(f)) raise else: raise ValueError( 'Unrecognised archive type value: {}'.format(filetype)) # Get members from archive members = myfile.namelist() # Filter member list for images members = [x for x in members if is_image(x) is True] # Sort members members = natsorted(members) # Get number of images member_count = len(members) # If images found... if member_count > 0: # Generate MD5 checksum for archive file md5 = md5sum(INPUT_PATH + f) # Parse title, tags from filename title, tags = tag.split_title_tags(f) # Add volume to DB Volume table vol = Volume.create(title=title, filename=f, md5=md5, filetype=filetype, num=member_count, comments='') # Add tags to DB Tags table for t in tags: # check if tag already exists, insert if not try: new_tag = Tag.get(Tag.name == t) except DoesNotExist: new_tag = Tag.create(name=t, descr='') # insert tag and volume id into TagRelation table TagRelation.create(relVolume=vol.id, relTag=new_tag.id) # Reset page counter (assume cover is page 0) page = 0 # Generate display title disptitle = title[:20] if len(disptitle) < len(title): disptitle = disptitle + '...' disptitle.ljust(24) # initialise progress bar display widgets = [ disptitle + ': ', Counter(), '/' + str(member_count) + ' ', Bar(marker='=', left='[', right=']'), ETA() ] pbar = ProgressBar(widgets=widgets, maxval=member_count).start() # Attempt to create thumbnail directory if it doesn't already exist path = THUMB_PATH + str(vol.id) try: os.makedirs(path) except OSError: if not os.path.isdir(path): raise # Iterate over images in zip file for m in members: # Guess mimetype for image from filename (mimetype, encoding) = mimetypes.guess_type(m) # Create record in Image table # >>> TODO: image record should include image height & width im = Image.create(volume=vol.id, page=page, mimetype=mimetype, filename=m) # Generate thumbnails # >>> TODO: May spawn greenlets to do this? # Read data from archive rawdata = myfile.read(m) # Generate Page object p = Page(rawdata) # Create thumbnail p.thumb(path + '/' + '{:03d}'.format(page) + '.jpg') # Update progress bar pbar.update(page) page += 1 # end progress bar pbar.finish() # Close archive myfile.close()
def scan_archive_file(archives, filetype): # Loop over zips & rars, get tags from titles # Add volume to Volume DB # Add tags to Tag DB (many-to-many mapping) # Add pages to Image DB # initialise mimetypes mimetypes.init() # Iterate over input list of archive files for f in archives: # Attempt to scan ZIP file if filetype == 'zip': try: myfile = zipfile.ZipFile(INPUT_PATH+f, 'r') except zipfile.BadZipfile as e: raise Exception('"{}" is not a valid ZIP file! Error: {}'.format(f, e)) except: print('Unknown error: ZIP extraction failed: {}'.format(f)) raise # Scan RAR file elif filetype == 'rar': try: myfile = rarfile.RarFile(INPUT_PATH+f, 'r') except (rarfile.BadRarFile, rarfile.NotRarFile) as e: raise Exception('"{}" is not a valid RAR file! Error: {}'.format(f, e)) except: print('Unknown error: RAR extraction failed: {}'.format(f)) raise else: raise ValueError('Unrecognised archive type value: {}'.format(filetype)) # Get members from archive members = myfile.namelist() # Filter member list for images members = [x for x in members if is_image(x) is True] # Sort members members = natsorted(members) # Get number of images member_count = len(members) # If images found... if member_count > 0: # Generate MD5 checksum for archive file md5 = md5sum(INPUT_PATH+f) # Parse title, tags from filename title, tags = tag.split_title_tags(f) # Add volume to DB Volume table vol = Volume.create(title=title, filename=f, md5=md5, filetype=filetype, num=member_count, comments='') # Add tags to DB Tags table for t in tags: # check if tag already exists, insert if not try: new_tag = Tag.get(Tag.name == t) except DoesNotExist: new_tag = Tag.create(name=t, descr='') # insert tag and volume id into TagRelation table TagRelation.create(relVolume=vol.id, relTag=new_tag.id) # Reset page counter (assume cover is page 0) page = 0 # Generate display title disptitle=title[:20] if len(disptitle) < len(title): disptitle = disptitle+'...' disptitle.ljust(24) # initialise progress bar display widgets = [disptitle+': ', Counter(),'/'+str(member_count)+' ', Bar(marker='=', left='[', right=']'), ETA()] pbar = ProgressBar(widgets=widgets, maxval=member_count).start() # Attempt to create thumbnail directory if it doesn't already exist path = THUMB_PATH + str(vol.id) try: os.makedirs(path) except OSError: if not os.path.isdir(path): raise # Iterate over images in zip file for m in members: # Guess mimetype for image from filename (mimetype, encoding) = mimetypes.guess_type(m) # Create record in Image table # >>> TODO: image record should include image height & width im = Image.create(volume=vol.id, page=page, mimetype=mimetype, filename=m) # Generate thumbnails # >>> TODO: May spawn greenlets to do this? # Read data from archive rawdata = myfile.read(m) # Generate Page object p = Page(rawdata) # Create thumbnail p.thumb(path+'/'+'{:03d}'.format(page)+'.jpg') # Update progress bar pbar.update(page) page += 1 # end progress bar pbar.finish() # Close archive myfile.close()
def page_image(title_id, page_num): """ Get archive file corresponding to title ID and member file corresponding to page number Returns extracted image to browser """ # Get title information try: volume = Volume.get(Volume.id == title_id) except Volume.DoesNotExist: return render_template("error.html", response="Volume " + title_id + " does not exist") # Get page information try: page = Image.select().join(Volume).where( (Volume.id == title_id) & (Image.page == page_num)).get() except Image.DoesNotExist: return render_template("error.html", response="Image " + page_num + " does not exist") if volume.filetype == 'zip': # TODO: Error check on archive open z = zipfile.ZipFile(INPUT_PATH + volume.filename) # Get page binary data try: zf = z.read(page.filename) except zipfile.BadZipFile: return render_template("error.html", response="Bad archive file: " + volume.filename) z.close() # Return extracted image to browser return send_file(BytesIO(zf), mimetype=page.mimetype) elif volume.filetype == 'rar': # TODO: Error check on archive open rar = rarfile.RarFile(INPUT_PATH + volume.filename) # Get page binary data # There must be a cleaner way of fixing paths, but pathlib doesn't seem to work try: rarpath = str(page.filename).replace('\\', '/') rardata = rar.read(rarpath) except rarfile.NoRarEntry: return render_template("error.html", response="No such file: " + rarpath + " in " + volume.filename) rar.close() # Return extracted image to browser return send_file(BytesIO(rardata), mimetype=page.mimetype) # unrecognised archive type else: return render_template("error.html", response="Unrecognised archive type: " + volume.filename)