Last active
August 7, 2021 02:32
-
-
Save RascalTwo/8303ad609a538201bdfb067c5885af5f to your computer and use it in GitHub Desktop.
Random Wattpad Story Fetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import random | |
from typing import List | |
API_STORYINFO = 'https://www.wattpad.com/api/v3/stories/' # ?name=username filter available | |
API_STORYTEXT = 'https://www.wattpad.com/apiv2/storytext?id=' | |
API_GETCATEGORIES = 'https://www.wattpad.com/api/v3/categories' | |
API_USERS = 'https://www.wattpad.com/api/v3/users/' | |
session = requests.session() | |
session.headers['User-Agent'] = '' | |
def get_categories(): | |
return { | |
int(category["id"]): category["name"] | |
for category in session.get(API_GETCATEGORIES).json() | |
} | |
def generate_random_story_id(): | |
while True: | |
uid = str(random.randrange(0000000, 9999999)) | |
storyinfo_req = session.get(API_STORYINFO + uid) | |
if storyinfo_req.ok: | |
return uid | |
def get_story_text(id: str): | |
return session.get( | |
"https://www.wattpad.com/apiv2/storytext?id={}".format( | |
id)).content.decode("utf-8") | |
def remove_tags(string: str, tags: List[str]): | |
for tag in tags: | |
string = string.replace("<{}>".format(tag), "").replace( | |
"</{}>".format(tag), | |
"").replace("<{}".format(tag), "").replace("</{}".format(tag), "") | |
return string | |
def cleanup_raw_story(raw: str): | |
story_lines: List[str] = [] | |
for text in raw.split("</p>"): | |
text = remove_tags(text.replace("<br>", "\n"), ["em", "strong"]) | |
if text == "": | |
story_lines.append(text) | |
continue | |
current = text.split(">")[1] | |
if text.split(">")[0] == current: | |
story_lines.append("") | |
continue | |
current = current.split("</")[-1:][0] + "\n" | |
story_lines.append(current) | |
return story_lines | |
if __name__ == '__main__': | |
story_id = generate_random_story_id() | |
print(story_id) | |
info = session.get(API_STORYINFO + story_id).json() | |
# User info: | |
# print(json.dumps(session.get(API_USERS + storyinfo["user"]["name"]).json(), indent=4, sort_keys=True, separators=(',', ' : '))) | |
raw_story_text = get_story_text(story_id) | |
for line in cleanup_raw_story(raw_story_text): | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment