Created
May 15, 2015 13:18
-
-
Save jwinternheimer/5ddcc47af63148b4023d to your computer and use it in GitHub Desktop.
Grab Helpscout Conversations and Store in JSON File
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 | |
import io | |
from requests.auth import HTTPBasicAuth | |
api_key = 'daa5ba542ef9e029ec90316f21647d97d74c20f5' | |
pages=50 | |
page=1 | |
while page < pages + 1: | |
raw_result = requests.get('https://api.helpscout.net/v1/mailboxes/%s/conversations.json?tag=%s&page=%s' % (320, 'pp business', page), auth=HTTPBasicAuth(api_key, 'pass')) | |
result = json.loads(raw_result.text) | |
pages = result['pages'] | |
page = result['page'] + 1 | |
for item in result['items']: | |
conversation_id = item['id'] | |
raw_result = requests.get('https://api.helpscout.net/v1/conversations/%s.json' % (conversation_id), auth=HTTPBasicAuth(api_key, 'pass')) | |
result = json.loads(raw_result.text) | |
# Find the email that the customer wrote in with originally. | |
num_threads = len(result['item']['threads']) | |
initial_email = result['item']['threads'][num_threads-1]['body'] | |
# Remove any browser info we added. | |
browser_info_index = initial_email.find('<hr/>\n<p>Browser: ') | |
if browser_info_index != -1: | |
initial_email = initial_email[0:browser_info_index] | |
# Also, for now, get rid of any HTML-y emails | |
if initial_email.find('</') != -1: | |
continue | |
print initial_email | |
with io.open("./hs/%s.json" % 'business', "a", encoding='utf8') as inbox_file: | |
raw_output = json.dumps(initial_email,ensure_ascii=False) | |
inbox_file.write(unicode('%s\n' % raw_output)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment