This object is in archive! 

Get prices from Norwegian ST1 petrol stations

Marius shared this idea 8 years ago
Under Consideration

I have written some rather rude PHP code to harvest the latest prices from the nearest ST1 petrol station in Norway. I'm not sure what I need it for yet :) but maybe some day Zipato can send me push messages (or Sonos speach) like "Your Aston Martin DB9 (I wish) has less than 30% petrol left. The price is rather good today at 10.50 kr/liter"


As I said, the code is rude, but it works. Here it is in case anybody is interested


  1. <?php
  2. define(OPPEGARD_STATION_ID, 46125, false); //You need to find the ID of your closest station. Look it up in the URL_STATIONS output
  3. define(TEMP_FILENAME, "/var/services/web/last_st1_price.txt", false);
  4. define(URL_B95_METER, "https://my.zipato.com/zipato-web/remoting/attribute/set?serial=REMOVED&apiKey=REMOVED&ep=REMOVED&value1=", false); //URL of Zipato meter
  5. define(URL_DIESEL_METER, "https://my.zipato.com/zipato-web/remoting/attribute/set?serial=REMOVED&apiKey=REMOVED&ep=REMOVED&value1=", false); //URL of Zipato meter
  6. define(URL_STATIONS, "http://st1.notch.no/prices.js", false);
  7. $force_update = false;
  8. $force_update = $_GET['force']; //You can force an update with ?force=true
  9. echo date("Y-m-d H:i:s") . ":"; //Output for my cron-job redirect log
  10. $price_b95 = 0.0;
  11. $price_diesel = 0.0;
  12. $json = GetStationsJSON(URL_STATIONS);
  13. foreach($json['stations'] as $station) {
  14. if ($station['stationId'] == OPPEGARD_STATION_ID) {
  15. $price_b95 = (double) str_replace(",", ".", $station['b95']);
  16. $price_diesel = (double) str_replace(",", ".", $station['diesel']);
  17. break;
  18. }
  19. }
  20. list($previous_b95, $previous_diesel) = explode(";", ReadPricesFromFile(TEMP_FILENAME));
  21. $previous_b95 = (double) str_replace(",",".", $previous_b95);
  22. $previous_diesel = (double) str_replace(",",".", $previous_diesel);
  23. if (($previous_b95 != $price_b95) || $force_update) { //write to zipato meter only if any of the price has changed
  24. UpdateZipatoMeter(URL_B95_METER, $price_b95);
  25. WritePricesToFile(TEMP_FILENAME, $price_b95, $price_diesel);
  26. echo " p_b: " . $previous_b95 . ", c_b: " . $price_b95;
  27. }
  28. else {
  29. echo " B95: no change"; //for the log
  30. }
  31. if (($previous_diesel != $price_diesel) || $force_update) { //Wirte to zipato meter only if the price has changed
  32. UpdateZipatoMeter(URL_DIESEL_METER, $price_diesel);
  33. WritePricesToFile(TEMP_FILENAME, $price_b95, $price_diesel);
  34. echo " p_D: " . $previous_diesel . ", c_D: " . $price_diesel; //for the log
  35. }
  36. else {
  37. echo " D: no change"; //for the log
  38. }
  39. echo "\n"; //newline for the log
  40. function GetStationsJSON($stations_url) { //strips JS code and returns JSON
  41. $content = file_get_contents($stations_url);
  42. $content = str_replace('var prices = \'', '', $content);
  43. $content = substr($content, 0, strlen($content)-2); //removing ';' at the end
  44. return json_decode($content, true);
  45. }
  46. function WritePricesToFile($filename, $b95_price, $diesel_price){ //Latest prices are written to file
  47. $Handle = fopen($filename, 'w');
  48. $Data = $b95_price . ';' . $diesel_price;
  49. fwrite($Handle, $Data);
  50. fclose($Handle);
  51. }
  52. function ReadPricesFromFile($filename) {
  53. if (!file_exists($filename)) {
  54. echo "\nFile " . $filename . "does not exist"; //for logging
  55. return "1000.0;1000.0"; //return a high price if no price have been recorded yet
  56. }
  57. $filehandler = fopen($filename, "r");
  58. $contents = fread($filehandler, filesize($filename));
  59. fclose($filehandler);
  60. return $contents;
  61. }
  62. function UpdateZipatoMeter($meter_url, $price) {
  63. $url = $meter_url . $price;
  64. $devnull = file_get_contents($url);
  65. }
  66. ?>

Replies (5)

photo
1

I have no idea why also, I think we will all be in Tesla cars by then. But nice work. Could you put your coding skills to better use, such as helping to develop plugins for Sonos amp, or better Nest integration, or how about a plugin for the doorbird intercoms (they have an open api). Others would like a netamo plugin. I am sure everyone here has a million things you could do, keep you busy for years. By then all the oil would have dried up and we'll be needing a code for what days are sunny enough to drive around in our solar paneled cars. Just a thought. Cool job though.

photo
1

I like fiddling with code, but normally my time is limited. Scripts like this is usually made after elleven o'clock at night :-)


I really miss scripting capabilities in Zipabox though. Python, javascript, lua or similar would be great.

photo
2

I wish I wish.... for cristmas Logitech Harmony integration?


Did not even know this was possible with zipabox?

photo
1

me too Jacob.


Hey Marius, someone put me onto this, as I am no coder I do not understand it, but maybe you will.

https://www.npmjs.com/package/zipabox

photo
1

Adrian and Jacob, all these integration scripts are running on separate devices. I write my scripts in PHP, simply because it is easily available on my NAS

The last link Adrian is something more comprehensive. As far as I understand. it is a Nodejs wrapper for the Zipato api. With this wrapper you can write server-side, event driven javascript code to integrate with the Zipabox. If the complete Api is covered, it will be possible to control the Zipabox almost entirely. It still needs to run on a separate server though. If someone has written a similar package (Nodejs api wrapper) for e.g. the Logitech (I guess someone already have :) ) it will make coding the communication between Zipabox and Logitech quite fast.

But still, it would need to run on a separate server.

Leave a Comment
 
Attach a file