Пример #1
0
    def post(self):
        """
        日期查询序列
        """
        # data_dic = request.json
        args = date_query_parser.parse_args()
        logger.info('/THS_DateQuery/ args:%s' % args)
        ret_data = None
        for nth in range(2):
            try:
                ret_data = ifind.THS_DateQuery(**args)
            except JSONDecodeError:
                # 目前原因不明,但是调用接口时会随机性的会抛出这类错误
                logger.exception('/THS_DateQuery/ args:%s' % args)
                time.sleep(0.1)
                continue

            error_code = ret_data['errorcode']
            if error_code != 0:
                # 错误处理
                if error_code in (-1010, -1020):
                    ths_login = ifind_login()
                    if ths_login == 0 or ths_login == -201:
                        logger.warning('尝试重新登陆成功,再次调用函数')
                        continue
                    else:
                        logger.error('尝试重新登陆失败')

                msg = ret_data['errmsg']
                logger.error('THS_DateQuery(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break

        if ret_data is None:
            return None
        tables = ret_data['tables']
        table_count = len(tables)
        data_df_list = []

        # 若没有数据,返回空
        if table_count == 0:
            return None

        data_df = pd.DataFrame(tables)
        data_df_list.append(data_df)
        ret_df = pd.concat(data_df_list)
        ret_df.index = [str(idx) for idx in ret_df.index]
        # print('ret_df\n', ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)

        return ret_dic
Пример #2
0
    def post(self):
        """
        EDB查询序列
        """
        # data_dic = request.json
        args = edb_query_parser.parse_args()
        logger.info('/THS_EDBQuery/ args:%s' % args)
        ret_data = None
        for nth in range(2):
            try:
                ret_data = ifind.THS_EDBQuery(**args)
            except JSONDecodeError:
                # 目前原因不明,但是调用接口时会随机性的会抛出这类错误
                logger.exception('/THS_EDBQuery/ args:%s' % args)
                time.sleep(0.1)
                continue

            error_code = ret_data['errorcode']
            if error_code != 0:
                # 错误处理
                if error_code in (-1010, -1020):
                    ths_login = ifind_login()
                    if ths_login == 0 or ths_login == -201:
                        logger.warning('尝试重新登陆成功,再次调用函数')
                        continue
                    else:
                        logger.error('尝试重新登陆失败')

                msg = ret_data['errmsg']
                logger.error('THS_EDBQuery(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break

        if ret_data is None:
            return None
        tables = ret_data['tables']
        table_count = len(tables)
        data_df_list = []
        if table_count > 0:
            for nth_table in range(table_count):
                table = tables[nth_table]
                if 'id' not in table:
                    logger.error('%d/%d) 当前结果没有 time 列,将被跳过 %s', nth_table,
                                 table_count, args)
                    continue
                if len(table['id']) > 0:
                    temp = table['id']
                    table.pop('id')
                    tab = pd.DataFrame(table)
                    tab['id'] = temp[0]
                    data_df_list.append(tab)
                else:
                    data_df = pd.DataFrame(table)
                    data_df_list.append(data_df)

        # 若没有数据,返回空
        if len(data_df_list) == 0:
            return None

        ret_df = pd.concat(data_df_list)
        # print('ret_df\n', ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)
        return ret_dic
Пример #3
0
    def post(self):
        """
        数据池序列
        """
        # data_dic = request.json
        args = data_pool_parser.parse_args()
        logger.info('/THS_DataPool/ args:%s' % args)
        ret_data = None
        for nth in range(2):
            try:
                ret_data = ifind.THS_DataPool(**args)
            except JSONDecodeError:
                # 目前原因不明,但是调用接口时会随机性的会抛出这类错误
                logger.exception('/THS_DataPool/ args:%s' % args)
                time.sleep(0.1)
                continue

            error_code = ret_data['errorcode']
            if error_code != 0:
                if error_code in (-1010, -1020):
                    ths_login = ifind_login()
                    if ths_login == 0 or ths_login == -201:
                        logger.warning('尝试重新登陆成功,再次调用函数')
                        continue
                    else:
                        logger.error('尝试重新登陆失败')

                msg = ret_data['errmsg']
                logger.error('THS_DataPool(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break

        if ret_data is None:
            return None
        tables = ret_data['tables']
        table_count = len(tables)
        data_df_list = []
        if table_count > 0:
            for nth_table in range(table_count):
                table = tables[nth_table]
                if 'table' not in table:
                    logger.error('%d/%d) 当前结果没有 time 列,将被跳过 %s', nth_table,
                                 table_count, args)
                    continue
                data = table['table']
                data_df = pd.DataFrame(data)
                if table['thscode'] != '':
                    data_df['ths_code'] = table['thscode']
                data_df_list.append(data_df)

        # 若没有数据,返回空
        if len(data_df_list) == 0:
            return None

        ret_df = pd.concat(data_df_list)
        ret_df.index = [str(idx) for idx in ret_df.index]
        # print('ret_df\n', ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)
        return ret_dic
Пример #4
0
    def post(self):
        """
        基础数据序列
        """
        # data_dic = request.json
        args = basic_data_parser.parse_args()
        logger.info('/THS_BasicData/ args:%s' % args)
        ret_data = None
        for nth in range(2):
            try:
                ret_data = ifind.THS_BasicData(**args)
            except JSONDecodeError:
                # 目前原因不明,但是调用接口时会随机性的会抛出这类错误
                logger.exception('/THS_BasicData/ args:%s' % args)
                time.sleep(0.1)
                continue

            error_code = ret_data['errorcode']
            if error_code != 0:
                # 错误处理
                if error_code in (-1010, -1020):
                    ths_login = ifind_login()
                    if ths_login == 0 or ths_login == -201:
                        logger.warning('尝试重新登陆成功,再次调用函数')
                        continue
                    else:
                        logger.error('尝试重新登陆失败')

                msg = ret_data['errmsg']
                logger.error('THS_BasicData(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break

        if ret_data is None:
            return None
        tables = ret_data['tables']
        table_count = len(tables)
        if table_count == 0:
            return None
        data_df_list = []
        for nth_table in range(table_count):
            table = tables[nth_table]
            if 'table' not in table:
                logger.error('%d/%d) 当前结果没有 time 列,将被跳过 %s', nth_table,
                             table_count, args)
                continue
            data = table['table']

            # 找到第一个符合格式的参数进行type判断
            data_df = pd.DataFrame(data)
            data_df['ths_code'] = table['thscode']
            data_df_list.append(data_df)

        # 若没有数据,返回空
        if len(data_df_list) == 0:
            return None

        ret_df = pd.concat(data_df_list).reset_index(drop=True)
        ret_df.index = [str(idx) for idx in ret_df.index]
        # print('ret_df\n', ret_df)
        ret_dic = ret_df.to_dict()
        for key, item_check in ret_dic.items():
            if item_check is not None:
                for n, val in item_check.items():
                    if type(val) in (np.int64, np.float64):
                        ret_dic[key][n] = format_2_str(val)
        # print('ret_dic:\n', ret_dic)
        return ret_dic
Пример #5
0
    def post(self):
        """
        json str:{"codes": "M0017126,M0017127,M0017128",
            "begin_time": "2016-11-10", "end_time": "2017-11-10", "options": "Fill=Previous"}
        :return: 返回万得返回数据dict
        """
        args = receive_edb_parser.parse_args()
        logger.info('/edb/ args:%s', args)
        codes = args['codes']
        # begin_time = args['begin_time']
        # end_time = args['end_time']
        # options = args['options']
        if args['options'] == "":
            args['options'] = None
        if not w.isconnected():
            w.start()
        ret_data = None
        for nth in range(2):
            ret_data = w.edb(**args)
            error_code = ret_data.ErrorCode
            if error_code != 0:
                if nth == 0 and error_code == -40521010:
                    w.stop()
                    w.start()
                    logger.warning('尝试重新登陆成功,再次调用函数')
                    continue

                msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "")
                logger.error('wst(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break
        else:
            if ret_data is None:
                msg = 'wst(%s) ret_data is None' % args
                logger.error(msg)
                raise RequestError(msg, None, 0)

        # 将 Data数据中所有 datetime date 类型的数据转换为 string
        data_len = len(ret_data.Data)

        for n_data in range(data_len):
            data = ret_data.Data[n_data]
            data_len2 = len(data)
            if data_len2 > 0:
                # 取出第一个部位None的数据
                item_check = None
                for item_check in data:
                    if item_check is not None:
                        break
                # 进行类型检查,如果发现是 datetime, date 类型之一,则进行类型转换
                if item_check is not None and type(item_check) in (datetime,
                                                                   date):
                    ret_data.Data[n_data] = [
                        format_2_date_str(dt) for dt in data
                    ]
                    logger.info('%d column["%s"]  date to str', n_data,
                                ret_data.Fields[n_data])
        # 组成 DataFrame
        ret_df = pd.DataFrame(
            ret_data.Data,
            index=[xx.strip() for xx in codes.split(',')],
            columns=[format_2_date_str(dt) for dt in ret_data.Times])
        # print(ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)
        return ret_dic
Пример #6
0
    def post(self):
        """
        json str:{"codes": "600008.SH, "fields": "ask1,bid1,asize1,bsize1,volume,amt,pre_close,open,high,low,last",
            "begin_time": "2017-01-04", "end_time": "2017-02-28", "options": ""}
        :return: 返回万得返回数据dict
        """
        args = receive_wst_parser.parse_args()
        logger.info('/wst/ args:%s', args)
        # codes = args['codes']
        # fields = args['fields']
        # begin_time = args['begin_time']
        # end_time = args['end_time']
        # options = args['options']
        if args['options'] == "":
            args['options'] = None
        if not w.isconnected():
            w.start()
        ret_data = None
        for nth in range(2):
            ret_data = w.wst(**args)
            error_code = ret_data.ErrorCode
            if error_code != 0:
                if nth == 0 and error_code == -40521010:
                    w.stop()
                    w.start()
                    logger.warning('尝试重新登陆成功,再次调用函数')
                    continue

                msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "")
                logger.error('wst(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break
        else:
            if ret_data is None:
                msg = 'wst(%s) ret_data is None' % args
                logger.error(msg)
                raise RequestError(msg, None, 0)
        # 将 Data数据中所有 datetime date 类型的数据转换为 string
        data_len = len(ret_data.Data)

        for n_data in range(data_len):
            data = ret_data.Data[n_data]
            data_len2 = len(data)
            if data_len2 > 0:
                # 取出第一个部位None的数据
                item_check = None
                for item_check in data:
                    if item_check is not None:
                        break
                # 进行类型检查,如果发现是 datetime, date 类型之一,则进行类型转换
                if item_check is not None and type(item_check) in (datetime,
                                                                   date):
                    ret_data.Data[n_data] = [
                        format_2_datetime_str(dt) for dt in data
                    ]
                    logger.info('%d column["%s"]  date to str', n_data,
                                ret_data.Fields[n_data])
        # 组成 DataFrame
        ret_df = pd.DataFrame(
            ret_data.Data,
            index=ret_data.Fields,
            columns=[format_2_datetime_str(dt) for dt in ret_data.Times])
        # print(ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)
        return ret_dic
Пример #7
0
    def post(self):
        """
        json str:{"tablename": "sectorconstituent", "options": "date=2017-03-21;sectorid=1000023121000000"}
        :return: 返回万得返回数据dict
        """
        args = receive_wset_parser.parse_args()
        logger.info('/wset/ args:%s' % args)
        # print('args:%s' % args)
        # table_name = args['table_name']
        # options = args['options']
        if args['options'] == "":
            args['options'] = None
        if not w.isconnected():
            w.start()
        ret_data = None
        for nth in range(2):
            ret_data = w.wset(**args)
            error_code = ret_data.ErrorCode
            if error_code != 0:
                if nth == 0 and error_code == -40521010:
                    w.stop()
                    w.start()
                    logger.warning('尝试重新登陆成功,再次调用函数')
                    continue

                msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "")
                logger.error('wset(%s) ErrorCode=%d %s' %
                             (args, error_code, msg))
                raise RequestError(msg, None, error_code)
            else:
                break
        else:
            if ret_data is None:
                msg = 'wst(%s) ret_data is None' % args
                logger.error(msg)
                raise RequestError(msg, None, 0)
        data_count = len(ret_data.Data)
        # if data_count > 0:
        # print('ret_data.Fields\n', ret_data.Fields)
        # ret_data.Data[0] = [format_2_date_str(dt) for dt in ret_data.Data[0]]
        # print('ret_data.Data\n', ret_data.Data)

        for n_data in range(data_count):
            data_list = ret_data.Data[n_data]
            data_list_len = len(data_list)
            if data_list_len > 0:
                # 取出第一个不为None的数据
                item_check = None
                for item_check in data_list:
                    if item_check is not None:
                        break
                # 进行类型检查,如果发现是 datetime, date 类型之一,则进行类型转换
                if item_check is not None and type(item_check) in (datetime,
                                                                   date):
                    ret_data.Data[n_data] = [
                        format_2_date_str(dt) for dt in data_list
                    ]
                    logger.debug('Data[%d] column["%s"]  date to str', n_data,
                                 ret_data.Fields[n_data])

        ret_df = pd.DataFrame(ret_data.Data,
                              index=ret_data.Fields,
                              columns=ret_data.Codes)
        # print('ret_df\n', ret_df)
        ret_dic = ret_df.to_dict()
        # print('ret_dic:\n', ret_dic)
        return ret_dic