def updateSingleStockData(root_path, symbol, force_check):
    startTime = time.time()
    message = ""

    if len(symbol) == 0: return startTime, message

    till_date = (datetime.datetime.now()).strftime("%Y-%m-%d")
    end_date = pd.Timestamp(till_date)

    stockData, message = getSingleStock(symbol)
    if stockData.empty == False:
        storeStock(root_path, "DB_STOCK", "SHEET_CHN", "_MONTHLY", symbol,
                   stockData, 'monthly_update')
    return startTime, message
def updateSingleStockData(root_path, symbol, force_check):
    startTime = time.time()
    message = ""

    if len(symbol) == 0: return startTime, message

    till_date = (datetime.datetime.now()).strftime("%Y-%m-%d")
    end_date = pd.Timestamp(till_date)

    stockData, lastUpdateTime = queryStock(root_path, "DB_STOCK",
                                           "SHEET_CHN_DAILY", symbol)

    if stockData.empty:
        stockData, message = getSingleStock(symbol)
        if stockData.empty == False:
            storeStock(root_path, "DB_STOCK", "SHEET_CHN_DAILY", symbol,
                       stockData)
        return startTime, message

    modified = False
    first_date = pd.Timestamp(stockData.index[0])
    last_date = pd.Timestamp(stockData.index[-1])
    updateOnce = end_date > lastUpdateTime

    if end_date > last_date and (updateOnce or force_check):
        to_date = (last_date + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        if judgeNeedPostDownload(to_date, till_date):
            message = message + ", download post data from " + to_date + " to " + till_date
            moreStockData, tempMessage = getSingleStockByTime(
                symbol, to_date, till_date)
            message = message + tempMessage
            if len(moreStockData) > 0:
                if isinstance(moreStockData.index, pd.DatetimeIndex):
                    moreStockData.index = moreStockData.index.strftime(
                        "%Y-%m-%d")
                modified = True
                stockData = pd.concat([stockData, moreStockData])
                stockData.index.name = 'date'

    if modified:
        stockData = stockData[~stockData.index.duplicated(keep='first')]
        storeStock(root_path, "DB_STOCK", "SHEET_CHN_DAILY", symbol, stockData)
    elif updateOnce:
        stockData = stockData[~stockData.index.duplicated(keep='first')]
        storeStock(root_path, "DB_STOCK", "SHEET_CHN_DAILY", symbol, stockData)
        message = message + ", nothing updated"
    else:
        message = ""

    return startTime, message
def updateSingleStockData(root_path, symbol, from_date, till_date,
                          force_check):
    startTime = time.time()
    message = ""

    if len(symbol) == 0: return startTime, message

    now_date = pd.Timestamp((datetime.datetime.now()).strftime("%Y-%m-%d"))
    start_date = pd.Timestamp(from_date)
    end_date = pd.Timestamp(till_date)

    if end_date == now_date:
        end_date = end_date - datetime.timedelta(days=1)

    stockData, lastUpdateTime = queryStock(root_path, "DB_STOCK", "SHEET_US",
                                           "_WEEKLY", symbol, "weekly_update")

    if stockData.empty:
        stockData, message = getSingleStock(symbol, from_date, till_date)
        if stockData.empty == False:
            storeStock(root_path, "DB_STOCK", "SHEET_US", "_WEEKLY", symbol,
                       stockData, "weekly_update")
            first_date = pd.Timestamp(stockData.index[0])
            to_date = (first_date -
                       datetime.timedelta(days=1)).strftime("%Y-%m-%d")
            if judgeNeedPreDownload(root_path, symbol, first_date, from_date,
                                    to_date):
                storePublishDay(root_path, "DB_STOCK", "SHEET_US", symbol,
                                first_date.strftime("%Y-%m-%d"))
            message = message + ", database updated"
        else:
            print("get stock from network failed", symbol)
        return startTime, message

    modified = False
    savePublishDay = False

    first_date = pd.Timestamp(stockData.index[0])
    last_date = pd.Timestamp(stockData.index[-1])

    if start_date < first_date:
        to_date = (first_date -
                   datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        if judgeNeedPreDownload(root_path, symbol, first_date, from_date,
                                to_date):
            message = message + ", download pre data from " + from_date + " to " + to_date
            moreStockData, tempMessage = getSingleStock(
                symbol, from_date, to_date)
            message = message + tempMessage
            if len(moreStockData) > 0:
                if isinstance(moreStockData.index, pd.DatetimeIndex):
                    moreStockData.index = moreStockData.index.strftime(
                        "%Y-%m-%d")
                modified = True
                stockData = pd.concat([moreStockData, stockData])
                stockData.index.name = 'date'
            else:
                savePublishDay = True
                storePublishDay(root_path, "DB_STOCK", "SHEET_US", symbol,
                                first_date.strftime("%Y-%m-%d"))
                message = message + ", save stock publish(IPO) day, next time won't check it again"

    updateOnce = now_date > lastUpdateTime

    if (end_date > last_date) and (updateOnce or force_check):
        to_date = (last_date + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        if judgeNeedPostDownload(now_date, to_date, till_date):
            message = message + ", download post data from " + to_date + " to " + till_date
            moreStockData, tempMessage = getSingleStock(
                symbol, to_date, till_date)
            message = message + tempMessage
            if len(moreStockData) > 0:
                if isinstance(moreStockData.index, pd.DatetimeIndex):
                    moreStockData.index = moreStockData.index.strftime(
                        "%Y-%m-%d")
                modified = True
                stockData = pd.concat([stockData, moreStockData])
                stockData.index.name = 'date'

    if modified:
        stockData = stockData[~stockData.index.duplicated(keep='first')]
        storeStock(root_path, "DB_STOCK", "SHEET_US", "_WEEKLY", symbol,
                   stockData, "weekly_update")
    elif updateOnce:
        now_date = datetime.datetime.now().strftime("%Y-%m-%d")
        stockList = queryStockList(root_path, "DB_STOCK", "SHEET_US")
        if stockList[stockList.index == symbol]['daily_update'][0] != now_date:
            stockList.set_value(symbol, 'weekly_update', now_date)
            storeStockList(root_path, "DB_STOCK", "SHEET_US", stockList,
                           symbol)
    elif savePublishDay == False:
        message = ""
    return startTime, message