def get(id): isUser = False story = Story.query.filter_by(id=id).first() if current_user.is_authenticated: if story.user_id == current_user.id: isUser = True if request.method == "POST": if request.form.get("form_name") == "newChapter": user = User.query.filter_by(id=current_user.id).first() chapterName = request.form.get("chapter_name") chapterCodes = request.form.get("newcodes") newChapter = Chapter(name=chapterName, codes=chapterCodes) user.chapters.append(newChapter) story.chapters.append(newChapter) db.session.add(newChapter) db.session.commit() if request.form.get("form_name") == "updateStory": newTitle = request.form.get("newtitle") newDesc = request.form.get("newdesc") print(newTitle, newDesc) story.title = newTitle story.description = newDesc db.session.commit() chapters = story.chapters return render_template("getStory.html", story=story, chapters=chapters, isUser=isUser)
def newChapter(id): story = Story.query.filter_by(id=id).first() chapters = story.chapters if request.method == "POST": if request.form.get("form_name") == "newChapter": user = User.query.filter_by(id=current_user.id).first() chapterName = request.form.get("chapter_name") chapterCodes = request.form.get("newcodes") newChapter = Chapter(name=chapterName, codes=chapterCodes) user.chapters.append(newChapter) story.chapters.append(newChapter) db.session.add(newChapter) db.session.commit() return render_template("newChapter.html", story=story, chapters=chapters)
def add_chapter(id): user_id = request.json.get('user_id') user = User.query.filter_by(id=int(user_id)).first() story = Story.query.filter_by(id=id).first() # story = Story.query.get(21) chapter = Chapter(name=request.json.get('title'), codes=request.json.get('codes')) user.chapters.append(chapter) if story.chapters is None: story.chapters.create(chapter) else: story.chapters.append(chapter) db.session.add(chapter) db.session.commit() return jsonify({'message': 'New chapter successfully created.'}), 200
def home(): if request.form: user = User.query.filter_by(id=current_user.id).first() chapter = Chapter(name=request.form.get("chapter_name"), codes=request.form.get("codes")) story = Story(title=request.form.get("title"), description=request.form.get("description")) user.stories.append(story) user.chapters.append(chapter) story.chapters.append(chapter) db.session.add(story) db.session.add(chapter) db.session.commit() stories = Story.query.all() #return render_template("home.html", stories = stories) return render_template("index.html", stories=stories)
def get_vertex(tag, element, law): if tag == Tag.Chapter: vertex = Chapter(law, element) elif tag == Tag.Point: vertex = Point(law, element) elif tag == Tag.Section: vertex = Section(law, element) elif tag == Tag.Part: vertex = Part(law, element) elif tag == Tag.Appendix: vertex = Appendix(law, element) elif tag == Tag.Preamble: vertex = Preamble(law, element) elif tag == Tag.Subtitle: vertex = Subtitle(law, element) elif tag == Tag.WrapUp: vertex = WrapUp(law, element) elif tag == Tag.Law: vertex = law else: raise Exception('Unexpected behavior') return vertex
def crawl_save_chapters(): chapter_list = crawl_chapter_list(CHAPTER_URL) for chapter in chapter_list: chapter_item = Chapter(id=chapter['id'], name=chapter['name'], url=chapter['url']) session.merge(chapter_item) session.commit()
def fetchall(connection): stories = [] retry = True sesh = None headers = { 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'accept-encoding': 'gzip', 'accept-language': 'en-US,en;q=0.9', 'cache-control': 'no-cache', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Connection': 'keep-alive' } if (not empty_db(connection)): return 0 session = requests.Session() with session as s: retry = True while retry: username = input("Enter RoyalRoadL username: "******"Enter RoyalRoadL password: "******"Attempting to log in to RoyalRoadL...") print() p = s.post(url, data=payload, headers=headers) if p.url.endswith("loginsuccess"): retry = False else: print("Invalid login. Please try again") print("Parsing data...") p = s.get(url2 + '/my/bookmarks') # Create soup to parse html soup = BeautifulSoup(str(p.text), "lxml") # Search soup for all links to other bookmark pages bookmark_number = RoyalRoadLSoupParser.grab_bookmark_number(soup) # Begin traversing bookmark pages to read story titles, links, and authors for i in range(1, bookmark_number, 1): # Load new page to get story titles, links, and authors p = s.get(url2 + suffix + str(i)) # Create soup to parse html soup = BeautifulSoup(str(p.text), "lxml") # Search soup for all story titles, authors, and links r = RoyalRoadLSoupParser.grab_story_titles_authors_links(soup) for i in range(0, len(r) - 1, 2): newStory = Story() newStory.setTitle(r[i].text) newStory.setStoryLink(r[i]['href']) newStory.setAuthor(r[i + 1].text) newStory.setAuthorLink(r[i + 1]['href']) # Add story to the list stories.append(newStory) try: # Begin traversing bookmarked stories to read chapter names, links, and upload times for i in range(0, len(stories) - 1, 1): # Grab link to new story story_link = stories[i].getStoryLink() # Load new page to get chapter titles, links, and upload times p = s.get(url2 + story_link) # Create soup to parse html soup = BeautifulSoup(str(p.text), 'lxml') counter = 0 to_skip = 0 times = soup.find_all("time", unixtime=True, format='ago') links = [a for a in soup.find_all("a", href=True) if a['href'].startswith(stories[i].getStoryLink()) \ and not a['href'].startswith(stories[i].getStoryLink() + '?reviews=')] for a in links: # Skips first valid link as its duplicated if to_skip == 0: to_skip = 1 elif to_skip == 1: # Create new chapter newChapter = Chapter() newChapter.setStoryLink(story_link) newChapter.setChapterLink(a['href']) newChapter.setChapterName((a.get_text()).strip()) newChapter.setChapterTime(times[counter]['unixtime']) stories[i].addChapter(newChapter) counter += 1 except Exception as e: print(e) exit() try: # STORE DATA IN MYSQL DB for i in stories: connection.store_story(i.getTitle(), i.getAuthor(), i.getStoryLink(), str(i.getLastUpdated())) chaps = i.getChapters() for a in range(0, i.getChapterCount(), 1): chapt = chaps[a] connection.store_chapter(i.getStoryLink(), chapt.getChapterName(), chapt.getChapterLink(), str(chapt.getChapterTime())) s.__str__() except Exception as e: print(e) exit() return 1