监测 Reddit 帖子和评论中提到的关键词对于监测您的品牌、产品、竞争对手,了解特定主题非常有用...Go 是开发此类社交监听应用程序的绝佳语言。本文将介绍如何编写一个简单的 Go 程序来监控 Reddit 上的关键词。
Reddit 上的社交聆听对于个人和组织来说都是一个非常强大的工具,可以实现各种目标。以下是其尤为重要的几个原因:
Reddit 提供了几个免费的 API 端点,让你可以获取平台上的每一条新帖子或评论。这些端点的文档并不完善。
要获取最近 100 篇 Reddit 帖子,应向以下 API 端点发出 GET HTTP 请求: https://www.reddit.com/r/all/new/.json?limit=100
要获取最近 100 条 Reddit 评论,应向以下 API 端点发出 GET HTTP 请求: https://www.reddit.com/r/all/comments/.json?limit=100
这些 API 端点的响应是一个 JSON 对象,其中包含一个帖子或评论列表。
下面是我请求帖子端点时得到的响应示例(截断):
curl https://www.reddit.com/r/all/new/.json?limit=100
{
"kind": "Listing",
"data": {
"after": "t3_1asad4n",
"dist": 100,
"modhash": "ne8fi0fr55b56b8a75f8075df95fa2f03951cb5812b0f9660d",
"geo_filter": "",
"children": [
{
"kind": "t3",
"data": {
"approved_at_utc": null,
"subreddit": "GunAccessoriesForSale",
"selftext": "Morning gents. I\u2019m looking to snag up your forgotten factory yellow spring for the 509T. I need to source one for a buddy who lost his and I cannot find any available anywhere! \n\nIf one of you have the yellow spring laying around, looking to pay $50 shipped\u2026 \n\nTo my 509t owners, it\u2019s the \u201clight\u201d spring that comes in a plastic bag in the carrying case. \n\nThanks in advance ",
"author_fullname": "t2_2ezh71n6",
"saved": false,
"mod_reason_title": null,
"gilded": 0,
"clicked": false,
"title": "[WTB] 509T yellow spring",
"link_flair_richtext": [],
"subreddit_name_prefixed": "r/GunAccessoriesForSale",
[...]
"contest_mode": false,
"mod_reports": [],
"author_patreon_flair": false,
"author_flair_text_color": "dark",
"permalink": "/r/GunAccessoriesForSale/comments/1asadbj/wtb_509t_yellow_spring/",
"parent_whitelist_status": null,
"stickied": false,
"url": "https://www.reddit.com/r/GunAccessoriesForSale/comments/1asadbj/wtb_509t_yellow_spring/",
"subreddit_subscribers": 182613,
"created_utc": 1708094934.0,
"num_crossposts": 0,
"media": null,
"is_video": false
}
},
[...]
]
}
}
如你所见,Reddit 会返回一个对象数组。每个对象都是一篇帖子或一条评论(取决于您请求的端点),其中包含提交内容、提交网址、创建日期和时间等全面的详细信息。其中大部分都不会有用,而 "自文 "无疑是最重要的元素,因为它包含了帖子或评论的内容。
结果按降序排列。请注意,您不能同时申请超过 100 个帖子或评论。
Go 编程语言具有多种强大功能,是实时检索 Reddit 帖子和评论的绝佳选择。虽然其他编程语言也可用于此目的,但使用 Go 有一些特殊的优势,尤其是在处理实时数据时。以下是 Go 脱颖而出的原因:
首先,Go 的轻量级线程(称为 goroutines)可以实现高效的多任务和并发性。这在同时获取多个 Reddit 帖子或评论时尤其有用,因为每个获取操作都可以在自己的 goroutine 中运行。
其次,Go 的标准库包含一个全面而高效的 HTTP 客户端,可以简化网络请求的过程。这对于与 Reddit 的 API 交互以获取帖子和评论至关重要。Go 的 HTTP 客户端支持上下文,允许请求超时和取消。这对于实时应用程序来说至关重要,因为在这种情况下,您需要确保您的应用程序保持响应,不会因响应延迟而挂起。
最后,Go 是一种编译语言,这通常意味着用 Go 编写的应用程序速度快、占用空间小。这对于需要快速处理大量数据的实时应用程序非常有利。Go 的垃圾回收器设计得非常高效,并能保持较低的延迟,这对于在实时数据获取场景中保持性能至关重要。
下面是一个关于制作 Go 程序的分步计划,该程序可以监控 Go 帖子中的关键字 "kwatch.io":
下面是围棋代码:
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type Post struct {
Selftext string `json:"selftext"`
Title string `json:"title"`
Permalink string `json:"permalink"`
}
type Data struct {
Children []struct {
Data Post `json:"data"`
} `json:"children"`
}
type Response struct {
Data Data `json:"data"`
}
func fetchPosts() {
resp, err := http.Get("https://www.reddit.com/r/all/new/.json?limit=100")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
var r Response
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
fmt.Println(err)
return
}
for _, child := range r.Data.Children {
if strings.Contains(child.Data.Title, "kwatch.io") || strings.Contains(child.Data.Selftext, "kwatch.io") {
fmt.Println("Title:", child.Data.Title)
fmt.Println("Selftext:", child.Data.Selftext)
fmt.Println("Permalink:", child.Data.Permalink)
fmt.Println()
}
}
}
func main() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
fetchPosts()
}
}
该程序将每秒(异步)从 Reddit 获取最近 100 篇新帖,并将每篇帖子的标题、自文和永久链接打印到控制台。只需更改 API 端点 URL,就能对 Reddit 评论做同样的处理。
以下是一些关于如何改进该计划的想法:
通过一个相当简单的围棋程序,就可以监控 Reddit 上的特定关键词。
不过,要制作这样一个程序是很有挑战性的。首先是因为 Reddit 擅长在请求过多时阻止你,而且他们的 API 端点会同时返回大量帖子和评论,这意味着你的 Go 程序必须巧妙地处理这些大量请求。
如果您不想自己建立和维护这样一个系统,我们建议您使用我们的平台: 点击此处在 KWatch.io 上注册。
Arthur
Go 开发者 KWatch.io