"""Deezer flow api utilities.""" import gzip import logging import os import shutil from zipfile import is_zipfile from zipfile import ZipFile import requests logger = logging.getLogger(__file__) def repack_source_file(local_file_path, local_dir, source_files_dict): """Extract .zip archive locally and put file in gzip archive.""" with ZipFile(local_file_path, 'r') as myzip: myzip.extractall(local_dir) source_files = [] for root, _, files in os.walk(local_dir): for name in files: if name in source_files_dict['files']: source_files.append(os.path.join(root, name)) files = [] for source_file in source_files: gzip_file = '{}.gz'.format(source_file) files.append({ 'source_file_name': os.path.split(source_file)[1], 'gzip_file_name': os.path.split(gzip_file)[1], 'gzip_file': gzip_file}) with open(source_file, 'rb') as f_in: with gzip.open(gzip_file, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) return files def download_from_zephir(zephir_settings, file_name, local_dir): """Download file from deezer's zephir to local file system. Args: zephir_settings (dict): settings for connecting to zephir service. file_name (str): name of the file to be uploaded. local_dir (str): local destination directory where the file will be stored. """ _url_login = '{}/accounts/login/'.format(zephir_settings['host']) _url_file = '{host}{path}{file_name}'.format( host=zephir_settings['host'], path=zephir_settings['path'], file_name=file_name) local_file_path = os.path.join(local_dir, file_name) client = requests.session() logger.info('Session acquired') client.get(_url_login) # get csrf token login_data = dict( username=zephir_settings['username'], password=zephir_settings['password'], csrfmiddlewaretoken=client.cookies['csrftoken'], next='/' ) login_response = client.post(_url_login, data=login_data) logger.info('Logged in into zephir..') failed_text = 'Please enter a correct username and password' if failed_text in login_response.text: raise ValueError("Was not able to login Deezer's Zephir") login_response.raise_for_status() logger.info(f'Getting file: {_url_file}...') response = client.get(_url_file) try: response.raise_for_status() except requests.exceptions.HTTPError: FileNotFoundError('Report file was not found in Zephir service') with open(local_file_path, 'wb') as fp: fp.write(response.content) if not is_zipfile(local_file_path): raise FileNotFoundError('Zephir service have not returned a zip file') logger.info(f'Download comlete: {_url_file} saved to {local_file_path}') return local_file_path