summaryrefslogtreecommitdiff
path: root/src/downloader/downloader.py
blob: 740caf8ba01897441a802972a0916648586dbc24 (plain)
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
"""
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))