forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_expired_schedules.py
67 lines (48 loc) · 1.76 KB
/
delete_expired_schedules.py
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
import yaml ### install the pyyaml package
from lookerapi import LookerApi
from datetime import datetime
from pprint import pprint
import json
import re
###############
# This script builds a list of all schedules for all users
# and deletes schedules that have an expiry date specified
# in the title. Checks for format "DELETE:YYYY-MM-DD" in
# title, which can be changed in line 58
##############
### ------- HERE ARE PARAMETERS TO CONFIGURE -------
look_to_get = 123
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)
### ------- GET ALL USERS -------
userlistarray = looker.get_all_users()
userlist = []
for user in userlistarray:
userlist.append(user['id'])
### ------- GET ALL SCHEDULE ID/TITLES -----
scheduleattrlist = []
for user in userlist:
data = looker.get_all_schedules(user)
for schedule in data:
scheduleattrlist.append({'id': schedule['id'],'title': schedule['title'] if schedule['title'] is not None else ""})
pprint(scheduleattrlist)
### ------- PARSE DATE IN TITLE AND DELETE EXPIRED SCHEDULES -----
for attrpair in scheduleattrlist:
deletematch = re.search(r'DELETE\:\d{4}-\d{2}-\d{2}', attrpair['title'])
if deletematch is None:
break
match = re.search(r'\d{4}-\d{2}-\d{2}', deletematch.group(0))
if match is not None and datetime.strptime(match.group(0), '%Y-%m-%d').date() < datetime.today().date():
print "FOUND EXPIRED SCHEDULE...DELETING"
looker.delete_schedule(attrpair['id'])
### ------- Done -------
print "Done"