-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_zendesk_tickets.py
142 lines (127 loc) · 5.37 KB
/
get_zendesk_tickets.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
"""
This script can be used to retrieve Zendesk tickets.
This can be run locally if you set the ZENDESK_API_KEY. Or the script can be run from a flask shell from a ssh session.
"""
# flake8: noqa: T001 (print)
import csv
import os
import urllib.parse
import requests
# Group: 3rd Line--Notify Support
NOTIFY_GROUP_ID = 360000036529
# Organization: GDS
NOTIFY_ORG_ID = 21891972
# the account used to authenticate with. If no requester is provided, the ticket will come from this account.
NOTIFY_ZENDESK_EMAIL = '[email protected]'
ZENDESK_API_KEY = os.environ.get('ZENDESK_API_KEY')
def get_tickets():
ZENDESK_TICKET_URL = 'https://govuk.zendesk.com/api/v2/search.json?query={}'
query_params = 'type:ticket group:{}'.format(NOTIFY_GROUP_ID)
query_params = urllib.parse.quote(query_params)
next_page = ZENDESK_TICKET_URL.format(query_params)
with open("zendesk_ticket_data.csv", 'w') as csvfile:
fieldnames = [
'Service id',
'Ticket id',
'Subject line',
'Date ticket created',
'Tags',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
while next_page:
print(next_page)
response = requests.get(
next_page,
headers={'Content-type': 'application/json'},
auth=(
'{}/token'.format(NOTIFY_ZENDESK_EMAIL),
ZENDESK_API_KEY
)
)
data = response.json()
print(data)
for row in data["results"]:
service_url = [x for x in row["description"].split('\n')
if x.startswith("https://www.notifications.service.gov.uk/services/")]
service_url = service_url[0][50:] if len(service_url) > 0 else None
if service_url:
writer.writerow({'Service id': service_url,
'Ticket id': row['id'],
'Subject line': row['subject'],
'Date ticket created': row["created_at"],
'Tags': row.get('tags', '')
})
next_page = data["next_page"]
def get_tickets_without_service_id():
ZENDESK_TICKET_URL = 'https://govuk.zendesk.com/api/v2/search.json?query={}'
query_params = 'type:ticket group:{}'.format(NOTIFY_GROUP_ID)
query_params = urllib.parse.quote(query_params)
next_page = ZENDESK_TICKET_URL.format(query_params)
with open("zendesk_ticket_data_without_service.csv", 'w') as csvfile:
fieldnames = [
'Ticket id',
'Subject line',
'Date ticket created',
'Tags',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
while next_page:
print(next_page)
response = requests.get(
next_page,
headers={'Content-type': 'application/json'},
auth=(
'{}/token'.format(NOTIFY_ZENDESK_EMAIL),
ZENDESK_API_KEY
)
)
data = response.json()
print(data)
for row in data["results"]:
service_url = [x for x in row["description"].split('\n')
if x.startswith("https://www.notifications.service.gov.uk/services/")]
service_url = service_url[0][50:] if len(service_url) > 0 else None
if not service_url:
writer.writerow({'Ticket id': row['id'],
'Subject line': row['subject'],
'Date ticket created': row["created_at"],
'Tags': row.get('tags', '')
})
next_page = data["next_page"]
def get_tickets_with_description():
ZENDESK_TICKET_URL = 'https://govuk.zendesk.com/api/v2/search.json?query={}'
query_params = 'type:ticket group:{}, created>2019-07-01'.format(NOTIFY_GROUP_ID)
query_params = urllib.parse.quote(query_params)
next_page = ZENDESK_TICKET_URL.format(query_params)
with open("zendesk_ticket.csv", 'w') as csvfile:
fieldnames = [
'Ticket id',
'Subject line',
'Description',
'Date ticket created',
'Tags',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
while next_page:
print(next_page)
response = requests.get(
next_page,
headers={'Content-type': 'application/json'},
auth=(
'{}/token'.format(NOTIFY_ZENDESK_EMAIL),
ZENDESK_API_KEY
)
)
data = response.json()
print(data)
for row in data["results"]:
writer.writerow({'Ticket id': row['id'],
'Subject line': row['subject'],
'Description': row['description'],
'Date ticket created': row["created_at"],
'Tags': row.get('tags', '')
})
next_page = data["next_page"]