Mastodon.py

halcyMIT活跃维护

用 Python 封装 Mastodon 的 REST API,方便开发者在脚本或应用里发帖、读取时间线、管理账号,不用自己拼 HTTP 请求。

Python wrapper for the Mastodon ( https://github.com/mastodon/mastodon/ ) API.

v2.3.3发布于 2026-10-18 13:12 UTC960star +1 / 7 天170fork同步于 2026-11-10 11:51 UTC

怎么上手

由 README 提炼,执行前请确认
pip install Mastodon.py
硬件要求按 README 说明

README 未说明,纯 Python 库无特殊硬件要求

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Mastodon 官方 API 的 Python 封装库,让你用几行代码完成注册应用、OAuth 登录和发帖

用 Python 封装 Mastodon 的 REST API,方便开发者在脚本或应用里发帖、读取时间线、管理账号,不用自己拼 HTTP 请求。

核心能力

  • 覆盖 Mastodon 4.7.3 公开 API 全部接口
  • 用 create_app 一次性注册应用并保存凭据
  • 支持 OAuth 授权码登录并持久化用户凭据
  • 一行 toot 调用即可发布带话题的嘟文

容易踩的坑

需要 Python 3.7 及以上,Python 2 已不再支持;首次使用需先调用 create_app 注册应用并保存 clientcred 文件,否则无法登录;登录需在浏览器打开授权 URL 并手动粘贴 OAuth 授权码

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v2.3.3
  1. v2.3.3最新正式版2026-10-18 · 2026-10-18 13:12 UTC
    • Document Collections endpoints in the accounts API reference.

    Instant followup to 2.3.2, with only a documentation change.

  2. v2.3.22026-10-18 · 2026-10-18 11:41 UTC
    查看更新说明
    • Bump support level to 4.7.3, update return types with newly added fields
    • Fix stream error handlers raising a tuple instead of an exception (Thanks @dylanpulver )
    • Fix logic issue in version check code that prevented correct functioning on Pixelfed (Thanks @gunchleoc , and @xsuchy for the report)
    • Fix issue with ratelimit code that could lead to crashes in some circumstances (Thanks @xsuchy for the report)
    • Cache type hints for massively improved performance (Thanks @toelke for the suggestion)
    • Make MaybeSnowflakeIDs sortable (Thanks @haraldg for the report and suggestion)
    • Improve error handling for media uploads (Thanks @aescling for the suggestion)
    • Fix timestamp parsing for Pleromalike Flake IDs
    • 4.6 support: Add supported_types to notification related endpoints
    • 4.6 support: Add exclude_direct to account_statuses
    • 4.6 support: Add collection_ids to report
    • 4.6 support: Add unresolved to admin_reports
    • 4.6 support: Add avatar and header description parameters to account_update_credentials
    • 4.6 support: Add new profile editing API support: profile and profile_update
    • 4.6 support: Add collections support: collection_create, collection, account_collections, account_in_collections, collection_update, collection_delete, collection_item_add, collection_item_delete, and collection_item_revoke
    • 4.6 support: Add annual reports / year in review support: annual_reports, annual_report, annual_report_state, annual_report_read, and annual_report_generate, including previously unsupported endpoints introduced in 4.3 and 4.4
    • Allow status creation with both media attachments and a poll, as supported by Mastodon 4.6 and newer
    • Various small bug and documentation fixes
    • Add only_media support for streams
    • Add support for websocket streaming: stream_open_websocket and a new handle class
    • Tests: Streaming test recording now works on newer Python versions
    • Better pagination support: Endpoints that use offset/limit pagination are now supported generally, and fetch_next(), fetch_previous(), and the newly added pagination_page_iterator() can be used to navigate through responses for endpoints that use a wrapper object with a nested list.
  3. v2.2.22026-08-03 · 2026-08-03 11:06 UTC
    查看更新说明
    • Improved instance information caching
    • Added a function to check whether a given timeline is available on an instance (Thanks @pdeitel for the report that triggered this)
    • Added warnings when a user tries to access the stream for a deprecated timeline (Thanks @pdeitel for the report that triggered this)
    • Added missing limit parameter for search (Thanks @Vitexus)
    • Various small fixes and improvements to testing
  4. v2.2.12026-04-12 · 2026-04-12 12:40 UTC
    查看更新说明
    • Updated citation information to match the new JOSS publication (Thanks @dataspider and @professornaite for reviewing and @danielskatz for editing)
  5. v2.2.02026-04-12 · 2026-04-12 09:31 UTC
    查看更新说明
    • Bumped support level to 4.5.8
    • Switch from grapheme to graphemeu package, which is more maintained (thanks @CyberTailor for the report).
    • Fix a crash due to unbound local in stream_public (Thanks @elnikkis for the report and fix)
    • Fix invalid event error in heartbeats (Thanks @tribela for the report and fix)
    • Add several missing parameters to account_update_credentials
    • Add quote support (Several new endpoints and entities)
    • Fix a bug in push subscriptions for status push subscriptions
    • Fix a bug in Pleroma ID parsing
    • Samples: Make samples work on Windows (Thanks @AllynH for the report and fix)
    • Add support for async refreshes
    • Fix various other bugs and documentation issues

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Mastodon.py

    Python wrapper for the Mastodon ( https://github.com/mastodon/mastodon/ ) API. Feature complete for public API as of Mastodon version 4.7.3 and easy to get started with:

    .. code-block:: python

    from mastodon import Mastodon
    
    # Register your app! This only needs to be done once (per server, or when 
    # distributing rather than hosting an application, most likely per device and server). 
    # Uncomment the code and substitute in your information:
    '''
    Mastodon.create_app(
        'pytooterapp',
        api_base_url = 'https://mastodon.social',
        to_file = 'pytooter_clientcred.secret'
    )
    '''
    
    # Then, log in. This can be done every time your application starts, or you can use the persisted information:
    mastodon = Mastodon(client_id = 'pytooter_clientcred.secret',)
    print(mastodon.auth_request_url())
    
    # open the URL in the browser and paste the code you get
    mastodon.log_in(
        code=input("Enter the OAuth authorization code: "),
        to_file="pytooter_usercred.secret"
    )
    
    # To post, create an actual API instance:
    mastodon = Mastodon(access_token = 'pytooter_usercred.secret')
    mastodon.toot('Tooting from Python using #mastodonpy !')
    

    You can install Mastodon.py via pypi:

    .. code-block:: Bash

    pip install Mastodon.py

    We currently try to support Python 3.7 and above, and try to at least not break Python 3 versions below that. Python 2 support is no longer a goal.

    Full documentation and basic usage examples can be found at https://mastodonpy.readthedocs.io/en/stable/ . Some more extensive examples can be found at https://github.com/halcy/MastodonpyExamples

    If you have any questions about using the library or think you have found a bug, please feel free to open an issue, a github discussion thread, or to just directly contact @[email protected] on the Fediverse or .halcy on Discord - we'll try to respond as quickly as possible.

    Citing

    If you use Mastodon.py as part of your research, please review and cite the following paper:

    Diener et al., (2026). Mastodon.py: A Python library for the Mastodon API. *Journal of Open Source Software*, 11(120), 8946, https://doi.org/10.21105/joss.08946
    

    .. image:: https://joss.theoj.org/papers/10.21105/joss.08946/status.svg :target: https://doi.org/10.21105/joss.08946

    Acknowledgements

    Mastodon.py contains work by a large amount of contributors, many of which have put significant work into making it a better library. You can find some information about who helped with which particular feature or fix in the changelog.

    .. image:: https://circleci.com/gh/halcy/Mastodon.py.svg?style=svg :target: https://app.circleci.com/pipelines/github/halcy/Mastodon.py .. image:: https://codecov.io/gh/halcy/Mastodon.py/branch/master/graph/badge.svg :target: https://codecov.io/gh/halcy/Mastodon.py