stable/scripts/rest_client.py

100 lines
2.8 KiB
Python
Raw Normal View History

2019-04-04 05:08:24 +00:00
#!/usr/bin/env python3
"""
Simple command line client into RPC commands
Can be used as an alternate to Telegram
2019-04-04 19:07:44 +00:00
Should not import anything from freqtrade,
so it can be used as a standalone script.
2019-04-04 05:08:24 +00:00
"""
2019-04-04 19:07:44 +00:00
import argparse
2019-04-09 04:40:15 +00:00
import json
2019-04-04 19:07:44 +00:00
import logging
2019-04-04 05:08:24 +00:00
from sys import argv
2019-04-09 04:40:15 +00:00
from pathlib import Path
2019-04-04 19:07:44 +00:00
from requests import get
from requests.exceptions import ConnectionError
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger("ft_rest_client")
2019-04-04 05:08:24 +00:00
2019-04-04 19:07:44 +00:00
COMMANDS_NO_ARGS = ["start",
"stop",
2019-04-09 04:40:15 +00:00
"stopbuy",
"reload_conf"
2019-04-04 19:07:44 +00:00
]
COMMANDS_ARGS = ["daily",
]
def add_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("command",
help="Positional argument defining the command to execute.")
2019-04-09 04:40:15 +00:00
parser.add_argument('-c', '--config',
help='Specify configuration file (default: %(default)s). ',
dest='config',
type=str,
metavar='PATH',
default='config.json'
)
2019-04-04 19:07:44 +00:00
args = parser.parse_args()
# if len(argv) == 1:
# print('\nThis script accepts the following arguments')
# print('- daily (int) - Where int is the number of days to report back. daily 3')
# print('- start - this will start the trading thread')
# print('- stop - this will start the trading thread')
# print('- there will be more....\n')
return vars(args)
2019-04-09 04:40:15 +00:00
def load_config(configfile):
file = Path(configfile)
if file.is_file():
with file.open("r") as f:
config = json.load(f)
return config
2019-04-04 19:07:44 +00:00
def call_authorized(url):
try:
return get(url).json()
except ConnectionError:
logger.warning("Connection error")
2019-04-09 04:40:15 +00:00
def call_command_noargs(server_url, command):
logger.info(f"Running command `{command}` at {server_url}")
r = call_authorized(f"{server_url}/{command}")
2019-04-04 19:07:44 +00:00
logger.info(r)
def main(args):
2019-04-09 04:40:15 +00:00
config = load_config(args["config"])
url = config.get("api_server", {}).get("server_url", "127.0.0.1")
port = config.get("api_server", {}).get("listen_port", "8080")
server_url = f"http://{url}:{port}"
2019-04-04 19:07:44 +00:00
# Call commands without arguments
if args["command"] in COMMANDS_NO_ARGS:
2019-04-09 04:40:15 +00:00
call_command_noargs(server_url, args["command"])
2019-04-04 19:07:44 +00:00
if args["command"] == "daily":
if str.isnumeric(argv[2]):
2019-04-09 04:40:15 +00:00
get_url = server_url + '/daily?timescale=' + argv[2]
2019-04-04 19:07:44 +00:00
d = get(get_url).json()
print(d)
else:
print("\nThe second argument to daily must be an integer, 1,2,3 etc")
if __name__ == "__main__":
args = add_arguments()
main(args)