Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| vendors:cisco:uc:ece:interactive-api-scripts [2024/05/28 09:20] – removed - external edit (Unknown date) 127.0.0.1 | vendors:cisco:uc:ece:interactive-api-scripts [2024/06/05 16:06] (current) – [ECE Purge Attachments by Queue ID and day range] gerardorourke | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== ECE Interactive API ====== | ||
| + | |||
| + | ===== Retrieve Activities for Date X ====== | ||
| + | note - this is a draft version - and currently doesn' | ||
| + | So this needs to be done! | ||
| + | |||
| + | <code python> | ||
| + | # Author Gerry O' | ||
| + | # Date: 29/03/2023 | ||
| + | # Version: 0.10 | ||
| + | |||
| + | import datetime | ||
| + | import requests # used for http requests | ||
| + | import sys # used for sys.exit | ||
| + | import urllib3 # | ||
| + | urllib3.disable_warnings() #Surpress cert warnings | ||
| + | import json | ||
| + | import os #check / creating folders | ||
| + | import argparse | ||
| + | |||
| + | eceUsername = ' | ||
| + | ecePassword = ' | ||
| + | eceHost = ' | ||
| + | rootFolder = ' | ||
| + | |||
| + | |||
| + | def getArguments(): | ||
| + | global myDate | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | type=lambda d: datetime.datetime.strptime(d, | ||
| + | help=' | ||
| + | parser.parse_args([" | ||
| + | args = parser.parse_args() | ||
| + | if args.date: | ||
| + | myDate = (args.date) | ||
| + | print(' | ||
| + | else: | ||
| + | myDate = datetime.date.today() - datetime.timedelta(days=1) | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def loginEce(): | ||
| + | #Attempt to Login | ||
| + | loginUrl = ' | ||
| + | loginUser=' | ||
| + | loginHeaders={" | ||
| + | print(' | ||
| + | response = requests.post(url=loginUrl, | ||
| + | |||
| + | #Store Response Values | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | #Get the X-egain-session Header value returned after successful (204) login. | ||
| + | responseToken = (responseHeaders[' | ||
| + | | ||
| + | #ECE defaults to returning XML unless you set the Accept header to application/ | ||
| + | #Here we are also setting the X-egain-session header (which we got when we logged in), which is needed for all requests. | ||
| + | #Note the user we logged in - must have the correct roles for the specific API calls | ||
| + | #we create a global variable here - so we can use it in other functions. | ||
| + | global eceHeaders | ||
| + | eceHeaders={" | ||
| + | print (' | ||
| + | else: | ||
| + | print (' | ||
| + | sys.exit(1) | ||
| + | | ||
| + | |||
| + | def logoutEce(): | ||
| + | logoutUrl = ' | ||
| + | response = requests.delete(url=logoutUrl, | ||
| + | #Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | if (responseStatusCode == 204): | ||
| + | print (' | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def listActivities(fromDate): | ||
| + | toDate = fromDate + datetime.timedelta(days=1) | ||
| + | fromDateString = (fromDate.strftime(' | ||
| + | toDateString = (toDate.strftime(' | ||
| + | |||
| + | listActivitiesUrl = ' | ||
| + | #Attempt to get Activity Id | ||
| + | response = requests.get(url=listActivitiesUrl, | ||
| + | |||
| + | #Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | print(' | ||
| + | activityDict = json.loads(responseText) | ||
| + | #print (activityDict.keys()) | ||
| + | # | ||
| + | # | ||
| + | myFolder = rootFolder + fromDate.strftime(' | ||
| + | # Check whether the specified path exists or not | ||
| + | isExist = os.path.exists(myFolder) | ||
| + | if not isExist: | ||
| + | # Create a new directory because it does not exist | ||
| + | os.makedirs(myFolder) | ||
| + | print(' | ||
| + | fileName = ' | ||
| + | filePath = myFolder + '/' | ||
| + | #Save the activity as a text file | ||
| + | text_file = open(filePath, | ||
| + | text_file.write(responseText) | ||
| + | text_file.close() | ||
| + | print(' | ||
| + | #Loop through the file and get each Activity id | ||
| + | for id in activityDict[' | ||
| + | #print(id [' | ||
| + | activityId = str(id [' | ||
| + | getActivity(activityId, | ||
| + | print(' | ||
| + | except Exception as e: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def getActivity(id, | ||
| + | print(' | ||
| + | activityUrl = ' | ||
| + | #Attempt to get Activity Id | ||
| + | response = requests.get(url=activityUrl, | ||
| + | |||
| + | #Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | #print (activityDict.keys()) | ||
| + | #print (activityDict[' | ||
| + | | ||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | caseId = str(activityDict[' | ||
| + | fileName = ' | ||
| + | myFolder = rootFolder + date.strftime(' | ||
| + | filePath = myFolder + '/' | ||
| + | #Check whether the specified path exists or not | ||
| + | isExist = os.path.exists(myFolder) | ||
| + | if not isExist: | ||
| + | # Create a new directory because it does not exist | ||
| + | os.makedirs(myFolder) | ||
| + | print(' | ||
| + | |||
| + | #Save the activity as a text file | ||
| + | text_file = open(filePath, | ||
| + | text_file.write(responseText) | ||
| + | text_file.close() | ||
| + | print (' | ||
| + | except Exception as e: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | #get the Command line parameters | ||
| + | getArguments() | ||
| + | |||
| + | #Login | ||
| + | loginEce() | ||
| + | |||
| + | #list and retrieve activities for the specific date | ||
| + | listActivities(myDate) | ||
| + | |||
| + | #Logout | ||
| + | logoutEce() | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Masking an activity or deleting attachments from an activity===== | ||
| + | |||
| + | *https:// | ||
| + | |||
| + | *https:// | ||
| + | |||
| + | *https:// | ||
| + | |||
| + | ===Permissions for Masking an activity=== | ||
| + | **All of the following are required:** | ||
| + | |||
| + | *User must have ' | ||
| + | *The activity must either belong to the user's home department, or to a department in which the user is a foreign user. | ||
| + | |||
| + | In PCCE - the above permissions are added to the specific user the following: ECE → User → Relationships: | ||
| + | *Manage Completed Activity | ||
| + | |||
| + | ==== Example Code to Mask or delete an attachment in an ECE Activity ==== | ||
| + | <code python> | ||
| + | # Author Gerry O' | ||
| + | # Date: 06/09/2023 | ||
| + | # Version: 2.0 | ||
| + | |||
| + | import requests | ||
| + | import sys # used for sys.exit | ||
| + | import urllib3 | ||
| + | |||
| + | urllib3.disable_warnings() | ||
| + | import json | ||
| + | import os # check / creating folders | ||
| + | import time | ||
| + | |||
| + | eceUsername = ' | ||
| + | ecePassword = ' | ||
| + | eceHost = ' | ||
| + | |||
| + | def loginEce(): | ||
| + | # Attempt to Login | ||
| + | loginUrl = ' | ||
| + | loginUser = ' | ||
| + | loginHeaders = {" | ||
| + | print(' | ||
| + | response = requests.post(url=loginUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | # Get the X-egain-session Header value returned after successful (204) login. | ||
| + | global responseToken | ||
| + | responseToken = (responseHeaders[' | ||
| + | |||
| + | # ECE defaults to returning XML unless you set the Accept header to application/ | ||
| + | # Here we are also setting the X-egain-session header (which we got when we logged in), which is needed for all requests. | ||
| + | # Note the user we logged in - must have the correct roles for the specific API calls | ||
| + | # we create a global variable here - so we can use it in other functions. | ||
| + | global eceHeaders | ||
| + | eceHeaders = {" | ||
| + | print(' | ||
| + | else: | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | |||
| + | def logoutEce(): | ||
| + | logoutUrl = ' | ||
| + | response = requests.delete(url=logoutUrl, | ||
| + | # Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | if (responseStatusCode == 204): | ||
| + | print(' | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def getActivity(id): | ||
| + | global caseId | ||
| + | global lastModifiedDate | ||
| + | global activityAttachmentsCount | ||
| + | print(' | ||
| + | activityUrl = ' | ||
| + | # Attempt to get Activity Id | ||
| + | response = requests.get(url=activityUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | caseId = str(activityDict[' | ||
| + | # print(' | ||
| + | lastModifiedDate = str(activityDict[' | ||
| + | # print(' | ||
| + | activityAttachmentsCount = str(activityDict[' | ||
| + | # print(' | ||
| + | # | ||
| + | |||
| + | except Exception as e: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | |||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def maskActivity(id, | ||
| + | maskActivityUrl = ' | ||
| + | maskActivityHeaders = {" | ||
| + | maskActivityBody = '<? | ||
| + | |||
| + | print(' | ||
| + | response = requests.put(url=maskActivityUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | print(' | ||
| + | confirm = input(' | ||
| + | if (confirm == ' | ||
| + | enterActivityId() | ||
| + | else: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | |||
| + | else: | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | |||
| + | |||
| + | def deleteAttachments(id): | ||
| + | deleteAttachmentsUrl = ' | ||
| + | deleteAttachmentsHeaders = {" | ||
| + | |||
| + | print(' | ||
| + | response = requests.delete(url=deleteAttachmentsUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | print(' | ||
| + | confirm = input(' | ||
| + | if (confirm == ' | ||
| + | enterActivityId() | ||
| + | else: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | |||
| + | else: | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | |||
| + | |||
| + | def enterActivityId(): | ||
| + | global activityId | ||
| + | activityId = input(' | ||
| + | if (activityId == ' | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | elif (activityId.isnumeric()): | ||
| + | getActivity(str(activityId)) | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | if (int(activityAttachmentsCount)> | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | confirm = input(' | ||
| + | if (confirm == ' | ||
| + | maskActivity(activityId, | ||
| + | elif (confirm == ' | ||
| + | | ||
| + | else: | ||
| + | logoutEce() | ||
| + | else: | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | confirm = input(' | ||
| + | if (confirm == ' | ||
| + | maskActivity(activityId, | ||
| + | else: | ||
| + | logoutEce() | ||
| + | else: | ||
| + | print(' | ||
| + | print(' | ||
| + | time.sleep(2) | ||
| + | enterActivityId() | ||
| + | |||
| + | os.system(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | # Login | ||
| + | loginEce() | ||
| + | |||
| + | # Enter the Activity Id to Mask | ||
| + | enterActivityId() | ||
| + | </ | ||
| + | |||
| + | ==== Sample output of the above script ==== | ||
| + | {{: | ||
| + | |||
| + | |||
| + | ===== Backup (draft version) of script to purge attachments ===== | ||
| + | |||
| + | <code python> | ||
| + | # Author Gerry O' | ||
| + | # Date: 07/09/2023 | ||
| + | # Version: 0.10 | ||
| + | |||
| + | import datetime | ||
| + | import requests # used for http requests | ||
| + | import sys # used for sys.exit | ||
| + | import urllib3 # | ||
| + | urllib3.disable_warnings() #Surpress cert warnings | ||
| + | import json | ||
| + | import os #check / creating folders | ||
| + | import argparse | ||
| + | |||
| + | eceUsername = ' | ||
| + | ecePassword = ' | ||
| + | eceHost = ' | ||
| + | rootFolder = ' | ||
| + | activityIdsSearched = [] | ||
| + | activityIdsWithAttachments = [] | ||
| + | |||
| + | def getArguments(): | ||
| + | global endDate | ||
| + | global startDate | ||
| + | global silentMode | ||
| + | global queueIds | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | 'want to purge more, enter day range' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | if args.days: | ||
| + | #days = (args.days) | ||
| + | endDate = datetime.date.today() - datetime.timedelta(days=args.days) | ||
| + | print(' | ||
| + | elif args.days == 0: | ||
| + | endDate = datetime.date.today() | ||
| + | print(' | ||
| + | else: | ||
| + | print(' | ||
| + | ' | ||
| + | exit(1) | ||
| + | |||
| + | if args.dayrange: | ||
| + | startDate = endDate - datetime.timedelta(days=args.dayrange) | ||
| + | print(' | ||
| + | |||
| + | if args.silent: | ||
| + | print(' | ||
| + | silentMode = true | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | if args.queue: | ||
| + | queueIds = args.queue | ||
| + | print(' | ||
| + | else: | ||
| + | print(' | ||
| + | 'Hence you must enter at least 1 queue you want to purge using the argument " | ||
| + | exit(1) | ||
| + | |||
| + | def loginEce(): | ||
| + | #Attempt to Login | ||
| + | loginUrl = ' | ||
| + | loginUser=' | ||
| + | loginHeaders={" | ||
| + | print(' | ||
| + | response = requests.post(url=loginUrl, | ||
| + | |||
| + | #Store Response Values | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | #Get the X-egain-session Header value returned after successful (204) login. | ||
| + | responseToken = (responseHeaders[' | ||
| + | global eceHeaders | ||
| + | eceHeaders={" | ||
| + | print (' | ||
| + | else: | ||
| + | print (' | ||
| + | sys.exit(1) | ||
| + | |||
| + | def logoutEce(): | ||
| + | logoutUrl = ' | ||
| + | response = requests.delete(url=logoutUrl, | ||
| + | #Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | if (responseStatusCode == 204): | ||
| + | print (' | ||
| + | else: | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | def listActivities(startDate, | ||
| + | global activityIdsSearched | ||
| + | global activityIdsWithAttachments | ||
| + | global activityCount | ||
| + | global currentPageNum | ||
| + | global pageSize | ||
| + | startDateString = (startDate.strftime(' | ||
| + | endDateString = (endDate.strftime(' | ||
| + | currentPageNum = pageNum | ||
| + | listActivitiesUrl = ' | ||
| + | print(' | ||
| + | |||
| + | #Attempt to Retrieve List of Activities | ||
| + | response = requests.get(url=listActivitiesUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | activityCount = activityDict[' | ||
| + | # | ||
| + | pageSize = activityDict[' | ||
| + | #Loop through the file and get each Activity id | ||
| + | for id in activityDict[' | ||
| + | activityId = str(id [' | ||
| + | activityIdsSearched.append(activityId) | ||
| + | activityAttachmentsCount = activityDict[' | ||
| + | if (activityAttachmentsCount > 0): | ||
| + | activityIdsWithAttachments.append(activityId) | ||
| + | except Exception as e: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | elif (currentPageNum == 1 and responseStatusCode == 204): | ||
| + | print(' | ||
| + | elif (responseStatusCode == 204): | ||
| + | print(' | ||
| + | else: | ||
| + | print(' | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | |||
| + | #get the Command line parameters | ||
| + | getArguments() | ||
| + | loginEce() | ||
| + | listActivities(startDate, | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | while ((currentPageNum * pageSize) < activityCount): | ||
| + | currentPageNum += 1 | ||
| + | print(' | ||
| + | listActivities(startDate, | ||
| + | |||
| + | print(' | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| + | |||
| + | #Login | ||
| + | #loginEce() | ||
| + | |||
| + | #list and retrieve activities for the specific date | ||
| + | |||
| + | #Logout | ||
| + | logoutEce() | ||
| + | </ | ||
| + | |||
| + | =====ECE Purge Attachments by Queue ID and day range===== | ||
| + | |||
| + | <code python> | ||
| + | # Author Gerry O' | ||
| + | # Date: 14/09/2023 | ||
| + | # Version: 1.0 | ||
| + | |||
| + | #import datetime | ||
| + | from datetime import datetime | ||
| + | from datetime import date | ||
| + | from datetime import timedelta | ||
| + | import requests # used for http requests | ||
| + | import sys # used for sys.exit and standard output | ||
| + | import urllib3 # | ||
| + | urllib3.disable_warnings() #Surpress cert warnings | ||
| + | import json | ||
| + | import os #check / creating folders | ||
| + | import argparse | ||
| + | import logging | ||
| + | from logging.handlers import RotatingFileHandler | ||
| + | import smtplib | ||
| + | from email.mime.text import MIMEText | ||
| + | |||
| + | #loglevel = logging.INFO | ||
| + | loglevel = logging.DEBUG | ||
| + | logging.basicConfig(handlers=[logging.StreamHandler(sys.stdout), | ||
| + | RotatingFileHandler(' | ||
| + | style=' | ||
| + | format=' | ||
| + | level=loglevel) | ||
| + | eceUsername = ' | ||
| + | ecePassword = ' | ||
| + | eceHost = ' | ||
| + | environment = ' | ||
| + | rootFolder = ' | ||
| + | activityIdsSearched = [] | ||
| + | activityIdsWithAttachments = [] | ||
| + | deletedActivityIds = [] | ||
| + | errorCounter = 0 | ||
| + | errorMessage = '' | ||
| + | startTime = datetime.now() | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | def getArguments(): | ||
| + | global endDate | ||
| + | global startDate | ||
| + | global silentMode | ||
| + | global queueIds | ||
| + | global writeAccess | ||
| + | global email | ||
| + | global errorCounter | ||
| + | global errorMessage | ||
| + | |||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | 'want to purge more, enter day range' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | if args.days: | ||
| + | endDate = date.today() - timedelta(days=args.days) | ||
| + | message = ('End Date set to: ' + endDate.strftime(' | ||
| + | logging.info(message) | ||
| + | elif args.days == 0: | ||
| + | endDate = date.today() | ||
| + | message = ('End Date set to: ' + endDate.strftime(' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = ('No date argument set. Enter the number of days from today, e.g. "-d 180", would purge data from 180 ' | ||
| + | ' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | errorMessage = ' | ||
| + | sendEmail() | ||
| + | exit(1) | ||
| + | |||
| + | if args.dayrange: | ||
| + | startDate = endDate - timedelta(days=(args.dayrange-1)) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | if args.silent: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | silentMode = True | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | silentMode = False | ||
| + | |||
| + | if args.write: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | writeAccess = True | ||
| + | else: | ||
| + | message = ('Read Only mode set - the application will NOT delete attachments.' | ||
| + | logging.info(message) | ||
| + | writeAccess = False | ||
| + | |||
| + | if args.email: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | email = True | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | email = False | ||
| + | |||
| + | if args.queue: | ||
| + | queueIds = args.queue | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = ('No queue data set. This app COULD (if we wanted to!) be used to purge all activities for a date range but that is too risky! ' | ||
| + | 'Hence you must enter at least 1 queue you want to purge using the argument " | ||
| + | logging.info(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | errorMessage = ' | ||
| + | exit(1) | ||
| + | |||
| + | def loginEce(): | ||
| + | global errorCounter | ||
| + | global errorMessage | ||
| + | loginUrl = ' | ||
| + | loginUser=' | ||
| + | loginHeaders={" | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.post(url=loginUrl, | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | #Get the X-egain-session Header value returned after successful (204) login. | ||
| + | global responseToken | ||
| + | responseToken = (responseHeaders[' | ||
| + | global eceHeaders | ||
| + | eceHeaders={" | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | errorMessage = ' | ||
| + | sendEmail() | ||
| + | sys.exit(1) | ||
| + | |||
| + | def logoutEce(): | ||
| + | logoutUrl = ' | ||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.delete(url=logoutUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | def listActivities(startDate, | ||
| + | global activityIdsSearched | ||
| + | global activityIdsWithAttachments | ||
| + | global activityCount | ||
| + | global currentPageNum | ||
| + | global pageSize | ||
| + | global retrievedData | ||
| + | global errorCounter | ||
| + | |||
| + | startDateString = (startDate.strftime(' | ||
| + | endDateString = (endDate.strftime(' | ||
| + | currentPageNum = pageNum | ||
| + | retrievedData = False | ||
| + | listActivitiesUrl = ' | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | #Attempt to Retrieve List of Activities | ||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.get(url=listActivitiesUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | activityCount = activityDict[' | ||
| + | # | ||
| + | pageSize = activityDict[' | ||
| + | #Loop through the file and get each Activity id | ||
| + | for id in activityDict[' | ||
| + | activityId = str(id [' | ||
| + | # | ||
| + | activityIdsSearched.append(activityId) | ||
| + | activityAttachmentsCount = id[' | ||
| + | # | ||
| + | if (activityAttachmentsCount > 0): | ||
| + | activityIdsWithAttachments.append(activityId) | ||
| + | retrievedData = True | ||
| + | except Exception as e: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | elif (currentPageNum == 1 and responseStatusCode == 204): | ||
| + | message = ('No Activities Content for that date range' | ||
| + | logging.info(message) | ||
| + | elif (responseStatusCode == 204): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | |||
| + | def deleteAttachments(id): | ||
| + | deleteAttachmentsUrl = ' | ||
| + | deleteAttachmentsHeaders = {" | ||
| + | message=(' | ||
| + | logging.info(message) | ||
| + | |||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.delete(url=deleteAttachmentsUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseBody = (response.text) | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | deletedActivityIds.append(activityId) | ||
| + | message(' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | errorCounter = errorCounter + 1 | ||
| + | |||
| + | def sendEmail(): | ||
| + | global activityIdsWithAttachments | ||
| + | global activityIdsSearched | ||
| + | global deletedActivityIds | ||
| + | global errorCounter | ||
| + | global errorMessage | ||
| + | global environment | ||
| + | global startTime | ||
| + | global email | ||
| + | global writeAccess | ||
| + | global queueIds | ||
| + | global endDate | ||
| + | global startDate | ||
| + | |||
| + | if writeAccess==True: | ||
| + | mode = 'Write Mode Enabled - will attempt to delete attachments' | ||
| + | else: | ||
| + | mode = 'Write Mode Disabled - will NOT attempt to delete attachments' | ||
| + | |||
| + | if (email==False): | ||
| + | message = ('Not sending email as email argument not enmabled' | ||
| + | logging.info(message) | ||
| + | return #don't continue if email is not enabled. | ||
| + | |||
| + | endTime = datetime.now() | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | s = smtplib.SMTP(' | ||
| + | sender = environment + ' | ||
| + | recipients = [' | ||
| + | |||
| + | msg = MIMEText(' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | '\nEnd Date: ' + endDate.strftime(' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | msg[' | ||
| + | msg[' | ||
| + | msg[' | ||
| + | |||
| + | s.sendmail(sender, | ||
| + | s.quit() | ||
| + | |||
| + | #get the Command line parameters | ||
| + | getArguments() | ||
| + | |||
| + | #Login | ||
| + | loginEce() | ||
| + | |||
| + | #Loop through queues provided | ||
| + | for queueId in queueIds: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | listActivities(startDate, | ||
| + | if (retrievedData): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | while ((currentPageNum * pageSize) < activityCount): | ||
| + | currentPageNum += 1 | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | listActivities(startDate, | ||
| + | |||
| + | # | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | if (len(activityIdsWithAttachments)> | ||
| + | confirm = input(' | ||
| + | if (confirm == ' | ||
| + | for activityIdWithAttachment in activityIdsWithAttachments: | ||
| + | deleteAttachments(activityIdWithAttachment) | ||
| + | |||
| + | if (len(activityIdsWithAttachments)> | ||
| + | for activityIdWithAttachment in activityIdsWithAttachments: | ||
| + | deleteAttachments(activityIdWithAttachment) | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | #Send Email | ||
| + | sendEmail() | ||
| + | |||
| + | #Logout | ||
| + | logoutEce() | ||
| + | |||
| + | endTime = datetime.now() | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | </ | ||
| + | |||
| + | ===== Retrieve Activity and Case ID info for Activities in specific Date Range and only for certain Queue IDs ===== | ||
| + | |||
| + | <code python> | ||
| + | # Author Gerry O' | ||
| + | # Date: 05/06/2024 | ||
| + | # Version: 2.02 | ||
| + | |||
| + | # Example1 | ||
| + | # ' | ||
| + | # This will get Activities for yesterday (-d defaults to 1 and -r default to only get 1 days worth) | ||
| + | # | ||
| + | # Example2 | ||
| + | # ' | ||
| + | # Get Activities for Queue ID 1015 from today (-d 0), and go back 10 days. i.e. the last 10 days including today. | ||
| + | |||
| + | # Example3 ' | ||
| + | # Get Activities for yesterday, the day before yesterday and the day before that, for queues 1015 AND 1023 | ||
| + | # End date is "-d 1" - i.e. 1 day ago and range (-r) is 3 days (-d 0) | ||
| + | # | ||
| + | # Note this script should be improved - as the Case ID for each Activity can be retrieved from the Activity Search Response (and there is no need to get the Activity to find the Case ID) | ||
| + | |||
| + | import time # used when pausing script for X milliseconds | ||
| + | import json | ||
| + | import os # check / creating folders | ||
| + | import argparse | ||
| + | import sys # used for sys.exit and standard output | ||
| + | import requests | ||
| + | import urllib3 | ||
| + | from datetime import datetime | ||
| + | from datetime import date | ||
| + | from datetime import timedelta | ||
| + | import logging | ||
| + | from logging.handlers import RotatingFileHandler | ||
| + | |||
| + | urllib3.disable_warnings() | ||
| + | |||
| + | # Initial Variables | ||
| + | loglevel = logging.DEBUG | ||
| + | logging.basicConfig(handlers=[logging.StreamHandler(sys.stdout), | ||
| + | RotatingFileHandler(' | ||
| + | backupCount=10)], | ||
| + | style=' | ||
| + | format=' | ||
| + | level=loglevel) | ||
| + | ece_username = ' | ||
| + | ece_password = ' | ||
| + | ece_host = ' | ||
| + | environment = ' | ||
| + | delay = 333 / 1000 # Time to pause when retrieving HTTP Request loops - to reduce load on server, e.g. 200/1000 -> 4 | ||
| + | # API Requests per second, 333/1000 -> 3 API requests per second, 500/100 => 2 API calls per second. | ||
| + | rootFolder = ' | ||
| + | activity_ids_searched = [] | ||
| + | activity_ids_with_attachments = [] | ||
| + | case_ids = [] | ||
| + | case_ids_closed = [] | ||
| + | error_counter = 0 | ||
| + | error_message = '' | ||
| + | startTime = datetime.now() | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | |||
| + | def getArguments(): | ||
| + | global endDate | ||
| + | global startDate | ||
| + | global queueIds | ||
| + | global retrieveActivities | ||
| + | global writeActivitiesToFile | ||
| + | global error_counter | ||
| + | global error_message | ||
| + | |||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | help=' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | help=' | ||
| + | parser.add_argument(' | ||
| + | help=' | ||
| + | parser.add_argument(' | ||
| + | help=' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | if args.days: | ||
| + | endDate = date.today() - timedelta(days=args.days) | ||
| + | message = ('End Date set to: ' + endDate.strftime(' | ||
| + | logging.info(message) | ||
| + | elif args.days == 0: | ||
| + | endDate = date.today() | ||
| + | message = ('End Date set to: ' + endDate.strftime(' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = ('No date argument set. Enter the number of days from today, e.g. "-d 1", would be 1 day ago.') | ||
| + | logging.error(message) | ||
| + | error_counter = error_counter + 1 | ||
| + | error_message = ' | ||
| + | exit(1) | ||
| + | |||
| + | if args.dayrange: | ||
| + | startDate = endDate - timedelta(days=(args.dayrange - 1)) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | if args.queue: | ||
| + | queueIds = args.queue | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = ( | ||
| + | 'No queue ID data set. This app COULD (if we wanted to!) be used to retrieve all activities for a date ' | ||
| + | 'range but that is not what we want' | ||
| + | 'Hence you must enter at least 1 queue with the " | ||
| + | 'front of each queue ID.') | ||
| + | logging.info(message) | ||
| + | error_counter = error_counter + 1 | ||
| + | error_message = ' | ||
| + | exit(1) | ||
| + | |||
| + | if args.activities: | ||
| + | retrieveActivities = True | ||
| + | else: | ||
| + | retrieveActivities = False | ||
| + | |||
| + | if args.write: | ||
| + | writeActivitiesToFile = True | ||
| + | else: | ||
| + | writeActivitiesToFile = False | ||
| + | |||
| + | |||
| + | def loginEce(): | ||
| + | global error_counter | ||
| + | global error_message | ||
| + | login_url = ' | ||
| + | loginUser = ' | ||
| + | loginHeaders = {" | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.post(url=login_url, | ||
| + | responseHeaders = (response.headers) | ||
| + | responseStatusCode = (response.status_code) | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | # Get the X-egain-session Header value returned after successful (204) login. | ||
| + | global responseToken | ||
| + | responseToken = (responseHeaders[' | ||
| + | global eceHeaders | ||
| + | eceHeaders = {" | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | error_counter = error_counter + 1 | ||
| + | error_message = ' | ||
| + | sys.exit(1) | ||
| + | |||
| + | |||
| + | def logoutEce(): | ||
| + | logoutUrl = ' | ||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.delete(url=logoutUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | response_text = response.text | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 204): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | |||
| + | def listActivities(startDate, | ||
| + | global activity_ids_searched | ||
| + | global activity_ids_with_attachments | ||
| + | global activityCount | ||
| + | global currentPageNum | ||
| + | global pageSize | ||
| + | global retrievedData | ||
| + | global error_counter | ||
| + | |||
| + | startDateString = (startDate.strftime(' | ||
| + | endDateString = (endDate.strftime(' | ||
| + | currentPageNum = pageNum | ||
| + | retrievedData = False | ||
| + | listActivitiesUrl = ' | ||
| + | queueId) + '& | ||
| + | currentPageNum) + '& | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | # Attempt to Retrieve List of Activities | ||
| + | responseStatusCode = 0 | ||
| + | try: | ||
| + | response = requests.get(url=listActivitiesUrl, | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | except: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | activityCount = activityDict[' | ||
| + | # currentPageNum = activityDict[' | ||
| + | pageSize = activityDict[' | ||
| + | # Loop through the file and get each Activity id | ||
| + | for id in activityDict[' | ||
| + | activityId = str(id[' | ||
| + | # print(' | ||
| + | activity_ids_searched.append(activityId) | ||
| + | activityAttachmentsCount = id[' | ||
| + | # print(' | ||
| + | if (activityAttachmentsCount > 0): | ||
| + | activity_ids_with_attachments.append(activityId) | ||
| + | retrievedData = True | ||
| + | except Exception as e: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | error_counter = error_counter + 1 | ||
| + | elif (currentPageNum == 1 and responseStatusCode == 204): | ||
| + | message = ('No Activities Content for that date range' | ||
| + | logging.info(message) | ||
| + | elif (responseStatusCode == 204): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | else: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | error_counter = error_counter + 1 | ||
| + | |||
| + | |||
| + | def getActivity(id, | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | activityUrl = ' | ||
| + | # Attempt to get Activity Id | ||
| + | response = requests.get(url=activityUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | # print (activityDict.keys()) | ||
| + | # print (activityDict[' | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | activityDict = json.loads(responseText) | ||
| + | caseId = str(activityDict[' | ||
| + | if caseId not in case_ids: | ||
| + | case_ids.append(caseId) | ||
| + | if (writeActivitiesToFile): | ||
| + | fileName = ' | ||
| + | myFolder = rootFolder + folder | ||
| + | filePath = myFolder + '/' | ||
| + | # Check whether the specified path exists or not | ||
| + | isExist = os.path.exists(myFolder) | ||
| + | if not isExist: | ||
| + | # Create a new directory because it does not exist | ||
| + | os.makedirs(myFolder) | ||
| + | message = ('New directory: "' | ||
| + | logging.info(message) | ||
| + | # Save the activity as a text file | ||
| + | text_file = open(filePath, | ||
| + | text_file.write(responseText) | ||
| + | text_file.close() | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | except Exception as e: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def getCase(id): | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | caseUrl = ' | ||
| + | # Attempt to get Activity Id | ||
| + | response = requests.get(url=caseUrl, | ||
| + | |||
| + | # Store Response Values | ||
| + | responseStatusCode = (response.status_code) | ||
| + | responseText = response.text | ||
| + | |||
| + | if (responseStatusCode == 200): | ||
| + | try: | ||
| + | caseDict = json.loads(responseText) | ||
| + | caseStatus = str(caseDict[' | ||
| + | if (caseStatus == ' | ||
| + | case_ids_closed.append(caseId) | ||
| + | if (writeActivitiesToFile): | ||
| + | fileName = ' | ||
| + | myFolder = rootFolder + ' | ||
| + | filePath = myFolder + '/' | ||
| + | # Check whether the specified path exists or not | ||
| + | isExist = os.path.exists(myFolder) | ||
| + | if not isExist: | ||
| + | # Create a new directory because it does not exist | ||
| + | os.makedirs(myFolder) | ||
| + | message = ('New directory: "' | ||
| + | logging.info(message) | ||
| + | # Save the activity as a text file | ||
| + | text_file = open(filePath, | ||
| + | text_file.write(responseText) | ||
| + | text_file.close() | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | except Exception as e: | ||
| + | message = (' | ||
| + | logging.error(message) | ||
| + | logoutEce() | ||
| + | sys.exit(2) | ||
| + | else: | ||
| + | print(' | ||
| + | |||
| + | |||
| + | # get the Command line parameters | ||
| + | getArguments() | ||
| + | |||
| + | # Login | ||
| + | loginEce() | ||
| + | |||
| + | # Loop through queues provided | ||
| + | for queueId in queueIds: | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | listActivities(startDate, | ||
| + | if (retrievedData): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | while ((currentPageNum * pageSize) < activityCount): | ||
| + | currentPageNum += 1 | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | time.sleep(delay) | ||
| + | listActivities(startDate, | ||
| + | |||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | if (retrieveActivities): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | for activityId in activity_ids_searched: | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | time.sleep(delay) | ||
| + | getActivity(activityId, | ||
| + | |||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | for caseId in case_ids: | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | time.sleep(delay) | ||
| + | getCase(caseId) | ||
| + | |||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | message = (' | ||
| + | logging.debug(message) | ||
| + | |||
| + | # Logout | ||
| + | logoutEce() | ||
| + | |||
| + | # Print Summary At the end | ||
| + | message = ('#########################################' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | if (retrieveActivities): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | |||
| + | if (retrieveActivities): | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | endTime = datetime.now() | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | duration = endTime - startTime | ||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | message = ('#########################################' | ||
| + | logging.info(message) | ||
| + | |||
| + | message = (' | ||
| + | logging.info(message) | ||
| + | </ | ||