def make_html(self, user, feeds, save_file=False): if self.template_file: fp = open(self.template_file, 'r') t = template.Template(fp.read()) fp.close() else: t = template.Template(TEMPLATES['book.html']) content = t.generate(user=user, feeds=feeds) if save_file: data_dir = os.path.join(self.work_dir, 'data') if not os.path.exists(data_dir): os.makedirs(data_dir) fp = open( os.path.join( data_dir, 'google-reader-%s.html' % time.strftime('%H-%M-%S')), 'w') fp.write(content) fp.close() return content
def make_mobi(self, user, feeds, format='book', title=''): """make a mobi file using kindlegen""" logging.info("generate .mobi file start... ") data_dir = os.path.join(krconfig.work_dir, 'data') if not os.path.exists(data_dir): os.makedirs(data_dir) for tpl in TEMPLATES: if tpl is 'book.html': continue t = template.Template(TEMPLATES[tpl]) content = t.generate(user=user, title=title, feeds=feeds, uuid=uuid.uuid1(), format=format, mobitime=datetime.utcnow() + krconfig.timezone) with open(os.path.join(data_dir, tpl), 'wb') as fp: fp.write(content) mobi8_file = "KindleReader8-%s.mobi" % ( datetime.utcnow() + krconfig.timezone).strftime('%Y%m%d-%H%M%S') mobi6_file = "KindleReader-%s.mobi" % ( datetime.utcnow() + krconfig.timezone).strftime('%Y%m%d-%H%M%S') opf_file = os.path.join(data_dir, "content.opf") subprocess.call('%s %s -o "%s" > log.txt' % (krconfig.kindlegen, opf_file, mobi8_file), shell=True) # kindlegen生成的mobi,含有v6/v8两种格式 mobi8_file = os.path.join(data_dir, mobi8_file) # kindlestrip处理过的mobi,只含v6格式 mobi6_file = os.path.join(data_dir, mobi6_file) if krconfig.kindlestrip == 1: # 调用kindlestrip处理mobi try: data_file = file(mobi8_file, 'rb').read() strippedFile = SectionStripper(data_file) file(mobi6_file, 'wb').write(strippedFile.getResult()) mobi_file = mobi6_file except Exception, e: mobi_file = mobi8_file logging.error("Error: %s" % e)
def make_mobi(self, user, feeds, format=2): """docstring for make_mobi""" print "generate .mobi file... " data_dir = os.path.join(self.work_dir, 'data') if not os.path.exists(data_dir): os.makedirs(data_dir) for tpl in TEMPLATES: if tpl is 'book.html': continue t = template.Template(TEMPLATES[tpl]) content = t.generate(user=user, feeds=feeds, uuid=uuid.uuid1(), format=format) fp = open(os.path.join(data_dir, tpl), 'wb') fp.write(content) fp.close() mobi_file = "GoogleReader(%s).mobi"% \ time.strftime('%m-%dT%Hh%Mm') opf_file = os.path.join(data_dir, "content.opf") subprocess.call('%s %s -o "%s" > log.txt' % (kindlegen, opf_file, mobi_file), shell=True) mobi_file = os.path.join(data_dir, mobi_file) if os.path.isfile(mobi_file) is False: print "failed!" return None else: fsize = os.path.getsize(mobi_file) print ".mobi save as: %s(%.2fMB)" % (mobi_file, fsize / 1048576) return mobi_file