-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_all.py
250 lines (201 loc) · 11 KB
/
delete_all.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Copyright (c) 2020 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
import sys
import os
import sqlite3
import requests
from lxml import etree
import argparse
import datetime
db = sqlite3.connect('recordings.sqlite')
cursor = db.cursor()
from config import siteID ,userID ,userPW, webServiceP, output_path
# Webex NBR API URL
vaNBRstor = webServiceP+'/NBRStorageService'
vaNBRsvc = webServiceP+'/nbrXMLService'
etLstRecording = etree.parse('wbx.LstRecording.xml').getroot()
etNBRRecordIdList = etree.parse('wbx.getNBRRecordIdList.xml').getroot()
etStorageAccessTicket = etree.parse('wbx.getStorageAccessTicket.xml').getroot()
etDlNbrStorageFile = etree.parse('wbx.downloadNBRStorageFile.xml').getroot()
#structures for listing conf ids and extractin what is needed for deletion
etgetNBRConfIdList=etree.parse('wbx.getNBRConfIdList.xml').getroot()
etGetNBRStorageFile=etree.parse('wbx.GetNBRStorageFile.xml').getroot()
etdeleteNBRStorageFile=etree.parse('wbx.deleteNBRStorageFile.xml').getroot()
etMeetingTicket = etree.parse('wbx.getMeetingTicket.xml').getroot()
# initializing ConfIDList and recordingConfIDs to empty
confIDList=[]
recordingConfIDs={}
toDeleteRecordingsPaths={}
missingRecordingFilesPaths={}
stXMLheaders = {'Content-Type': 'text/xml'}
stSOAPheaders = {'Content-Type': 'text/xml', 'SOAPAction': ""}
parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8')
def isRecFileInRange(theFilename,fromDate,toDate):
#extract date from the filename
filename_without_time_and_extension = theFilename[:theFilename.rfind('_')]
just_the_date = filename_without_time_and_extension[-8:]
strFromDate=''
strToDate=''
#initialize return variable to true in case there is no date range defined
boolDateInRange = True
#set up which arguments where used
if fromDate != None:
strFromDate = fromDate.strftime('%Y%m%d')
if toDate != None:
strToDate = toDate.strftime('%Y%m%d')
#determine if date is in the range
if strFromDate != '' and strToDate != '':
boolDateInRange=just_the_date>=strFromDate and just_the_date<=strToDate
elif strFromDate != '':
boolDateInRange=just_the_date >= strFromDate
elif strToDate != '':
boolDateInRange = just_the_date<=strToDate
return boolDateInRange
if __name__ == "__main__":
#evaluate and store script arguments
argsparser = argparse.ArgumentParser()
argsparser.add_argument('-i', '--interactive', action='store_true', help="Make the script interactive")
argsparser.add_argument('-f', "--fromdate",
help="The From Date - format YYYY-MM-DD",
required=False,
type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'))
argsparser.add_argument('-t', "--todate",
help="The To Date format YYYY-MM-DD (Inclusive)",
required=False,
type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'))
args = argsparser.parse_args()
runInteractive = args.interactive
fromDate = args.fromdate
toDate = args.todate
# Get Meeting session Ticket
etMeetingTicket[1][0][0].text = siteID
etMeetingTicket[1][0][1].text = userID
etMeetingTicket[1][0][2].text = userPW
etMeetingTicket[1][0][3].text = 'MC'
rMeetingTicket = requests.post(vaNBRsvc, data=etree.tostring(etMeetingTicket), headers=stSOAPheaders)
rSATxml = etree.fromstring(rMeetingTicket.text.encode('utf-8'), parser=parser)
sessionSAT = rSATxml[0][0][0].text
# call getNBRConfIdList to get the list of conferences that have recordings, store in confIDList
etgetNBRConfIdList[0][0][0].text = siteID
etgetNBRConfIdList[0][0][1].text = sessionSAT
rgetNBRConfIdList = requests.post(vaNBRsvc, data=etree.tostring(etgetNBRConfIdList), headers=stSOAPheaders)
#print("rgetNBRConfIdList= ", rgetNBRConfIdList)
docgetNBRConfIdList = etree.fromstring(rgetNBRConfIdList.text.encode('utf-8'), parser=parser)
#print("docgetNBRConfIdList= ", docgetNBRConfIdList)
for tag in docgetNBRConfIdList.iter():
if not len(tag) and tag.text is not None:
confIDList.append(tag.text)
#print("ConfID List = ",confIDList)
# - for each conference ID, call GetNBRStorageFile and request all recording IDs. Create a Dict in memory indexed by
# recording ID with the ConfID that corresponds to it
# Get Storage Access Ticket
etStorageAccessTicket[1][0][0].text = siteID
etStorageAccessTicket[1][0][1].text = userID
etStorageAccessTicket[1][0][2].text = userPW
rStorageAccessTicket = requests.post(vaNBRstor, data=etree.tostring(etStorageAccessTicket), headers=stSOAPheaders)
rSSATxml = etree.fromstring(rStorageAccessTicket.text.encode('utf-8'), parser=parser)
sessionSSAT = rSSATxml[0][0][0].text
for confID in confIDList:
#print("Checking recording IDs for conference ID = ",confID)
# Build XML for request
etGetNBRStorageFile[0][0][0].text = siteID
etGetNBRStorageFile[0][0][1].text = confID
etGetNBRStorageFile[0][0][2].text = sessionSSAT
#leave file type empty
etGetNBRStorageFile[0][0][3].text = ''
#leave from and to dates empty, we check range based on recording filename
etGetNBRStorageFile[0][0][4].text = ''
etGetNBRStorageFile[0][0][5].text = ''
# Send API POST request
rGetNBRStorageFile = requests.post(vaNBRstor, data=etree.tostring(etGetNBRStorageFile), headers=stSOAPheaders, stream=True)
#print("rGetNBRStorageFile= ", rGetNBRStorageFile)
docGetNBRStorageFile = etree.fromstring(rGetNBRStorageFile.text.encode('utf-8'), parser=parser)
#print("docGetNBRStorageFile= ", docGetNBRStorageFile)
for tag in docGetNBRStorageFile.iter():
if not len(tag) and tag.text is not None:
#print(tag.text)
recordingID=tag.text
# Here we need to find all text values within RecordId tags
# and create Dict entries as such recordingConfIDs[RecordId]=confID
recordingConfIDs[recordingID] = confID
#print("recordingConfIDs = ",recordingConfIDs)
cursor.execute("SELECT name, meetingname, status FROM recordings WHERE status = ?", ('COMPLETED',))
while True:
record = cursor.fetchone()
#print(record)
try:
#keep track of recording IDs of those that are COMPLETED and their paths are valid
#to delete them all afterwards. If a file is missing, remove the COMPLETED status so it is
#downloaded on the next run of the download script
theRecordID=record[0]
theRecordPath=record[1]
if os.path.isfile(theRecordPath):
#add to our dict to delete if in range or script parameters to consider for deletion
if isRecFileInRange(theRecordPath,fromDate,toDate):
toDeleteRecordingsPaths[theRecordID]=theRecordPath
else:
#add to our dict to mark as missing file if in range or script parameters to consider for deletion
if isRecFileInRange(theRecordPath, fromDate, toDate):
missingRecordingFilesPaths[theRecordID]=theRecordPath
except TypeError:
print("Done finding records for deletion")
break
print("About to mark the following recordings as missing for re-download:")
for theRecID, theRecPath in missingRecordingFilesPaths.items():
print(f"ID: {theRecID} Path: {theRecPath}")
print('')
print("and about to delete the following recordings:")
for theRecID, theRecPath in toDeleteRecordingsPaths.items():
if theRecID in recordingConfIDs:
print(f"ID: {theRecID} Path: {theRecPath}")
if runInteractive:
if not input("Procced (you will get a chance to validate each marking and deletion)? (y/n): ").lower().strip()[:1] == "y": sys.exit(1)
for theRecID, theRecPath in missingRecordingFilesPaths.items():
# first mark records with missing files by setting status as NULL so it is downloaded again
print(f"Recording file {theRecPath} for key {theRecID} not found!! Marking it as NULL in database so it is downloaded on the next run of the download script.")
if runInteractive:
if not input("Procced (y) or skip (n)?: ").lower().strip()[:1] == "y": continue
cursor.execute('''UPDATE recordings SET status = ? WHERE name = ? ''',(None, theRecID))
db.commit()
# Call deleteNBRStorageFile for all recording IDs marked for deletion in toDeleteRecordingsPaths
# and corresponding ConfID
for theRecID, theRecPath in toDeleteRecordingsPaths.items():
if theRecID in recordingConfIDs:
print(f"About to delete recording ID: {theRecID} with conf ID: {recordingConfIDs[theRecID]}")
if runInteractive:
if not input("Procced (y) or skip (n)?: ").lower().strip()[:1] == "y": continue
# Build XML for request
etdeleteNBRStorageFile[0][0][0].text = siteID
etdeleteNBRStorageFile[0][0][1].text = recordingConfIDs[theRecID]
etdeleteNBRStorageFile[0][0][2].text = theRecID
etdeleteNBRStorageFile[0][0][3].text = sessionSSAT
# Send API POST request
rdeleteNBRStorageFile = requests.post(vaNBRstor, data=etree.tostring(etdeleteNBRStorageFile), headers=stSOAPheaders,
stream=True)
print("rdeleteNBRStorageFile= ", rdeleteNBRStorageFile)
docdeleteNBRStorageFile = etree.fromstring(rdeleteNBRStorageFile.text.encode('utf-8'), parser=parser)
print("docdeleteNBRStorageFile= ", docdeleteNBRStorageFile)
print(f"Recording with ID {theRecID} has been deleted and marked as SCRIPTDELETED in database. Recording file is still at {theRecPath} ")
# Update recording status
cursor.execute('''UPDATE recordings SET status = ? WHERE name = ? ''',
('SCRIPTDELETED', theRecID))
db.commit()
else:
print(f"Recording with key {theRecID} not found in cloud!! Marking it as MANUALDELETED in database")
if runInteractive:
if not input("Procced (y) or skip (n)?: ").lower().strip()[:1] == "y": continue
cursor.execute('''UPDATE recordings SET status = ? WHERE name = ? ''',
('MANUALDELETED', theRecID))
db.commit()
db.close()