def GET(self, id): db = NovelDB() novel = db.get_novel_info(id, cache=False) if novel is None or novel.status in (0, 100): raise web.notfound() db.incr_view_count(id) # 获得推荐小说 rd_recommends = db.get_random_recommend(id=id) page = web.input(page=1).page page_count = db.get_novel_pageinfo(id) pager = Paginator(page_count, 1, page) try: novel_html = open(get_novel_html_path(id, pager.page), "r") except: return "内容还没准备好,稍等一会哦:)" # 访问twisted更新 temp_date = novel.last_update_time.replace(tzinfo=None) if not pager.has_next() and novel.status not in (1,3) and (datetime.datetime.now() - temp_date).seconds > 3600: start_work(id) novel = db.get_novel_info(id, cache=False) novel.tag = db.get_tags_by_id(id) novel.view_count = db.get_view_count(id) return render_to_response("show_novel", { "novel_id": id, "novel": novel, "novel_html": novel_html, "pager": pager, "rd_recommends": rd_recommends, })
def init_txt(id, title): """ 初始化txt文件 """ root = get_novel_html_path(id) obj_file = open(_path(root, FILE_NAME), "w") files = [x for x in os.listdir(root) if x.endswith(".html")] files.sort(key=lambda x:int(x.split(".")[0])) files = [_path(root, x) for x in files] obj_file.write( windows_mode(HEAD % (title, id)) ) for file in files: content = open(file, 'r').read().replace("[设为书签]", "") content = CONTENT_TOP_RE.sub('', content) content = replace_br(content) content = TAG_RE.sub("", content) content = HH_RE.sub("\n\n", content) content = content.replace(" ", " ")\ .replace("<", "<")\ .replace( ">", ">")\ .replace(""", '"')\ .replace("&", "&")\ .replace("\n", "\r\n") content = replace_html_entity(content) obj_file.write( windows_mode(content) ) obj_file.close()
def GET(self, id): db = NovelDB() novel = db.get_novel_info(id, cache=False) if novel is None or novel.status in (0, 100): raise web.notfound() db.incr_view_count(id) # 获得推荐小说 rd_recommends = db.get_random_recommend(id=id) page = web.input(page=1).page page_count = db.get_novel_pageinfo(id) pager = Paginator(page_count, 1, page) try: novel_html = open(get_novel_html_path(id, pager.page), "r") except: return "内容还没准备好,稍等一会哦:)" # 访问twisted更新 temp_date = novel.last_update_time.replace(tzinfo=None) if not pager.has_next() and novel.status not in ( 1, 3) and (datetime.datetime.now() - temp_date).seconds > 3600: start_work(id) novel = db.get_novel_info(id, cache=False) novel.tag = db.get_tags_by_id(id) novel.view_count = db.get_view_count(id) return render_to_response( "show_novel", { "novel_id": id, "novel": novel, "novel_html": novel_html, "pager": pager, "rd_recommends": rd_recommends, })
def get_txt_file(id, title): """ 获得小说txt,逻辑: 首先查找目录下的nowater.txt文件,如果存在而且最后修改时间到现在不超过两个小时 则直接返回,否则生成txt文件后返回 """ root = get_novel_html_path(id) file_path = _path(root, FILE_NAME) if not (os.path.exists(file_path) and (time.time() - os.path.getmtime(file_path)) < 1800): init_txt(id, title) # size = get_filesize(file_path) return file_path[1:]