Created
January 7, 2020 19:04
-
-
Save henriquetorquato/d7d0298f422dab7082fd41dd1161a442 to your computer and use it in GitHub Desktop.
Script used to migrate Content Provider combinations to the new Content Assistant application from BLiP: https://portal.blip.ai/
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 json, sys, uuid, os | |
from argparse import ArgumentParser | |
from http import client | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
commands_uri = 'hmg.msging.net' | |
connection = client.HTTPSConnection(commands_uri) | |
def main(args): | |
total = 0 | |
current = 0 | |
def render_progress(): | |
os.system('cls' if os.name == 'nt' else 'clear') | |
print("[%s%s] %i/%i" % ("#"*current, "."*(total - current - 1), current, total - 1)) | |
try: | |
contents = read_content(args.file) | |
headers['Authorization'] = 'Key %s' % args.authorization | |
total = len(contents['data']) | |
for i, content in enumerate(contents['data']): | |
set_content(content) | |
current = i | |
render_progress() | |
finally: | |
connection.close() | |
def set_content(content): | |
body = json.dumps({ | |
'id': str(uuid.uuid1()), | |
'to': '[email protected]', | |
'uri': '/content', | |
'type': 'application/vnd.iris.ai.content-result+json', | |
'method': 'set', | |
'resource': { | |
'id': content['contentId'], | |
'result': { | |
'type': 'text/plain', | |
'content': str(content['text']) | |
}, | |
'combinations': [ | |
{ | |
'intent': content['intent'], | |
'entities': content['entities'] | |
} | |
] | |
} | |
}) | |
connection.request('POST', '/commands', body, headers) | |
response = connection.getresponse() | |
connection.close() | |
return response.status == 200 | |
def read_content(file_path): | |
with open(file_path, 'rb') as file: | |
content = file.read().decode('utf-8') | |
return json.loads(content) | |
if __name__ == '__main__': | |
parser = ArgumentParser() | |
parser.add_argument('-f', '--file', help='File with the exported contents', required=True) | |
parser.add_argument('-a', '--authorization', help='Authorization without `Key`', required=True) | |
main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment