forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_schedule_plan.py
More file actions
69 lines (53 loc) · 2.06 KB
/
update_schedule_plan.py
File metadata and controls
69 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import yaml ### install the pyyaml package
from lookerapi import LookerApi
from datetime import datetime
from pprint import pprint
import json
import re
###############
# This script takes the schedule that's been made for a Look
# and updates it with the emails of users found in another Look
##############
### ------- HERE ARE PARAMETERS TO CONFIGURE -------
host = 'localhost'
### ------- OPEN THE CONFIG FILE and INSTANTIATE API -------
f = open('config.yml')
params = yaml.load(f)
f.close()
my_host = params['hosts'][host]['host']
my_secret = params['hosts'][host]['secret']
my_token = params['hosts'][host]['token']
looker = LookerApi(host=my_host,
token=my_token,
secret = my_secret)
# These Look IDs should be updated to match the Look ID of
# whatever the new reports will be.
author_report = 157
task_report = 158
# get emails from a Look(157) (Author Report)
print "Getting Look information"
emails = looker.get_look(author_report)
email_list = []
for email in emails:
for key, value in email.iteritems():
email_list.append(value)
print "Getting Schedule information from Look"
# get look plan ID from a Look(158).
look_plan_id = looker.get_look_schedule(task_report)[0]['id']
# Pass those emails into an update_schedule call using the above look_plan_id into below plan
# Loop through the email list, appending to and creating and concatenating the string to be used for the
# body of the API call in json form
i = 1
list_of_schedule_plan_destinations = ''
for email in email_list:
if i == len(email_list):
list_of_schedule_plan_destinations += ('{{"scheduled_plan_id": {},"format": "csv","address": "{}","type": "email"}}]}}'.format(look_plan_id,email))
else:
list_of_schedule_plan_destinations += ('{{"scheduled_plan_id": {},"format": "csv","address": "{}","type": "email" }},'.format(look_plan_id,email))
i += 1
start_string = '{"scheduled_plan_destination":['
full_body = start_string+list_of_schedule_plan_destinations
# Make the call
print "Updating Schedule"
looker.update_schedule(look_plan_id,body=full_body)
print "Done"