Skip to content

Commit

Permalink
use a market cache and also make final improvements to multilang.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
frosty00 committed Mar 12, 2022
1 parent 977b455 commit c20ffbe
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
15 changes: 14 additions & 1 deletion examples/js/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let [processPath, , exchangeId, methodName, ... params] = process.argv.filter (x
, table = process.argv.includes ('--table')
, iso8601 = process.argv.includes ('--iso8601')
, cors = process.argv.includes ('--cors')
, cache_markets = process.argv.includes ('--cache-markets')
, testnet =
process.argv.includes ('--test') ||
process.argv.includes ('--testnet') ||
Expand Down Expand Up @@ -42,6 +43,7 @@ const ccxt = require ('../../ccxt.js')
, util = require ('util')
, { execSync } = require ('child_process')
, log = require ('ololog').configure ({ locate: false }).unlimited
, fsPromises = require ('fs/promises')
, { ExchangeError, NetworkError } = ccxt

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -141,6 +143,7 @@ function printUsage () {
log ('--sandbox Use the exchange sandbox if available, same as --testnet')
log ('--testnet Use the exchange testnet if available, same as --sandbox')
log ('--test Use the exchange testnet if available, same as --sandbox')
log ('--cache-markets Cache the loaded markets in the .cache folder in the current directory')
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -214,8 +217,18 @@ async function main () {
exchange.verbose = verbose
}

const path = '.cache/' + exchangeId + '-markets.json'

if (!no_load_markets) {
await exchange.loadMarkets ()
try {
await fsPromises.access (path, fs.constants.R_OK)
exchange.markets = JSON.parse (await fsPromises.readFile (path))
} catch {
await exchange.loadMarkets ()
if (cache_markets) {
await fsPromises.writeFile (path, JSON.stringify (exchange.markets))
}
}
}

if (signIn && exchange.has.signIn) {
Expand Down
32 changes: 20 additions & 12 deletions examples/php/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@

if (count($argv) > 2) {

$id = $argv[1];
$member = $argv[2];
$args = array_slice($argv, 3);
$exchange_found = in_array($id, \ccxt\Exchange::$exchanges);
# first we filter the args
$verbose = count(array_filter($argv, function ($option) { return strstr($option, '--verbose') !== false; })) > 0;
$args = array_values(array_filter($argv, function ($option) { return strstr($option, '--verbose') === false; }));

if ($exchange_found) {
$verbose = count(array_filter($args, function ($option) { return strstr($option, '--verbose') !== false; })) > 0;
$args = array_filter($args, function ($option) { return strstr($option, '--+') === false; });
$test = count(array_filter($args, function ($option) { return strstr($option, '--test') !== false || strstr($option, '--testnet') !== false || strstr($option, '--sandbox') !== false; })) > 0;
$args = array_values(array_filter($args, function ($option) { return strstr($option, '--test') === false || strstr($option, '--testnet') !== false || strstr($option, '--sandbox') !== false; }));

$debug = count(array_filter($args, function ($option) { return strstr($option, '--debug') !== false; })) > 0;
$args = array_values(array_filter($args, function ($option) { return strstr($option, '--debug') === false; }));

$test = count(array_filter($args, function ($option) { return strstr($option, '--test') !== false || strstr($option, '--testnet') !== false || strstr($option, '--sandbox') !== false; })) > 0;
$args = array_filter($args, function ($option) { return strstr($option, '--test') === false || strstr($option, '--testnet') !== false || strstr($option, '--sandbox') !== false; });
$id = $args[1];
$member = $args[2];
$args = array_slice($args, 3);
$exchange_found = in_array($id, \ccxt\Exchange::$exchanges);

$debug = count(array_filter($args, function ($option) { return strstr($option, '--debug') !== false; })) > 0;
$args = array_filter($args, function ($option) { return strstr($option, '--debug') === false; });
if ($exchange_found) {

$keys_global = './keys.json';
$keys_local = './keys.local.json';
Expand Down Expand Up @@ -71,7 +73,13 @@
return $arg;
}, $args);

$exchange->load_markets();
$markets_path = '.cache/' . $exchange->id . '-markets.json';
if (file_exists($markets_path)) {
$markets = json_decode(file_get_contents($markets_path), true);
$exchange->markets = $markets;
} else {
$exchange->load_markets();
}

$exchange->verbose = $verbose;

Expand Down
7 changes: 6 additions & 1 deletion examples/py/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ def print_usage():
if argv.verbose and argv.debug:
exchange.verbose = argv.verbose

exchange.load_markets()
markets_path = '.cache/' + exchange.id + '-markets.json'
if os.path.exists(markets_path):
with open(markets_path, 'r') as f:
exchange.markets = json.load(f)
else:
exchange.load_markets()

exchange.verbose = argv.verbose # now set verbose mode

Expand Down
39 changes: 28 additions & 11 deletions multilang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function usage() {
echo " -l View in less editor"
echo " -s Remove special characters"
echo " -h Display help"
echo " -v Verbose mode"
exit 1
}

Expand All @@ -20,6 +21,7 @@ pythonCli="${cliFolder}/py/cli.py"
phpCli="${cliFolder}/php/cli.php"

useLess=false
verbose=false
removeSpecial=false
numLines=0

Expand All @@ -45,7 +47,12 @@ function condense {
# Trims output down to a set number of lines on the top and the bottom
local pythonLength=$1
local half=$(($numLines / 2))
local continueFrom=$((pythonLength - half))
local continueFrom=
if [ $half -gt $pythonLength ]; then
continueFrom=${half}
else
continueFrom=$(($pythonLength - $half))
fi
if [ ${numLines} -gt 0 ]; then
sed -n "1,${half}p;${continueFrom},${pythonLength}p"
else
Expand All @@ -68,31 +75,37 @@ function padOutput {
local input="$1"
local length="$2"
echo "$input"
for ((i=$(wc -l <<< "$input"); i < $length; i++)); do
echo "$(tput setaf 1)$(tput sgr0)"
done
lines=$(wc -l <<< "$input")
toPad=$(($length - $lines))
yes "$(tput setaf 1)$(tput sgr0)" | head -n $toPad
}

# Loop through command line arguments
while getopts 'hc:sl' flag; do
while getopts 'hc:slv' flag; do
case "${flag}" in
h) usage ;;
c) numLines="${OPTARG}" ;;
s) removeSpecial=true ;;
l) useLess=true ;;
v) verbose=true ;;
*) usage ;;
esac
done

shift $((OPTIND - 1))

args="$@"
jsArgs=$(echo "$args" | sed -E -e 's/(null|None)/undefined/g')
pythonArgs=$(echo "$args" | sed -E -e 's/(undefined|null)/None/g')
phpArgs=$(echo "$args" | sed -E -e 's/(undefined|None)/null/g')
if ${verbose}; then
args="--verbose $@"
else
args="$@"
fi

jsArgs=$(<<< "$args" sed -E -e 's/(null|None)/undefined/g')
pythonArgs=$(<<< "$args" sed -E -e 's/(undefined|null)/None/g')
phpArgs=$(<<< "$args" sed -E -e 's/(undefined|None)/null/g')

color=3
jsOutput=$(writeOutput node $jsCli "--no-table $jsArgs")
jsOutput=$(writeOutput node $jsCli "--no-table --cache-markets $jsArgs")
((color++))

pythonOutput=$(writeOutput python3 $pythonCli "$pythonArgs")
Expand All @@ -106,4 +119,8 @@ length=$(wc -l <<< "$phpOutput")
jsOutput=$(padOutput "$jsOutput" $length)
pythonOutput=$(padOutput "$pythonOutput" $length)

paste <(echo "$jsOutput") <(echo "$phpOutput") <(echo "$pythonOutput") | column -s $'\t' -t | condense $pythonLength | display
if ${verbose}; then
echo "$jsOutput $phpOutput $pythonOutput"
else
paste <(echo "$jsOutput") <(echo "$phpOutput") <(echo "$pythonOutput") | column -s $'\t' -t | condense $pythonLength | display
fi

0 comments on commit c20ffbe

Please sign in to comment.