Add Local .json file Loading

This commit is contained in:
Bloodhunter4rc 2022-12-08 00:52:54 +01:00
parent b144a6357d
commit da2747d487

View File

@ -5,6 +5,7 @@ Provides pair list fetched from a remote source
""" """
import json import json
import logging import logging
from pathlib import Path
from typing import Any, Dict, List from typing import Any, Dict, List
import requests import requests
@ -110,21 +111,36 @@ class RemotePairList(IPairList):
:param tickers: Tickers (from exchange.get_tickers). May be cached. :param tickers: Tickers (from exchange.get_tickers). May be cached.
:return: List of pairs :return: List of pairs
""" """
time_elapsed = 0
pairlist = self._pair_cache.get('pairlist') pairlist = self._pair_cache.get('pairlist')
if pairlist: if pairlist:
# Item found - no refresh necessary # Item found - no refresh necessary
return pairlist.copy() return pairlist.copy()
else: else:
# Fetch Pairlist from Remote URL if self._pairlist_url.startswith("file:///"):
plist, time_elapsed, info = self.fetch_pairlist() filename = self._pairlist_url.split("file:///", 1)[1]
pairlist = [] file_path = Path(filename)
for i in plist: if file_path.exists():
if i not in pairlist: with open(filename) as json_file:
pairlist.append(i) # Load the JSON data into a dictionary
jsonp = json.load(json_file)
plist = jsonp['pairs']
else: else:
continue raise ValueError(f"{self._pairlist_url} does not exist.")
else:
# Fetch Pairlist from Remote URL
plist, time_elapsed, info = self.fetch_pairlist()
pairlist = []
for i in plist:
if i not in pairlist:
pairlist.append(i)
else:
continue
pairlist = self.filter_pairlist(pairlist, tickers) pairlist = self.filter_pairlist(pairlist, tickers)
self._pair_cache['pairlist'] = pairlist.copy() self._pair_cache['pairlist'] = pairlist.copy()