From 747300af3249fb3133d70cc4e005bf6707ff4304 Mon Sep 17 00:00:00 2001 From: TheLeo Date: Mon, 7 Oct 2024 16:09:54 +0000 Subject: [PATCH] Added ticket building function to account for pagination --- ticket_transfer.py | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/ticket_transfer.py b/ticket_transfer.py index 468900f..ba7b352 100644 --- a/ticket_transfer.py +++ b/ticket_transfer.py @@ -11,7 +11,8 @@ import time import sys import base64 import urllib.parse -import mimetypes +from datetime import datetime +from dateutil.relativedelta import relativedelta ################################### # Function Definitions @@ -31,7 +32,7 @@ def makeCall (target, verb, key, payload= None, files= None): return responseData except requests.exceptions.HTTPError as http_error: print(f'{RED}HTTP Error:{RESET}\n{http_error}') - pint(f'Reponse Content: {response.content}') + print(f'Reponse Content: {response.content}') raise except Exception as error: print(f'{RED}Error:{RESET}\n{error}') @@ -77,6 +78,39 @@ def checkRate(response): else: return False +def buildTicketArray(domain, groupId, key): + retArray = [] + page = 1 + moreTickets = True + dateFormat = "%Y-%m-%d" + currentDate = datetime.utcnow() + ticketBatch = 30 + maxPages = 10 + + while moreTickets: + page = 1 + morePages = True + endDate = currentDate.strftime(dateFormat) + startDate = (currentDate - relativedelta(months=1)).strftime(dateFormat) + query = f'"(group_id:{groupId}) AND (created_at:>\'{startDate}\' AND created_at:<\'{endDate}\')"' + encodeQuery = urllib.parse.quote(query, safe=":<>() AND") + + while morePages and page <= maxPages: + url = f'{domain}/api/v2/search/tickets?query={encodeQuery}&page={page}' + response = makeCall(url, 'get', key) + + if 'results' in response and len(response['results']) > 0: + retArray.extend(response['results']) + else: + morePages = False + page += 1 + + if len(response['results']) < ticketBatch: + moreTickets = False + + currentDate -= relativedelta(months=1) + return retArray + ################################### # Globals Variables ################################### @@ -173,14 +207,15 @@ print(f'Obtaining account information...{GREEN}COMPLETE{RESET}') ## Building Ticket Array from Source print(f'Gathering list of tickets from source...') #ticketArray = makeCall(sourceDom + '/api/v2/search/tickets?query=' + urllib.parse.urlencode(f'"group_id:\'{targetGroupId}\'"'), 'get', sourceKey) -ticketArray = makeCall(f'{sourceDom}/api/v2/search/tickets?query="group_id:{targetGroupId}"', 'get', sourceKey) +#ticketArray = makeCall(f'{sourceDom}/api/v2/search/tickets?query="group_id:{targetGroupId}"', 'get', sourceKey) +ticketArray = buildTicketArray(sourceDom, targetGroupId, sourceKey) clear(1) print(f'Gathering list of tickets from source...{GREEN}COMPLETE{RESET}') ## Transferring tickets to Destination print(f'{YELLOW}*This may take a while*{RESET}') print(f'Transferring tickets to destination account...◐') -for i, ticket in enumerate(ticketArray['results']): +for i, ticket in enumerate(ticketArray): attachments = {} anim = ['◓', '◑', '◒', '◐'] ticketData = makeCall(sourceDom + f'/api/v2/tickets/{ticket["id"]}?include=conversations,requester', 'get', sourceKey)