from typing import List import snowflake.connector import config import json from utils.snowflake.gras_data.queries import ArtistMomentsQuery from utils.snowflake.gras_data.models import GRASArtistMomentModel from utils.snowflake.gras_data.constants import MomentTypes class ArtistMomentsImporter: def __init__(self): self.snowflake_connector = snowflake.connector.connect( user=config.SNOWFLAKE_USER, password=config.SNOWFLAKE_PASS, account=config.SNOWFLAKE_ACCOUNT, database=config.PROJECTS_SNOWFLAKE_DB, schema=config.PROJECTS_SNOWFLAKE_SCHEMA, warehouse=config.SNOWFLAKE_WAREHOUSE, ) def fetch(self, gras_ids: List[str]) -> List[GRASArtistMomentModel]: with self.snowflake_connector as con: data = con.cursor().execute(ArtistMomentsQuery.moments_queries(gras_ids)).fetchall() return self.__map_moments(data) def __map_moments(self, sf_moments: List[tuple]) -> List[GRASArtistMomentModel]: moments = [] for moment in sf_moments: moment_type = moment[3] moment_date = json.loads(moment[2]) if moment_type == MomentTypes.PERSONAL_DEATH_BIRTH.value: if moment_date.get("death", None): moments.append(GRASArtistMomentModel(moment=moment, moment_type=MomentTypes.DEATH.value)) if moment_date.get("birth", None): moments.append(GRASArtistMomentModel(moment=moment, moment_type=MomentTypes.BIRTH.value)) else: moments.append(GRASArtistMomentModel(moment=moment, moment_type=moment_type)) return moments