Skip to content

Instantly share code, notes, and snippets.

@projectivemotion
Last active August 10, 2016 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save projectivemotion/1b483edb6d178d5044475fc3e78e62ae to your computer and use it in GitHub Desktop.
Save projectivemotion/1b483edb6d178d5044475fc3e78e62ae to your computer and use it in GitHub Desktop.
i3 status php wrapper for appending currency rate / bitcoin rate in i3 status bar
#!/usr/bin/env php
<?php
/**
* Author: Amado Martinez
* License: MIT
* Date: 2016-07-22
*
* modify .i3status.conf general section: output_format = "i3bar"
* modify i3 config: status_command i3status -c .i3status.conf | php -f ~/bin/i3statusphp
*
* Example:
* $ i3status -c .i3status.conf | php -f i3statusphp
**/
$stdin = fopen('php://stdin', 'r');
$version = fgets($stdin);
$json = fgets($stdin);
echo "$version$json";
while(true):
$json = fgets($stdin);
if($json === false) break;
$obj = json_decode(trim($json, ','));
$mydata = mydata();
#array_unshift($obj, $mydata);
$obj = array_merge($mydata, $obj);
#print_r($obj);continue;
echo json_encode($obj),",\n";
endwhile;
function mydata(){
return [(object)[
'name' => 'Currency',
'full_text' => currency(1, 'EUR', 'USD'),
'color' => '#FFFF00'
], (object)[
'name' => 'Currency 2',
'full_text' => currency(1, 'BTC', 'USD'),
'color' => '#FF00FF'
]];
}
function currency($amt, $from, $to){
static $last = null;
if(!isset($last[$amt . $from . $to]))
{
$last[$amt . $from . $to] = ['last' => 0, 'result' => '#processing#'];
}
$data = &$last[$amt . $from . $to];
if(time() - $data['last'] < 60*60*1) // refresh every 60 minutes
return $data['result'] ; // . ' ' . $data['last'];
$data['last'] = time();
$res = file_get_contents("http://www.google.com/finance/converter?a=$amt&from=$from&to=$to");
if(!preg_match('#<div id=currency_converter_result>([\s\S]*?)</div>#', $res, $match))
$lastvalue = "#error#";
else
$lastvalue = trim(strip_tags($match[1]));
$data['result'] = $lastvalue;
return $lastvalue;
}
@projectivemotion
Copy link
Author

screenshot_20160722_081744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment