Brighttux's chronicles of playing with and learning steem.js API #2
Hey guys,
Welcome back to my adventure with steem.js API.
NOTE to @teammalaysia admin
Kindly tell me if it's okay that i tag multiple people who are using #teammalaysia in their post :)
If you're wanting to learn some basic programming, i believe you'll be able to find yourself comfortable here :)
tl;dr version
Before we get started, here's photo i took:
While my PC is busy working in the background, i thought i could use some time to explore steem.js again today.
In the previous exploration, we discovered that we're able to pull some data by sending a simple command.
var query = {
limit: 10,
tag: 'teammalaysia'
};
steem.api.getDiscussionsByActive(query, function(err, result) {
console.log(err, result);
});
Now, that's all fine and dandy, but useless... somewhat.. because the data returned is not really useful, because it is only extracted based on whether or not it is currently active...
Objective:
What i'm trying to achieve today is to pull all the "latest" discussions, or discussions in created today and see how well the post are doing.
Let's see how it goes:
var query = {
limit: 100,
tag: 'teammalaysia',
truncate_body: 1
};
steem.api.getDiscussionsByCreated(query, function(err, result) {
var currentDate = 19;
for(var i = 0; i < result.length; i++)
{
//create new variables
var thisAuthor = result[i].author
var thisDate = result[i].created;
var thisValue = result[i].pending_payout_value;
var thisNumVotes = result[i].active_votes.length;
var thisTitle = result[i].title;
var newDate = thisDate.substring(0,10);
var newTime = thisDate.substring(11,19);
var newDate_DD, newDate_MM, newDate_CCYY;
newDate_CCYY = newDate.substring(0,4);
newDate_MM = newDate.substring(5,7);
newDate_DD = newDate.substring(8,11);
//use this to check if you're extracting the dates correctly
//console.log(newDate_CCYY, newDate_MM, newDate_DD);
if(newDate_DD < currentDate)
{
console.log("This post is the previous day post. Time to exit this loop");
console.log("Just to prove:");
console.log(thisAuthor, ", ", newDate, ", ", newTime, ", ", thisTitle, ", ", thisValue, ", ", thisNumVotes);
break;
}
else
{
console.log(thisAuthor, ", ", newDate, ", ", newTime, ", ", thisTitle, ", ", thisValue, ", ", thisNumVotes);
}
}
});
Explanation:
var query = {
limit: 100,
tag: 'teammalaysia',
truncate_body: 1
};
- The query i used had a limit of 100, meaning, the API call would only be able to pull a maximum number of 100 results.
tag: 'teammalaysia'
Only extract post with the tag #teammalaysiatruncate_body: 1
basically means i'm only extracting 1 byte of data from the<body>
element
steem.api.getDiscussionsByCreated (query, function(err, result) {});
- Is used to pull out all discussions based on the "creation date", ie: when you posted it.
- this function takes in 2 inputs:
- query <- we have provided above
- function(err,result) <- this is a
callback
function which is basically a fancy way of saying that, upon completion of the previous operations in this function, run these (kindly ask me if you would like me to explain more aboutcallback
functions)
var currentDate = 19;
- Since today is the 19th of April, i'm gonna make sure i only extract post that are created on the 19th
for(var i = 0; i < result.length; i++)
Now, since we know the limit of the extracted data is 100 posts, but we currently do not know many post were created today yet. So i'm gonna implement a for loop
which will run from 0
to result.length
ie 100
.
var thisAuthor = result[i].author
var thisDate = result[i].created;
var thisValue = result[i].pending_payout_value;
var thisNumVotes = result[i].active_votes.length;
var thisTitle = result[i].title;
var newDate = thisDate.substring(0,10);
var newTime = thisDate.substring(11,19);
var newDate_DD, newDate_MM, newDate_CCYY;
newDate_CCYY = newDate.substring(0,4);
newDate_MM = newDate.substring(5,7);
newDate_DD = newDate.substring(8,11);
In this for loop
, i created some new variables to help us visualize and understand better. The object result
has plenty members which has plenty of properties such as the ones listed here: author
, created
, pending_payout_value
, active_votes
and more... Basically, these values belongs to the object result
which is an array of arrays... if that makes any sense to you haha.
A simple way of understanding it is this:
My name is Clarence, i'm 30 years old, i'm married, i'm a student and i'm a human. My ID is 123
.
So translating that into an object
would looks something like this.
- Since there are many humans on this planet, the object
human
would haven
numbers -> population of humans.
In this example, let me use the number123
to represent myself
human[123].name = Clarence
human[123].age = 30
human[123].marital_status = married
human[123].occupation = student
Does this help you understand better? I hope it does haha.
So the new variables created are basically to pull those data out into a form where i can see and use them easily.
I'm gonna skip the rest of the codes, basically, it's just me, pulling the codes, and formatting them in ways i can understand them easily.
Output:
Now with some clever formatting, and messing around, i was able to generate this table easily:
Author | Date | Time | Title | Post Value | Number of Votes | |
---|---|---|---|---|---|---|
@kokuryo | 2018-04-19 | 09:05:00 | 李王张刘好声音 - 信乐团《One Night in 北京》 | 0.008 SBD | 1 | |
@orangila | 2018-04-19 | 08:48:42 | Orangila's Belief and Lifestyle As A Minimalist - Part 2 of 2 | 0.098 SBD | 4 | |
@alimamasstory | 2018-04-19 | 08:48:03 | 5-minute Freewrite: Prompt- Coin | 0.027 SBD | 2 | |
@amalinaahmd | 2018-04-19 | 08:44:33 | Speed drawing of Lonely Girl | 0.000 SBD | 0 | |
@coloringiship | 2018-04-19 | 08:34:45 | My Sketchbook - Sketches Inspired By Obscure Sorrows | 2.918 SBD | 32 | |
@debbie89 | 2018-04-19 | 08:33:15 | ⛪CHILDREN'S SUNDAY SCHOOL⛪ | 0.023 SBD | 1 | |
@fundurian | 2018-04-19 | 08:29:03 | Dubuyo Korean spicy beef noodle, Dubuyo韩国牛肉辣面 | 4.956 SBD | 73 | |
@legrandkei | 2018-04-19 | 08:12:33 | I'm starting a new start-up, to show myself that I can do it. | 0.002 SBD | 1 | |
@kadishakho | 2018-04-19 | 08:10:15 | 一则重要的分享! | 0.000 SBD | 0 | |
@nazirullsafry | 2018-04-19 | 07:43:57 | Basheer Graphic Books new store location | 0.377 SBD | 24 | |
@bboyady | 2018-04-19 | 07:00:54 | When Negativity over Positivity//当负能量超越正能量 | 0.249 SBD | 4 | |
@angeljames | 2018-04-19 | 06:45:24 | Musicoin (Musicians get paid without intermediaries) | 2.932 SBD | 63 | |
@itrmarcusliew | 2018-04-19 | 06:41:57 | makemesmile 小农夫 | 2.680 SBD | 68 | |
@snakesolidus | 2018-04-19 | 06:26:21 | My Name Is Bond, James Bond : Skyfall | 0.000 SBD | 2 | |
@tvlee | 2018-04-19 | 06:10:18 | Harmony 和谐 | 0.054 SBD | 7 | |
@mrnoob | 2018-04-19 | 06:08:36 | Mr. Noob plays Dark Souls Prepare to Die Edition EP08 | 2.697 SBD | 62 | |
@zaraa15 | 2018-04-19 | 05:52:39 | Horror Story : Empty Room..! | 2.714 SBD | 64 | |
@yasminep | 2018-04-19 | 05:46:48 | And the winner is... Contest Week 3 Winner! | 3.473 SBD | 8 | |
@kadishakho | 2018-04-19 | 05:44:33 | 这样说对不对,有道理吗? | 0.001 SBD | 1 | |
@alansthoughs | 2018-04-19 | 05:37:00 | Steemit sandwich contest week 37: fries X lasagna | 0.165 SBD | 6 | |
@zublizainordin | 2018-04-19 | 05:36:36 | Zubli Zainordin dan Sharil Tawaran demi SteemBahasa Bukan Tawar tapi Manis | 2.860 SBD | 7 | |
@myach | 2018-04-19 | 05:30:21 | TeamMalaysia's Daily Picks #9 - @veenang, @dxn & @danieldoughty | 20.999 SBD | 122 | |
@evangelinesp | 2018-04-19 | 05:19:36 | Phantom Of The Opera??? | 2.823 SBD | 74 | |
@abellehera | 2018-04-19 | 05:07:24 | Orang cakap hobi saya tidak menarik | 2.681 SBD | 75 | |
@smil3 | 2018-04-19 | 05:00:42 | 5种会被社会慢慢淘汰的人! | 2.683 SBD | 69 | |
@annekins | 2018-04-19 | 04:56:06 | Maybe some days we are better wired for particular tasks… | 2.676 SBD | 70 | |
@liewsc | 2018-04-19 | 04:24:30 | 你的旋律,我的主题 # 20 - 夜已深 - 月旦评 | 不放心吐槽大会 | 2.803 SBD | 82 |
@nazirullsafry | 2018-04-19 | 04:21:15 | 100th Days in Steemit | 3.100 SBD | 71 | |
@kenclickrich | 2018-04-19 | 04:21:12 | London Travel Blog 2017 Part 2 | 2.699 SBD | 69 | |
@v1tko | 2018-04-19 | 04:08:00 | Recipe 102 : Apam Balik(Peanut Pancake) | 2.988 SBD | 75 | |
@wilhb81 | 2018-04-19 | 04:06:27 | 纽约的司机驾着北京的梦 – 张学友-李王张刘好声音 | Music Challenge E | 3.451 SBD | 115 |
@nickychu | 2018-04-19 | 03:39:06 | LTAM : Peavey Transtube EFX Amplifier | 0.616 SBD | 10 | |
@ryanng | 2018-04-19 | 03:36:03 | Daily Forex #28 - Is Forex Education Really Important? | 4.915 SBD | 41 | |
@fr3eze | 2018-04-19 | 03:32:54 | The best way to crack Photoshop CC | 59.427 SBD | 45 | |
@howtostartablog | 2018-04-19 | 02:58:42 | Guess the movie #17 - Results | 11.870 SBD | 131 | |
@digitalmind | 2018-04-19 | 02:44:21 | PETALS CONTEST-FLOWERS - Pink Flower of Fu Gui Flower | 1.200 SBD | 10 | |
@superoo7 | 2018-04-19 | 02:41:00 | Trying out parley.io | 1.612 SBD | 35 | |
@ryanng | 2018-04-19 | 02:35:45 | BBQ Wings & Bak Kwa!!! - Singapore | 0.014 SBD | 6 | |
@nostalgic1212 | 2018-04-19 | 02:34:48 | 【Vicky's 旅行】MySmartphonePhotography - 我完成了第一次下潜🎉 | 4.167 SBD | 37 | |
@ryanng | 2018-04-19 | 02:30:33 | 麻辣香锅 - Home Cooked Version - Singapore | 0.007 SBD | 3 | |
@arwine | 2018-04-19 | 02:16:12 | Tips menjaga penampilan diri agar kelihatan cantik dan menarik | 3.110 SBD | 96 | |
@lilacse | 2018-04-19 | 01:56:30 | Reflection on my 1 month holiday :P | 0.058 SBD | 6 | |
@janicechua | 2018-04-19 | 01:47:48 | 李王张刘好声音 隐形的翅膀 + 有形的翅膀 | 1.134 SBD | 45 | |
@walkinharmony | 2018-04-19 | 01:45:57 | People who prefer to be alone are more excellent | 2.881 SBD | 65 | |
@zublizainordin | 2018-04-19 | 01:44:36 | Zubli Zainordin Kongsi Informasi Ibarat Air | 2.907 SBD | 13 | |
@maverickfoo | 2018-04-19 | 01:25:00 | Mini Motivation #94 - The Worst Advice You Can Ever Get | 4.072 SBD | 63 | |
@aurah | 2018-04-19 | 01:20:42 | Steem Cartoon : From Colour Script to Background Animation | 21.177 SBD | 65 | |
@chloephuan93 | 2018-04-19 | 01:02:21 | Breakfast! | 0.165 SBD | 7 | |
@spaceranger | 2018-04-19 | 00:55:42 | Diary of Life | 2.657 SBD | 63 | |
@joeliew | 2018-04-19 | 00:30:27 | 巴占花园里看不起眼的屋边炒粉 | 2.916 SBD | 75 |
Issues faced:
During my experiments, i found out through my previous post that the
limit
parameter had a maximum value of 100.FC_ASSERT( limit <= 100 );
This is probably so that the machine doesn't take too long to process/extract all the data in the database, however, this also means that i will not be able to use this method to pull enough data IF there are more and more users who actually use the tag #teammalaysia.I also noticed that i was not able to pull all of the post which was created today with the #teammalaysia tag. Example is this post from my friend @danielwong ... which created 2 hours ago, but was not extracted...
The time of the post ... probably using some other time zone, hence the difference in time... i'll have to figure that out later... haha
Final note:
Thanks for reading, and if you would like to try it for yourself... you can find my codes here:
https://github.com/BrightTux/Steem-js-Exploration/blob/master/Exploration%20Episode%20%232
Bonus potato haha:
Great job bro. Thank you for this data
you're welcome... i just thought of playing around with the APIs which were made available by Steem and see how i could use it for the benefit of teammalaysia
Cheers
Instead of for loop, try map, it is cleaner
Try this:
ah yea, that certainly looks cleaner :)
i'm so used to traditional loops... cuz thats all i had when programming using COBOL (my previous job)... hahaha
Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase
https://
3. Type
re
Get Featured Instantly & Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness
You've been upvoted by TeamMalaysia Community :-
To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach
Vote TeamMalaysia witness bitrocker2020 using this link vote for witness
This post has received a 5.61 % upvote from @boomerang.