mirror of
https://github.com/yongghongg/stock-symbol.git
synced 2024-01-14 00:27:38 +03:00
[add] added get index method
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
include stock-symbols/data/*.csv
|
||||
include stock-symbols/data/*.json
|
||||
@@ -1,2 +0,0 @@
|
||||
from importlib import resources
|
||||
import io
|
||||
3
stock_symbols/__init__.py
Normal file
3
stock_symbols/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .stock_symbols import StockSymbols
|
||||
|
||||
__all__ = [StockSymbols]
|
||||
BIN
stock_symbols/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
stock_symbols/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
stock_symbols/__pycache__/stock_symbols.cpython-39.pyc
Normal file
BIN
stock_symbols/__pycache__/stock_symbols.cpython-39.pyc
Normal file
Binary file not shown.
62
stock_symbols/stock_symbols.py
Normal file
62
stock_symbols/stock_symbols.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import requests
|
||||
|
||||
_BASE_URL_ = 'http://localhost:3001/api/'
|
||||
|
||||
_UNAUTHORIZED_ = 401
|
||||
_NOT_FOUND_ = 404
|
||||
_INVALID_KEY_ = 'Invalid API Key'
|
||||
_SERVER_ERROR_ = 'Something is wrong. Please contact the creator'
|
||||
|
||||
class StockSymbols():
|
||||
def __init__(self, api_key: str):
|
||||
self._base_url = _BASE_URL_
|
||||
self.api_key = api_key
|
||||
|
||||
def get_symbol_list(self, market=None, index=None, symbols_only=False):
|
||||
headers = {'x-api-key': self.api_key}
|
||||
try:
|
||||
if index != None:
|
||||
params = {'market': market, 'index': index}
|
||||
r = requests.get(self._base_url + 'indexes', params=params, headers=headers)
|
||||
else:
|
||||
params = {'market': market}
|
||||
r = requests.get(self._base_url + 'symbols', params=params, headers=headers)
|
||||
if r.status_code == _UNAUTHORIZED_:
|
||||
raise ValueError(_INVALID_KEY_)
|
||||
if r.status_code == _NOT_FOUND_:
|
||||
raise ValueError(r.json()['error'])
|
||||
data = r.json()['data']
|
||||
if symbols_only:
|
||||
return [q['symbol'] for q in data[0]['quotes']]
|
||||
else:
|
||||
return data[0]['quotes']
|
||||
except ValueError:
|
||||
raise
|
||||
except:
|
||||
raise Exception(_SERVER_ERROR_)
|
||||
|
||||
def get_market_list(self):
|
||||
headers = {'x-api-key': self.api_key}
|
||||
try:
|
||||
r = requests.get(self._base_url + 'market/all', headers=headers)
|
||||
if r.status_code == _UNAUTHORIZED_:
|
||||
raise ValueError(_INVALID_KEY_)
|
||||
data = r.json()['data']
|
||||
return data
|
||||
except ValueError:
|
||||
raise
|
||||
except:
|
||||
raise Exception(_SERVER_ERROR_)
|
||||
|
||||
def get_index_list(self):
|
||||
headers = {'x-api-key': self.api_key}
|
||||
try:
|
||||
r = requests.get(self._base_url + 'indexes/all', headers=headers)
|
||||
if r.status_code == _UNAUTHORIZED_:
|
||||
raise ValueError(_INVALID_KEY_)
|
||||
data = r.json()['data']
|
||||
return data
|
||||
except ValueError:
|
||||
raise
|
||||
except:
|
||||
raise Exception(_SERVER_ERROR_)
|
||||
Reference in New Issue
Block a user