Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aprimoramento do script para downloads paralelos com aumento de velocidade e monitoramento de progresso #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions dados_cnpj_baixa.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,71 @@
lista relação de arquivos na página de dados públicos da receita federal
e faz o download
"""

from bs4 import BeautifulSoup
import requests, wget, os, sys, time, glob
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm

url = 'https://www.gov.br/receitafederal/pt-br/assuntos/orientacao-tributaria/cadastros/consultas/dados-publicos-cnpj'
url = 'http://200.152.38.155/CNPJ/'

pasta_compactados = r"dados-publicos-zip" # local dos arquivos zipados da Receita

pasta_compactados = r"dados-publicos-zip" #local dos arquivos zipados da Receita

if len(glob.glob(os.path.join(pasta_compactados,'*.zip'))):
if len(glob.glob(os.path.join(pasta_compactados, '*.zip'))):
print(f'Há arquivos zip na pasta {pasta_compactados}. Apague ou mova esses arquivos zip e tente novamente')
sys.exit()

page = requests.get(url)
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data)
soup = BeautifulSoup(data, 'html.parser')
lista = []
print('Relação de Arquivos em ' + url)
for link in soup.find_all('a'):
if str(link.get('href')).endswith('.zip'):
if str(link.get('href')).endswith('.zip'):
cam = link.get('href')
# if cam.startswith('http://http'):
# cam = 'http://' + cam[len('http://http//'):]
if not cam.startswith('http'):
print(url+cam)
lista.append(url+cam)
print(url + cam)
lista.append(url + cam)
else:
print(cam)
lista.append(cam)

resp = input(f'Deseja baixar os arquivos acima para a pasta {pasta_compactados} (y/n)?')
if resp.lower()!='y' and resp.lower()!='s':
if resp.lower() != 'y' and resp.lower() != 's':
sys.exit()

def bar_progress(current, total, width=80):
if total>=2**20:
tbytes='Megabytes'
unidade = 2**20
if total >= 2 ** 20:
tbytes = 'Megabytes'
unidade = 2 ** 20
else:
tbytes='kbytes'
unidade = 2**10
progress_message = f"Baixando: %d%% [%d / %d] {tbytes}" % (current / total * 100, current//unidade, total//unidade)
# Don't use print() as it will print in new line every time.
tbytes = 'kbytes'
unidade = 2 ** 10
progress_message = f"Baixando: {current / total * 100:.2f}% [{current // unidade} / {total // unidade}] {tbytes}"
sys.stdout.write("\r" + progress_message)
sys.stdout.flush()

for k, url in enumerate(lista):
print('\n' + time.asctime() + f' - item {k}: ' + url)
wget.download(url, out=os.path.join(pasta_compactados, os.path.split(url)[1]), bar=bar_progress)

print('\n\n'+ time.asctime() + f' Finalizou!!! Baixou {len(lista)} arquivos.')

def download_file(url, pbar):
filename = os.path.join(pasta_compactados, os.path.split(url)[1])
wget.download(url, out=filename, bar=bar_progress)
pbar.update(1)
print("\n" + os.path.basename(url) + " baixado com sucesso.")

start_time = time.time()

# Cria a barra de progresso geral
with tqdm(total=len(lista), desc="Progresso Geral", unit="arquivo") as pbar:
# Utiliza ThreadPoolExecutor para fazer o download em paralelo
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(download_file, url, pbar) for url in lista]
for future in futures:
future.result()

end_time = time.time()
print('\n\nFinalizou!!!')
print(f'Baixou {len(lista)} arquivos.')
print(f'Tempo total: {end_time - start_time:.2f} segundos.')

#lista dos arquivos
'''
Expand Down