(P5.js) How To Get Data From An Url And Put It Into A Variable?

edited April 2016 in p5.js

Dear Friends,

I need to get data from this url "http://webrates.truefx.com/rates/connect.html?f=html" and put it into a variable....or better i need to get data of the money pairs from each moneypair... For example i need to retrieve the numbers the second piece and third piece of numbers, like aud/usd or the other money pair...to work with that.... I thank you a lot in advance..

Lestroso :\">

Tagged:

Answers

  • Seems like that URL also accepts ?f=csv besides ?f=html. Maybe other types as well.
    It means you can use loadTable() or loadStrings() + split() + trim() in order to retrieve those "rates":

    1. https://Processing.org/reference/loadTable_.html
    2. https://Processing.org/reference/loadStrings_.html
    3. https://Processing.org/reference/split_.html
    4. https://Processing.org/reference/trim_.html
  • Dear GoToLoop....I thank you so Much!!!!! now i will look yours links....but....can you show me an example...by you if you can??? thank a lot again...best regards, lestroso

  • edited April 2016
    // forum.Processing.org/two/discussion/16175/
    // how-to-get-data-from-an-url-and-put-it-into-a-variable
    
    // GoToLoop (2016-Apr-21)
    
    static final String URL =
      "http://" + "WebRates.TrueFX.com" + "/rates/connect.html" + "?f=csv";
    
    Table t;
    
    void setup() {
      t = loadTable(URL, "csv");
      final int cols = t.getColumnCount();
    
      for (final TableRow r : t.rows()) {
        for (int i = 0; i < cols; ++i)  print(r.getString(i), TAB);
        println();
      }
    
      exit();
    }
    
  • edited April 2016
    String lines[] = loadStrings("http:" + "//webrates.truefx.com/rates/connect.html?f=html","header");
    println("there are " + lines.length + " lines");
    for (int i = 0 ; i < lines.length; i++) {
      println(lines[i]);
    }
    

    I have tryed this... but nothing works here....can some body help me please??? i would like to work with P5 code...and not Processing....thanks lestroso :\">

  • Dear Gotoloop..... i'm sorry but i tryed your software.... but don't work either in processing3, either in P5....but have i miss something??? thanks again Lestroso :\">

  • can somebody help me with an example working for P5 please??? Thanks a lot in advance....Lestroso X_X

  • Answer ✓

    duplicate deleted.

    please don't start duplicate threads.

  • I'm sorry about that..... but can somebody help me?? the last example don't work for me....I need to retrieve data with a little script...because i'm new and i don't know how to start with p5....

  • Answer ✓

    the p5.js loadStrings() example looks pretty clear

    https://p5js.org/reference/#/p5/loadStrings

    but i can't get that to work with the url you supply, which might be a cross-site scripting problem.

  • Thanks a lot for your help... i'll try to understand that instruction....thanks again lestroso >:D<

  • edited April 2016

    var result; function preload() { result = loadStrings(' http://WebRates.TrueFX.com/rates/connect.html?f=html '); }

    function setup() { background(200); var ind = floor(random(result.length)); text(result[ind], 10, 10, 80, 80); }

    I have tryed this demo in P5....but nothing works here....but if you goto the this web address go fine... you see the data change....

    But how i can work with it??? i thank you in advance...Lestroso

  • there are rules about websites reading data from other websites. they may block you based on user-agent string or it could be your browser disallowing 3rd party access.

    (the processing example, not the p5.js example, works fine fwiw)

  • many thanks koogs....i must find another site data forex feeds....to work with....lestroso :\">

  • @fasasoftware: predictably this is a cross domain issue. It doesn't look like the WebRates site has explicitly blocked script access to their data; just that they haven't made it straightforward by enabling CORS. You should double-check their terms and conditions before using this; but in principle you can use a service like crossorigin.me to get around the issue:

    var result; 
    function preload() { 
        result = loadStrings('https://crossorigin.me/http://WebRates.TrueFX.com/rates/connect.html?f=csv ');
    }
    
    function setup() {
        background(200); 
        console.info(result);
    }
    

    I'm amused by the size of the small print on their about us page :D

  • Dear blindfish....thank you thery much for your answer...but i tryed your code...but don't work either in P5, either in processing3....i think to move to coding with visual studio 2015 community....best regards...lestroso :D

  • edited April 2016

    i'm sorry but i tryed your software.... but don't work either in processing3, ...

    I'm pretty sure I've run my sketch many times before publishing it here for ya! [-(
    Moreover, it doesn't seem you had been paying much attention at all.

    Just 1 example to illustrate it: I've tipped ya about using ?f=csv in place of ?f=html.
    However, you've kept relying on ?f=html in your next examples! 3:-O

    Regardless, I've decided to convert the "Java Mode" version to "p5.js".
    And to further shield me & prove that it indeed works, I've hosted it here too: :(|)
    http://p5ide.HerokuApp.com/editor#?sketch=571b73c18d44c60300fd6496

    <script src=http://p5js.org/js/p5.min.js async></script>
    
    <script>
    
    // forum.Processing.org/two/discussion/16175/
    // how-to-get-data-from-an-url-and-put-it-into-a-variable
    
    // p5ide.HerokuApp.com/editor#?sketch=571b73c18d44c60300fd6496
    
    // GoToLoop (2016-Apr-22)
    
    "use strict";
    
    const HTTP = "http://",
          CORS = HTTP + 'CrossOrigin.me/',
          SITE = HTTP + 'WebRates.TrueFX.com/' + 'rates/connect.html' + '?f=csv';
    
    let t;
    
    function preload() {
      t = loadTable(CORS + SITE);
    }
    
    function setup() {
      const cols = t.getColumnCount();
      let s = '';
    
      for (const r of t.getRows()) {
        for (let i = 0; i < cols; ++i)  s += r.get(i) + '\t ';
        s += '\n';
      }
    
      print(s);
    }
    
    </script>
    
  • I saw also visual studio is too complicated....i should learn P5 ....is better...but i need to know that code for a work of mine....very important for me...can somebody help with a code working?? thanks...lestroso

  • edited April 2016

    Console output (hit F12 key):

    EUR/USD  1461357878071   1.12    291     1.12    302     1.12179     1.13091     1.12867     
    USD/JPY  1461357900084   111.    710     111.    719     109.260     111.812     109.427     
    GBP/USD  1461357900050   1.44    223     1.44    237     1.43134     1.44525     1.43195     
    EUR/GBP  1461357900050   0.77    859     0.77    867     0.77827     0.78879     0.78819     
    USD/CHF  1461357846936   0.97    813     0.97    834     0.97321     0.97976     0.97517     
    EUR/JPY  1461357900084   125.    442     125.    466     123.403     125.505     123.507     
    EUR/CHF  1461357891924   1.09    861     1.09    871     1.09773     1.10164     1.10066     
    USD/CAD  1461357900024   1.26    665     1.26    690     1.26219     1.27588     1.27326     
    AUD/USD  1461357890768   0.77    112     0.77    120     0.76937     0.77748     0.77367     
    GBP/JPY  1461357900154   161.    112     161.    145     156.515     161.196     156.704     
         undefined   undefined   undefined   undefined   undefined   undefined   undefined   undefined   
    
  • Dear blindfish....thank you thery much for your answer...but i tryed your code...but don't work either in P5, either in processing3....i think to move to coding with visual studio 2015 community....best regards...lestroso

    @fasasoftware: Odd as I obviously tested the code before posting it and it worked just fine (in p5.js). You either don't understand what it does; or perhaps tried to run it off the local file system.

    From your responses you clearly haven't taken the time to learn the basics of coding in p5js. If you want some code written; but don't want to take the time to learn can I suggest you post a request in events/opportunities and offer suitable remuneration...

Sign In or Register to comment.