-
Notifications
You must be signed in to change notification settings - Fork 2
/
rightcall.py
executable file
·67 lines (59 loc) · 2.58 KB
/
rightcall.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 1. Download mp3 files from www.prosodie.com to local machine.
# 2. Upload mp3 files from local machine to 'mp3.rightcall' AWS S3 bucket.
# 3. Trinscribe mp3 files from 'mp3.rightcall' AWS S3 bucket to
# 'transcribe.rightcall' AWS S3 bucket.
# 4. Save data to Google Sheets.
import json
from os import remove
from os.path import basename, join
import boto3
from comprehend import get_sentiment
from sheets import append_row
from text import check_promo
transcribe_bucket_name = 'transcribe.rightcall'
mp3_bucket_name = 'mp3.rightcall'
def main(transcribe_bucket_name, mp3_bucket_name):
"""Right Call/Contact Center Monitoring written in Python"""
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
if bucket.name == transcribe_bucket_name:
for key in bucket.objects.all():
if key.key.endswith('.json'):
r = {}
# Get reference number
reference = basename(key.key).replace('.json', '')
r['ref'] = reference
# Get URL
location = boto3.client('s3') \
.get_bucket_location(
Bucket=mp3_bucket_name)['LocationConstraint']
base_url = join('https://s3-%s.amazonaws.com' % location,
mp3_bucket_name)
url = join(base_url, key.key.replace('.json', '.mp3'))
r['url'] = url
# Download json file
try:
s3.Bucket(transcribe_bucket_name) \
.download_file(key.key, key.key)
except Exception as exception:
return 1
# Get text
with open(key.key, 'r') as f:
data = json.load(f)
text = data['results']['transcripts'][0]['transcript']
r['text'] = text
# Get sentiment
sentiment = get_sentiment(text)
r['sentiment'] = sentiment
# Check promotion
promo = check_promo(text)
r['promo'] = promo
# Save to Gooogle Sheets
values = [r['ref'], r['text'], r['promo'], r['sentiment'],
r['url']]
append_row(values)
# Remove tmp json file from local machine
remove(key.key)
main(transcribe_bucket_name, mp3_bucket_name)