怎么用JS读取作者部分或者所有的帖子?
由于官方的steemjs开发文档实在太简陋了,所以打算具体介绍一些函数的怎么使用,并且把代码放到了github上.
刚看到@lemooljiang蒋老师在用getDiscussionsByAuthorBeforeDate的函数,并且看到他遇到了一些问题,所以打算介绍一下这个函数。
官方的文档对这个函数的介绍:
steem.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function(err, result) {
console.log(err, result);
});
- author: 作者
- startPermlink: 从哪篇帖子开始。如果是最早的那篇,这里填null
- beforeDate: 从哪个时间开始的文章
- limit:一次读取多少篇帖子。(最高限制100篇)
如果你要显示最近100篇帖子的标题, 程序可以这样写:
const steem = require('steem');
start();
async function start() {
let posts = await getPosts('ericet');
posts.forEach(post => {
console.log(post.root_title);
});
}
function getPosts(author, startPermlink = null, limit = 100) {
return new Promise((resolve, reject) => {
let beforeDate = new Date().toISOString().split('.')[0];
steem.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function (err, result) {
if (result.length ) {
resolve(result)
}
});
});
}
但是,如果你想要显示超过100篇的帖子,那你可能和蒋老师一样陷入麻烦:SteemJS开发的难与易
这里介绍一个简单的方法,读取超过100篇帖子。那就是用Recursion(递归)。Recursion的意思是函数的定义中使用函数自身的方法。方法如下:
function getPosts(author, startPermlink = null, limit = 100, posts = []) {
return new Promise((resolve, reject) => {
let beforeDate = new Date().toISOString().split('.')[0];
steem.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function (err, result) {
if (result.length > 1) {
posts = [...posts, ...result];
getPosts(author, result[result.length - 1].permlink, limit, posts)
.then(resolve)
.catch(reject);
} else {
resolve(posts);
}
});
});
}
这段代码里,当返回的结果多于1,就会用最后一篇帖子的permlink作为起始点,继续读取帖子。比如第一轮读取1-100,第二轮100-200,第三轮200-300,。。。,直到没有文章可以读取。
但是有一个小问题,那就是每轮的最后一篇和下一轮的第一篇是重复的。这里可以过滤掉重复的那篇,如果startPermlink等于文章的permlink,就跳过,代码如下:
function getPosts(author, startPermlink = null, limit = 100, posts = []) {
return new Promise((resolve, reject) => {
let beforeDate = new Date().toISOString().split('.')[0];
steem.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function (err, result) {
if (result.length > 1) {
let newResult = [];
result.forEach(post =>{
if(post.permlink!=startPermlink){
newResult.push(post);
}
});
posts = [...posts, ...newResult];
getPosts(author, result[result.length - 1].permlink, limit, posts)
.then(resolve)
.catch(reject);
} else {
resolve(posts);
}
});
});
}
完整的代码如下:
const steem = require('steem');
start();
async function start() {
let posts = await getPosts('ericet');
posts.forEach(post => {
console.log(post.root_title);
});
}
function getPosts(author, startPermlink = null, limit = 100, posts = []) {
return new Promise((resolve, reject) => {
let beforeDate = new Date().toISOString().split('.')[0];
steem.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function (err, result) {
if (result.length > 1) {
let newResult = [];
result.forEach(post =>{
if(post.permlink!=startPermlink){
newResult.push(post);
}
});
posts = [...posts, ...newResult];
getPosts(author, result[result.length - 1].permlink, limit, posts)
.then(resolve)
.catch(reject);
} else {
resolve(posts);
}
});
});
}
这是结果:
本文的代码可在这里找到:https://github.com/ericet/steemjs-fun/blob/master/getDiscussionsByAuthorBeforeDate.js
自己也是js的初学者,所以有任何问题,欢迎留言指出~
STEEM编程系列:
cn
cn-stem
steemstem
cn-reader
whalepower
steemleo
jjm
build-it
sct
sct-cn
sct-freeboard
palnet
zzan
dblog
mediaofficials
actnearn
marlians
neoxian
lassecash
upfundme
0
0
0.000
@tipu curate 4
Upvoted 👌 (Mana: 0/4)
Support your post (by LEO Token)
Thank you for sharing, Nice post
Thank you for supporting @CatsMakeKittens by being a part of our community @ericet.
Each CATS you purchase gets you daily upvotes from me @CatScientist as our community grows so do your rewards for being a member!
我绝对看不懂……来凑个热闹……😄
来自于 [WhereIn Android] (http://www.wherein.io)
!shop 不需要看得懂
Posted using Partiko iOS
你好鸭,糖糖!
@ericet赠送1枚SHOP币给你!
目前你总共有: 56枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.布
来自于 [WhereIn Android] (http://www.wherein.io)
You win!!!! 你赢了!愿赌服输! 给你1枚SHOP币!
😀 现在赢了有shop币送呀……
来自于 [WhereIn Android] (http://www.wherein.io)
我不知道你在说什么,因为大智若愚。
哈哈哈哈……大智若愚……真会装……😂😂😂
来自于 [WhereIn Android] (http://www.wherein.io)
今天天气不错,你觉得呢?
是的,一天最多2个
Posted using Partiko iOS
jcar토큰 10월 구독보팅입니다. 항상 jcar를 응원해주셔서 감사드립니다. 날마다 좋은 하루 되세요. ^^
Support your post (by ACTNEARN Token)
Thank you for sharing, Nice post
村长威武!帮了大忙!
不客气,你遇到的问题我也遇到过
Posted using Partiko iOS
加油!@ericet,
来自鸽子在Marlians的支持。
加油!@ericet
鸽子在mediaofficials支持你。
你今天过的开心吗?还在发愁自己的好文没人发现,收益惨淡吗?记得加上cn-curation标签,让飞鸽传书 帮你走出困境吧!倘若你想让我隐形,请回复“取消”。
Du erhieltest aufgrund deiner LanaCharleenToken Miner ein Upvote von @sebescen81
Vielen lieben Dank für euren Support. Der Account meiner Tochter wächst und gedeiht.
Hi @ericet!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 4.693 which ranks you at #1669 across all Steem accounts.
Your rank has not changed in the last three days.
In our last Algorithmic Curation Round, consisting of 91 contributions, your post is ranked at #13.
Evaluation of your UA score:
Feel free to join our @steem-ua Discord server
ericet,感谢你赠送SHOP币给1名用户, 你将获得来自我的32.25%点赞~
@tipu curate
Thank you for using the #build-it tag. We have manually rewarded this post from our @build-it.curator account with BUILD tokens in the form of an upvote.
Build-it is a central hub for DIY and How-To projects. It's an opportunity to find and share the latest in DIY, and How-To tutorials. The recommended tags required when creating a DIY article are #diy, #build-it. #how-to, or #doityourself. Please read our guide
Need help? Live technical support on Discord or Telegram
This project is run and supported by our witness @gulfwaves.net. If you like what we do, click here to vote for us
哈哈.我也是湊熱鬧的
以前會寫
現在都還給老師了
!shop
!BEER
你好鸭,村长!
@cloudblade赠送1枚SHOP币给你!
目前你总共有: 178枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.!thumbup
恭喜你!您的这篇文章入选 @justyy 今日 (2019-10-19) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com
Congratulations! This post has been selected by @justyy as today's (2019-10-19) 【Good Posts】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^
SteemIt 工具、API接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial
If you believe what I am doing, please consider a spare vote voting me here, thank you very much indeed.
@justyy - the author of https://SteemYY.com and I have been a Steem Witness for more than a year now.
谢谢
Posted using Partiko iOS