Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active March 11, 2017 08:43
Show Gist options
  • Save fwolf/8168480 to your computer and use it in GitHub Desktop.
Save fwolf/8168480 to your computer and use it in GitHub Desktop.
Send reminder when trade price reach setting criteria.
#! /usr/bin/php
<?php
/**
* btc38-reminder.php
*
* Copyright 2013 Fwolf <fwolf.aide+bin.public@gmail.com>
* All rights reserved.
*
* Distributed under the MIT License.
* http://opensource.org/licenses/mit-license
*
* Send reminder when trade price reach setting criteria.
*
* Suggest use xfce4-notifyd, supports expired-time option, which
* nitification-daemon doesn't. The execute file path in Arch Linux is
* '/usr/lib/xfce4/notifyd/xfce4-notifyd`.
*
* @copyright Copyright 2013 Fwolf
* @author Fwolf <fwolf.aide+gist@gmail.com>
* @license http://opensource.org/licenses/mit-license MIT
* @since 2013-12-29
* @version 1.0
*/
use Fwlib\Net\Curl;
// Include
require 'fwlib/autoload.php';
// Check parameter amount, at least 2 param needed
if (3 > $argc) {
PrintUsage();
exit(-1);
}
// Main body
$url = 'http://www.btc38.com/trade/getTradeList.php?coinname=' .
strtoupper($argv[1]);
$curl = new Curl;
$priceMin = floatval($argv[2]);
$priceMax = isset($argv[3]) ? floatval($argv[3]) : 0;
if (0 == $priceMax) {
sendNotify("Monitor for price lower than $priceMin");
} else {
sendNotify("Monitor for price out of $priceMin and $priceMax");
}
// Start a endless loop with pause
while (true) {
$result = $curl->get($url);
if ('{' != $result{0}) {
// Remove BOM
$result = substr($result, 3);
}
$result = json_decode($result, true);
if (is_null($result)) {
sendNotify('Curl Error');
} else {
$mostRecentTrade = $result['trade'][0];
$price = floatval($mostRecentTrade['price']);
if (0 != $priceMin && $price <= $priceMin) {
sendNotify(
'Low price',
"$price with amount " . $mostRecentTrade['volume']
);
} elseif (0 != $priceMax && $price >= $priceMax) {
sendNotify(
'High price',
"$price with amount " . $mostRecentTrade['volume']
);
}
}
sleep(42);
}
// Functions define
/**
* Print usage message
*/
function printUsage()
{
$s = basename(__FILE__);
echo <<<EOF
Usage: $s CoinName Min [Max]
Parameters:
-Min Send reminder when trade price lower than this, 0 to disable.
-Max Send reminder when trade price higher than this, 0 to disable.
EOF;
}
/**
* Send notify
*
* @param string $title
* @param string $message
*/
function sendNotify($title, $message = '')
{
$cmd = "notify-send -t 42000 '$title'";
if (!empty($message)) {
$cmd .= " '$message'";
}
exec($cmd);
echo "$title: $message\n";
}
/**
* ChangeLog
*
* v1.0
* - Basic feature, claw and notify
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment