Skip to content

Instantly share code, notes, and snippets.

@maurobaraldi
Last active August 11, 2024 20:31
Show Gist options
  • Save maurobaraldi/40ec1daf0005530dd94a to your computer and use it in GitHub Desktop.
Save maurobaraldi/40ec1daf0005530dd94a to your computer and use it in GitHub Desktop.
Cotações da Bovespa Intraday
#!/usr/bin/env python
from datetime import datetime
from json import loads
from time import gmtime, mktime, strptime
# LevelDict é um wrapper usando dicionário para LevelDB
# https://github.com/maurobaraldi/leveldict
from leveldict import LevelJsonDict
from requests import get
base = 'http://cotacoes.economia.uol.com.br/ws/asset'
assets = base + '/stock/list?size=10000'
intraday = base + '/{asset}/intraday?size=400&callback=uolfinancecallback0'
assets = {i['code']: i['idt'] for i in get(assets).json()['data']}
# 3 ativos para teste
# assets = {'PETR4.SA': 484, 'CTAX4.SA': 227, 'IGUA3.SA': 364}
db = LevelJsonDict('./intraday')
def from_ts(ts):
''' Convert timestamp (13 digits support) to datetime'''
return datetime.fromtimestamp(mktime(gmtime(ts / 1000.0)))
def to_ts(dt):
''' From strftime to timestamp (13 digits support)'''
return int(mktime(strptime(dt, "%Y-%m-%d %H:%M")) * 1000)
def get_intraday(asset):
url = intraday.format(**{'asset': asset})
return loads(get(url).content[20:-2])
if __name__ == '__main__':
for asset, code in assets.iteritems():
today = datetime.now().strftime('%Y%m%d')
quote = get_intraday(code).get('data', {})
db.setdefault(asset)
db[asset] = {today: quote}
@ZotInfo
Copy link

ZotInfo commented Aug 2, 2021

https://api.cotacoes.uol.com/asset/list/?format=JSON&fields=id,abbreviation,name,companies,exchange

Alguém sabe qual o nome do "fields" para mostrar o tipo: Ações, FII, ETF, etc.???
ps: o Type "S" significa o que?

Também gostaria de saber os fields disponíveis para essa API

@isaacangello
Copy link

consegui dar uma ratreada e saber mas campos
https://api.cotacoes.uol.com/currency/intraday/list?currency=1&fields=bidvalue,askvalue,date,variationpercentbid
bidvalue = valor de venda
askvalue = valor de compra ()
variationpercentbid = variação da moeda (importante pra saber se a moeda tá subindo ou descendo)

@isaacangello
Copy link

mas alguém sabe como resolver esse erro aqui ?
"has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment