Skip to content

Commit

Permalink
1.26.2
Browse files Browse the repository at this point in the history
支持用户上传CSS样式表文件来定制推送到书籍样式。
  • Loading branch information
cdhigh committed Sep 14, 2019
1 parent f9bbb42 commit 15c56aa
Show file tree
Hide file tree
Showing 52 changed files with 542 additions and 726 deletions.
44 changes: 44 additions & 0 deletions apps/View/Adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,50 @@ def POST(self):
ret['status'] = str(e)

return json.dumps(ret)

#在本地选择一个样式文件上传做为所有书籍的样式
class AdvUploadCss(BaseHandler):
__url__ = "/advuploadcss"
@etagged()
def GET(self, tips=None):
user = self.getcurrentuser()
return self.render('advuploadcss.html', "Stylesheet", current='advsetting',
user=user, advcurr='uploadcss', formaction=AdvUploadCssAjax.__url__,
deletecsshref=AdvDeleteCssAjax.__url__, tips=tips)

#AJAX接口的上传CSS处理函数
class AdvUploadCssAjax(BaseHandler):
__url__ = "/advuploadcssajax"
def POST(self):
ret = 'ok'
user = self.getcurrentuser(forAjax=True)
try:
x = web.input(cssfile={})
file_ = x['cssfile'].file
if user and file_:
#这里应该要验证样式表的有效性,但是现在先忽略了
user.css_content = db.Text(file_.read(), encoding="utf-8")
user.put()
except Exception as e:
ret = str(e)

return ret

#删除上传的CSS
class AdvDeleteCssAjax(BaseHandler):
__url__ = "/advdeletecssajax"
def POST(self):
ret = {'status': 'ok'}
user = self.getcurrentuser(forAjax=True)
try:
confirmKey = web.input().get('action')
if user and confirmKey == 'delete':
user.css_content = ''
user.put()
except Exception as e:
ret['status'] = str(e)

return json.dumps(ret)

#集成各种网络服务OAuth2认证的相关处理
class AdvOAuth2(BaseHandler):
Expand Down
1 change: 1 addition & 0 deletions apps/View/Library.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def POST(self):
dbItem = SharedRss.all().filter('url = ', url).get()
prevCategory = ''
if dbItem:
dbItem.title = title
dbItem.isfulltext = isfulltext
dbItem.invalid_report_days = 0
if category:
Expand Down
2 changes: 1 addition & 1 deletion apps/Work/Url2Book.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jinja2
from apps.BaseHandler import BaseHandler
from apps.dbModels import *
from apps.utils import InsertToc, local_time
from apps.utils import local_time
from lib.makeoeb import *

from books.base import BaseUrlBook
Expand Down
14 changes: 9 additions & 5 deletions apps/Work/Worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from collections import OrderedDict
from apps.BaseHandler import BaseHandler
from apps.dbModels import *
from apps.utils import InsertToc, local_time, get_exc_location
from apps.utils import local_time, get_exc_location
from lib.makeoeb import *
from calibre.ebooks.conversion.mobioutput import MOBIOutput
from calibre.ebooks.conversion.epuboutput import EPUBOutput
Expand Down Expand Up @@ -186,6 +186,7 @@ def GET(self):
# 对于html文件,变量名字自文档,thumbnail为文章第一个img的url
# 对于图片文件,section为图片mime,url为原始链接,title为文件名,content为二进制内容,
# img的thumbail仅当其为article的第一个img为True
# 对于CSS文件,sec_or_media 为 'text/css',url 和 title 都为文件名
try: #书的质量可能不一,一本书的异常不能影响其他书籍的推送
for sec_or_media, url, title, content, brief, thumbnail in book.Items():
if not sec_or_media or not title or not content:
Expand All @@ -197,15 +198,18 @@ def GET(self):
if thumbnail:
toc_thumbnails[url] = href
imgindex += 1
elif sec_or_media == 'text/css':
if url not in oeb.manifest.hrefs: #Only one css needed
oeb.manifest.add('css', url, sec_or_media, data=content)
else:
#id, href = oeb.manifest.generate(id='feed', href='feed%d.html'%itemcnt)
#item = oeb.manifest.add(id, href, 'application/xhtml+xml', data=content)
#oeb.spine.add(item, True)
sections.setdefault(sec_or_media, [])
sections[sec_or_media].append((title, brief, thumbnail, content))
itemcnt += 1
except:
main.log.exception(u"Failed to push <%s>" % book.title)
except Exception as e:
main.log.exception(u"Failed to push <%s>, Err:%s" % (book.title, str(e)))
continue

volumeTitle = ''
Expand Down Expand Up @@ -472,9 +476,9 @@ def push_comic_book(self, username, user, book, opts=None):

def ProcessComicRSS(self, username, user, feed):
opts = getOpts(user.device, "comic")
for ComicBaseClass in ComicBaseClasses:
for comicClass in ComicBaseClasses:
if feed.url.startswith(ComicBaseClass.accept_domains):
book = ComicBaseClass(opts=opts, user=user)
book = comicClass(opts=opts, user=user)
break
else:
msg = u"No base class for {}".format(feed.title)
Expand Down
2 changes: 1 addition & 1 deletion apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import __builtin__, sys
from google.appengine.ext import vendor

__Version__ = '1.26.1'
__Version__ = '1.26.2'

__builtin__.__dict__['__Version__'] = __Version__

Expand Down
1 change: 1 addition & 0 deletions apps/dbModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class KeUser(db.Model): # kindleEar User
browser = db.BooleanProperty()
qrcode = db.BooleanProperty() #是否在文章末尾添加文章网址的QRCODE
cover = db.BlobProperty() #保存各用户的自定义封面图片二进制内容
css_content = db.TextProperty() #added 2019-09-12 保存用户上传的css样式表

book_mode = db.StringProperty() #added 2017-08-31 书籍模式,'periodical'|'comic',漫画模式可以直接全屏
expiration_days = db.IntegerProperty() #added 2018-01-07 账号超期设置值,0为永久有效
Expand Down
113 changes: 0 additions & 113 deletions apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,119 +96,6 @@ def wrapper(*args, **kwds):
return wrapper
return decorator

#创建OEB的两级目录,主要代码由rexdf贡献
#sections为有序字典,关键词为段名,元素为元组列表(title,brief,humbnail,content)
#toc_thumbnails为字典,关键词为图片原始URL,元素为其在oeb内的href。
def InsertToc(oeb, sections, toc_thumbnails, insertHtmlToc=True, insertThumbnail=True):
css_pat = r'<style type="text/css">(.*?)</style>'
css_ex = re.compile(css_pat, re.M | re.S)
body_pat = r'(?<=<body>).*?(?=</body>)'
body_ex = re.compile(body_pat, re.M | re.S)

num_articles = 1
num_sections = 0

ncx_toc = []
#html_toc_2 secondary toc
html_toc_2 = []
name_section_list = []
for sec in sections.keys():
css = ['.pagebreak{page-break-before:always;}h1{font-size:2.0em;}h2{font-size:1.5em;}h3{font-size:1.4em;}h4{font-size:1.2em;}h5{font-size:1.1em;}h6{font-size:1.0em;}']
html_content = []
secondary_toc_list = []
first_flag = False
sec_toc_thumbnail = None
for title, brief, thumbnail, content in sections[sec]:
#获取自定义的CSS
for css_obj in css_ex.finditer(content):
if css_obj and css_obj.group(1) and css_obj.group(1) not in css:
css.append(css_obj.group(1))

if first_flag:
html_content.append('<div id="%d" class="pagebreak">' % (num_articles)) #insert anchor && pagebreak
else:
html_content.append('<div id="%d">' % (num_articles)) #insert anchor && pagebreak
first_flag = True
if thumbnail:
sec_toc_thumbnail = thumbnail #url

#将body抽取出来
body_obj = re.search(body_ex, content)
if body_obj:
html_content.append(body_obj.group()+'</div>') #insect article
secondary_toc_list.append((title, num_articles, brief, thumbnail))
num_articles += 1
else:
html_content.pop()
html_content.append('</body></html>')

html_content.insert(0, '<html><head><title>%s</title><style type="text/css">%s</style></head><body>' % (sec, ''.join(css)))

#add section.html to maninfest and spine
#We'd better not use id as variable. It's a python builtin function.
id_, href = oeb.manifest.generate(id='feed', href='feed%d.html' % num_sections)
item = oeb.manifest.add(id_, href, 'application/xhtml+xml', data=''.join(html_content))
oeb.spine.add(item, True)

#在目录分类中添加每个目录下的文章篇数
sec_with_num = '%s (%d)' % (sec, len(sections[sec]))
ncx_toc.append(('section', sec_with_num, href, '', sec_toc_thumbnail)) #Sections name && href && no brief

#generate the secondary toc
if insertHtmlToc:
html_toc_ = ['<html><head><title>toc</title></head><body><h2>%s</h2><ol>' % (sec_with_num)]
for title, anchor, brief, thumbnail in secondary_toc_list:
if insertHtmlToc:
html_toc_.append('&nbsp;&nbsp;&nbsp;&nbsp;<li><a href="%s#%d">%s</a></li><br />'%(href, anchor, title))
ncx_toc.append(('article',title, '%s#%d'%(href,anchor), brief, thumbnail)) # article name & article href && article brief
if insertHtmlToc:
html_toc_.append('</ol></body></html>')
html_toc_2.append(html_toc_)
name_section_list.append(sec_with_num)

num_sections += 1

if insertHtmlToc:
#Generate HTML TOC for Calibre mostly
##html_toc_1 top level toc
html_toc_1 = [u'<html><head><title>Table Of Contents</title></head><body><h2>%s</h2><ul>'%(TABLE_OF_CONTENTS)]
html_toc_1_ = []
#We need index but not reversed()
for a in xrange(len(html_toc_2)-1,-1,-1):
#Generate Secondary HTML TOC
id_, href = oeb.manifest.generate(id='section', href='toc_%d.html' % (a))
item = oeb.manifest.add(id_, href, 'application/xhtml+xml', data=" ".join(html_toc_2[a]))
oeb.spine.insert(0, item, True)
html_toc_1_.append('&nbsp;&nbsp;&nbsp;&nbsp;<li><a href="%s">%s</a></li><br />'%(href,name_section_list[a]))
html_toc_2 = []
for a in reversed(html_toc_1_):
html_toc_1.append(a)
html_toc_1_ = []
html_toc_1.append('</ul></body></html>')
#Generate Top HTML TOC
id_, href = oeb.manifest.generate(id='toc', href='toc.html')
item = oeb.manifest.add(id_, href, 'application/xhtml+xml', data=''.join(html_toc_1))
oeb.guide.add('toc', 'Table of Contents', href)
oeb.spine.insert(0, item, True)

#Generate NCX TOC for Kindle
po = 1
toc = oeb.toc.add(unicode(oeb.metadata.title[0]), oeb.spine[0].href, id='periodical', klass='periodical', play_order=po)
po += 1
for ncx in ncx_toc:
if insertThumbnail and ncx[4]:
toc_thumbnail = toc_thumbnails[ncx[4]]
else:
toc_thumbnail = None

if ncx[0] == 'section':
sectoc = toc.add(unicode(ncx[1]), ncx[2], klass='section', play_order=po, id='Main-section-%d'%po,
toc_thumbnail=toc_thumbnail)
elif sectoc:
sectoc.add(unicode(ncx[1]), ncx[2], description=ncx[3] if ncx[3] else None, klass='article', play_order=po,
id='article-%d'%po, toc_thumbnail=toc_thumbnail)
po += 1

#-----------以下几个函数为安全相关的
def new_secret_key(length=8):
import random
Expand Down
21 changes: 0 additions & 21 deletions books/D5yuansu.py

This file was deleted.

64 changes: 0 additions & 64 deletions books/Gongshi.py

This file was deleted.

19 changes: 0 additions & 19 deletions books/ZhihuDailyRss.py

This file was deleted.

Loading

0 comments on commit 15c56aa

Please sign in to comment.