Last active
August 10, 2016 12:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
} |
Author
projectivemotion
commented
Jul 22, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment