Created
October 2, 2022 09:22
-
-
Save wilsonfreitas/338319c06f131e77261c115b0ac7a844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class B3FilesURLDownloader(SingleDownloader): | |
calendar = bizdays.Calendar.load('ANBIMA.cal') | |
def download(self, refdate=None): | |
filename = self.attrs.get('filename') | |
refdate = refdate or self.get_refdate() | |
logging.info('refdate %s', refdate) | |
date = refdate.strftime('%Y-%m-%d') | |
url = f'https://arquivos.b3.com.br/api/download/requestname?fileName={filename}&date={date}&recaptchaToken=' | |
res = requests.get(url) | |
msg = 'status_code = {} url = {}'.format(res.status_code, url) | |
logg = logging.warn if res.status_code != 200 else logging.info | |
logg(msg) | |
if res.status_code != 200: | |
return None, None, res.status_code, refdate | |
ret = res.json() | |
url = f'https://arquivos.b3.com.br/api/download/?token={ret["token"]}' | |
verify_ssl = self.attrs.get('verify_ssl', True) | |
fname, temp_file, status_code, res = download_url(url, verify_ssl=verify_ssl) | |
if res.status_code != 200: | |
return None, None, res.status_code, refdate | |
f_fname = self.get_fname(fname, refdate) | |
logging.info('Returned from download %s %s %s %s', f_fname, temp_file, status_code, refdate) | |
return f_fname, temp_file, status_code, refdate | |
def get_refdate(self): | |
offset = self.attrs.get('offset', 0) | |
refdate = self.calendar.offset(self.now, offset) | |
refdate = datetime(refdate.year, refdate.month, refdate.day) | |
refdate = pytz.timezone('America/Sao_Paulo').localize(refdate) | |
return refdate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment