Mastodon
  • 什么是 Mastodon?
  • 使用 Mastodon
    • 注册帐户
    • 设置你的个人资料
    • 在你的账户发布内容
    • 使用网络功能
    • 处理不想看到的内容
    • 推广你和他人
    • 进行偏好设置
    • 更多设置
    • 在站点外使用 Mastodon
    • 迁移或离开账户
    • 运行你自己的站点
  • 运营 Mastodon
    • 准备你的服务器
    • 从源代码安装
    • 配置你的环境
    • 安装可选功能
      • 对象存储
      • 洋葱服务
      • 验证码
      • 单点登录
    • 配置全文搜索
    • 设置你的新实例
    • 使用管理 CLI
    • 升级到新版本
    • 备份你的服务器
    • 迁移到新机器
    • 扩大你的站点规模
    • 审核操作
    • 故障排除
      • 数据库索引损坏
    • 用户组
  • 开发 Mastodon 应用
    • API 入门指南
    • 使用公开数据
    • 获取客户端应用访问权限
    • 使用帐户登录
    • 库与实现
  • 向 Mastodon 项目做贡献
    • 技术概览
    • 设置开发环境
    • 代码结构
    • 路由
    • 漏洞赏金与责任披露
  • 遵循的标准
    • ActivityPub
    • WebFinger
    • 安全性
    • Microformats
    • OAuth
    • Bearcaps
  • REST API
    • Datetime 格式
    • 指南与最佳实践
    • OAuth 令牌
    • OAuth 作用域
    • 速率限制
  • API 方法
    • apps
      • oauth
      • emails
    • accounts
      • bookmarks
      • favourites
      • mutes
      • blocks
      • domain_blocks
      • filters
      • reports
      • follow_requests
      • endorsements
      • featured_tags
      • preferences
      • followed_tags
      • suggestions
      • tags
    • profile
    • statuses
      • media
      • polls
      • scheduled_statuses
    • timelines
      • conversations
      • lists
      • markers
      • streaming
    • grouped notifications
    • notifications
      • push
    • search
    • instance
      • trends
      • directory
      • custom_emojis
      • announcements
    • admin
      • accounts
      • canonical_email_blocks
      • dimensions
      • domain_allows
      • domain_blocks
      • email_domain_blocks
      • ip_blocks
      • measures
      • reports
      • retention
      • trends
    • proofs
    • oembed
  • API 实体
    • Account
    • AccountWarning
    • Admin::Account
    • Admin::CanonicalEmailBlock
    • Admin::Cohort
    • Admin::Dimension
    • Admin::DomainAllow
    • Admin::DomainBlock
    • Admin::EmailDomainBlock
    • Admin::Ip
    • Admin::IpBlock
    • Admin::Measure
    • Admin::Report
    • Announcement
    • Appeal
    • Application
    • Context
    • Conversation
    • CustomEmoji
    • DomainBlock
    • Error
    • ExtendedDescription
    • FamiliarFollowers
    • FeaturedTag
    • Filter
    • FilterKeyword
    • FilterResult
    • FilterStatus
    • IdentityProof
    • Instance
    • List
    • Marker
    • MediaAttachment
    • Notification
    • NotificationPolicy
    • NotificationRequest
    • Poll
    • Preferences
    • PreviewCard
    • PreviewCardAuthor
    • PrivacyPolicy
    • Reaction
    • Relationship
    • RelationshipSeveranceEvent
    • Report
    • Role
    • Rule
    • ScheduledStatus
    • Search
    • Status
    • StatusEdit
    • StatusSource
    • Suggestion
    • Tag
    • TermsOfService
    • Token
    • Translation
    • V1::Filter
    • V1::Instance
    • V1::NotificationPolicy
    • WebPushSubscription

使用公开数据

熟悉端点与实体。

    • 关于端点
    • 获取公开时间线
    • 获取公开帐户和嘟文
    • 获取公开实例数据

现在,你已经知道如何使用 cURL 或你喜欢的编程语言的 HTTP 实用程序或库来构造 HTTP 请求,那么现在是时候了解端点和响应了。

关于端点

所有 HTTP 请求都是针对目标 URL 发出的。当你请求网站的数据时,你需要使用特定的 URL。根据 URL 的不同,你的请求将被 HTTP 服务器解释,并向你返回适当的响应。

本文的示例将使用虚构的 Mastodon 网站 mastodon.example,该网站托管在 https://mastodon.example。此网站的根目录为 /,特定的子目录和路径被称为端点。Mastodon 的 API 端点位于 /api 命名空间下,目前大多数方法都将其端点放置于 /api/v1 下。请求将按其 HTTP 方法和端点列出;例如,GET /api/v1/endpoint 应解释为对你域名上的该端点发出的 GET 请求,或者换句话说,https://mastodon.example/api/v1/endpoint。

获取公开时间线

让我们来了解 Mastodon 公开数据最基础的用例之一——公共时间线。

我们可以尝试请求 GET /api/v1/timelines/public,如下所示:

curl https://mastodon.example/api/v1/timelines/public

哇,响应中有很多文本!公共时间线默认返回 20 条嘟文。

一些 Mastodon 实例可能会通过管理设置禁用对其时间线的公开访问。如果你的实例进行了这样的配置,那么你将收到错误响应。

我们可以使用 limit 参数来请求更少的内容。让我们尝试请求相同的端点,但这次限制为 2:

curl https://mastodon.example/api/v1/timelines/public?limit=2

这次我们的响应应该更容易管理。我们可以使用我们选择的实用程序解析或美化这个 JSON,我们应该看到响应类似这样:

[
  {
    "id": "103206804533200177",
    "created_at": "2019-11-26T23:27:31.000Z",
    // ...
    "visibility": "public",
    // ...
  },
  {
    "id": "103206804086086361",
    "created_at": "2019-11-26T23:27:32.000Z",
    // ...
    "visibility": "public",
    // ...
  }
]

对于话题标签,我们可以通过调用 GET /api/v1/timelines/tag/:hashtag 来执行类似的操作——这里,冒号表示端点的这一部分是路径参数。对于 :hashtag,这意味着我们使用标签的名称(我们再次将返回的结果数限制为 2):

curl https://mastodon.example/api/v1/timelines/tag/cats?limit=2

我们应该再次看到,在 Status 实体的 JSON 数组中返回了 2 条嘟文。我们可以按数组,然后按对象解析 JSON。如果我们使用的是 Python,我们的代码可能如下所示:

import requests
import json

response = requests.get("https://mastodon.example/api/v1/timelines/tag/cats?limit=2")
statuses = json.loads(response.text) # this converts the json to a python list of dictionary
assert statuses[0]["visibility"] == "public" # we are reading a public timeline
print(statuses[0]["content"]) # this prints the status text
解析 JSON 并在你的程序中使用它超出了本教程的范围,因为它会因你选择的编程语言和你的程序设计方案而异。你可以查找有关如何在你选择的编程语言中与 JSON 一起使用的其他教程。
MastoVue 是一个应用示例,可让你浏览公开时间线。

获取公开帐户和嘟文

现在,我们熟悉了如何发出请求以及如何处理响应,你可以尝试更多公开数据。以下方法可能会引起你的兴趣:

  • 一旦获取了帐户的 id,你就可以使用 GET /api/v1/accounts/:id 来查看 Account 实体。
    • 要查看该帐户发布的公开嘟文,你可以使用 GET /api/v1/accounts/:id/statuses 并解析生成的 Status 实体数组。
  • 一旦获取了嘟文的 id,你就可以使用 GET /api/v1/statuses/:id 来查看 Status 实体。
    • 你还可以使用 GET /api/v1/statuses/:id/reblogged_by 查看谁转发了该嘟文,
    • 或 GET /api/v1/statuses/:id/favourited_by 查看谁喜欢了该嘟文。
    • 请求 GET /api/v1/statuses/:id/context 将向你显示该嘟文在对话串中的上级嘟文和下级嘟文。
    • 如果该嘟文附加了一个投票,你可以使用 GET /api/v1/polls/:id 单独查看该投票。

帐户和嘟文的 ID 是 Mastodon 网站数据库本地的,并且每个 Mastodon 网站都会有所不同。

获取公开实例数据

你可以使用匿名请求做的最后一件事是查看有关 Mastodon 实例的信息。

  • 使用 GET /api/v1/instance 查看常规信息,
    • 使用 GET /api/v1/instance/peers 查看其联合列表,或
    • 使用 GET /api/v1/instance/activity 查看其每周活动,或
    • 使用 GET /api/v1/custom_emojis 列出所有可用的自定义表情。
  • 有关所有可用配置文件的目录,参见 GET /api/v1/directory。
  • 有关当前热门话题,参见 GET /api/v1/trends。
有关仅使用实例数据可以做什么的实际示例,请查看 emojos.in,它允许你预览给定实例中的所有自定义表情符号。

翻译状态: 本文是英文页面 Playing with public data 的翻译,最后翻译时间:2025-04-21,点击这里可以查看翻译后页面的改动。

最后更新于 April 21, 2025 · 改进此页面
也可在此找到: English

赞助商

Dotcom-Monitor LoadView Stephen Tures Swayable SponsorMotion

加入Mastodon · 博客 ·

查看源代码 · CC BY-SA 4.0 · 版权信息