BlockCoin API Wrapper
Project description
BlockCoin API Wrapper
BlockCoin is a Python library that provides an easy-to-use wrapper for interacting with the BlockCoin API. The library uses curl_cffi with Chrome impersonation to mimic browser behavior and bypass certain anti-bot measures. It gives you convenient access to login, register, fetch user data, create posts, and more.
Note: This library is currently in early development. Some features (like registration) are still a work-in-progress (WIP) and may change soon.
Table of Contents
Installation
BlockCoin depends on only one external package: curl_cffi==0.7.4. Other modules used are Python's standard library (re, json, datetime, etc.).
To install the package, use pip:
pip install blockcoin
Usage
Authentication
To start working with BlockCoin, you need to log in. The library exposes a login function from the module's main package (blockcoin/__init__.py). This function creates a Session object after a successful login.
from blockcoin import login
# Log in with your BlockCoin credentials
session = login("your_username", "your_password")
# Check out your session details
print(session)
Working with Sessions
The Session class (in session.py) handles the authentication flow and maintains the session state. It automatically parses the server's responses, handles errors, and sets up the session for subsequent API calls.
# Update session data (e.g., after posting or making changes)
session.update()
# Retrieve your user profile from the session
print("Logged in as:", session.user.username)
If an error occurs during login, a LoginError or RegisterError is raised with a descriptive message.
Creating and Fetching Posts
The Session class provides methods for creating posts and fetching existing posts.
from blockcoin.post import Post
# Create a new post
new_post = session.create_post("Hello BlockCoin!", price=10)
print("New post created with ID:", new_post.id)
# Fetch an existing post by its ID
fetched_post = session.fetch_post("some_post_id")
print("Post:", fetched_post.body)
# Repost the existing post
reposted_post = session.create_post(
"This is a repost!",
price=50,
repost=fetched_post
)
print("Reposted original post. Reposted post ID:", reposted_post.id)
The Post class (in post.py) parses the server response and stores properties like body, likes, price, etc., automatically converting timestamps into datetime objects.
Working with Users and Themes
The User class (in user.py) fetches and updates user information including profile details and posts. The user's theme data is represented by the Theme class (in theme.py).
# Retrieve user data for a specific username
user_profile = session.get_user("another_username")
print("User Profile:", user_profile)
# List posts' likes by the user
for post in user_profile.posts:
print(post.likes)
# Print the user's theme name
print("Theme:", user_profile.theme)
API Documentation
Below is an overview of the main classes, their public attributes, and methods with type hints where applicable.
Session Class (session.py)
Represents an active session with the BlockCoin API.
Attributes:
username: str— The username used for login.session— The underlyingcurl_cffisession used to make HTTP requests.user: User— The logged-in user's profile information (an instance ofUser).
Public Methods:
-
update() -> None
Refreshes session data (e.g., user profile, dashboard data). -
create_post(body: str, price: int = 0, repost: Optional[Post] = None) -> Post
Arguments:body (str): The content of the post.price (int, default=0): The price associated with the post.repost (Optional[Post], default=None): A post object to repost; if provided, itsidis used.
Returns:
A newPostobject representing the created post. -
fetch_post(id: str) -> Post
Arguments:id (str): The identifier of the post to fetch.
Returns:
APostobject corresponding to the given post ID. -
get_user(username: str) -> User
Arguments:username (str): The username of the profile to fetch.
Returns:
AUserobject with the requested user's data.
Exceptions:
LoginError— Raised if login fails.RegisterError— Raised during registration issues (WIP).
Post Class (post.py)
Represents a BlockCoin post.
Attributes (after initialization via API response):
id: str— The unique identifier for the post.boost_end— Timestamp or value indicating post boost ending.boost_multi— Multiplier used during boosting.buyer— AUserobject representing the buyer (if any), orNone.body: str— The textual content of the post.date: datetime— A datetime object representing the post creation time.hashtags— A list of hashtags associated with the post.is_buyable— Boolean flag indicating if the post can be bought.likes: int— Number of likes.price: int— The price of the post.profanity— Value denoting profanity flags (or related information).repost: Optional[Post]— If the post is a repost, contains aPostobject; elseNone.reposting— Additional data on reposting, if applicable.author: User— The author of the post.views: int— View count of the post.reposts_number: int— Number of times reposted.comments_number: int— Number of comments.
Properties:
liked -> bool
ReturnsTrueif the current session user has liked the post, elseFalse.
Internally performs a POST request to/post/liked.
Methods:
-
like(exist_ok: bool = False) -> bool
Likes the post.- If the post is already liked and
exist_okisFalse, raises an exception. - Returns
Trueif the request succeeds.
- If the post is already liked and
-
unlike(exist_ok: bool = False) -> bool
Unlikes the post.- If the post is not liked and
exist_okisFalse, raises an exception. - Returns
Trueif the request succeeds.
- If the post is not liked and
-
__repr__()and__str__()— Provide string representations of the post.
User Class (user.py)
Represents a BlockCoin user profile.
Attributes:
username: str— The user's username (case-sensitive).id: str— The unique identifier for the user.display_name: str— The user’s display name.about: str— The "about" description.profile_picture: str— URL for the profile picture.banner: str— URL for the profile banner.badges: list— A list containing the user’s badges.badge_count: int— Count of badges.balance: float— User’s current BlockCoin balance.followers: int— Number of followers.theme: Theme— AThemeinstance holding the user’s theme data.
Properties:
posts -> List[Post]
Lazily loads and returns a list ofPostobjects created from the user's profile data. Results are cached until the profile is updated.
Methods:
update() -> None
Fetches the latest user profile data from the API.- Internal update methods (
_update_from_dataand_update_from_dashboard_data) are not publicly accessible.
Exceptions:
UserNotFound— Raised when a user cannot be found.
Theme Class (theme.py)
Handles the user's theme information.
Attributes:
data: dict— Parsed theme metadata from a JSON string.
Methods:
__str__() -> str
Returns a string with the theme name and author.__repr__() -> str
Returns the theme key as a representation.
Utility Functions (utils.py)
Public Functions:
-
extract_data_array(js_code: str, keyword: str = "data") -> str
Arguments:js_code (str): The raw JavaScript code as a string.keyword (str, default="data"): The variable name to search for in the code.
Returns:
A substring containing the JavaScript array (without the trailing semicolon). -
get_script_data(body: str, keyword: str = "data") -> list
Parses the JavaScript from an HTML body into Python objects. -
get_error(url: str) -> Optional[str]
Arguments:url (str): The URL which contains an error code.
Returns:
A description for the error code if found; otherwise, returns"Unknown Error"orNone. -
login(username: str, password: str) -> "Session"
Arguments:username (str): Your BlockCoin username.password (str): Your password.
Returns:
ASessionobject after successful login. -
_register(*args, **kwargs) -> "Session"
(Work in progress) — Intended to handle account registration. -
deep_merge(a: dict, b: dict) -> dict
Recursively merges dictionarybinto dictionarya.
Modules Overview
-
__init__.py
Exposes the library’s public API by exportinglogin. -
utils.py
Contains helper functions for parsing JavaScript data, error fetching, login, registration (WIP), and dictionary merging. -
session.py
Defines theSessionclass, managing user login, session updates, and API requests. -
post.py
Contains thePostclass, parsing and representing post data from BlockCoin. -
user.py
Provides theUserclass for handling user profiles and caching their posts. -
theme.py
Defines theThemeclass, which parses and represents theme data.
Contributing
Contributions are welcome! Feel free to fork the repository, open issues, or submit pull requests. When submitting changes, please ensure your code follows the existing style guidelines and that tests pass locally.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blockcoin-0.2.0.tar.gz.
File metadata
- Download URL: blockcoin-0.2.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
702ba8f03ca1a9e9e5fbc8f414fcee1a4bdebbbb2db0bd8b2f5e2af17bb94f28
|
|
| MD5 |
ce9709b37830bc94cae1f9b3706aff1b
|
|
| BLAKE2b-256 |
bb87abc69911cc3f6d81d49554256ff2774a4e70406ab5fda7eaf0e03b53fc43
|
File details
Details for the file blockcoin-0.2.0-py3-none-any.whl.
File metadata
- Download URL: blockcoin-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0eae46e7a48b22b2b8c5e977ce598c2369455bded5ab5965a57b6b9c70e99ce4
|
|
| MD5 |
272ff4d258b2a9d842e7516c270e865f
|
|
| BLAKE2b-256 |
763ad29a5560263243d22ce66ecc6d434262caa005b25f1b696cb2d681b4d371
|