""" Module used to download a given webpage with several options related to the wikimedia API """ import urllib.request # For system proxy import os class Downloader(): """Class used to download a given webpage considering system proxy""" def __init__(self): self.proxy_address = os.environ.get("HTTP_Proxy") self.proxy = urllib.request.ProxyHandler({'http': self.proxy_address}) self.opener = urllib.request.build_opener(self.proxy) urllib.request.install_opener(self.opener) def download(self, url): """ Download the given URL and return the source code """ return urllib.request.urlopen(url).read().decode("utf8") def download_in_file(self, url, output_file_path): """ Download the given URL and write to the given file """ with open(output_file_path, "w") as output_file: output_file.write(self.download(url))