Skip to content

Instantly share code, notes, and snippets.

@bmccormack
Last active November 16, 2017 14:32
Show Gist options
  • Save bmccormack/4f88821a0e1cc82e41426281b0b6b9fe to your computer and use it in GitHub Desktop.
Save bmccormack/4f88821a0e1cc82e41426281b0b6b9fe to your computer and use it in GitHub Desktop.
Delete a bunch of sh*t tickets from Zendesk. Ahem.
import requests
import urllib
import re
import datetime
from secrets import zendesk_user, zendesk_token
from util import jprint
import sys
import time
USER = zendesk_user + '/token'
PWD = zendesk_token
query = "requester:qqq created>2017-10-10" #this is the search that corresponds to the crap you want to delete
query_url_encoded = urllib.quote_plus(query)
#I'm not using this in the final version
def delete_ticket(ticket_id):
url = "https://fullstoryhelp.zendesk.com/api/v2/tickets/%s.json" % ticket_id
response = requests.delete(url, auth=(USER, PWD))
print response.status_code
print response.text
def delete_many_tickets(arr_ticket_id):
s_ticket_ids = ",".join(arr_ticket_id)
url = "https://fullstoryhelp.zendesk.com/api/v2/tickets/destroy_many.json?ids=%s" % s_ticket_ids
print url
response = requests.delete(url, auth=(USER, PWD))
print response.status_code
print response.text
data = response.json()
url = data["job_status"]["url"]
print url
### I had written a bunch of code to synchronously wait until
### each batch finishes, but that's unnecessary
# keep_going = True
# while keep_going:
# response = requests.get(url, auth=(USER,PWD))
# data = response.json()
# keep_going = data["job_status"]["status"] != "completed"
# print data["job_status"]["progress"]
# time.sleep(3)
url = 'https://fullstoryhelp.zendesk.com/api/v2/search.json?query=%s' % query_url_encoded
keep_going = True
while keep_going:
response = requests.get(url, auth=(USER, PWD))
print response.status_code
#print response.text
data = response.json()
tickets = data["results"]
keep_going = data["next_page"] is not None #this is redundant, but we can keep it <shrug>
url = data["next_page"]
data.pop("results")
jprint(data)
arr_ticket_id = [str(x["id"]) for x in tickets]
delete_many_tickets(arr_ticket_id)
import requests
import urllib
import re
import datetime
from secrets import zendesk_user, zendesk_token
from util import jprint
import sys
import time
# https://gist.github.com/bmccormack/4f88821a0e1cc82e41426281b0b6b9fe
USER = zendesk_user + '/token'
PWD = zendesk_token
query = 'created>2017-11-15 status:new Subject:"Some common subject"' #this is the search that corresponds to the crap you want to delete
query_url_encoded = urllib.quote_plus(query)
#I'm not using this in the final version
def delete_ticket(ticket_id):
url = "https://fullstoryhelp.zendesk.com/api/v2/tickets/%s.json" % ticket_id
response = requests.delete(url, auth=(USER, PWD))
print response.status_code
print response.text
def delete_many_tickets(arr_ticket_id):
s_ticket_ids = ",".join(arr_ticket_id)
url = "https://fullstoryhelp.zendesk.com/api/v2/tickets/destroy_many.json?ids=%s" % s_ticket_ids
print url
response = requests.delete(url, auth=(USER, PWD))
print response.status_code
print response.text
data = response.json()
url = data["job_status"]["url"]
print url
### I had written a bunch of code to synchronously wait until
### each batch finishes, but that's unnecessary
# keep_going = True
# while keep_going:
# response = requests.get(url, auth=(USER,PWD))
# data = response.json()
# keep_going = data["job_status"]["status"] != "completed"
# print data["job_status"]["progress"]
# time.sleep(3)
def should_we_delete(ticket):
N_CHINESE_CHARS_LIMIT = 10
N_OFFENDING_URLS_LIMIT = 1
body = ticket["description"]
n_chinese_chars = 0
#print body.decode('utf-8')
for c in body:
if u'\u4e00' <= c <= u'\u9fff':
n_chinese_chars += 1
m = re.findall(r'www\.\d{5,20}\.com', body)
n_offending_urls = len(m)
stop_execution = (n_chinese_chars >= N_CHINESE_CHARS_LIMIT and n_offending_urls >= N_OFFENDING_URLS_LIMIT)
output = {"n_chinese_chars": n_chinese_chars, "n_offending_urls": n_offending_urls, "stop_execution": stop_execution}
return stop_execution
url = 'https://fullstoryhelp.zendesk.com/api/v2/search.json?query=%s' % query_url_encoded
keep_going = True
while keep_going:
response = requests.get(url, auth=(USER, PWD))
print response.status_code
#print response.text
data = response.json()
tickets = data["results"]
keep_going = data["next_page"] is not None #this is redundant, but we can keep it <shrug>
url = data["next_page"]
data.pop("results")
arr_ticket_id = []
for ticket in tickets:
#jprint(ticket["description"])
if should_we_delete(ticket):
arr_ticket_id.append(str(ticket["id"]))
#print ticket["id"], should_we_delete(ticket)
print arr_ticket_id
delete_many_tickets(arr_ticket_id)
#change these values to match
zendesk_user = 'email_address_of_user@example.com'
zendesk_token = 'zendesk_api_token'
#you don't really need this <shrug>
import json
def jprint(to_print, indent=4, sort_keys=True):
print json.dumps(to_print, indent=indent, sort_keys=sort_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment