Making A Steem Browser With SteemJ

in #steem8 years ago

title_steem_browser.png

I love messing around with Java and programming in general and I thought I'd give the SteemJ API a go. The first thing I had to do was import the API into my project and it was really simple since I use Gradle. All I had to do was add compile 'eu.bittrade.libs:steemj-core:0.4.3' into my Gradle build file then refresh my dependencies.

Now I had all the dependencies I needed it was time to play. I first initialized the SteemJ API with the following code

        myConfig.setResponseTimeout(parent.preferences.getTimeout());
        myConfig.setDefaultAccount(new AccountName(parent.preferences.getDefaultUser()));
        
        if(parent.preferences.getPostKey().length() > 20){
            //Add and manage private keys:
            List<ImmutablePair<PrivateKeyType, String>> privateKeys = new ArrayList<ImmutablePair<PrivateKeyType, String>>();
            privateKeys.add(new ImmutablePair<PrivateKeyType, String>(PrivateKeyType.POSTING, parent.preferences.getPostKey()));
            privateKeys.add(new ImmutablePair<PrivateKeyType, String>(PrivateKeyType.ACTIVE, parent.preferences.getActiveKey()));
            // ... add more keys if needed.
        
            myConfig.getPrivateKeyStorage().addAccount(myConfig.getDefaultAccount(), privateKeys);
        }
        
        try {
            steemJ = new SteemJ();
        } catch (SteemCommunicationException e) {
            e.printStackTrace();
        } catch (SteemResponseException e) {
            e.printStackTrace();
        }

SteemJ is now initialized and I can use it to explore the Steem blockchain. 😁

I wanted to see some information about myself in Steem so I used this to pull my information
List<String> accounts = steemJ.lookupAccounts("cryptoissweet", 1);

And.. it got me my name. Apparently, all this does is get your name and ones similar to it by increasing the number. So I tried it with 5 and I got these results:

cryptoissweet
cryptoist
cryptoit
cryptoitalia
cryptoitalians

Onto something a bit more interesting. Getting my follower counts.

        String foli = "";
        String fole = "";
        FollowCountApiObject followData = steemJ.getFollowCount(currentUser);
        foli = ""+followData.getFollowingCount();
        fole = ""+followData.getFollowerCount();

As expected this got me my follower and following counts which I later use in the browser. This was a bit more interesting but I think I could do better. This time I wanted to see more information about what I have done, who I voted for, who commented or even some of my posts and SteemJ did not dissapoint.

Map<Integer, AppliedOperation> accountHistory = steemJ.getAccountHistory(currentUser, Integer.MAX_VALUE, 20);
            
            for(Map.Entry<Integer, AppliedOperation> op : accountHistory.entrySet() ){
                Gdx.app.log("info",op.getKey()+":"+op.getValue().getOp());
                
                if(op.getValue().getOp() instanceof CommentOperation){
                    CommentOperation comment = (CommentOperation) op.getValue().getOp();
                    
                    Label lb = new Label(comment.getBody(),skin);
                    content.add(lb).align(Align.left);
                    content.row();
                }
                
                if(op.getValue().getOp() instanceof VoteOperation){
                    VoteOperation vote = (VoteOperation) op.getValue().getOp();
                    if(vote.getVoter() == currentUser){
                        String msg = "You Voted For "+vote.getAuthor().getName()+"'s post "+vote.getPermlink().getLink();
                        Label lb = new Label(msg,skin);
                        content.add(lb).align(Align.left);
                        content.row();
                    }else{
                        String msg = vote.getAuthor().getName()+" voted for your post "+vote.getPermlink().getLink();
                        Label lb = new Label(msg,skin);
                        content.add(lb).align(Align.left);
                        content.row();
                    }
                }
            }

This code allowed me to view all my historical data. I have used it here to get all my comments and all the votes from me or to my content. The raw data comes out similar to this:

331:eu.bittrade.libs.steemj.base.models.operations.VoteOperation@61a5c717[voter=eu.bittrade.libs.steemj.base.models.AccountName@5ef00d8c[name=paasuv],author=eu.bittrade.libs.steemj.base.models.AccountName@8bef1de[name=cryptoissweet],permlink=eu.bittrade.libs.steemj.base.models.Permlink@4fa0e49b[link=new-coins-on-the-block-6th-february-2018],weight=10000,virtual=false]

332:eu.bittrade.libs.steemj.base.models.operations.VoteOperation@49ea5c42[voter=eu.bittrade.libs.steemj.base.models.AccountName@17a8b49e[name=mrmayo],author=eu.bittrade.libs.steemj.base.models.AccountName@68e787b4[name=cryptoissweet],permlink=eu.bittrade.libs.steemj.base.models.Permlink@20ab320f[link=new-coins-on-the-block-6th-february-2018],weight=10000,virtual=false]

I now have enough information to start playing around with it and putting it into a human-readable format. Which is what I have done here:

SteemitBrowser.png

Wel,l that's all I have for now. I will be continuing to work on this in my spare time. I know it's not pretty to look at but I only spent a few hours on this today and It was fun for me.

If you want to know more about SteemJ check out the developer here on Steemit @dez1337

Coin Marketplace

STEEM 0.09
TRX 0.32
JST 0.033
BTC 111017.83
ETH 3958.15
USDT 1.00
SBD 0.63