Created
March 20, 2019 03:15
-
-
Save brk21/47bfd98fc2597c473521ea3695d5b24b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pandas.io.json import json_normalize | |
import requests | |
from configparser import ConfigParser | |
dir_path = '.' | |
config = ConfigParser() | |
config_file = dir_path+'/config.ini' | |
config.read(config_file) | |
api_key=config['zendesk']['api_key'] | |
email = config['zendesk']['email'] | |
subdomain = config['zendesk']['subdomain'] | |
user = 'USEREMAIL' + '/token' | |
pwd = api_key | |
def get_voice_comments(ticket_ids): | |
all_voice_comment_dfs = [] | |
total_tickets = len(ticket_ids) | |
for i, ticket_id in enumerate(ticket_ids): | |
event_dfs = [] | |
url = f'https://YOUR-ZENDESK-DOMAIN.com/api/v2/tickets/{ticket_id}/audits.json' | |
response = requests.get(url, auth=(user, pwd)) | |
response_json = response.json() | |
event_dfs = [] | |
for audit in response_json['audits']: | |
try: | |
audit_events_df = json_normalize(audit['events'],sep='_') | |
event_dfs.append(audit_events_df) | |
except Exception as ex: | |
print(ex) | |
continue | |
all_audit_events_df = pd.concat(event_dfs,ignore_index=True, sort=False) | |
voice_comment_df = all_audit_events_df[all_audit_events_df['type'].str.lower().str.contains('voice')] | |
if i % 100 == 0 and i != 0: | |
print(i / total_tickets * 100.0, "% of tickets completed") | |
print(len(all_voice_comment_dfs), ' dfs extracted with voice comments') | |
if voice_comment_df.empty == False: | |
voice_comment_df['ticket_id'] = ticket_id | |
all_voice_comment_dfs.append(voice_comment_df) | |
else: | |
continue | |
voice_comment_df = pd.concat(all_voice_comment_dfs, ignore_index=True, sort=False) | |
#voice_comment_df.to_csv('./voice_comments.csv',index=False) | |
return voice_comment_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment