-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhs-getconversations.py
executable file
·73 lines (57 loc) · 2.01 KB
/
hs-getconversations.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
71
72
73
#!/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 conversations')
parser.add_argument('--token-id', required=True, help='HS auth access token')
parser.add_argument('--outfile', required=True, help='File to save conversation list')
parser.add_argument('--loglevel', default='INFO')
args = parser.parse_args()
logging.basicConfig(stream=sys.stderr, level=args.loglevel)
url = "https://api.helpscout.net/v2/conversations"
headers = {
'Authorization': f'Bearer {args.token_id}'
}
payload = {
'status': 'all'
}
session = requests.Session()
# first check the number of pages in the output
try:
r = session.get(url, headers=headers, params=payload)
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
conversations = []
page = 1
while (page <= num_pages):
payload = {
'status': 'all',
'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
convs_page = r.json()['_embedded']['conversations']
for conv in convs_page:
conversations.append(conv)
except Exception as err:
logging.error(f"Exception: {err}")
return 1
page += 1
logging.info(f"Number of conversations: {len(conversations)}")
# write conversation list to a file
outf = open(args.outfile, "w")
# pretty print json to file
json.dump(conversations, outf, indent=4)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))