def ensure_data_directory(relative_path: Path = None) -> Path: """Checks if a directory in the data dir path exists. Creates it if necessary Args: relative_path: A Path object pointing to a file relative to the data directory Returns: The absolute path Path object """ if relative_path is None: return Path(config.data_dir()) try: path = Path(config.data_dir(), relative_path) # if path points to a file, create parent directory instead if path.suffix: if not path.parent.exists(): path.parent.mkdir(exist_ok=True, parents=True) else: if not path.exists(): path.mkdir(exist_ok=True, parents=True) return path except OSError as exception: if exception.errno != errno.EEXIST: raise
def ensure_data_directory(relative_path: Path = None ) -> Path: """Checks if a directory in the data dir path exists. Creates it if necessary Args: relative_path: A Path object pointing to a file relative to the data directory Returns: The absolute path Path object """ if relative_path is None: return Path(config.data_dir()) try: path = Path(config.data_dir(), relative_path) # if path points to a file, create parent directory instead if path.suffix: if not path.parent.exists(): path.parent.mkdir(exist_ok=True, parents=True) else: if not path.exists(): path.mkdir(exist_ok=True, parents=True) return path except OSError as exception: if exception.errno != errno.EEXIST: raise def parse_labels(labels: [{}] ) -> {str: str}: """Extracts labels from a string. Args: labels: Labels in the form of [{"id": "1", "name": "{key_1=value_1}"}, {"id": "2", "name": "{key_2=value_2}"}]"' Returns: A dictionary of labels with {key_1 : value_1, ...} format """ labels_dict = {} for label in labels: match = re.search("{([a-zA-Z|_]+)=([a-zA-Z|_]+)}", label['name']) if match: key = match.group(1).strip().lower().title() value = match.group(2).strip() labels_dict[key] = value return labels_dict @rate_limiting def _get_ad_accounts() -> [adaccount.AdAccount]: """Retrieves the ad accounts of the user whose access token was provided and returns them as a list. Returns: A list of ad accounts """ system_user = user.User(fbid='me') ad_accounts = system_user.get_ad_accounts(fields=['account_id', 'name', 'created_time', 'timezone_offset_hours_utc']) return list(ad_accounts) def _upsert_ad_performance(ad_insights: [adsinsights.AdsInsights], con : sqlite3.Connection): """Creates the ad performance table if it does not exists and upserts the ad insights data afterwards Args: ad_insights: A list of Insights objects con: A sqlite database connection """ con.execute(""" CREATE TABLE IF NOT EXISTS ad_performance ( date DATE NOT NULL, ad_id BIGINT NOT NULL, device TEXT NOT NULL, performance TEXT NOT NULL, PRIMARY KEY (ad_id, device) );""") con.executemany("INSERT OR REPLACE INTO ad_performance VALUES (?,?,?,?)", _to_insight_row_tuples(ad_insights)) def _to_insight_row_tuples(ad_insights: [adsinsights.AdsInsights] ) -> Generator[tuple, None, None]: """Transforms the Insights objects into tuples that can be directly inserted into the ad_performance table Args: ad_insights: A list of Insights objects for an ad on a specific day Returns: A list of tuples of ad performance data """ for ad_insight in ad_insights: actions = ad_insight.get('actions') or [] actions = [_floatify_values(action) for action in actions] action_values = ad_insight.get('action_values') or [] action_values = [_floatify_values(action_value) for action_value in action_values] performance = {'impressions': int(ad_insight['impressions']), 'spend': float(ad_insight['spend']), 'actions': actions, 'action_values': action_values} ad_insight_tuple = (ad_insight['date_start'], ad_insight['ad_id'], ad_insight['impression_device'], json.dumps(performance)) yield ad_insight_tuple def _floatify(value: str ) -> Union[str, float]: try: return float(value) except ValueError: return value def _floatify_values(inp: {} ) -> {}: return {key: _floatify(value) for key, value in inp.items()} def _first_download_date_of_ad_account(ad_account: adaccount.AdAccount ) -> datetime.date: """Finds the first date for which the ad account's performance should be downloaded by comparing the first download date from the configuration and the creation date of the account and returning the maximum of the two. Args: ad_account: An ad account to download Returns: The first date to download the performance data for """ config_first_date = datetime.datetime.strptime(config.first_date(), '%Y-%m-%d').date() if 'created_time' in ad_account: account_created_date = datetime.datetime.strptime(ad_account['created_time'], "%Y-%m-%dT%H:%M:%S%z").date() return max(config_first_date, account_created_date) else: return config_first_date