Skip to content
Snippets Groups Projects
download.py 1.70 KiB
"""Implementation of download method."""
import os
import xml.etree.ElementTree as ET
import re

import requests
from six.moves.urllib.request import urlopen
from six.moves import configparser


config = configparser.ConfigParser()
config_path = os.path.join(os.path.dirname(
    os.path.abspath(__file__)), "config.ini")
config.read(config_path)


def get_available_models():
    """Returns available models."""
    root = ET.parse(urlopen("https://minio.clarin-pl.eu/public")).getroot()
    available_models = []
    for child in root.findall(
            "{http://s3.amazonaws.com/doc/2006-03-01/}Contents"):
        if "models/plwn_api_dumps/" in str(
                child.find(
                    "{http://s3.amazonaws.com/doc/2006-03-01/}Key").text):
            string = child.find(
                "{http://s3.amazonaws.com/doc/2006-03-01/}Key").text
            substring = r"models/plwn_api_dumps/"
            available_models.append(re.sub(substring, r'', string))
    return available_models


def download(name="default_model"):
    """After called it downloads a specified database model."""
    models = get_available_models()
    if name == "default_model":
        url = config["DOWNLOAD"][name]
        r = requests.get(url)
        with open(name, "wb") as f:
            f.write(r.content)
            f.close()
        return
    if name in models:
        url = config["DOWNLOAD"]["default_model"]
        url = url.replace("plwn_dump_new_07-12-2022.sqlite", name)
        r = requests.get(url)
        with open(name, "wb") as f:
            f.write(r.content)
            f.close()
    else:
        print("Cannot download: ",
              name,
              "\n Possible download options: ",
              models)