From 82d4864df7308466081451d2517c5d65b51a14df Mon Sep 17 00:00:00 2001 From: rochi88 Date: Thu, 29 Feb 2024 12:50:20 +0600 Subject: [PATCH] added api helpers and updated util --- bdshare/__init__.py | 2 ++ bdshare/_version.py | 2 +- bdshare/api/__init__.py | 0 bdshare/api/api_data.py | 30 +++++++++++++++++++++ bdshare/api/client.py | 50 ++++++++++++++++++++++++++++++++++ bdshare/util/cons.py | 2 ++ bdshare/util/store.py | 4 +-- bdshare/util/upass.py | 60 +++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + setup.cfg | 2 +- tests/test_dsex.py | 3 ++- 11 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 bdshare/api/__init__.py create mode 100644 bdshare/api/api_data.py create mode 100644 bdshare/api/client.py create mode 100644 bdshare/util/cons.py create mode 100644 bdshare/util/upass.py diff --git a/bdshare/__init__.py b/bdshare/__init__.py index 9e4f255..633863e 100644 --- a/bdshare/__init__.py +++ b/bdshare/__init__.py @@ -18,3 +18,5 @@ for market data """ from bdshare.stock.market import (get_market_inf, get_latest_pe, get_market_inf_more_data, get_market_depth_data) + +from bdshare.util.store import Store \ No newline at end of file diff --git a/bdshare/_version.py b/bdshare/_version.py index 9d9f480..0ee93b9 100644 --- a/bdshare/_version.py +++ b/bdshare/_version.py @@ -1,2 +1,2 @@ -version_info = (0, 5, 0) +version_info = (0, 5, 1) __version__ = '.'.join(map(str, version_info)) diff --git a/bdshare/api/__init__.py b/bdshare/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bdshare/api/api_data.py b/bdshare/api/api_data.py new file mode 100644 index 0000000..098d468 --- /dev/null +++ b/bdshare/api/api_data.py @@ -0,0 +1,30 @@ +# -*- coding:utf-8 -*- +""" +Created on 2024/02/29 +@author: Raisul Islam +@group : bdshare.xyz +@contact: raisul.me@gmail.com +""" +from bdshare.api import client +from bdshare.util import upass + + +PRICE_COLS = ['open', 'close', 'high', 'low', 'pre_close'] +FORMAT = lambda x: '%.2f' % x +FREQS = {'D': '1DAY', + 'W': '1WEEK', + 'Y': '1YEAR', + } + + +def api_data(token=''): + """ + Initialize the API. For the first time, you can record your token credentials through bs.set_token('your token'). The temporary token can be passed in through this parameter. + """ + if token == '' or token is None: + token = upass.get_token() + if token is not None and token != '': + api = client.DataApi(token) + return api + else: + raise Exception('api init error.') \ No newline at end of file diff --git a/bdshare/api/client.py b/bdshare/api/client.py new file mode 100644 index 0000000..097f1b1 --- /dev/null +++ b/bdshare/api/client.py @@ -0,0 +1,50 @@ +# !/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2024/02/29 +@author: Raisul Islam +@group : bdshare.xyz +""" + +import pandas as pd +import simplejson as json +from functools import partial +import requests + + +class DataApi: + + __token = '' + __http_url = 'https://api.bdshare.xyz' + + def __init__(self, token, timeout=10): + """ + Parameters + ---------- + token: str + API TOKEN, Used for user authentication + """ + self.__token = token + self.__timeout = timeout + + def query(self, api_name, fields='', **kwargs): + req_params = { + 'api_name': api_name, + 'token': self.__token, + 'params': kwargs, + 'fields': fields + } + + res = requests.post(self.__http_url, json=req_params, timeout=self.__timeout) + result = json.loads(res.text) + if result['code'] != 0: + raise Exception(result['msg']) + data = result['data'] + columns = data['fields'] + items = data['items'] + + return pd.DataFrame(items, columns=columns) + + def __getattr__(self, name): + return partial(self.query, name) \ No newline at end of file diff --git a/bdshare/util/cons.py b/bdshare/util/cons.py new file mode 100644 index 0000000..4cd7559 --- /dev/null +++ b/bdshare/util/cons.py @@ -0,0 +1,2 @@ +TOKEN_F_P = 'tk.csv' +TOKEN_ERR_MSG = 'Please set the token voucher code of bdshare. If not, please visit https://bdshare.xyz to register and apply.' \ No newline at end of file diff --git a/bdshare/util/store.py b/bdshare/util/store.py index 4537844..f882f7c 100644 --- a/bdshare/util/store.py +++ b/bdshare/util/store.py @@ -1,7 +1,7 @@ # -*- coding:utf-8 -*- import pandas as pd -import bdshare as ds +from pandas import compat import os @@ -21,7 +21,7 @@ def save_as(self, name, path, to='csv'): if path is None: path = self.path file_path = '%s%s%s.%s' - if isinstance(name) and name is not '': + if isinstance(name, compat.string_types) and name is not '': if (path is None) or (path == ''): file_path = '.'.join([name, to]) else: diff --git a/bdshare/util/upass.py b/bdshare/util/upass.py new file mode 100644 index 0000000..30c1e1b --- /dev/null +++ b/bdshare/util/upass.py @@ -0,0 +1,60 @@ +# -*- coding:utf-8 -*- + +""" +Created on 2024/02/29 +@author: Raisul Islam +@group : bdshare.xyz +@contact: raisul.me@gmail.com +""" + +import pandas as pd +import os +import cons as ct + +BK = 'bk' + +def set_token(token): + df = pd.DataFrame([token], columns=['token']) + user_home = os.path.expanduser('~') + fp = os.path.join(user_home, ct.TOKEN_F_P) + df.to_csv(fp, index=False) + + +def get_token(): + user_home = os.path.expanduser('~') + fp = os.path.join(user_home, ct.TOKEN_F_P) + if os.path.exists(fp): + df = pd.read_csv(fp) + return str(df.ix[0]['token']) + else: + print(ct.TOKEN_ERR_MSG) + return None + + +def set_broker(broker='', user='', passwd=''): + df = pd.DataFrame([[broker, user, passwd]], + columns=['broker', 'user', 'passwd'], + dtype=object) + if os.path.exists(BK): + all = pd.read_csv(BK, dtype=object) + if (all[all.broker == broker]['user']).any(): + all = all[all.broker != broker] + all = all.append(df, ignore_index=True) + all.to_csv(BK, index=False) + else: + df.to_csv(BK, index=False) + + +def get_broker(broker=''): + if os.path.exists(BK): + df = pd.read_csv(BK, dtype=object) + if broker == '': + return df + else: + return df[df.broker == broker] + else: + return None + + +def remove_broker(): + os.remove(BK) diff --git a/pyproject.toml b/pyproject.toml index e6b7cbc..0fb8e2a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,5 +5,6 @@ requires = [ "requests", "html5lib", "pandas", + "simplejson", ] build-backend = "setuptools.build_meta" \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 16dc247..48a5241 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = bdshare -version = 0.5.0 +version = 0.5.1 author= Raisul Islam author_email= raisul.me@gmail.com description = A utility for crawling historical and Real-time Quotes of dse diff --git a/tests/test_dsex.py b/tests/test_dsex.py index d246791..d7d1dcb 100644 --- a/tests/test_dsex.py +++ b/tests/test_dsex.py @@ -3,7 +3,8 @@ Created on 2021-Jun-09 @author: Raisul Islam ''' -from bdshare import get_dsex_data +from bdshare import get_dsex_data, Store df = get_dsex_data() +Store(data=df) print(df.to_string()) \ No newline at end of file