如果你是一名前端开发者,那么你一定会经常用到 npm 包来加快你的工作进程。今天,我们来介绍一个非常实用的 npm 包:node-red-contrib-tumblr ,它可以帮助你在 Node-RED 中使用 Tumblr,实现自动化的网站内容管理。
什么是 Node-RED
Node-RED 是一个基于 JavaScript 编写的开源流程编排工具,它的主要目的是帮助开发人员快速构建物联网和其他基于物联网应用的流程。Node-RED 的核心思想是连接和流程化,它通过节点(Node)和线(Wire)的连接方式,让用户可以以可视化方式编写流程图。
什么是 Tumblr
Tumblr 是一种非常流行的博客平台,它的用户可以分享图像、音乐、视频等多媒体资源。Tumblr 是一个独立的社交平台,尤其适合年轻人。大量的年轻人在 Tumblr 上创作和分享各种内容,并与其他用户交流。
node-red-contrib-tumblr
node-red-contrib-tumblr 是一个方便使用 Tumblr 的 Node-RED 插件。你可以通过它在 Tumblr 中搜索、发布、更新、删除帖子等功能,同时还支持批量操作。
安装 node-red-contrib-tumblr
在 Node-RED 中安装 node-red-contrib-tumblr 很简单,只需要在命令行中输入以下代码:
npm install node-red-contrib-tumblr
使用 node-red-contrib-tumblr
使用 node-red-contrib-tumblr 操作 Tumblr 非常简单。首先,你需要创建一个 Tumblr API 的开发者账号,然后获取你的 API 密钥。然后,在 Node-RED 中,你需要添加 Tumblr 节点,输入你的 API 密钥和相关参数即可完成设置。
比如说,我想搜索 Tumblr 中的猫咪图片,如下图所示:
参数如下:
- Tags:搜索的关键字,这里我选择“cat”。
- Limit:返回的结果数量,这里我选择了 10。
- Offset:从第几个结果开始返回,这里我设置为 0。
然后,你只需要连接 Tumblr 节点后面的流程,即可完成 Tumblr 的搜索操作。
还可以使用 node-red-contrib-tumblr 进行其他操作,比如更新或删除一个帖子。需要注意的是,所有操作都需要先获取一个 OAuth Token。
示例代码
最后,我们来看一段使用 node-red-contrib-tumblr 的示例代码。这个示例代码演示了如何在 Node-RED 中发布一条新的 Tumblr 帖子。
[{"id":"3e3f1637.83e18c","type":"tumblr-post","z":"cb21c30b.bf3c38","tumblr":"386d7f5f.a14a18","inputType":"payload","stateLocation":"payload","name":"Tumblr Post","account":"salon-102157","tags":"cat","content":"<h2>Hello from Node-RED!</h2>","format":"html","contentUrl":"","summary":"","excerpt":"","caption":"","description":"","link":"","quote":"","source":"","state":"","notes":"","publish":["draft"],"note":"new note","draftTitle":"Test post from Node-RED","idToDelete":"","x":560,"y":200,"wires":[[]]},{"id":"26a99493.9b9edc","type":"inject","z":"cb21c30b.bf3c38","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":390,"y":200,"wires":[["3e3f1637.83e18c"]]},{"id":"f9b5117.2f099a","type":"comment","z":"cb21c30b.bf3c38","name":"Tumblr Post","info":"","x":370,"y":160,"wires":[]},{"id":"386d7f5f.a14a18","type":"tumblr-config","name":"Tumblr Account","host":"","consumerKey":"","consumerSecret":"","oauthToken":"","oauthTokenSecret":"","logOutput":"false"}]
这段代码的主要作用是在 Tumblr 中发布一条新的帖子。具体操作如下:
- 在 Tumblr 上发表一篇新文章:
[{"id":"3e3f1637.83e18c","type":"tumblr-post","z":"cb21c30b.bf3c38","tumblr":"386d7f5f.a14a18","inputType":"payload","stateLocation":"payload","name":"Tumblr Post","account":"salon-102157","tags":"cat","content":"<h2>Hello from Node-RED!</h2>","format":"html","contentUrl":"","summary":"","excerpt":"","caption":"","description":"","link":"","quote":"","source":"","state":"","notes":"","publish":["draft"],"note":"new note","draftTitle":"Test post from Node-RED","idToDelete":"","x":560,"y":200,"wires":[[]]}]
- 执行上面的代码。得到新文章的 ID。
- 把新文章的 ID 修改到这个 json 中: {"id":"", "text": "你的评论"}。
- 部署,启动评论逻辑。
[{"id":"3e3f1637.83e18c","type":"tumblr-post","z":"cb21c30b.bf3c38","tumblr":"386d7f5f.a14a18","inputType":"payload","stateLocation":"payload","name":"Tumblr Post","account":"salon-102157","tags":"cat","content":"{\"id\":\"34343443434\",\"text\":\"评论内容\"}","format":"html","contentUrl":"","summary":"","excerpt":"","caption":"","description":"","link":"","quote":"","source":"","state":"","notes":"","publish":["draft"],"note":"new note","draftTitle":"Test post from Node-RED","idToDelete":"","x":560,"y":200,"wires":[[]]}]
总结
通过本文,你已经了解了如何使用 npm 包 node-red-contrib-tumblr。这个插件非常实用,可以帮助你在 Node-RED 中快速管理 Tumblr 网站的内容。同时,我们也对 Tumblr 和 Node-RED 有了更深入的认识。希望这篇文章对你有所启发,让你在前端开发中更加得心应手。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005726d81e8991b448e8a31