Created
March 1, 2017 18:32
-
-
Save dpawluk/2b7af5179e326e85a4ce181b48d1eca5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 requests | |
| import json | |
| from itertools import zip_longest | |
| import pandas as pd | |
| import numpy as np |
This file contains hidden or 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
| #create the dataframe, list comprehension creates an array of rows based on the column names we got | |
| #Let pandas do the hard part with csvs | |
| csv_frame = pd.DataFrame([[i[j] for j in col_keys] for i in all_rows_as_obj], columns=col_keys) |
This file contains hidden or 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
| #coerce ticket_id to be int not float | |
| csv_frame['ticket_id'] = csv_frame['ticket_id'].fillna(0).astype(np.int64) | |
| #fill blank values with the string 'N/A' | |
| csv_frame = csv_frame.fillna("N/A") | |
| #output to a file | |
| csv_frame.to_csv('october_stats.csv') |
This file contains hidden or 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
| config = { | |
| "username": "EMAILGOESHERE", | |
| "password": "PASSWORDGOESHERE", | |
| "base_path": "https://www.zopim.com/api/v2" | |
| } |
This file contains hidden or 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
| #Since we are hitting the same subdomain with same endpoints lets take care of headers and auth just one time and reuse | |
| s = requests.Session() | |
| s.auth = (config['username'], config['password']) | |
| s.headers.update({"content-type":"application/json"}) |
This file contains hidden or 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
| #This is a little helper funciton that will take x of something, and let you chunk through a subset | |
| #Note that 'n' is the chunk size | |
| def _grouper(n, iterable, fillvalue=None): | |
| args = [iter(iterable)] * n | |
| return zip_longest(fillvalue=fillvalue, *args) |
This file contains hidden or 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
| #Since you want a report by month, lets just create a function to get all the chat IDs for a two digit month and 4 digit year | |
| def get_chat_ids_for_month(year, month): | |
| all_chat_ids = [] | |
| search_query = "timestamp:[{0}-{1}-01 TO {0}-{1}-31]".format(year, month) | |
| params = {"q":search_query} | |
| url = "{0}/chats/search".format(config['base_path']) | |
| while url: | |
| r = s.get(url, params=params) | |
| if r.status_code == 200: | |
| response = json.loads(r.text) | |
| for chat in response['results']: | |
| all_chat_ids.append(chat['id']) | |
| url = response['next_url'] | |
| else: | |
| print("A request failed in the loop") | |
| return all_chat_ids |
This file contains hidden or 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
| def build_csv_data(queried_id_list): | |
| all_chats_for_csv = [] | |
| for chunk in _grouper(50, queried_id_list): | |
| filtered = [id for id in chunk if id is not None] #Only needed for last set of 50 that will have some sort of remainder filled with 'None' values by the grouper() function | |
| url = "{0}/chats".format(config['base_path']) | |
| stringified_ids = ",".join(filtered) | |
| params = {"ids":stringified_ids} | |
| r = s.get(url, params=params) | |
| if r.status_code == 200: | |
| response = json.loads(r.text) | |
| docs = response['docs'] | |
| for k in docs: | |
| doc_obj = docs[k] #This is the 'doc' object. Note that some keys may not exist for all items. Duration does not exist for offline messages for example | |
| all_chats_for_csv.append(doc_obj) | |
| else: | |
| print("error getting bulk chats") | |
| return all_chats_for_csv |
This file contains hidden or 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
| #grab the ids for the specified range NOTE!! This is where you can change the year/month for the date range. | |
| queried_id_list = get_chat_ids_for_month(2016, 10) | |
| #turn those into raw objects | |
| csv_obj_data = build_csv_data(queried_id_list) | |
This file contains hidden or 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
| #create a list to hold our rows | |
| all_rows_as_obj = [] | |
| #turn the raw objects into nice and tasty ones that pandas can digest | |
| for record in csv_obj_data: | |
| try: | |
| response_time = record.get('response_time', {}) | |
| count = record.get('count',{}) | |
| csv_obj = { | |
| "id": record['id'], | |
| "agent": ",".join(record.get('agent_names', "N/A")), | |
| "visitor": record['visitor']['name'], | |
| "department": record['department_name'], | |
| "url": record.get('webpath', "N/A"), | |
| "missed": record.get('missed', "N/A"), | |
| "resp_first": response_time.get('first', []), | |
| "resp_max": response_time.get('max', []), | |
| "resp_avg": response_time.get('avg', []), | |
| "start timestamp": record['session']['start_date'], | |
| "end timestamp": record['session']['end_date'], | |
| "total messages": count.get('total', 0), | |
| "Agent Msg Count": count.get('agent', 0), | |
| "Visitor Msg Count": count.get('visitor', 0), | |
| "rating": record.get('rating',[]), | |
| "ticket_id": record['zendesk_ticket_id'] | |
| } | |
| except KeyError as e: | |
| print(e) | |
| print("key error occured for record with id: {}".format(record['id'])) | |
| print(record) | |
| pass | |
| all_rows_as_obj.append(csv_obj) |
This file contains hidden or 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
| #with prettier objects our column names can just come from a call to the first object's keys | |
| col_keys = all_rows_as_obj[0].keys() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment