Yet another piece of code (People are not interested in Energy consumption)

So far I haven’t found a good resource from which retrieve data in real-time about power consumption in the world. People are more interested on football real-time statistics and other, to me, irrelevant data.

Something interested but probably not very original is stock market. Yahoo finance service has a query service that responds with a flat text file with the information about the specific stock market that you want to retrieve.

I have to check out the fluctuation of this data, and see if they move during the day and if these data moves a lot. But in the meantime I have had time to play a bit with Processing and I did try to read this little string of code that came out from a Yahoo query. As always I found everything that I needed on the Internet so I will publish everything here with the relative sources.

First of all, the php script that send a query to Yahoo and prints out the result. I will leave the copyright untoched and write down the link of the original file: Link; You can find an example of the output by going at this address: stock.php;

/*

Copyright (c) 2000, Jason Costomiris
All rights reserved.

Don't be scared, it's just a BSD-ish license.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Jason Costomiris.
4. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

Class quote {
function get($symbol){
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv", $symbol);
$fp = fopen($url, "r");
if(!$fp){
echo "Error: Cannot connect";
} else {
$array = fgetcsv($fp, 4096, ',');
fclose($fp);
$this->symbol = $array[0];
$this->last = $array[1];
$this->date = $array[2];
$this->time = $array[3];
$this->change = $array[4];
$this->open = $array[5];
$this->high = $array[6];
$this->low = $array[7];
$this->volume = $array[8];
}
}
}
?>

$q = new quote;
$q->get(”RHAT”);
printf(”%s: %s(%s)”, $q->symbol, $q->last, $q->change, $q->open, $q->high, $q->low, $q->volume);
?>

and now the processing code to read the data output from stock.php

/**
* HTTP Client
* by Tom Igoe.
*
* Starts a network client that connects to a server on port 80,
* sends an HTTP 1.1 GET request, and prints the results.
*
* created March 18, 2005
*/

import processing.net.*;

Client client;

void setup()
{
size(200, 200);
noStroke();
// Open a TCP socket to the host:
client = new Client(this, “everybody.is-a-cyb.org”, 80);

// Print the IP address of the host:
//println(client.ip());

// Send the HTTP GET request:
client.write(”GET /stock.php HTTP/1.1\n”);
client.write(”HOST: everybody.is-a-cyb.org\n\n”);
}

void draw()
{
background(0);
// Print the results of the GET:
if (client.available() > 0) {
int inByte = client.read();
print((char)inByte);
}
}

You can actually try it without making the php file on your server. Processing will query the server of http://everybody.is-a-cyb.org and return the required results.

No Comments

No comments yet.

Comments RSS TrackBack Identifier URI

Sorry, the comment form is closed at this time.