"""Snowflake connector class for MRC tasks.""" import datetime from snowflake_connector.etl_connector import SQLLoader from snowflake_connector.etl_connector import SnowflakeSQLExecutor import config # Load SQL templates sql_loader = SQLLoader(__file__) looker_sql_loader = SQLLoader(__file__, folder='/looker_queries') class YouTubeStatsSFExecutor(SnowflakeSQLExecutor): """Helper class to abstract Snowflake operations.""" @property def stats_name(self): """Name of the stats. Should match dir name of this stats, stats_name in config.py of a statistics. Returns: str: Stats name, e.g. 'youtube_statistics'. """ return config.STATS_NAME @property def video_statistics_table(self): """Name of the artist_to_track table for the statistics. Returns: str: {stats}_artist_to_track table name. """ return config.snowflake_table_names['video_statistics'] def create_video_statistics_table(self): """Create artists links table.""" params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=self.video_statistics_table) return self.fetchone_query(sql_loader, 'create_video_statistics', params) def insert_video_statistics_table(self, video_link, stats: dict): """First youtube statistics insertion.""" params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], table_name=self.video_statistics_table, video_id=stats.get('video_id'), video_title=stats.get('video_title'), video_full_link=video_link, description=stats.get('description'), author=stats.get('author'), channel_id=stats.get('channel_id'), views=stats.get('views'), likes=stats.get('likes'), dislikes=stats.get('dislikes'), comments_count=stats.get('comments_count'), comments=stats.get('comments'), processing_date=datetime.datetime.now().strftime("%d-%m-%Y") ) return self.fetchall_query(sql_loader, 'insert_video_statistics', params)