def m(book): p_green("[I] Saving in epub...") p_green("[I] Meta and information generation...") characters = book["characters"] bookr = epub.EpubBook() id_gen = "wattpad" + str(book["id"]) bookr.set_identifier(id_gen) bookr.set_title(book["title"]) bookr.set_language(book["description"]) bookr.add_author(book["authors"]) bookr.add_item(epub.EpubNcx()) bookr.add_item(epub.EpubNav()) bookr.set_cover("cover.jpg", book["cover"]) book_spine = ['cover', 'nav'] p_green("[I] Characters generation...") book_characters = [] count_characters = 1 for one_char in characters: print('[{c_num}/{c_all}] Generation "{chapter_title}": {chapter_id}'. format(c_num=str(count_characters), c_all=str(len(characters) + 1), chapter_title=one_char["title"], chapter_id=one_char["id"])) capt = chapter_gen(one_char["text"]) captr = epub.EpubHtml(title=one_char["title"], file_name='chap_' + str(count_characters) + '.xhtml', lang='hr') count_characters += 1 captr.content = capt["text"] book_spine.append(captr) book_characters.append(captr) bookr.add_item(captr) bookr.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'), (epub.Section('Simple book'), tuple(book_characters))) p_green("[I] Saving to epub...") save_name = os.getcwd() + "/downloads/" + str( book["id"]) + " - " + save_file.rename_valid_f(book["title"]) + ".epub" bookr.spine = book_spine epub.write_epub(save_name, bookr, {}) p_green("[I] Saved to epub")
def m(book): p_green("[I] Saving in txt...") info_book = templ_main().format(book["title"], book["authors"], book["description"], book["source"]) save_file.save_txt_info(book["id"] + " - " + book["title"], info_book) chapters = book["characters"] chapter_count = 0 for capter in chapters: chapter_count += 1 print( '[{c_num}/{c_all}] Saving "{chapter_title}": {chapter_id}'.format( c_num=str(chapter_count), c_all=str(len(chapters)), chapter_title=capter["title"], chapter_id=capter["id"])) chapter_text = "" title = capter["title"] text = capter["text"] text = text.replace("<br>", "\n") soup = BeautifulSoup(text, 'html5lib') tags = soup.find_all('img', src=True) for ii in range(len(tags)): img_tag_url = (tags[ii])["src"] save_file.save_url_name( "#" + str(chapter_count) + "~" + str(ii + 1) + ".jpg", book["id"] + " - " + book["title"], img_tag_url) img_tag = soup.select_one("img:nth-of-type(1)") img_tag.decompose() tags = soup.find_all("p") for tag in tags: chapter_add = "\n\n" + tag.text chapter_text += chapter_add save_file.save_txt(book["id"] + " - " + book["title"], "#" + str(chapter_count) + " " + title + ".txt", templ_ch().format(capter["title"], chapter_text)) p_green("[I] Saved to txt")
def create_profile(nickname, type_save): p_green("[I] Reading lists from " + nickname + " are added to the queue...") session = requests.session() session.headers['User-Agent'] = '' lists_url = 'https://www.wattpad.com/api/v3/users/{nickname}/lists?limit=100' list_json = session.get(lists_url.format(nickname=nickname)).json() count_all = list_json["total"] list_prep = list_json["lists"] count_one = 0 list_url_ready = [] for list_one in list_prep: count_one += 1 print( "[{count_one}/{count_all}] The {name} list is added to the queue : {id}" .format(count_all=count_all, count_one=count_one, name=list_one["name"], id=list_one["id"])) list_url_ready.append("https://www.wattpad.com/list/" + str(list_one["id"])) for down_url in list_url_ready: create_list(down_url, type_save) p_green("[I] All reading lists saved")
def m(book): p_green("[I] Saving in fb2...") root_dir = os.getcwd() try: os.mkdir(root_dir + "/downloads") except: pass chapters = book["characters"] binary = "" chapters_text = "" chapter_count = 0 for chapter in chapters: chapter_count += 1 print('[{c_num}/{c_all}] Generation "{chapter_title}": {chapter_id}'. format(c_num=str(chapter_count), c_all=str(len(chapters) + 1), chapter_title=chapter["title"], chapter_id=chapter["id"])) capt = chapter_gen(chapter["text"]) chapters_text += templ_char().format(title=chapter["title"], text=capt["text"]) binary += capt["image"] p_green('[{c_all}/{c_all}] Meta and information generation'.format( c_all=str(len(chapters) + 1))) ready_book = templ().format(link=book["source"], namebook=book["title"], annotation=book["description"], nickname=str(book["authors"]), characters=chapters_text, binarys=binary, cover=base64_get(book["cover"])) file = open(root_dir + "/downloads/" + str(book["id"]) + " - " + save_file.rename_valid_f(book["title"]) + ".fb2", "w", encoding="utf-8") file.write(ready_book) file.close() p_green("[I] Saved to fb2")
def download_story(story_id, type_save): storyinfo = session.get(API_STORYINFO + story_id, params={ 'drafts': 1, 'include_deleted': 1 }).json() story_title = storyinfo['title'] story_description = storyinfo['description'] story_createDate = dateutil.parser.parse(storyinfo['createDate']) story_modifyDate = dateutil.parser.parse(storyinfo['modifyDate']) story_author = storyinfo['user']['name'] story_categories = [ categories[c] for c in storyinfo['categories'] if c in categories ] story_rating = storyinfo['rating'] # TODO: I think 4 is adult? story_cover = session.get(storyinfo['cover']).content if type_save == "txt": save_file.save_bytes( str(story_id) + " - " + story_title, "cover.jpg", story_cover) story_url = storyinfo['url'] p_green('[I] Story "{story_title}": {story_id}'.format( story_title=story_title, story_id=story_id)) book = {} book["title"] = story_title book["id"] = story_id book["authors"] = story_author book["categories"] = story_categories book["description"] = story_description book["url"] = story_url book["'publisher"] = "Wattpad Downloader" book["source"] = story_url book["rating"] = story_rating book["cover"] = storyinfo["cover"] characters = [] countsp = 0 countspa = len(storyinfo["parts"]) for part in storyinfo['parts']: countsp += 1 chapter_title = part['title'] if part['draft']: p_red( '[{c_num}/{c_all}] Skipping "{chapter_title}": {chapter_id}, part is draft' .format(c_num=countsp, c_all=countspa, chapter_title=chapter_title, chapter_id=chapter_id)) continue if 'deleted' in part and part['deleted']: p_red( '[{c_num}/{c_all}] Skipping "{chapter_title}": {chapter_id}, part is deleted' .format(c_num=countsp, c_all=countspa, chapter_title=chapter_title, chapter_id=chapter_id)) continue chapter_id = part['id'] chapter_modifyDate = dateutil.parser.parse(part['modifyDate']) print('[{c_num}/{c_all}] Downloading "{chapter_title}": {chapter_id}'. format(c_num=countsp, c_all=countspa, chapter_title=chapter_title, chapter_id=chapter_id)) chapter_html = session.get(API_STORYTEXT, params={ 'id': chapter_id, 'output': 'json' }).json()['text'] chapter_html = smartypants.smartypants(chapter_html) section = {} section["text"] = chapter_html section["title"] = chapter_title section["id"] = chapter_id characters.append(section) book["characters"] = characters if type_save == "epub": epub_create.m(book) if type_save == "fb2": fb2_create.m(book) elif type_save == "txt": txt_create.m(book) else: pass
def create_list(list_url, type_save): try: os.rename("downloads", "downloads_temp") except: aa = 00 sessionreq = get_session() resp = sessionreq.get(list_url) soup = BeautifulSoup(resp.text, 'html5lib') storys_url = "" count_url = 0 id_list = [] match = re.search(r'\d+', list_url) url_id = match.group() tags = soup.find_all("h1") list_name = (tags[0]).text p_green("[I] " + (tags[0]).text + " list") tags = soup.find_all("a", href=True) for tag in tags: if (("on-navigate" and "story/") in str(tag)) and ("img" not in str(tag)): count_url += 1 add_url = "https://www.wattpad.com" + tag["href"] + " " match = re.search(r'\d+', add_url) url_id = match.group() id_list.append(url_id) storys_url += add_url print("[" + str(count_url) + "] " + tag.text + " added to list. Url: https://www.wattpad.com" + tag["href"]) p_green("[I] Redirect to download script") os.system("python scrape.py " + type_save + " " + storys_url) p_green("[I] Directory Organization...") try: os.mkdir(os.getcwd() + "/downloads") except: aa = 0 try: os.mkdir(os.getcwd() + "/lists") except: aa = 0 try: os.mkdir(os.getcwd() + "/lists/" + url_id + " - " + save_file.rename_valid_f(list_name)) except: aa = 0 list_dir = os.listdir(path=os.getcwd() + "/downloads") for file_d in list_dir: if file_d.split(" ")[0] in id_list: try: shutil.copytree( os.getcwd() + "/downloads/" + file_d, os.getcwd() + "/lists/" + url_id + " - " + save_file.rename_valid_f(list_name) + "/" + file_d) except: aa = 00 try: shutil.copy( os.getcwd() + "/downloads/" + file_d, os.getcwd() + "/lists/" + url_id + " - " + save_file.rename_valid_f(list_name) + "/") except: aa = 00 shutil.rmtree(os.getcwd() + "/downloads") try: os.rename("downloads_temp", "downloads") except: aa = 00 p_green("[I] List Folder Ready") p_green("[I] " + list_name + " downloaded")