def add_chapter(manga, chapter_name, chapter_num, pages): # Ensure that chapter_num is a float chapter_num = float(chapter_num) # Ensure that the manga does not have a chapter with the same number if manga.chapters.filter_by(num=chapter_num).first(): return new_chapter = Chapter(chapter_name, chapter_num, manga) # Representation is different if its an integer num_string = chapter_to_string(chapter_num) # Make a new folder for the chapter url = new_chapter.manga.url + "/Chapter_" + num_string # Rename and add all the chapter pages curr_page = 1 for page in pages: # Skip resource forks if "__MACOSX" in page.filename: continue filename = rename(page, "%03d" % curr_page) # Make sure nothing that isn't an image file doesn't get through. if not allowed_file(page.filename): continue new_page = Page(curr_page, save_file(page, url, filename), new_chapter) db.session.add(new_page) # Remember to increment curr_page curr_page += 1 zip_directory(manga, num_string, url) # Manga has been updated, so update the last updated date manga.last_updated = datetime.utcnow() # Add and commit to the database db.session.add(new_chapter) db.session.commit()
def fake_chapter(count=1000): fake = Faker() course_count = Course.query.count() for i in range(count): u = Course.query.offset(randint(0, course_count - 1)).first() c = Chapter(name=fake.name(), course=u) db.session.add(c) db.session.commit()
def setUp(self) -> None: super().setUp() for img in RefImage.query.filter(~RefImage.id.in_({1, 2})).all(): db.session.delete(img) FavoriteStory.query.delete() FollowingStory.query.delete() Chapter.query.delete() Story.query.delete() User.query.delete() users = [User(**data) for data in USERDATA] db.session.add_all(users) db.session.commit() self.user_ids = [id for id in map(lambda user: user.id, users)] stories = [ Story(**STORYDATA[i], author_id=self.user_ids[i]) for i in range(len(STORYDATA)) ] db.session.add_all(stories) db.session.commit() self.story_ids = [id for id in map(lambda story: story.id, stories)] chapters = [ Chapter(**data, story_id=self.story_ids[0]) for data in CHAPTERDATA1 ] chapters += [ Chapter(**data, story_id=self.story_ids[1]) for data in CHAPTERDATA2 ] chapters += [ Chapter(**data, story_id=self.story_ids[2]) for data in CHAPTERDATA3 ] db.session.add_all(chapters) db.session.commit() self.chapter_ids = [ id for id in map(lambda chapter: chapter.id, chapters) ] self.client = app.test_client()
def get_chapter_list(cls, links): chapters = [] for url in links: soup = get_soup(url) html = cls.get_chapter_html(soup) chapter_title = cls.get_chapter_title(html) print('Retrieving contents of Chapter %s""', chapter_title) chapter_text = cls.get_chapter_contents(html) chapters.append(Chapter(chapter_title, chapter_text)) return chapters
def get_chapter(html): soup = BeautifulSoup(html, 'html.parser') list = soup.find_all('li', class_="c3") session = Session_class() for i in list: body = get_context("http://www.quanben5.com" + i.a['href']) chapter = Chapter(href="http://www.quanben5.com" + i.a['href'], name=i.a.span.get_text(), chapter_text=body) print(i.a.span.get_text()) session.add(chapter) session.commit()
def add_chapter(story_id): new_chapter_data = json.loads(request.data) new_chapter = Chapter(number=new_chapter_data['number'], title=new_chapter_data['title'], synopsis=new_chapter_data['synopsis'], story_id=story_id) # Try to add the new chapter try: insert(new_chapter) except Exception as e: abort(500) return jsonify({'success': True, 'updated': return_object})
def parseChapter(url, titleFilter=""): soup = getSoup(url) title = str(soup.find(class_='entry-title').string) post = soup.find(class_='entry-content') title = title.replace(titleFilter, "") print('Parsing Chapter at %s: "%s"' % (url, title)) elements = [] for e in post.find_all(): elements.append(e) doc = buildChapterContent(title, elements) return Chapter(title, doc)
def insert_course(course_id): chapter = Chapter(parent_id=0, course_id=course_id) db.session.add(chapter) db.session.commit() return redirect(url_for('modify_chapter', course_id=course_id))
def insert_chapter(chapter_id): chapter1 = Chapter.query.filter_by(id=chapter_id).first_or_404() course_id = chapter1.course_id chapter = Chapter(parent_id=chapter_id, course_id=course_id) chapter.insert() return redirect(url_for('modify_chapter', course_id=course_id))