使用 Python 自动化 Mastodon 互动

跟随我一起玩转 Mastodon API,创建一个新的应用程序。
1 位读者喜欢这篇文章。
Women in computing and open source v5

kris krüg

联邦制的 Mastodon 社交网络最近非常流行。在社交媒体上发帖很有趣,但自动化你的互动也很有趣。关于面向客户端的 API,有一些 文档,但示例有点少。本文旨在对此提供帮助。

在尝试学习本文之前,你应该对 Python 相当有信心。如果你对 Python 还不熟悉,请查看 Seth Kenlon 的Python 入门 文章和我写的 编写一个简单的游戏 文章。

创建一个应用程序

第一步是转到 Mastodon 中的偏好设置,然后选择开发类别。在开发面板中,单击新建应用程序按钮。

创建应用程序后,复制访问令牌。请小心保管。这是您发布到您的 Mastodon 帐户的授权。

有一些 Python 模块可以提供帮助。

  • 考虑到它是一个 Web API,httpx 模块非常有用。
  • getpass 模块允许您安全地将令牌粘贴到会话中。
  • Mastodon 使用 HTML 作为其帖子内容,因此以内联方式显示 HTML 的好方法非常有用。
  • 沟通完全是关于时机的,dateutilzoneinfo 模块将有助于处理时间问题。

这是我的典型导入列表的样子

import httpx
import getpass
from IPython.core import display
from dateutil import parser
import zoneinfo

将令牌粘贴到 getpass 输入中

token=getpass.getpass()

创建 httpx.Client

client = httpx.Client(headers=dict(Authorization=f"Bearer {token}"))

verify_credentials 方法用于验证令牌是否有效。这是一个很好的测试,它可以为您提供有关您帐户的有用元数据

res = client.get("https://mastodon.social/api/v1/accounts/verify_credentials")

您可以查询您的 Mastodon 身份

res.raise_for_status()
result = res.json()
result["id"], result["username"]

>>> ('27639', 'moshez')

您的结果可能会有所不同,但您会收到您的内部 ID 和用户名作为响应。ID 稍后可能会有用。

现在,抽象出 raise_for_status 并解析 JSON 输出

def parse(result):
    result.raise_for_status()
    return result.json()

这就是它的用途。现在您可以按 ID 检查您的帐户数据。这是一种很好的交叉检查一致性的方法

result = parse(client.get("https://mastodon.social/api/v1/accounts/27639"))
result["username"]

>>> 'moshez'

当然,有趣的是获取您的时间线。幸运的是,有一个 API 可以做到这一点

statuses = parse(client.get("https://mastodon.social/api/v1/timelines/home"))
len(statuses)

>>> 20

这只是帖子计数,但这现在足够了。现在还不需要处理分页。问题是,您可以使用帖子列表做什么?嗯,您可以查询各种有趣的数据。例如,谁发布了第四个状态?

some_status = statuses[3]
some_status["account"]["username"]

>>> 'donwatkins'

太棒了,来自 Opensource.com 通讯员 Don Watkins 的一条推文!总是很棒的内容。我会去看看

display.HTML(some_status["content"])

<p>Just finished installed <span class="h-card"><a href="https://fosstodon.org/@fedora" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>fedora</span></a></span> <a href="https://fosstodon.org/tags/Silverblue" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>Silverblue</span></a> 37 on <span class="h-card"><a href="https://fosstodon.org/@system76" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>system76</span></a></span> <a href="https://fosstodon.org/tags/DarterPro" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>DarterPro</span></a></p>

“刚刚”完成?等等,这条推文是什么时候发布的?我住在加利福尼亚州,所以我想知道我当地时区的时间

california = zoneinfo.ZoneInfo("US/Pacific")
when = parser.isoparse(some_status["created_at"])
print(when.astimezone(california))

>>> 2022-12-29 13:56:56-08:00

今天(在撰写本文时),下午 2 点前一点。谈论及时性。

您想亲自查看帖子吗?这是 URL

some_status["url"]

>>> 'https://fosstodon.org/@donwatkins/109599196234639478'

享受发嘟嘟声吧,现在 API 增加了 20%!

Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
Moshe 自 1998 年以来一直参与 Linux 社区,帮助举办 Linux “安装派对”。他自 1999 年以来一直编写 Python 程序,并为核心 Python 解释器做出了贡献。Moshe 自这些术语出现之前就一直担任 DevOps/SRE,非常关心软件可靠性、构建可重现性以及其他此类事情。

2 条评论

感谢您的提及 Moshez。您真是太好了。我真的很喜欢这篇文章,您启发了我尝试这种 Python 用法。

非常好。又多了一个让我开始使用 Mastodon 的理由。

© . All rights reserved.