I am trying to use zenpy to get information on tickets created or modified in the last week. This is the code I am using:
stream_status = False #Exit condition
while(stream_status==False):
print(start_date)
all_tickets = zendesk.tickets.incremental(start_time=start_date, include=['users','organizations'])
print(len(all_tickets))
print(all_tickets.end_time)
for ticket in all_tickets:
#grab ticket fields and store them in a Dataframe
df.loc[len(df)] = [ticket_id,created_at,requester,organization,product,subject,assignee,status,priority,opened_at,opened_by,solved_at,solved_by,closed_at,closed_by]
count +=1
print(count,ticket_id)
start_date = all_tickets.end_time
stream_status = all_tickets.end_of_stream
print(start_date,stream_status)
today = datetime.now().strftime("%Y%m%d%H%M")
df.to_excel('Ticket_report{0}.xlsx'.format(today))
Now there are several issues here. The date is calculated correctly and it's indeed 7 days ago. But the tickets I am getting are going back to at least April and they were definitely not been modified since. I stopped it at this point cause we have thousands of tickets. Also, the incremental method returns a max of 1000 ticket objects, but even after 1000 the loop doesn't seem to restart (print statement at the end doesn't trigger). I am not sure I am using the stream_status flag correctly. Any advice is more than welcome. Thank you!
Zenpy documentation on incremental can be found here: http://docs.facetoe.com.au/zenpy.html#incremental-exports
It was an API issue after all.
For anyone facing similar issues, turns out that the start date is compared against the generated_timestamp instead of the updated_at or created_at fields. Updated_at holds the last action that generated a ticket event(e.g. change) but generated_timestamp is updated every time the ticket is affected even from the system. That leads to grabbing tickets updated or created before the input date. A solution for that is to filter out the results after the call:
all_tickets = zendesk_client.incremental(start_time=start_date)
accurate_tickets = [ticket for ticket in all tickets if parser.parse(ticket.updated_at) > start_date]
Not very sure why they designed the API like this, seems a bit wrong, but that's how it works unfortunately.