Making your own currency tracker with Jsoup!

in #utopian-io7 years ago (edited)

What Will I Learn?

Greetings the aim of this tutorial is to make you familiar with the Jsoup library of Java. By using this library and integrating it to your java class you will be able to gather web site or server data’s and be able to identify, list it according to your needs. For example, in the previous tutorial we gather the emergency numbers of several different countries and list them according to the users search. To start using Jsoup you must first download it’s core library in jar format and put it in to your project. Then generate a class and call its library by using import function. To simulate and see the output we will use Eclipse’s java ide but in your design you may use any ide or even compile it online by referencing the Jsoup library.

Requirements

  • IDE is required to test the code (preferably Eclipse IDE for java developers)
  • Basic knowledge on Java.
  • Basic knowledge about Jsoup library.

Difficulty

This tutorial is prepared for indivuduals who have a prior knowledge about Java classes, libraries and programming languages,

  • Intermediate

Tutorial Contents

In this tutorial we will pull our data's from several different currency sites (blocktrades, coinmarketcap and koinim.com) and process it according to our needs. There are quite a lot of methods and ways to index a webpage in java but the fastest and accurate one is to use api of the desired page if its possible. Firstly we should go to the page that we want to get datas. Then we should find the div class that we want to pull and after processing the data we will be able to get the below outputs,


1.png

Of course once you learn data processing with Jsoup you will be able to change the display resources or the topic of the project. In this project i decided to pull data from blocktades, koinim and coinmarketcap but in yours you can pick any web-site. Now we shall begin by importing the libraries that we want to use in this project.

The first librarty that we need to locate is the java.io.IOException which is capable of showing/displaying detailed errors when user enters an unexpected input. Briefly it is used to optimize input/output (i/o) relationship,

import java.io.IOException;

Now we can add the Jsoup libray having functions that are enabling us to get the datas on a webpage

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


Now we should add one last library that will help us to get the user entered values,

import java.util.Scanner

Then we can declare our class

public class

The name of the class can be picked by you. Its just need to be same with name located on workbench.And we need to define our method by saying public static void, we mean that the code is visible,no return value and a class type.

public static void main(String[] args) throws IOException

Then we can continue calling the connect or parse function from Jsoup library. To do that Jsoup.connect'Yourwebsite') command will be enough

Jsoup.connect(yourwebsite).get();

In this example, we should first get the account information/wallet of the uesr. To do we can use steemit's homepage. To get a users wallet we should just type 'https://steemit.com/@user/transfers' to steemit. Thereby we first need to ask the user to type his/her username and then let the Jsoup connect to the site get the wallet information. To perform this task below code was written.

System.out.println("What is your account name?");
    String account=keyboard.next();
    String accountall =("https://steemit.com/@" +account+ "/transfers");

Now this code will ask the user to enter his account name and then get the wallet information from steemit in pure html format. In your work you can pick another website and let your user index it like finding a user's favourite movies in IMDB. Now we can proceed of getting the steem dollars of the user. Firslty we need to check to class of this variable. To do that in Chrome you can simply tracke the element and get its div tag.

1.png

As you can see it's div class is named as 'div.column.small-12.medium-4'. The sbd of steemit users are displayed under this div tag. Now since we have the entire document in html format we just need to index the desired div part. To do that doc.select function of Jsoup will be enough,

Elements initialtable = doc.select("div.column.small-12.medium-4");

Now we have the selected the desired div class. But its in plain text format we need to conver this string into int or double in order to do some calculations, conversions. Below code first removes the tags, secondly finds the location of SBD column and get its value finally displays it by converting the string into double,

 for (Element row : initialtable ) 
            {
            full = row.text();
            //System.out.println(full);
            int m = full.indexOf('S');
            int d = full.indexOf('$');
          
            if (m + 6 == d  && d > 1)
            {
            //System.out.println("Saving: " + full);
            }
            else if ( m > 0)
            {
            System.out.println("Your steem power: " +full);
      
            }
            else if (d >= 0 && i < 2)
            {
                i = 3;
                System.out.println("Your steem dolar: " +full+ "\n");
                int length = full.length();
                String sbd = full.substring(1,length);
                Double y = Double.parseDouble(sbd);
                double y1 = y * coincapfinalsbd;
                double y2 = y * coincapfinalbtc;
                double coincapltc = y * blocktradescoincapltc;
                double coincappeth = y * coincapethdouble;
                double trycoincap = y * coincapetrydouble;
        }
}

Briefly with this code we first get the user entered steemit accounts wallet, pull the account information table, remove the tags and unwanted spaces, find the column showing the SBD , get the value on that column and convert it to double. Now the string 'full' is the entire line that has SBD information. (In the below example full is 'Your steem dolar: $17.876' ) then string sbd variable is the double value that has the sbd value (in this case its 17.876) and finally double y is the double transfered verion of sbd. We need to convert this string value to double to make calculations. Now we have the sbd value of the user entered account. We can procced on adding another site able to convert this sbd to btc,ltc or other coins. To do that same procedure for the steemit page is performed, Jsopup called, tag is located by using coincapmarkets api and then the output is converted to double and displayed on console.

String coincapsbd = ("https://coinmarketcap.com/currencies/steem-dollars/");
    Document docsbd =  Jsoup.connect(coincapsbd).get();
    Elements item = docsbd.select("div.col-xs-6.col-sm-8.col-md-4.text-left");
    Double coincapfinalsbd = 0.0;
    Double coincapfinalbtc = 0.0;
    String dr = " ";
    
    String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
    Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
    String a13 = docblocktradescoincapeth13.toString();
    int a23 = a13.indexOf("price_usd") + 13; 
    int a33 = a13.indexOf("price_btc") - 4;
    String a43 = a13.substring(a23,a33);
    Double a53 = Double.parseDouble(a43);

Now we have the conversion of sbd,btc,ltc and eth to usd. This information will be usefull to compare coincapmarket with other sites. In your design you can pick any currency site and pull their data with Jsoup by applying same connect, get , parse and parseDouble commands. Then by applying simple mathmeatics we ware now able to display usd and try representaion of our sbd's accourding to the coincapmarket's datas. Now we can also get the conversion between sbd and other coins to compare it later with blocktrades.us.


To get the conversion rates of blocktrades we again get its api, search for the coin type we want and display it by converting string to double. Below is the sample conversion from sbd to eth in blocktrades. By doing this method you can get any coin information supplied on the website,

String urleth = "https://blocktrades.us/api/v2/estimate-output-amount?inputAmount=1&inputCoinType=sbd%20&outputCoinType=eth";
    Document docblocktradeseth = Jsoup.parse(new URL(urleth).openStream(), "UTF-8", "", Parser.xmlParser());
    String dblocktradeseth = docblocktradeseth.toString();
    int begineth  = dblocktradeseth.indexOf("outputAmount") + 15; 
    int endeth= dblocktradeseth.indexOf("outputCoinType") - 3;
    dblocktradeseth = dblocktradeseth.substring(begineth,endeth);
    Double blocktradeseth = Double.parseDouble(dblocktradeseth);

Then the same procedure is repeated for btc and ltc. Then to get the commision rate of blocktrades we compared its rates with coincapmarkets. As you can see for 101 usd worth (or 00938293364 btc) sbd blocktrades.us takes around 69 usd (or arround .00938293364 - .0303713240 = 0.0063458 btc) which is quite much more than the half the sbd. For ltc it only takes 20 usd for commision and 15 usd for etherium. Or in other words currently for 1 sbd blocktrades.us takes 3.863107677 dolars btc conversion comission , 1.14230294778 dollars for ltc and 0.84416408065 usd for eth. For sure these prices change rapidly thereby its highly recommended that you have a notification system for instant peeks. In the following tutorails we will add a mail notofication function that will notify us whenever our sbd's is at the desired usd rate.

Same procedure also repeated for koinim.com another exchange site capable of changing btc and ltc to Try (turkish liras) and as a result if we want to exchange 17.876 steem dollar we will have 122 TRY if we use to bitcoin, 301 TRY if we decide to use litecoin.

The overall code is shown below, feel free to add your own favourite currency sites and obtain their instant rates,

import java.io.IOException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import java.util.Scanner;


public class JsoupRun {
public static void main(String[] args) throws IOException  {
    //steemit
    Scanner keyboard =new Scanner (System.in);
    System.out.println("What is your account name?");
    String account=keyboard.next();
    String accountall =("https://steemit.com/@" +account+ "/transfers");
    Document doc = Jsoup.connect(accountall).get();
    Elements initialtable = doc.select("div.column.small-12.medium-4");
    initialtable.remove(0);
    String full = " ";
    int i = 1;
    System.out.println(" ");
    
    //coinmarketcap 
    String coincapsbd = ("https://coinmarketcap.com/currencies/steem-dollars/");
    Document docsbd =  Jsoup.connect(coincapsbd).get();
    Elements item = docsbd.select("div.col-xs-6.col-sm-8.col-md-4.text-left");
    Double coincapfinalsbd = 0.0;
    Double coincapfinalbtc = 0.0;
    String dr = " ";
    
    String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
    Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
    String a13 = docblocktradescoincapeth13.toString();
    int a23 = a13.indexOf("price_usd") + 13; 
    int a33 = a13.indexOf("price_btc") - 4;
    String a43 = a13.substring(a23,a33);
    Double a53 = Double.parseDouble(a43);
    
    
    //coincapmarketcap sbd - try
    String coincaptry = "https://api.coinmarketcap.com/v1/ticker/steem-dollars/?convert=TRY";
    Document coincapethdoc = Jsoup.parse(new URL(coincaptry).openStream(), "UTF-8", "", Parser.xmlParser());
    String dbcoincaptry = coincapethdoc.toString();
    int begincoincaptry  = dbcoincaptry.indexOf("price_try") + 13; 
    int endcoincaptry= dbcoincaptry.indexOf("24h_volume_try") - 4;
    dbcoincaptry = dbcoincaptry.substring(begincoincaptry,endcoincaptry);
    Double coincapetrydouble = Double.parseDouble(dbcoincaptry);
    
    String dbcoincaptry2 = coincapethdoc.toString();
    int beginsbdusd  = dbcoincaptry2.indexOf("price_usd") + 13; 
    int endsbdusd= dbcoincaptry2.indexOf("price_btc") - 4;
    String dbcoincaptry3 = dbcoincaptry2.substring(beginsbdusd,endsbdusd);
    Double coincapetrydouble2 = Double.parseDouble(dbcoincaptry3);
    
    //coinmarketcap ltc - try
    String urlcoincapeth = "https://api.coinmarketcap.com/v1/ticker/steem-dollars/?convert=LTC";
    Document docblocktradescoincapeth = Jsoup.parse(new URL(urlcoincapeth).openStream(), "UTF-8", "", Parser.xmlParser());
    String dblocktradecoincapseth = docblocktradescoincapeth.toString();
    int begincoincapeth  = dblocktradecoincapseth.indexOf("price_ltc") + 13; 
    int endcoincapeth= dblocktradecoincapseth.indexOf("24h_volume_ltc") - 4;
    dblocktradecoincapseth = dblocktradecoincapseth.substring(begincoincapeth,endcoincapeth);
    Double blocktradescoincapltc = Double.parseDouble(dblocktradecoincapseth);
    
    String urlcoincapeth1 = "https://api.coinmarketcap.com/v1/ticker/litecoin/";
    Document docblocktradescoincapeth1 = Jsoup.parse(new URL(urlcoincapeth1).openStream(), "UTF-8", "", Parser.xmlParser());
    String a1 = docblocktradescoincapeth1.toString();
    int a2 = a1.indexOf("price_usd") + 13; 
    int a3 = a1.indexOf("price_btc") - 4;
    String a4 = a1.substring(a2,a3);
    Double a5 = Double.parseDouble(a4);
    
    //coinmarketcap eth - try
    String coincapeth = "https://api.coinmarketcap.com/v1/ticker/steem-dollars/?convert=ETH";
    Document coincapethdoc2 = Jsoup.parse(new URL(coincapeth).openStream(), "UTF-8", "", Parser.xmlParser());
    String dbcoincapeth = coincapethdoc2.toString();
    int begincoincapeth2  = dbcoincapeth.indexOf("price_eth") + 13; 
    int endcoincapeth2= dbcoincapeth.indexOf("24h_volume_eth") - 4;
    dbcoincapeth = dbcoincapeth.substring(begincoincapeth2,endcoincapeth2);
    Double coincapethdouble = Double.parseDouble(dbcoincapeth);
    
    String urlcoincapeth12 = "https://api.coinmarketcap.com/v1/ticker/ethereum/";
    Document docblocktradescoincapeth12 = Jsoup.parse(new URL(urlcoincapeth12).openStream(), "UTF-8", "", Parser.xmlParser());
    String a12 = docblocktradescoincapeth12.toString();
    int a22 = a12.indexOf("price_usd") + 13; 
    int a32 = a12.indexOf("price_btc") - 4;
    String a42 = a12.substring(a22,a32);
    Double a52 = Double.parseDouble(a42);
    
    //blocktrades-bitcoin
    String urlbtc = "https://blocktrades.us/api/v2/estimate-output-amount?inputAmount=1&inputCoinType=sbd&outputCoinType=btc";
    Document docblocktrades = Jsoup.parse(new URL(urlbtc).openStream(), "UTF-8", "", Parser.xmlParser());
    String dblocktrades = docblocktrades.toString();
    dblocktrades = dblocktrades.replaceAll("\\(.*\\)", "");
    int begin  = dblocktrades.indexOf("outputAmount") + 15; 
    int end = dblocktrades.indexOf("outputCoinType") - 3;
    String dblocktrades1 = dblocktrades.substring(begin,end);
    Double blocktradesbtc = Double.parseDouble(dblocktrades1);
    
    //blocktrades-litecoin
    String urlltc = "https://blocktrades.us/api/v2/estimate-output-amount?inputAmount=1&inputCoinType=sbd%20&outputCoinType=ltc";
    Document docblocktradesltc = Jsoup.parse(new URL(urlltc).openStream(), "UTF-8", "", Parser.xmlParser());
    String dblocktradesltc = docblocktradesltc.toString();
    int beginltc  = dblocktradesltc.indexOf("outputAmount") + 15; 
    int endltc= dblocktradesltc.indexOf("outputCoinType") - 3;
    String dblocktrades1ltc = dblocktradesltc.substring(beginltc,endltc);
    Double blocktradesltc = Double.parseDouble(dblocktrades1ltc);
    
    //blocktrades-etherium
    String urleth = "https://blocktrades.us/api/v2/estimate-output-amount?inputAmount=1&inputCoinType=sbd%20&outputCoinType=eth";
    Document docblocktradeseth = Jsoup.parse(new URL(urleth).openStream(), "UTF-8", "", Parser.xmlParser());
    String dblocktradeseth = docblocktradeseth.toString();
    int begineth  = dblocktradeseth.indexOf("outputAmount") + 15; 
    int endeth= dblocktradeseth.indexOf("outputCoinType") - 3;
    dblocktradeseth = dblocktradeseth.substring(begineth,endeth);
    Double blocktradeseth = Double.parseDouble(dblocktradeseth);
    
    //koinim.com - btc
    
    String k = "https://koinim.com/ticker/";
    Document k1 = Jsoup.parse(new URL(k).openStream(), "UTF-8", "", Parser.xmlParser());
    String k2 = k1.toString();
    int k3  = k2.indexOf("sell") + 7; 
    int k4= k2.indexOf("high") - 4;
    String k5 = k2.substring(k3,k4);
    Double k6 = Double.parseDouble(k5);
    //System.out.println(k6);
    
    //koinim.com - ltc
    
    String k9 = "https://koinim.com/ticker/ltc/";
    Document k19 = Jsoup.parse(new URL(k9).openStream(), "UTF-8", "", Parser.xmlParser());
    String k29 = k19.toString();
    int k39  = k29.indexOf("sell") + 7; 
    int k49= k29.indexOf("high") - 4;
    String k59 = k29.substring(k39,k49);
    Double k69 = Double.parseDouble(k59);
    //System.out.println(k69);
        
        for (Element d : item) {
            dr = d.text();
            int parant = dr.indexOf(")") + 2;
            int btccoincap = dr.indexOf("B") - 1;
            String coincapbtc = dr.substring(parant,btccoincap);
            coincapfinalbtc = Double.parseDouble(coincapbtc);
            int space = dr.indexOf(' ');
            String coincapdolar = dr.substring(0,space);
            coincapfinalsbd = Double.parseDouble(coincapdolar);
        }
        for (Element row : initialtable ) 
            {
            full = row.text();
            //System.out.println(full);
            int m = full.indexOf('S');
            int d = full.indexOf('$');
          
            if (m + 6 == d  && d > 1)
            {
            //System.out.println("Saving: " + full);
            }
            else if ( m > 0)
            {
            System.out.println("Your steem power: " +full);
      
            }
            else if (d >= 0 && i < 2)
            {
                i = 3;
                System.out.println("Your steem dolar: " +full+ "\n");
                int length = full.length();
                String sbd = full.substring(1,length);
                Double y = Double.parseDouble(sbd);
                double y1 = y * coincapfinalsbd;
                double y2 = y * coincapfinalbtc;
                double coincapltc = y * blocktradescoincapltc;
                double coincappeth = y * coincapethdouble;
                double trycoincap = y * coincapetrydouble;
                
                
                System.out.println("Coinmarketcap \n-----------------");
                // coincapmarket 1 btc,eth,ltc
                
                System.out.println("1 sbd in usd: " +coincapetrydouble2+ " USD.");
                System.out.println("1 btc in usd: " +a53+ " USD.");
                System.out.println("1 ltc in usd: " +a5+ " USD.");
                System.out.println("1 etc in usd: " +a52+ " USD. \n");

                
                // coincapmarket sbd 
                System.out.println("Your sbd money in usd: " +y1+ " USD.");
                System.out.println("Your sbd money in try: " +trycoincap+ " TRY. \n");
                
                System.out.println("Your sbd money in btc: " +y2+ " BTC.");
                System.out.println("Your sbd money in ltc: " +coincapltc+ " LTC.");
                System.out.println("Your sbd money in eth: " +coincappeth+ " ETH. \n");
                
                
                //blocktrades btc
                double y3 = y * blocktradesbtc;
                double y4 = ((y2-y3)*a53);
                System.out.println("Blocktrades.us \n-----------------");
                System.out.println("Your sbd money in btc: " +y3+ " BTC.");
                
                //blocktrades ltc
                double y5 = y * blocktradesltc;
                double y6 = (coincapltc- y5) * a5;
                System.out.println("Your sbd money in ltc: " +y5+ " LTC.");
                
                //blocktrades ether
                double y7 = y * blocktradeseth;
                double d8 = (coincappeth-y7) * a52;
                System.out.println("Your sbd money in eth: " +y7+ " ETH. \n");
                
                //Koinim.com
                double k7 = k6 * y3;
                double k8 = y5* k69 ; 
                System.out.println("Koinim.com \n-----------------");
                System.out.println("Your btc money in TRY: " +k7+ " TRY.");
                System.out.println("Your ltc money in TRY: " +k8+ " TRY. \n");
                
                //Overall
                System.out.println ("Comparision \n------------------");
                System.out.println("BTC Comission of Blocktrades.us: " +y4+ " USD.");
                System.out.println("LTC Comission of Blocktrades.us: " +y6+ " USD.");
                System.out.println("ETC Comission of Blocktrades.us: " +d8+ " USD.");
            }
            
            
            
            
        }    
      
    } 
}

And the output for differnt users,

1.png


1.png


1.png

In our next tutorail we will add more functions to the output , add a notification system and compare multiple currencies.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @rufans, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @wodsuz I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x