Skip to main content

python version of xiaobo tool module

Project description

Xiaobo Tool X (formerly Twitter) Follow

Python Version from PEP 621 TOML Project Version from TOML

Python 通用工具库,提供任务执行器、代理池、X(Twitter) 客户端、临时邮箱等模块。

安装

uv add xiaobo-tool

模块概览

task_executor - 任务执行器

支持同步(线程池)和异步(asyncio)两种模式,内置重试、代理轮换、回调通知和统计。

from xiaobo_tool.task_executor import TaskExecutor, Target


def my_task(target: Target):
    print(f"执行任务: {target.data}, 代理: {target.proxy}")


with TaskExecutor(name="Demo", max_workers=3, retries=2) as executor:
    executor.submit_tasks(
        task_func=my_task,
        source=["item1", "item2", "item3"],
        on_success=lambda t, r: print(f"{t.data} 成功"),
        on_error=lambda t, e: print(f"{t.data} 失败: {e}"),
    )
    executor.wait()
    executor.statistics()

异步模式:

import asyncio
from xiaobo_tool.task_executor import AsyncTaskExecutor, Target


async def my_async_task(target: Target):
    print(f"执行任务: {target.data}")


async def main():
    async with AsyncTaskExecutor(name="AsyncDemo", max_workers=5) as executor:
        executor.submit_tasks(task_func=my_async_task, source=10)
        await executor.wait()
        await executor.statistics()


asyncio.run(main())

从文件批量提交任务:

# 读取 accounts.txt,每行按 "----" 分割
executor.submit_tasks_from_file(task_func=my_task, filename="accounts", separator="----")

配置通过 Settings 管理,支持构造参数、环境变量、.env 文件三种方式:

配置项 默认值 说明
MAX_WORKERS 5 最大线程数
PROXY (空) 代理,支持 host:port / user:pass@host:port,占位符 ***** 自动替换为 index 或第一位数据
PROXY_IPV6 (空) IPv6 代理,格式同 PROXY
PROXY_API (空) 代理提取 API 地址(一行一个),格式同 PROXY
PROXY_IPV6_API (空) IPv6 代理提取 API 地址
RETRIES 2 重试次数(抛出 TaskFailed 不重试)
RETRY_DELAY 0 重试延迟(秒)
SHUFFLE false 是否打乱任务顺序,按照数量运行的任务,支持布尔值或任务名称,多个任务用&拼接,如: task1&task2
USE_PROXY_IPV6 false 是否优先使用 IPv6 代理,支持布尔值或任务名称,多个任务用&拼接,如: task1&task2
DISABLE_PROXY false 是否禁用代理,支持布尔值或任务名称,多个任务用&拼接,如:task1&task2

抛出 TaskFailed 异常可跳过重试,直接标记任务失败:

from xiaobo_tool.task_executor import TaskFailed


def my_task(target: Target):
    if invalid(target.data):
        raise TaskFailed("数据无效,无需重试")

proxy_pool - 代理池

管理代理获取与轮换,支持直连代理和 API 代理(带 3 分钟缓存)。

from xiaobo_tool.proxy_pool import ProxyPool

pool = ProxyPool(proxy_api="http://your-proxy-api.com/get")
proxy = pool.get_proxy()

x - X(Twitter) 客户端

基于 auth_token 操作 X API,支持发推和 OAuth2 授权。

from xiaobo_tool.x import XClient

client = XClient(auth_token="your_auth_token", proxy="http://127.0.0.1:7890")
tweet_url = client.send_tweet("Hello World!")

OAuth2 授权:

redirect_uri = client.authorize_oauth2("https://x.com/i/oauth2/authorize?client_id=xxx&...")

temp_email - 临时邮箱

创建临时邮箱并接收邮件,支持同步和异步两种模式。

from xiaobo_tool.temp_email import TempEmail

with TempEmail() as mail:
    domains = mail.query_domains()
    mailbox = mail.create_mailbox()
    result = mail.get_new_mail(mailbox)

异步模式:

from xiaobo_tool.temp_email import AsyncTempEmail

async with AsyncTempEmail() as mail:
    mailbox = await mail.create_mailbox(domain="example.com", mail_type=1)
    result = await mail.get_new_mail(mailbox, title="验证码")

utils - 工具函数

  • read_txt_file_lines(filename) - 按行读取 txt 文件
  • write_txt_file(filename, data) - 写入/追加 txt 文件
  • get_session(proxy) / get_async_session(proxy) - 创建 HTTP 会话(curl_cffi)
  • json_get(data, path, default) - 通过路径访问嵌套 JSON 数据
  • raise_response_error(name, response) - 解析 HTTP 错误响应并抛出异常

开发

# 安装开发依赖
uv sync --dev

# 运行测试
uv run pytest

依赖

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

xiaobo_tool-1.0.5.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xiaobo_tool-1.0.5-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file xiaobo_tool-1.0.5.tar.gz.

File metadata

  • Download URL: xiaobo_tool-1.0.5.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xiaobo_tool-1.0.5.tar.gz
Algorithm Hash digest
SHA256 0f33a50a639861fcbafd103ac66ccc4adaeb7b17a9071631e06ea2b293a05b59
MD5 e094f8e30bd36d3a658509e111765b5f
BLAKE2b-256 e6413269707b972def3d803fe454be61baf740a7d6893904ed4192a6f7ec340b

See more details on using hashes here.

Provenance

The following attestation bundles were made for xiaobo_tool-1.0.5.tar.gz:

Publisher: workflow.yml on Xiaobooooo/xiaobo-tool-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xiaobo_tool-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: xiaobo_tool-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xiaobo_tool-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 29dcfbda511d184234e4eae22733245fa2930b3f917c43e5cfab64ef49f93688
MD5 eb8850e96f5ef5ce178eb45c3cb7b274
BLAKE2b-256 b38c4d8870828ad042709bf88e645116409a6e3536b210d6cf114815a436b3cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for xiaobo_tool-1.0.5-py3-none-any.whl:

Publisher: workflow.yml on Xiaobooooo/xiaobo-tool-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page