From 372a25783a0eb07542cf36bbeca20d260763b6cb Mon Sep 17 00:00:00 2001 From: Doug Shore Date: Mon, 9 Oct 2023 16:19:05 -0400 Subject: [PATCH 1/2] Activate targeting values by network code, key id and names (#149) --- examples/bin/activate_targeting_value.py | 69 ++++++++++++++++++++++++ line_item_manager/config.py | 6 +-- 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 examples/bin/activate_targeting_value.py diff --git a/examples/bin/activate_targeting_value.py b/examples/bin/activate_targeting_value.py new file mode 100644 index 0000000..e67f14c --- /dev/null +++ b/examples/bin/activate_targeting_value.py @@ -0,0 +1,69 @@ +""" +Prompted activation of a targeting value by network code and targeting key name. +""" +import argparse +from pprint import pprint +from os import path + +from line_item_manager.config import config +from line_item_manager.gam_operations import client as gam_client +from line_item_manager.operations import AppOperations, TargetingValues + +class MyTargetingValue(AppOperations): + service = 'CustomTargetingService' + def activate(self) -> dict: + return self.svc().performCustomTargetingValueAction( + {'xsi_type': 'ActivateCustomTargetingValues'}, + self.statement().ToStatement()) # type: ignore[union-attr] + +def file_exists(filename: str) -> str: + if not path.exists(filename): + raise argparse.ArgumentTypeError('Private key file does not exist.') + return filename + +def is_yes(question: str, answer=None) -> bool: + while answer not in {'y', 'n'}: + answer = input(f"{question}? (y/n) ").lower() + return answer == 'y' + +def activate() -> None: + args = cli_args() + config.cli = dict( + network_code=args.network_code, + private_key_file=args.private_key_file + ) + config.set_client_factory(gam_client) + + for _v in args.names: + tgt_value = TargetingValues(key_id=args.targeting_key_id, name=_v).fetchone() + if not tgt_value: + print('Targeting value not found. Exiting.') + continue + if args.verbose: + pprint(tgt_value) + if tgt_value['status'] == 'ACTIVE': + print('Targeting value is already ACTIVE. Exiting.') + continue + print(f"Targeting value: id={tgt_value['id']}, name={tgt_value['name']}") + if is_yes('Activate', answer='y' if args.yes else None): + print('Activating...') + pprint(MyTargetingValue(id=[tgt_value['id']]).activate()) + print('Exiting.') + +def cli_args(): + parser = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('network_code', type=int, help='GAM network code.') + parser.add_argument('targeting_key_id', help='Targeting Key Id') + parser.add_argument('names', nargs='+', help='Targeting value names') + parser.add_argument('-k', '--private_key_file', default='gam_creds.json', type=file_exists, + help='Path to json GAM credentials file.') + parser.add_argument('-y', '--yes', action='store_true', help='Archive without prompting.') + parser.add_argument('-v', '--verbose', action='store_true', help='Show full object') + return parser.parse_args() + +def main() -> None: + activate() + +if __name__ == '__main__': + main() diff --git a/line_item_manager/config.py b/line_item_manager/config.py index b09c173..8265d81 100644 --- a/line_item_manager/config.py +++ b/line_item_manager/config.py @@ -90,7 +90,7 @@ def network_name(self) -> str: @property def schema(self) -> dict: if self._schema is None: - self._schema = load_file(self.cli['schema']) if self.cli['schema'] else \ + self._schema = load_file(self.cli['schema']) if self.cli.get('schema') else \ load_package_file('schema.yml') return self._schema @@ -139,13 +139,13 @@ def micro_amount(self, cpm: Union[str, float]) -> int: return int(float(cpm) * self.app['googleads']['line_items']['micro_cent_factor']) def template_src(self) -> str: - if self.cli['template']: + if self.cli.get('template'): with open(self.cli['template']) as fp: return fp.read() return read_package_file('line_item_template.yml') def settings_obj(self) -> dict: - if self.cli['settings']: + if self.cli.get('settings'): return load_file(self.cli['settings']) return load_package_file('settings.yml') From 1300c637e0d796a6456849fe46e885b28372fbf4 Mon Sep 17 00:00:00 2001 From: Doug Shore Date: Mon, 9 Oct 2023 16:20:07 -0400 Subject: [PATCH 2/2] Activate targeting values by network code, key id and names (#149) --- line_item_manager/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/line_item_manager/__init__.py b/line_item_manager/__init__.py index 19730a6..97951ed 100644 --- a/line_item_manager/__init__.py +++ b/line_item_manager/__init__.py @@ -5,7 +5,7 @@ __version__ = '0.2.11' # For an official release, use dev_version = '' -dev_version = '' +dev_version = '1' version = __version__ if dev_version: