Create download.py

This commit is contained in:
manhduonghn 2023-08-20 17:16:17 +07:00 committed by GitHub
parent 0744268263
commit 541dd9d665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 0 deletions

33
src/utils/download.py Normal file
View File

@ -0,0 +1,33 @@
import requests
import sys
from loguru import logger
from configparser import ConfigParser
logger.add(sys.stdout, level="INFO")
class Downloader:
def __init__(self, url_file, outfile):
self.urls = self.read_urls_from_ini(url_file)
self.outfile = outfile
def read_urls_from_ini(self, url_file):
config = ConfigParser()
config.read(url_file)
urls = []
for section in config.sections():
for key in config.options(section):
urls.append(config.get(section, key))
return urls
def download_hosts(self):
for url in self.urls:
response = requests.get(url)
if response.status_code == 200:
logger.info(f"Successfully downloaded URL: {url}")
with open(self.outfile, "a") as file:
file.write(response.text)
else:
logger.error(f"Failed to download URL: {url}, Response code: {response.status_code}")