-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
60 lines (48 loc) · 1.68 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import requests
import csv
import datetime
import os
import pandas as pd
from datetime import timedelta
import bizdays
from tqdm import tqdm
def check_download(dt_referencia, file_name):
if not isbizday(dt_referencia):
print(dt_referencia, 'não é dia útil')
return False
if os.path.exists(file_name):
print(file_name, 'arquivo já baixado')
return False
return True
def download_file(url, file_name):
response = requests.get(url, stream=True)
with open(file_name, "wb") as handle:
for data in tqdm(response.iter_content()):
handle.write(data)
handle.close()
def get_ultima_data_disponivel_base(path_file_base):
# verifica a última data disponívl na base
with open(path_file_base, 'r') as f:
for row in reversed(list(csv.reader(f))):
data = row[0].split(';')[0]
if data == 'Data':
return None
data = row[0].split(';')[0]
return datetime.datetime.strptime(data, '%d/%m/%Y').date()
def remove_old_files(start_name):
file_list = os.listdir(r"downloads")
for file_name in file_list:
if not file_name.startswith(start_name):
continue
today = datetime.datetime.now().strftime('%d.%m.%Y')
data_arquivo = file_name.split(start_name)[-1][-20:][0:10]
if today != data_arquivo:
os.remove(os.path.join('downloads', file_name))
def get_calendar():
holidays = bizdays.load_holidays('ANBIMA.txt')
return bizdays.Calendar(holidays, ['Sunday', 'Saturday'])
def isbizday(dt_referencia):
cal = get_calendar()
return cal.isbizday(dt_referencia)