Xify is a Python tool for interacting with the X API.
Project description
Xify
Xify is a modern, asynchronous Python client for interacting with the X (formerly Twitter) API.
It provides authentication, error handling, and a clean async interface for common tasks.
Features
- OAuth 1.0a Authentication: Sign and authenticate requests to the X API.
- Async HTTP Requests: Uses
aiohttpfor efficient, non-blocking network operations. - Custom Error Handling: Clear exception hierarchy for all error cases.
- Logging: Built-in logging with easy integration into your application’s logging setup.
- Example Usage: See
examplesfor working examples scripts.
Installation
You can install Xify from source or from PyPI.
From Source
git clone https://github.com/filming/xify.git
cd xify
pip install -e .[dev]
- The
[dev]extra will install development dependencies likemypyandruff.
From PyPI
pip install xify
Usage
Here’s a minimal example for posting a tweet:
import asyncio
import os
import logging
from dotenv import load_dotenv
from xify import Xify
from xify.errors import APIError, XifyError
load_dotenv()
# Configure logging to see output from Xify
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
async def main():
try:
xify_client = Xify(
consumer_key=os.getenv("CONSUMER_KEY"),
consumer_secret=os.getenv("CONSUMER_SECRET"),
access_token=os.getenv("ACCESS_TOKEN"),
access_token_secret=os.getenv("ACCESS_TOKEN_SECRET"),
)
async with xify_client:
content = {"msg": "Hello X!"}
response = await xify_client.tweet(content)
logging.info("Tweet posted successfully! ID: %s", response["id"])
except APIError as e:
logging.error("API error: %s", e)
logging.error("Full error response from API: %s", e.response)
except XifyError as e:
logging.error("A library error occurred: %s", e)
except Exception:
logging.exception("An unexpected error occurred.")
if __name__ == "__main__":
asyncio.run(main())
See examples for ready-to-run scripts.
Configuration
Xify requires four credentials to authenticate with the X API:
consumer_keyconsumer_secretaccess_tokenaccess_token_secret
You can provide these credentials to the Xify initializer in any way you prefer—such as reading from environment variables, a .env file, a configuration file, a database, or any other method.
Example using environment variables:
Set your environment variables in your shell (for example, in .bashrc, .zshrc, or directly in your terminal):
export CONSUMER_KEY="your_consumer_key"
export CONSUMER_SECRET="your_consumer_secret"
export ACCESS_TOKEN="your_access_token"
export ACCESS_TOKEN_SECRET="your_access_token_secret"
Then, in your code:
import os
from xify import Xify
xify_client = Xify(
consumer_key=os.getenv("CONSUMER_KEY"),
consumer_secret=os.getenv("CONSUMER_SECRET"),
access_token=os.getenv("ACCESS_TOKEN"),
access_token_secret=os.getenv("ACCESS_TOKEN_SECRET"),
)
Error Handling
Xify provides a robust error hierarchy:
XifyError: Base class for all library errors.AuthError: Raised for authentication issues.APIError: Raised for errors returned by the X API (includes the full API response in.response).RequestError: Raised for network or request issues.
You should catch these exceptions in your application for consistent error handling.
Logging
- Xify uses Python’s standard
logginglibrary. - By default, it adds a
NullHandlerto prevent "No handler found" warnings if you have not set up logging in your application.
Dependencies
All dependencies are managed via pyproject.toml.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions, bug reports, and feature requests are welcome!
Please open an issue or submit a pull request on GitHub.
Project details
Release history Release notifications | RSS feed
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 xify-1.1.5.tar.gz.
File metadata
- Download URL: xify-1.1.5.tar.gz
- Upload date:
- Size: 81.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a047b0dae9943776072bfe9021df48a680bb2a4aa5da9d72790f010ba16aa47f
|
|
| MD5 |
317d5e22ccd9e1b325b992498cb4b256
|
|
| BLAKE2b-256 |
c927e7f5c33b646e2c93869c95d5c8fc50b99811a6185015ba9f7205677a31f9
|
Provenance
The following attestation bundles were made for xify-1.1.5.tar.gz:
Publisher:
release.yml on filming/xify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xify-1.1.5.tar.gz -
Subject digest:
a047b0dae9943776072bfe9021df48a680bb2a4aa5da9d72790f010ba16aa47f - Sigstore transparency entry: 254300881
- Sigstore integration time:
-
Permalink:
filming/xify@c506ce61d55da9c4426fa347ae15b302995edcf5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/filming
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c506ce61d55da9c4426fa347ae15b302995edcf5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file xify-1.1.5-py3-none-any.whl.
File metadata
- Download URL: xify-1.1.5-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63bc71bf6ad401859b73d23b9398c13b9b1ca4342c75fcf6dfd767d5db6b5526
|
|
| MD5 |
807afa8ba90a569118dcb90ecef0faab
|
|
| BLAKE2b-256 |
2ef23de8ce87bdac6f22752c7441d8d03c95719302c678efb02f9efee098ca49
|
Provenance
The following attestation bundles were made for xify-1.1.5-py3-none-any.whl:
Publisher:
release.yml on filming/xify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xify-1.1.5-py3-none-any.whl -
Subject digest:
63bc71bf6ad401859b73d23b9398c13b9b1ca4342c75fcf6dfd767d5db6b5526 - Sigstore transparency entry: 254300888
- Sigstore integration time:
-
Permalink:
filming/xify@c506ce61d55da9c4426fa347ae15b302995edcf5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/filming
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c506ce61d55da9c4426fa347ae15b302995edcf5 -
Trigger Event:
push
-
Statement type: