Beispiel #1
0
    company['CODNEG'] = quote['CODNEG']
    company['PRECO'] = preco
    company['ESPECI'] = quote['ESPECI']
    company['P/L'] = divSafe(preco, company['LPA'])
    company['P/VPA'] = divSafe(preco, company['VPA'])
    return company

def sumarizeAll():
    logging.info('Summarizing Quotes')
    for quote in getQuotes():
        try:
            saveCompany(sumarize(quote))
        except Exception as error:
            logging.error('Exception summarizing quote: %s', error)

def saveCompany(company):
    logging.info('Saving company id %s', company['id'])

    companyid = '{0}-{1}-{2}'.format(company['id'], company['CODNEG'], company['FILEDT']) 
    es.index(index=INDEX_HISTORY, id=companyid, doc_type='company', body=company)

    companyid = '{0}-{1}-{2}'.format(company['id'], company['CODNEG'], company['period'])
    es.index(index=INDEX_COMPANY, id=companyid, doc_type='company', body=company)

if __name__ == '__main__':
    configureLog()
    es = Elasticsearch([URL_ES])
    normalizeFds()
    sumarizeAll()
    logging.info('Done, exiting...')
Beispiel #2
0
        logging.warning('CODNEG not found, ignoring quote %s', quote)
        return
    es.index(index=INDEX_QUOTE, id=quote['CODNEG'], doc_type=quote['TIPREG'], body=quote)

def downloadBdiFile(dt):
    fileurl = '{0}bdi{1}.zip'.format(URL_BDI, dt.strftime('%m%d')) 
    logging.info('Downloading file %s', fileurl)

    # save file
    fp = StringIO(openUrl(fileurl, True))    
    z = zipfile.ZipFile(fp)
    for name in z.namelist():
        yield z.open(name)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--date", dest="date", 
        type=lambda s: datetime.strptime(s, '%Y%m%d'), 
        help="Set the quote date to get")
    configureLog(parser)
    args = parser.parse_args() 
    
    dt = date.today() - timedelta(days=1) if args.date is None else args.date
    for bdiFile in downloadBdiFile(dt):
        for quote in parseFileLike(bdiFile):
            quote['FILEDT'] = dt
            quote['REGDT'] = datetime.now() 
            saveQuotes(quote)

    logging.info('Done, exiting...')