예제 #1
0
def fhdatas(kf, kfv, tabn, tabc):
    tuapi = tabc['tuapi']
    dfield = tabc['dfield']
    GLOGGER.debug('==S==> table:%s tc:%s tuapi:%s dfield:%s' %
                  (tabn, kfv, tuapi, dfield))
    conn = tuh.getMysqlConn()
    cursor = conn.cursor()
    rdf = None
    maxdate = tuh.getmaxdate(TABMAXVAL[tabn], kfv,
                             tabc.get('sdate', tuh.TUSDATE))
    if tabc['loop']:
        sdate = cd.ymd2date(maxdate)
        edate = cd.preday()
        edatestr = tabc.get('edate', '')
        if edatestr:
            edate = cd.ymd2date(edatestr)
        while edate >= sdate:
            kwargs = {
                kf: kfv,
                'start_date': sdate.strftime('%Y%m%d'),
                'end_date': edate.strftime('%Y%m%d')
            }
            cut = tabc.get('cut', False)
            cfield = tabc.get('cfield', '')
            clen = tabc.get('clen', tuh.CUTMAXLEN)
            df = tuh.gettudf(tuapi,
                             tabc['fields'],
                             kwargs,
                             spm=tabc['spm'],
                             cut=cut,
                             cfield=cfield,
                             clen=clen)
            GLOGGER.debug('====LOOP DATAS==> sdate:%s edate:%s dflen:%s' %
                          (sdate, edate, len(df)))
            if len(df) > 0:
                if rdf is None:
                    rdf = df
                else:
                    rdf = rdf.append(df)
            if len(df) < 1 or pd.isnull(df[dfield].min()):
                edate = cd.preday(sdate)
            else:
                edate = cd.preday(cd.ymd2date(df[dfield].min()))
    else:
        kwargs = {kf: kfv}
        df = tuh.gettudf(tuapi, tabc['fields'], kwargs, spm=tabc['spm'])
        df = df[df[dfield] >= maxdate]
        if len(df) > 0:
            rdf = df
        GLOGGER.debug('====DATAS==> dflen:%s' % (len(df)))
    if rdf is None:
        rdf = pd.DataFrame(columns=(kf, ))
    emsgs = tuh.savedialydf(tabn, rdf)
    for emsg in emsgs:
        GLOGGER.error(emsg)
    GLOGGER.debug('==E==> table:%s tc:%s tuapi:%s rdflen:%s' %
                  (tabn, kfv, tuapi, len(rdf)))
    cursor.close()
    conn.close()
    return rdf
예제 #2
0
def fetchdataone(ind):
    logger = __logmap[ind[0:1] + 'log']
    conn = tuh.getMysqlConn()
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    if __fav == 0 or __fav == 1:
        cursor.execute(
            "select t.ts_code from stk_basic t where t.del<>1 and t.fav=" +
            str(__fav) + " order by t.ts_code;")
    else:
        cursor.execute(
            "select t.ts_code from stk_basic t where t.del<>1 order by t.ts_code;"
        )
    stkbs = cursor.fetchall()
    cursor.close()

    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute(
        "select t.ts_code as stkc,max(t.trade_date) as md from stk_" + ind +
        " t group by t.ts_code;")
    stkmtdd = tuh.listToDict(cursor.fetchall(), 'stkc', 'md')
    cursor.close()
    tuapi = tuh.tuApi
    isql = ''

    cursor = conn.cursor()
    for stk in stkbs:
        stkc = stk['ts_code']
        sdate = cd.ymd2date(tuh.getsdate(stkmtdd, stkc, __force))
        edate = cd.preday()
        while edate >= sdate:
            df = fetchtudata(tuapi, ind, sdate.strftime('%Y%m%d'),
                             edate.strftime('%Y%m%d'), stkc)
            logger.debug('====SSSS====> %s %s===>sd:%s ed:%s len(df):%s' %
                         (stkc, ind, sdate, edate, len(df)))
            if len(df) > 0:
                cols = df.columns
                isql = tuh.geninssql(cols, 'stk_' + ind, isql)
                rowvs = []
                for index, row in df.iterrows():
                    rowv = []
                    for col in cols:
                        rowv.append(row[col])
                    rowvs.append(rowv)
                emsgs = tuh.saveorupdate({'sql': isql, 'vals': rowvs})
                for m in emsgs:
                    logger.error(m)
                logger.debug(
                    '====SAVEEND====> %s %s===>sd:%s ed:%s ldf:%d mtd:%s emc:%d'
                    % (stkc, ind, sdate, edate, len(df),
                       df['trade_date'].min(), len(emsgs)))
            if df is None or pd.isnull(df['trade_date'].min()):
                edate = cd.preday(sdate)
            else:
                edate = cd.preday(cd.ymd2date(df['trade_date'].min()))
    cursor.close()
    conn.close()
예제 #3
0
def fetchstk(cursor, logger, table, tumethod, isql, stktc, sdate, edate,
             batch):
    while edate >= sdate:
        df = fetchtudata(table, tumethod, stktc, sdate.strftime('%Y%m%d'),
                         edate.strftime('%Y%m%d'))
        logger.debug('====SSSS====> %s ===>sd:%s ed:%s len(df):%s' %
                     (stktc, sdate, edate, len(df)))
        if len(df) > 0:
            cols = df.columns
            isql = tuh.geninssql(cols, table, isql)
            rowvs = []
            for index, row in df.iterrows():
                rowv = []
                for col in cols:
                    rowv.append(row[col])
                rowvs.append(rowv)
            emsgs = tuh.saveorupdate({
                'sql': isql,
                'vals': rowvs
            }, batch, cursor)
            for m in emsgs:
                logger.error(m)
            logger.debug(
                '====SAVEEND====> %s ===>sd:%s ed:%s ldf:%d mtd:%s emc:%d' %
                (stktc, sdate, edate, len(df), df['trade_date'].min(),
                 len(emsgs)))
        if len(df) < 1 or pd.isnull(df['trade_date'].min()):
            edate = cd.preday(sdate)
        else:
            edate = cd.preday(cd.ymd2date(df['trade_date'].min()))
예제 #4
0
def fetchdata(table):
    if table not in LOGGERMAP:
        logger = tul.TuLog('fetch_' + table + '_x', '/log', True).getlog()
        LOGGERMAP[table] = logger
    logger = LOGGERMAP.get(table)
    force = FORCEMAP.get(table, FORCE)
    fav = FAVMAP.get(table, FAV)
    batch = BATCHMAP.get(table, BATCH)
    conn = tuh.getMysqlConn()

    stktcs = tuh.getstktcs(fav)
    stkmtdm = tuh.getstkmtdm(table)

    tumethod = TUMETHODMAP[table]
    logger.debug(
        '====S====> table:%s tumethod:%s ===>force:%s fav:%s batch:%s' %
        (table, tumethod, force, fav, batch))

    cursor = conn.cursor()
    isql = ''
    for stktcm in stktcs:
        stktc = stktcm['ts_code']
        sdate = cd.ymd2date(tuh.getsdate(stkmtdm, stktc, force))
        edate = cd.preday()
        logger.debug('====SS====> %s ==== init ===>sd:%s ed:%s' %
                     (stktc, sdate, edate))
        fetchstk(cursor, logger, table, tumethod, isql, stktc, sdate, edate,
                 batch)
    cursor.close()
    conn.close()

    logger.debug('====E====> %s ============================' % (table, ))
예제 #5
0
def getIdxSdate(idxsdd, idxc, force=False):
    sdate = idxsdd.get(idxc, tuh.tuSdate)
    if sdate > tuh.tuSdate:
        dsdate = cd.ymd2date(sdate)
        dsdate = cd.preday(dsdate, -1)
        sdate = dsdate.strftime('%Y%m%d')
    if force or sdate < tuh.tuSdate:
        sdate = tuh.tuSdate
    return sdate
예제 #6
0
def fetchdataone():
    logger = LOGGER
    conn = tuh.getMysqlConn()

    stktcs = tuh.getstktcs(FAV)
    stkmtdm = tuh.getstkmtdm('stk_daily_basic')

    tuapi = tuh.tuApi
    isql = ''

    cursor = conn.cursor()
    for stktcm in stktcs:
        stktc = stktcm['ts_code']
        sdate = cd.ymd2date(tuh.getsdate(stkmtdm, stktc, FORCE))
        edate = cd.preday()
        while edate >= sdate:
            df = fetchtudata(tuapi, stktc, sdate.strftime('%Y%m%d'),
                             edate.strftime('%Y%m%d'))
            logger.debug('====SSSS====> %s ===>sd:%s ed:%s len(df):%s' %
                         (stktc, sdate, edate, len(df)))
            if df is not None and len(df) > 0:
                cols = df.columns
                isql = tuh.geninssql(cols, TABLE, isql)
                rowvs = []
                for index, row in df.iterrows():
                    rowv = []
                    for col in cols:
                        rowv.append(row[col])
                    rowvs.append(rowv)
                emsgs = tuh.saveorupdate({'sql': isql, 'vals': rowvs}, BATCH)
                for m in emsgs:
                    logger.error(m)
                logger.debug(
                    '====SAVEEND====> %s ===>sd:%s ed:%s ldf:%d mtd:%s emc:%d'
                    % (stktc, sdate, edate, len(df), df['trade_date'].min(),
                       len(emsgs)))
            if df is None or pd.isnull(df['trade_date'].min()):
                edate = cd.preday(sdate)
            else:
                edate = cd.preday(cd.ymd2date(df['trade_date'].min()))
    cursor.close()
    conn.close()
예제 #7
0
파일: tuhelper.py 프로젝트: sretof/tuala
def getmaxdate(maxvmap, gfield, defv=TUSDATE, force=False):
    if not maxvmap:
        maxd = defv
    else:
        maxd = maxvmap.get(gfield, defv)
    if maxd > defv:
        dmaxd = cd.preday(cd.ymd2date(maxd), -1)
        maxd = dmaxd.strftime('%Y%m%d')
    if force or maxd < defv:
        maxd = defv
    return maxd
예제 #8
0
파일: tuhelper.py 프로젝트: sretof/tuala
def getsdate(dmap, stkc, defsdate=TUSDATE, force=False):
    if not dmap:
        sdate = defsdate
    else:
        sdate = dmap.get(stkc, defsdate)
    if sdate > defsdate:
        dsdate = cd.ymd2date(sdate)
        dsdate = cd.preday(dsdate, -1)
        sdate = dsdate.strftime('%Y%m%d')
    if force or sdate < defsdate:
        sdate = defsdate
    return sdate
예제 #9
0
def fetchData(ind):
    logger = tul.TuLog('fetch_idx_' + ind + '_x', '/log').getlog()
    conn = tuh.getMysqlConn()
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    if __fav == 0 or __fav == 1:
        cursor.execute(
            "select t.ts_code from idx_basic t where t.del<>1 and t.fav=" +
            str(__fav) + " order by t.ts_code;")
    else:
        cursor.execute(
            "select t.ts_code from idx_basic t where t.del<>1 order by t.ts_code;"
        )
    idxcs = cursor.fetchall()
    cursor.close()

    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute(
        "select t.ts_code as idc,max(t.trade_date) as itd from idx_" + ind +
        " t group by t.ts_code;")
    idxsdd = tuh.listToDict(cursor.fetchall(), 'idc', 'itd')
    cursor.close()
    tuapi = tuh.tuApi
    isql = ''

    cursor = conn.cursor()
    for idxd in idxcs:
        idxc = idxd['ts_code']
        sdate = cd.ymd2date(getIdxSdate(idxsdd, idxc, __force))
        edate = cd.preday()
        while edate >= sdate:
            try:
                df = fetchTuData(tuapi, ind, sdate.strftime('%Y%m%d'),
                                 edate.strftime('%Y%m%d'), idxc)
                if len(df) > 0:
                    cols = df.columns
                    isql = tuh.genInsSql(cols, 'idx_' + ind, isql)
                    rowvs = []
                    for index, row in df.iterrows():
                        rowv = []
                        for col in cols:
                            rowv.append(row[col])
                        rowvs.append(rowv)
                    emsgs = tuh.insertDatas({'sql': isql, 'vals': rowvs})
                    for m in emsgs:
                        logger.error(m)
                    print(idxc, '===>sd:', sdate, ' ed:', edate, ' ldf:',
                          len(df), ' mtd:', df['trade_date'].min(), ' emc:',
                          len(emsgs))
                else:
                    print(idxc, '===>sd:', sdate, ' ed:', edate, ' ldf:',
                          len(df))
            except BaseException as e:
                print(e)
                time.sleep(60)
                continue
            if pd.isnull(df['trade_date'].min()):
                edate = cd.preday(sdate)
            else:
                edate = cd.preday(cd.ymd2date(df['trade_date'].min()))
    cursor.close()
    conn.close()