This Wattpad API wrapper provides a convenient way to interact with the Wattpad API, allowing you to retrieve information about stories, chapters, comments, and users on the Wattpad platform.
To use the Wattpad API wrapper, you'll need to import the WattpadAPI
class and create an instance:
from wattpad_api import WattpadAPI
# Initialize the API client
api = WattpadAPI(api_key='your_api_key_here')
Note: Some endpoints don't require authentication, so you can omit the api_key
if you're only using those endpoints.
The main class for interacting with the Wattpad API.
Retrieve information about a specific story.
- Parameters:
story_id
(int): The ID of the story to retrieve.
- Returns: A
Story
object.
Example:
story = api.story(1234567)
print(story.title)
Retrieve information about a specific chapter.
- Parameters:
chapter_id
(int): The ID of the chapter to retrieve.
- Returns: A
Chapter
object.
Example:
chapter = api.get_chapter(9876543)
print(chapter.title)
Search for stories based on a query.
- Parameters:
query
(str): The search query.limit
(int, optional): The maximum number of results to return. Default is 20.offset
(int, optional): The offset for pagination. Default is 0.
- Returns: A list of
Story
objects.
Example:
results = api.search_stories("fantasy", limit=10)
for story in results:
print(story.title)
Retrieve comments for a specific story.
- Parameters:
story_id
(int): The ID of the story to retrieve comments for.limit
(int, optional): The maximum number of comments to return. Default is 20.offset
(int, optional): The offset for pagination. Default is 0.
- Returns: A list of
Comment
objects.
Example:
comments = api.get_story_comments(1234567, limit=5)
for comment in comments:
print(comment.text)
Represents a Wattpad story.
id
(int): The story's unique identifier.title
(str): The title of the story.description
(str): The story's description.cover_url
(str): URL of the story's cover image.url
(str): The story's URL on Wattpad.read_count
(int): Number of reads for the story.vote_count
(int): Number of votes for the story.comment_count
(int): Number of comments on the story.parts
(list): A list ofChapter
objects representing the story's chapters.
Represents a chapter in a Wattpad story.
id
(int): The chapter's unique identifier.title
(str): The title of the chapter.url
(str): The chapter's URL on Wattpad.create_date
(str): The date the chapter was created.modify_date
(str): The date the chapter was last modified.read_count
(int): Number of reads for the chapter.vote_count
(int): Number of votes for the chapter.comment_count
(int): Number of comments on the chapter.
Represents a comment on a Wattpad story or chapter.
resource
(Resource): Information about the resource the comment is on.user
(User): The user who made the comment.comment_id
(CommentId): The comment's unique identifier.text
(str): The content of the comment.created
(datetime): The date and time the comment was created.modified
(datetime): The date and time the comment was last modified.status
(str): The status of the comment.sentiments
(dict): Sentiment information for the comment.reply_count
(int): Number of replies to the comment.labels
(list): Labels associated with the comment.deeplink
(str): A deep link to the comment.
Represents a Wattpad user.
name
(str): The user's name.avatar
(str): URL of the user's avatar image.
The wrapper defines two custom exceptions:
WattpadAPIError
: Raised for general API errors.RateLimitError
: Raised when the API rate limit is exceeded.
Example of handling exceptions:
from wattpad_api.exceptions import WattpadAPIError, RateLimitError
try:
story = api.story(1234567)
except RateLimitError:
print("Rate limit exceeded. Please wait before making more requests.")
except WattpadAPIError as e:
print(f"An API error occurred: {str(e)}")
The wrapper implements rate limiting to prevent exceeding the API's rate limits. By default, it allows 5 calls per second. If you exceed this limit, a RateLimitError
will be raised.
api = WattpadAPI()
# Get a story
story = api.story(1234567)
print(f"Title: {story.title}")
print(f"Description: {story.description}")
print(f"Read Count: {story.read_count}")
# Print information about each chapter
for chapter in story.parts:
print(f"Chapter: {chapter.title}")
print(f"Read Count: {chapter.read_count}")
print(f"URL: {chapter.url}")
print("---")
api = WattpadAPI()
# Search for fantasy stories
results = api.search_stories("fantasy", limit=5)
for story in results:
print(f"Title: {story.title}")
print(f"URL: {story.url}")
print(f"Votes: {story.vote_count}")
print("---")
api = WattpadAPI()
# Get comments for a story
comments = api.get_story_comments(1234567, limit=10)
for comment in comments:
print(f"User: {comment.user.name}")
print(f"Comment: {comment.text}")
print(f"Created: {comment.created}")
print("---")
This documentation provides a comprehensive overview of the Wattpad API wrapper, including its classes, methods, and usage examples. Users can refer to this documentation to understand how to interact with the Wattpad API using this wrapper.