Brighttux's chronicles of playing with and learning steem.js API #2

in #teammalaysia6 years ago

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

I'm testing out the steem.js APIs, today i'm trying to pull all the discussions in created today with the #teammalaysia tag and see how well the post are doing.


Before we get started, here's photo i took:


SONY DSC-RX100M2, f/4, 1/160sec, 160 ISO


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:

*after 30 minutes of messing around, here's what i got*

        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 #teammalaysia
  • truncate_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 about callback 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 have n numbers -> population of humans.
    In this example, let me use the number 123 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:

AuthorDateTimeTitlePost ValueNumber of Votes
@kokuryo2018-04-1909:05:00李王张刘好声音 - 信乐团《One Night in 北京》0.008 SBD1
@orangila2018-04-1908:48:42Orangila's Belief and Lifestyle As A Minimalist - Part 2 of 20.098 SBD4
@alimamasstory2018-04-1908:48:035-minute Freewrite: Prompt- Coin0.027 SBD2
@amalinaahmd2018-04-1908:44:33Speed drawing of Lonely Girl0.000 SBD0
@coloringiship2018-04-1908:34:45My Sketchbook - Sketches Inspired By Obscure Sorrows2.918 SBD32
@debbie892018-04-1908:33:15⛪CHILDREN'S SUNDAY SCHOOL⛪0.023 SBD1
@fundurian2018-04-1908:29:03Dubuyo Korean spicy beef noodle, Dubuyo韩国牛肉辣面4.956 SBD73
@legrandkei2018-04-1908:12:33I'm starting a new start-up, to show myself that I can do it.0.002 SBD1
@kadishakho2018-04-1908:10:15一则重要的分享!0.000 SBD0
@nazirullsafry2018-04-1907:43:57Basheer Graphic Books new store location0.377 SBD24
@bboyady2018-04-1907:00:54When Negativity over Positivity//当负能量超越正能量0.249 SBD4
@angeljames2018-04-1906:45:24Musicoin (Musicians get paid without intermediaries)2.932 SBD63
@itrmarcusliew2018-04-1906:41:57makemesmile 小农夫2.680 SBD68
@snakesolidus2018-04-1906:26:21My Name Is Bond, James Bond : Skyfall0.000 SBD2
@tvlee2018-04-1906:10:18Harmony 和谐0.054 SBD7
@mrnoob2018-04-1906:08:36Mr. Noob plays Dark Souls Prepare to Die Edition EP082.697 SBD62
@zaraa152018-04-1905:52:39Horror Story : Empty Room..!2.714 SBD64
@yasminep2018-04-1905:46:48And the winner is... Contest Week 3 Winner!3.473 SBD8
@kadishakho2018-04-1905:44:33这样说对不对,有道理吗?0.001 SBD1
@alansthoughs2018-04-1905:37:00Steemit sandwich contest week 37: fries X lasagna0.165 SBD6
@zublizainordin2018-04-1905:36:36Zubli Zainordin dan Sharil Tawaran demi SteemBahasa Bukan Tawar tapi Manis2.860 SBD7
@myach2018-04-1905:30:21TeamMalaysia's Daily Picks #9 - @veenang, @dxn & @danieldoughty20.999 SBD122
@evangelinesp2018-04-1905:19:36Phantom Of The Opera???2.823 SBD74
@abellehera2018-04-1905:07:24Orang cakap hobi saya tidak menarik2.681 SBD75
@smil32018-04-1905:00:425种会被社会慢慢淘汰的人!2.683 SBD69
@annekins2018-04-1904:56:06Maybe some days we are better wired for particular tasks…2.676 SBD70
@liewsc2018-04-1904:24:30你的旋律,我的主题 # 20 - 夜已深 - 月旦评不放心吐槽大会2.803 SBD82
@nazirullsafry2018-04-1904:21:15100th Days in Steemit3.100 SBD71
@kenclickrich2018-04-1904:21:12London Travel Blog 2017 Part 22.699 SBD69
@v1tko2018-04-1904:08:00Recipe 102 : Apam Balik(Peanut Pancake)2.988 SBD75
@wilhb812018-04-1904:06:27纽约的司机驾着北京的梦 – 张学友-李王张刘好声音Music Challenge E3.451 SBD115
@nickychu2018-04-1903:39:06LTAM : Peavey Transtube EFX Amplifier0.616 SBD10
@ryanng2018-04-1903:36:03Daily Forex #28 - Is Forex Education Really Important?4.915 SBD41
@fr3eze2018-04-1903:32:54The best way to crack Photoshop CC59.427 SBD45
@howtostartablog2018-04-1902:58:42Guess the movie #17 - Results11.870 SBD131
@digitalmind2018-04-1902:44:21PETALS CONTEST-FLOWERS - Pink Flower of Fu Gui Flower1.200 SBD10
@superoo72018-04-1902:41:00Trying out parley.io1.612 SBD35
@ryanng2018-04-1902:35:45BBQ Wings & Bak Kwa!!! - Singapore0.014 SBD6
@nostalgic12122018-04-1902:34:48【Vicky's 旅行】MySmartphonePhotography - 我完成了第一次下潜🎉4.167 SBD37
@ryanng2018-04-1902:30:33麻辣香锅 - Home Cooked Version - Singapore0.007 SBD3
@arwine2018-04-1902:16:12Tips menjaga penampilan diri agar kelihatan cantik dan menarik3.110 SBD96
@lilacse2018-04-1901:56:30Reflection on my 1 month holiday :P0.058 SBD6
@janicechua2018-04-1901:47:48李王张刘好声音 隐形的翅膀 + 有形的翅膀1.134 SBD45
@walkinharmony2018-04-1901:45:57People who prefer to be alone are more excellent2.881 SBD65
@zublizainordin2018-04-1901:44:36Zubli Zainordin Kongsi Informasi Ibarat Air2.907 SBD13
@maverickfoo2018-04-1901:25:00Mini Motivation #94 - The Worst Advice You Can Ever Get4.072 SBD63
@aurah2018-04-1901:20:42Steem Cartoon : From Colour Script to Background Animation21.177 SBD65
@chloephuan932018-04-1901:02:21Breakfast!0.165 SBD7
@spaceranger2018-04-1900:55:42Diary of Life2.657 SBD63
@joeliew2018-04-1900:30:27巴占花园里看不起眼的屋边炒粉2.916 SBD75

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:

Sort:  

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

Instead of for loop, try map, it is cleaner

for(var i = 0; i < result.length; i++) {}

Try this:

result.map(function(r) {
  var thisAuthor = r.author;
} )

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.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63799.64
ETH 3130.40
USDT 1.00
SBD 3.97