|
import numpy as np |
|
import pandas as pd |
|
import os |
|
import tqdm |
|
import gc |
|
import feather |
|
from google.cloud import storage |
|
|
|
PATH = os.getenv('TEMPORARY_DOWNLOADS_PATH', './downloads/') |
|
|
|
bucket_name = os.getenv('BUCKET_NAME', 'modeler-non-prod') |
|
|
|
os.makedirs(PATH, exist_ok=True) # succeeds even if directory exists. |
|
|
|
storage_client = storage.Client() |
|
bucket = storage_client.bucket(bucket_name) |
|
blobs = storage_client.list_blobs(bucket_name) |
|
|
|
for blob in blobs: |
|
if 'indicator' in blob.name and blob.name.endswith('.csv'): |
|
print('Indicator file found: ' + blob.name) |
|
|
|
filename = PATH + blob.name.replace('/', '_') |
|
feather_filename = os.path.splitext(filename)[0] + '.ftr' |
|
destination_blob_name = os.path.splitext(blob.name)[0] + '.ftr' |
|
|
|
destination_blob_exists = storage.Blob(bucket=bucket, name=destination_blob_name).exists(storage_client) |
|
if destination_blob_exists: |
|
print('Destination file already exists, skipping...') |
|
continue |
|
|
|
print('Downloading...') |
|
blob.download_to_filename(filename) |
|
|
|
print('Indicator file downloaded: ' + filename) |
|
print('Converting to feather file format...') |
|
|
|
df = pd.read_csv(filename) |
|
df.reset_index() |
|
df.to_feather(feather_filename) |
|
|
|
print('Indicator feather file created: ' + feather_filename) |
|
|
|
print('Uploading to bucket as \'' + destination_blob_name + '\'...') |
|
new_blob = bucket.blob(destination_blob_name) |
|
new_blob.upload_from_filename(feather_filename) |
|
print('File uploaded: ' + destination_blob_name) |
|
|
|
print('Deleting local files...') |
|
os.remove(filename) |
|
os.remove(feather_filename) |
|
|
|
print('Indicator file DONE! \n') |