Hi everyone! Love discourse, it’s the backbone of our community.
We are an open source project with a strong presence on Github. We have a robot written in Python which manages our Github repositories and does things like merge pull requests automatically if they have been approved. Since we have a lot of repositories and not everyone follows every repository it would be awesome if the robot could post in the forums when a new project is created or when a pull request needs to be reviewed.
Can anyone point me in the direction of how I could best call the Discourse API from Python?
3 个赞
blake
(Blake Erickson)
2018 年5 月 15 日 16:44
2
There is a python discourse api library that is a few years old:
A Python library for the Discourse API
And a forked version that has been updated somewhat recently:
A Python library for the Discourse API
Hopefully the latter one will work for you.
Also this plugin may be of interest to you:
https://meta.discourse.org/t/the-github-linkback-plugin/66081
7 个赞
Thanks @blake ! That is exactly the kind of thing I needed to get started.
I will report back with my successes.
2 个赞
I recently started a project for this, and am actively updating it. Currently working towards the next dot release:
https://github.com/samamorgan/discourse
10 个赞
Did it work out for you? I am trying this as well. I keep getting “DiscourseClientError: Payload Too Large”.
1 个赞
I’m suddenly seeing this error too over the weekend when attempting to fetch a topic. This topic fetch code hasn’t changed in over a year, so something about the API must have changed. I’m investigating.
2 个赞
Payload Too Large is because of this change .
I don’t know about the project mentioned above, but here is the status for bennylope’s project. Maybe the same fix is needed on other wrappers as well.
GitHub - pydiscourse/pydiscourse: A Python library for the Discourse API issue #38 fixes the problem, but the fixed version has not yet been pushed to pypi- pydiscourse.exceptions.DiscourseClientError: Payload Too Large · Issue #38 · pydiscourse/pydiscourse · GitHub . Waiting for github @bennylope to do it. So you can wait for pypi to be updated, or git pull master.
5 个赞
Thanks! This worked. I uninstalled pydiscourse, and used a local copy based on what you suggested. And now, it works!
I am trying to use Python’s Sentiment analyzer to get a sentiment analysis of the latest posts. I will keep you updated on how it goes.
1 个赞
Is anybody else using this? Are you able to get more than 20 posts per topic using this?
1 个赞
You probably need to use pagination. I haven’t tried this for posts specifically, but something like:
pagesize = 20
for page in range(100):
client.posts(topic_id, limit=pagesize, offset=page*pagesize)
3 个赞
EricGT
(EricGT)
2023 年8 月 31 日 07:22
11
是的,我知道这是一个旧帖子,但由于它基本上是我想要的,所以没有必要创建一个新帖子,因为它起作用,只是需要更新。
虽然我确实知道如何用许多编程语言编程,但很抱歉,Ruby 不是其中之一。我想知道这个问题是否有更新、更好的答案。
从 Python 与 Discourse 交互?
作为附带问题,可以使用 Python 创建 Discourse 插件和/或主题吗?
1 个赞
主题,绝对不行,那只涉及 Javascript、CSS 和 HTML。
插件:可以说是。
您可以在基于 Ruby (on Rails) 的 Discourse 插件中导入 Python 模块,并使用 Pycall 调用 Python 函数:GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language
我在生产环境中使用它,因为它有一个非常特定的用途,在 RoR 生态系统中没有维护等效的模块,因此我无需将所有专业代码翻译成 Ruby 并进行维护。
它的效果出奇地好。
话虽如此,该插件的基础仍然是用 Ruby 编写的。
我建议您也学习 Ruby on Rails——它确实是一种非常令人愉快的语言,在我看来,它的代码比 Python 更易读
如果需要,我也应该花时间学习更多 Python。
有趣的附带说明:Discourse Chatbot 🤖 中创建代理行为的一些代码基于一个名为“Funkagent”的 Python 代理,我首先将其翻译成 Ruby on Rails(然后进行了修改、增强)。因此,翻译当然也是一个选项,如果这样做有意义的话。在这种情况下,这是有意义的。
3 个赞
pfaffman
(Jay Pfaffman)
2023 年8 月 31 日 08:47
14
在我真正“学会”Ruby之前,我写了几个导入脚本。根据你想做什么,你可能会发现用你现有的技能集能做的事情比你想象的要多。使用Ruby可能比弄清楚如何使用其他东西更容易。
不过,如果你正在与API交互,那么你可以使用任何你想要的语言。
5 个赞
Ruby 设计优美,而且我认为很容易学习。
Ruby on Rails 需要多学一点,而且作为一个(依赖约定)的框架,有时会有点棘手,但一旦你掌握了它,就会非常强大。
2 个赞
pfaffman
(Jay Pfaffman)
2023 年8 月 31 日 13:00
17
EricGT:
可以推荐一个更简单的吗?
你可以看看 GitHub - discourse/all-the-plugins 来了解有哪些可能性。
选择一个你想要做并且看起来简单的东西。如果你想要像 cakeday 那样改变周年纪念日的定义,那么 cakeday 可以是一个很好的起点。你需要选择一个你想改变的小东西,然后一次只做一小步。
2 个赞
remidm
(Rémi Delaporte-Mathurin)
2026 年3 月 4 日 18:43
19
大家好,我也对从 Python 中获取 Discourse 的数据(主要是关于浏览量和用户数量的统计数据)感兴趣。这里列出的很多包似乎都不再活跃了。在 2026 年,是否有新的标准方法来做这件事?
谢谢!
1 个赞
remidm:
我也对从 discourse 获取数据感兴趣
你不总是需要使用别人的界面。
通过内置的 requests 库来做这件事并不太麻烦,如果你使用像 Postman(我已经设置好了)这样的工具,你可以将我们的 API 规范 导入到其中,然后让它生成代码:
然后根据你的需求进行调整:
import json
import pandas
import requests
url = "https://try.discourse.org/categories.json?include_subcategories=false"
payload = {}
headers = {
'Accept': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload)
response_data = json.loads(response.text)
data = pandas.DataFrame(response_data['category_list']['categories']).set_index('id')
现在我有了类别信息:
name color text_color ... uploaded_background_dark topics can_vote
id ...
5 general 25AAE2 FFFFFF ... None [{'fancy_title': 'Testing dulu ya jangan di hi... NaN
4 videos 258af1 FFFFFF ... None [{'fancy_title': 'Ikan ganteng yang’&rdq... NaN
86 calendar 12A89D FFFFFF ... None [{'fancy_title': 'Category Calendar demo topic... NaN
2 tech 444 FFFFFF ... None [{'fancy_title': 'Poll: What’s your pref... NaN
1 discourse 00B355 FFFFFF ... None [{'fancy_title': 'Welcome to our demo!', 'id':... NaN
53 Topic Voting F7941D FFFFFF ... None [{'fancy_title': 'Is this topic worth voting f... True
6 gaming 800080 FFFFFF ... None [{'fancy_title': 'Impressions Games City Build... NaN
8 movies B22222 FFFFFF ... None [{'fancy_title': 'What’s your all-time f... NaN
9 sports 0000FF FFFFFF ... None [{'fancy_title': 'Modernizing the antiquated b... NaN
[9 rows x 45 columns]
如果你想访问受保护的信息,你需要使用 API 密钥 。
6 个赞