"""Model Sample. This is just a model sample. It depends on which database you want to use but basically make sure this file only contains methods and classes that are related to this model. """ from owsrequest import request from owsresponse import response from socials.connectors import snowflake from socials.utils.format import format_row SQLLoader = snowflake.SQLLoader(__file__) SOCIAL_STATS_COLUMN_NAMES = ['followers', 'popularity', 'monthlyListeners'] SOCIAL_ACCOUNT_STATS_COLUMN_NAMES = [ 'platform', 'url', 'followers', 'popularity', 'monthlyListeners', 'fanConversionPercentage' ] SOCIAL_ACCOUNT_STATS_PLATFORMS = { 'SPOTIFY': 'spotify', 'DEEZER': 'deezer', 'FACEBOOK': 'facebook', 'INSTAGRAM': 'instagram', 'TWITTER': 'twitter', 'YOUTUBE': 'youtube', 'SOUNDCLOUD': 'soundcloud', 'TIKTOK': 'tiktok' } SOCIAL_STATS_FOR_MULTIPLE_ACCOUNTS_COLUMNS = [ 'chartmetricArtistId', 'spotifyMonthlyListeners', 'instagramFollowers', 'tiktokFollowers', 'spotifyFollowers', 'facebookFollowers', 'youtubeFollowers', 'twitterFollowers', 'soundcloudFollowers', 'deezerFollowers', ] def get_stats(account_id): """Get the participant for an account_id, given the lower bound timestamp. Args: model_id (int): the id of the model. Returns: response.Response: the data of the model. """ params = {'account_id': account_id} sql = SQLLoader.load_query('stats') row = snowflake.fetchone(sql, params) return {c: v for c, v in zip(SOCIAL_STATS_COLUMN_NAMES, row)} if row else {} def get_stats_by_account(account_id, platform): """Get the account stats for a cm account_id, given the lower bound timestamp. Args: model_id (int): the id of the model. Returns: response.Response: the data of the model. """ params = {'account_id': account_id} sql_query = 'stats_by_account' if (platform): platform = SOCIAL_ACCOUNT_STATS_PLATFORMS.get(platform) sql_query = 'stats_by_account_by_platform_filter' params = {**params, 'platform': platform} sql = SQLLoader.load_query(sql_query) rows = snowflake.fetchall(sql, params) return [ {c: v for c, v in zip(SOCIAL_ACCOUNT_STATS_COLUMN_NAMES, format_row(row))} for row in rows ] def get_stats_by_account_url(account_url: str): """Get the account stats for account_url, given the lower bound timestamp. Args: account_url (str): url of social account Returns: response.Response: the data of the model. """ params = {'account_url': account_url} sql = SQLLoader.load_query('stats_by_url') row = snowflake.fetchone(sql, params) return {c: v for c, v in zip(SOCIAL_ACCOUNT_STATS_COLUMN_NAMES, row)} if row else {} def get_stats_by_artists_ids(artists_ids): """Get the participants for artists_ids. Args: artists_ids (list(int)): the ids of the artists. Returns: response.Response: the stats data for the given artists ids. """ params = {'artists_ids': artists_ids} sql = SQLLoader.load_query('stats_by_artists_ids') data = snowflake.fetchall(sql, params) return [{c: v for c, v in zip(SOCIAL_STATS_FOR_MULTIPLE_ACCOUNTS_COLUMNS, row)} for row in data]