Exemple #1
0
class PivNetDownloader:
    def __init__(self, api_key):
        self.token = api_key
        self.secure_url = end_point
        self.headers = {"content-type": "application/json"}
        self.secure_headers = {
            "content-type": "application/json",
            "Accept": "application/json",
            "Authorization": "Token token=" + self.token,
        }
        self.database = Database()

    def download_files(self, file_name, download_path):
        file = open(file_name)

        for line in file.readlines():
            items = []
            for x in line.split("  "):
                if len(x) > 1:
                    items.append(x)
            name = items[0]
            release_version = items[1]
            file_name = items[2].strip()

            product_id, slug = self.database.get_product_details(name)
            # print('slug=%s,version=%s,file=%s'%(slug,release_version,file_name))
            data = self.database.get_release_id(slug, release_version)
            print(data)
            if data:
                release_id = data[0]
                file_data = self.database.get_file_id(release_id, file_name)
                if file_data:
                    file_id = file_data[0]
                    # print('product id =%s,release=%s,file_name=%s'%(product_id,release_id,file_name))
                    self.acceptEULA(product_id, release_id)

                    self.downloadFile(product_id, release_id, file_id, file_name, download_path)

    def acceptEULA(self, product_id, release_id):
        url = (
            self.secure_url
            + "/api/v2/products/"
            + str(product_id)
            + "/releases/"
            + str(release_id)
            + "/eula_acceptance"
        )
        r = requests.post(url, headers=self.secure_headers, proxies=proxies)
        return r

    def downloadFile(self, product_id, release_id, file_id, file_name, download_path):
        if not os.path.exists("product_files"):
            os.makedirs("product_files")
        url = (
            self.secure_url
            + "/api/v2/products/"
            + str(product_id)
            + "/releases/"
            + str(release_id)
            + "/product_files/"
            + str(file_id)
            + "/download"
        )
        # print(url)
        r = requests.post(url, headers=self.secure_headers, stream=True, proxies=proxies)
        # print(download_path)
        print("Going to download %s from %s" % (file_name, url))
        full_path = os.path.join(download_path, file_name)
        with open(full_path, "wb") as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)
                    f.flush()
        return file_name

    def unzipper(self, file_name):
        path = "product_files"
        subdir = file_name[:-8]  # remove '.pivotal'
        if not os.path.exists("product_files/" + subdir):
            os.makedirs("product_files/" + subdir)
        with zipfile.ZipFile("product_files/" + file_name, "r") as z:
            z.extractall("product_files/" + subdir)
        return subdir