-
Notifications
You must be signed in to change notification settings - Fork 0
/
hs-getthreads.py
executable file
·70 lines (54 loc) · 1.98 KB
/
hs-getthreads.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import json
import logging
import sys
import requests
def main(arguments):
parser = argparse.ArgumentParser(description='Get list of HelpScout threads for a conversation')
parser.add_argument('--token-id', required=True, help='HS auth access token')
parser.add_argument('--convo-id', required=True, help='conversation id')
parser.add_argument('--outfile', required=True, help='File to save threads')
parser.add_argument('--loglevel', default='INFO')
args = parser.parse_args()
logging.basicConfig(stream=sys.stderr, level=args.loglevel)
url = f"https://api.helpscout.net/v2/conversations/{args.convo_id}/threads"
headers = {
'Authorization': f'Bearer {args.token_id}'
}
session = requests.Session()
# first check the number of pages in the output
try:
r = session.get(url, headers=headers)
r.raise_for_status()
num_pages = r.json()['page']['totalPages']
logging.info(f"Total pages: {num_pages}")
except Exception as err:
logging.error(f"Exception: {err}")
return 1
threads = []
page = 1
while (page <= num_pages):
payload = {
'page': f'{page}'
}
try:
r = session.get(url, headers=headers, params=payload)
r.raise_for_status()
# each page response is json, with an array _embedded.conversations
# containing a list of conversation items
thr_page = r.json()['_embedded']['threads']
for thr in thr_page:
threads.append(thr)
except Exception as err:
logging.error(f"Exception: {err}")
return 1
page += 1
logging.info(f"Number of threads: {len(threads)}")
# write conversation list to a file
outf = open(args.outfile, "w")
# pretty print json to file
json.dump(threads, outf, indent=4)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))