def __init__(self, *, url, novel, serial_number): super().__init__(url) self.novel = novel self.chapter = Chapter() self.chapter.serial = serial_number self.chapter.novel_id = self.novel.id self.text = Text()
def post(self): try: section_id = self.request.GET['section_id'] except: self.redirect("/error?msg=Key missing for deletion.") return user = users.get_current_user() if user: try: section = ndb.Key(urlsafe=section_id).get() chapter = Chapter.get_by_id(section.chapter) story = Story.get_by_id(chapter.story) except: self.redirect("/error?msg=Key was not found.") return self.redirect("/info?msg=Section deleted: " + story.title.encode("ascii", "replace") + ": " + chapter.title.encode("ascii", "replace") + " - " + str(section.num) + "&url=/manage_sections?chapter_id=" + chapter.key.urlsafe()) # Remove story model.remove_data.remove_section(section.key) else: self.redirect("/")
def remove_all_chapters_of(story_key): chapters_keys = Chapter.query(Chapter.story == story_key.id()).fetch( keys_only=True) for chapter_key in chapters_keys: remove_all_sections_of(chapter_key) ndb.delete_multi(chapters_keys)
def get(self): try: id = self.request.GET['story_id'] except: self.redirect("/error?msg=story was not found") return user = users.get_current_user() if user: try: story = ndb.Key(urlsafe=id).get() except: self.redirect("/error?msg=Key was not found.") return num_chapters = len(Chapter.query(Chapter.story == story.key.id()).fetch(keys_only=True)) + 1 chapter = Chapter() chapter.story = story.key.id() chapter.num = num_chapters chapter.title = "Untitled " + str(num_chapters) chapter.summary = "An awesome chapter." key = model.chapter.update(chapter) self.redirect("/chapters/modify?story_id=" + story.key.urlsafe() + "&chapter_id=" + key.urlsafe()) else: self.redirect("/") return
def count(story_id): """Extracts the stats from a complete story. :param story_id: The id for a story to extract stats from. """ toret = {"crs": 0, "ws": 0, "pgs": 0} chapter_keys = Chapter.query(Chapter.story == story_id).fetch( keys_only=True) # Access all chapters for chapter_key in chapter_keys: extend_dictionary(toret, count_chapter(chapter_key.id())) return toret
def get(self): id = self.request.GET.get('story_id') if not id: self.redirect("/error?msg=Key missing for exporting.") return user = users.get_current_user() if user: user_name = user.nickname() access_link = users.create_logout_url("/") try: story = ndb.Key(urlsafe=id).get() # Collect all chapters chapters = Chapter.query( Chapter.story == story.key.id()).order(Chapter.num) # Collect all sections all_sections = {} for chapter in chapters: # Retrieve all sections sections = Section.query( Section.chapter == chapter.key.id()).order(Section.num).fetch() all_sections[chapter.num] = sections except: self.redirect("/error?msg=Key was not found.") return template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "chapters": chapters, "all_sections": all_sections } jinja = jinja2.get_jinja2(app=self.app) self.response.headers["target"] = "_blank" self.response.write(jinja.render_template("export_story.html", **template_values)); else: self.redirect("/")
def post(self): try: chapter_id = self.request.GET['chapter_id'] except: self.redirect("/error?msg=missing key for modifying") return user = users.get_current_user() if user: # Get story and chapter by key try: chapter = ndb.Key(urlsafe=chapter_id).get() story = Story.get_by_id(chapter.story) except: self.redirect("/error?msg=key does not exist") return chapter.title = self.request.get("title", "").strip() chapter.summary = self.request.get("summary", "").strip() # Chk if len(chapter.title) < 1: self.redirect("/error?msg=Aborted modification: missing title") return # Chk title existing_chapters = Chapter.query(Chapter.title == chapter.title) if (existing_chapters and existing_chapters.count() > 0 and existing_chapters.get() != chapter): self.redirect("/error?msg=Chapter with title \"" + chapter.title.encode("ascii", "replace") + "\" already exists.") return # Save model.chapter.update(chapter) self.redirect("/info?msg=Chapter modified: \"" + (story.title + ": " + chapter.title).encode("ascii", "replace") + "\"&url=/manage_chapters?story_id=" + story.key.urlsafe()) else: self.redirect("/")
def post(self): try: section_id = self.request.GET['section_id'] except: self.redirect("/error?msg=missing key for modification") return user = users.get_current_user() if user: try: section = ndb.Key(urlsafe=section_id).get() chapter = Chapter.get_by_id(section.chapter) story = Story.get_by_id(chapter.story) except: self.redirect("/error?msg=key does not exist") return section.text = self.request.get("text", "").strip() # Chk if len(section.text) < 1: self.redirect( "/error?msg=Aborted modification: missing section's text") return if section.num < 1: self.redirect( "/error?msg=Aborted modification: missing section's number" ) return # Save model.section.update(section) self.redirect("/info?msg=Section modified: \"" + (story.title + ": " + chapter.title).encode("ascii", "replace") + ": " + str(section.num) + "\"&url=/manage_sections?chapter_id=" + chapter.key.urlsafe()) else: self.redirect("/")
def get(self): try: section_id = self.request.GET['section_id'] except: self.redirect("/error?msg=missing key for modification") return user = users.get_current_user() if user: user_name = user.nickname() access_link = users.create_logout_url("/") try: section = ndb.Key(urlsafe=section_id).get() chapter = Chapter.get_by_id(section.chapter) story = Story.get_by_id(chapter.story) characters = Character.query(Character.story == story.key.id()) except: self.redirect("/error?msg=key does not exist") return template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "characters": characters, "chapter": chapter, "section": section } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("modify_section.html", **template_values)) else: self.redirect("/")
def get(self): user = users.get_current_user() if user: user_name = user.nickname() try: id = self.request.GET['story_id'] except: id = None if not id: self.redirect("/error?msg=Key missing for management.") return story = ndb.Key(urlsafe=id).get() chapters = Chapter.query(Chapter.story == story.key.id()).order( Chapter.num) access_link = users.create_logout_url("/") total_stats = count(story.key.id()) template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "chapters": chapters, "total_crs": total_stats["crs"], "total_ws": total_stats["ws"], "total_pgs": total_stats["pgs"] } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("chapters.html", **template_values)) else: self.redirect("/") return
def get(self): try: id = self.request.GET['section_id'] except: self.redirect("/error?msg=Key missing for deletion.") return user = users.get_current_user() if user: user_name = user.nickname() access_link = users.create_logout_url("/") try: section = ndb.Key(urlsafe=id).get() chapter = Chapter.get_by_id(section.chapter) story = Story.get_by_id(chapter.story) except: self.redirect("/error?msg=Key was not found.") return template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "chapter": chapter, "section": section } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("delete_section.html", **template_values)) else: self.redirect("/")
def get_chapter(self, chapter_param): log.info("get_chapter function is call") log.info(chapter_param) book_title = None book_thumb = None book_author = None chapter_title = None chapter_content = None upper_char = None url = system_cfg.CHAPTER_URL chapter_param = html_tool.decode_param_to_dict(chapter_param) site_rs = self.try_request(url, 'POST', data=chapter_param) if site_rs: content_list = site_rs.content.split('--!!tach_noi_dung!!--', 3) if len(content_list) >= 3: ##################### # get book_thumb from css ##################### css_soup = BeautifulSoup(content_list[0], 'html.parser') style_tag = css_soup.find('style') if style_tag: thumb_re = re.search('background:url\((http://(\w|\W)*)\)', style_tag.string) if thumb_re: book_thumb = thumb_re.group(1) ##################### # get book title # get book author # get chapter title ##################### desc_soup = BeautifulSoup(content_list[1], 'html.parser') book_title_tag = desc_soup.find('span', class_='chuto40') if book_title_tag: book_title = book_title_tag.string.strip() tuade_tag = desc_soup.find('div', class_='tuade') if tuade_tag: chutieude_tags = desc_soup.find_all('span', class_='chutieude') chutieude_list = [] for chutieude_tag in chutieude_tags: if chutieude_tag.string and chutieude_tag.string.strip(): chutieude_list.append(chutieude_tag.string.strip()) if len(chutieude_list) >= 2: book_author = chutieude_list[0] del chutieude_list[0] for chutieude in chutieude_list: if chapter_title: chapter_title = chapter_title + chutieude + " " else: chapter_title = chutieude + " " elif len(chutieude_list) == 1: chapter_title = chutieude_list[0] else: tac_gia_tag = desc_soup.find('span', class_='tacgiaphai') if tac_gia_tag: book_author = tac_gia_tag.string.strip() chutieude_tags = desc_soup.find_all('span', class_='chutieude') chutieude_list = [] for chutieude_tag in chutieude_tags: if chutieude_tag.text and chutieude_tag.text.strip(): chutieude_list.append(chutieude_tag.text.strip()) if len(chutieude_list) == 2: chapter_title = chutieude_list[0] + ": " + chutieude_list[1] elif len(chutieude_list) == 1: chapter_title = chutieude_list[0] ##################### # get chapter content( add chapter title to chapter content) ##################### content_soup = BeautifulSoup(content_list[2], 'html.parser') if content_soup: chuhoain_tag = content_soup.find(id='chuhoain') if chuhoain_tag: chuinhoa_img = chuhoain_tag.img if chuinhoa_img: chuhoain_src = chuinhoa_img['src'] if (isinstance(chuhoain_src, str) or isinstance(chuhoain_src, unicode)) and chuhoain_src.find( system_cfg.UPPER_CHAR_URL) != -1: chuhoain = chuhoain_src.replace(system_cfg.UPPER_CHAR_URL, '') chuinhoa_img['src'] = system_cfg.UPPER_CHAR_PATH + chuhoain upper_char = chuhoain chapter_content = content_soup.prettify() # Add chapter title to chapter_content if chapter_title and chapter_content: chapter_content = '<div><h2 align=\'center\'>' + chapter_title + '</h2></div>' + chapter_content chapter = Chapter(title=chapter_title, content=chapter_content) if book_title: chapter.set_book_title(book_title) if book_author: chapter.set_book_author(book_author) if book_thumb: chapter.set_book_thumb(book_thumb) if upper_char: chapter.set_upper_char(upper_char) log.info("Crawler chapter: %s", chapter_title) return chapter else: log.error("Can't get content of this chapter") return None