Skip to content

Commit

Permalink
Activate targeting values by network code, key id and names (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
dshore committed Oct 9, 2023
1 parent 70a2b59 commit 372a257
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
69 changes: 69 additions & 0 deletions examples/bin/activate_targeting_value.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 3 additions & 3 deletions line_item_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit 372a257

Please sign in to comment.