mime_message.py
Go to the documentation of this file.
00001 # !/usr/bin/env python
00002 # !-*- coding: utf-8 -*-
00003 
00004 # import python libraries
00005 import email
00006 
00007 # local library
00008 import stml_type
00009 
00010 
00011 __all__ = ['MIMEMessage']
00012 
00013 
00014 class MIMEMessage(object):
00015     @classmethod
00016     def create_http_header(cls, cookie=None):
00017         headers = {
00018             'Content-Type': 'application/x-www-form-urlencoded',
00019             'Cache-Control': 'no-cache',
00020             'pragma': 'no-cache',
00021         }
00022 
00023         # add cookie data to http header
00024         if cookie is not None:
00025             headers['cookie'] = cookie
00026         return headers
00027 
00028     @classmethod
00029     def create_header_request(cls, language, max_nbest):
00030         stml_str = cls._create_stml(language, max_nbest)
00031         boundary = cls._create_mime_boundary_string()
00032         message = cls._create_mime_multipart(boundary)
00033         message += "--{}\r\n".format(boundary)
00034         message += cls._create_mime_text(stml_str)
00035         message += "--{}--\r\n".format(boundary)
00036         return message
00037 
00038     @classmethod
00039     def create_finish_request(cls):
00040         boundary = cls._create_mime_boundary_string()
00041         message = cls._create_mime_multipart(boundary)
00042         message += "--{}\r\n".format(boundary)
00043         message += cls._create_mime_text('')
00044         message += "--{}--\r\n".format(boundary)
00045         return message
00046 
00047     @classmethod
00048     def create_data_request(cls, data):
00049         boundary = cls._create_mime_boundary_string()
00050         message = cls._create_mime_multipart(boundary)
00051         message += "--{}\r\n".format(boundary)
00052         message += cls._create_mime_text('')
00053         message += "--{}\r\n".format(boundary)
00054         message += cls._create_mime_binary(data)
00055         message += "--{}--".format(boundary)
00056         return message
00057 
00058     @classmethod
00059     def create_bundle_request(cls, stml_str, data):
00060         boundary = cls._create_mime_boundary_string()
00061         message = cls._create_mime_multipart(boundary)
00062         message += "--{}\r\n".format(boundary)
00063         message += cls._create_mime_text(stml_str)
00064         message += "--{}\r\n".format(boundary)
00065         message += cls._create_mime_binary(data)
00066         message += "--{}--".format(boundary)
00067         return message
00068 
00069     @classmethod
00070     def _create_stml(cls, language, max_nbest):
00071         """ create stml string
00072         """
00073         form = stml_type.STMLType()
00074         form.add_version('1.1')
00075         form.add_utterance_id('10')
00076 
00077         user_form = stml_type.UserFormat()
00078         user_form.add_id('S2TDU0100A0000UU0150NICTS2SSample-Unknown')
00079         form.add_user(user_form)
00080 
00081         sr_in = stml_type.SRInputType()
00082         sr_in.add_language(language)
00083         sr_in.add_domain('Travel')
00084         sr_in.add_max_nbest(str(max_nbest))
00085 
00086         voice = stml_type.VoiceFormat()
00087         voice.add_id('INPUT')
00088         sr_in.add_voice(voice)
00089 
00090         output_form = stml_type.OutputTextFormat()
00091         output_form.add_form('SurfaceForm')
00092         sr_in.add_output_text_format(output_form)
00093 
00094         # Endian : Little
00095         # Audio Format : RAW
00096         input_form = stml_type.InputAudioFormat()
00097         input_form.add_endian('Little')
00098         input_form.add_audio('RAW')
00099         sr_in.add_input_audio_format(input_form)
00100         form.add_sr_in(sr_in)
00101         return form.tostring()
00102 
00103     @classmethod
00104     def _create_mime_boundary_string(cls):
00105         """ create mime multipart boundary string
00106         """
00107         boundary = "--ohgaohdaghahdfaldkhf"
00108         return boundary
00109 
00110     @classmethod
00111     def _create_mime_multipart(cls, boundary_str):
00112         """ create mime mutipart string
00113         """
00114         message = "Message-ID: {}\r\n".format(email.utils.make_msgid())
00115         message += "MIME-Version: 1.0\r\n"
00116         message += "Content-Type: multipart/mixed;\r\n"
00117         message += "\tboundary=\"{}\"\r\n\r\n".format(boundary_str)
00118         return message
00119 
00120     @classmethod
00121     def _create_mime_text(cls, text=''):
00122         """ create mime text string
00123 
00124         @param text:
00125         @return: mime text
00126         """
00127         message = "Content-Type: text/xml;charset=utf-8\r\n"
00128         message += "Content-Transfer-Encoding: 7bit\r\n\r\n"
00129         message += text
00130         message += "\r\n"
00131         return message
00132 
00133     @classmethod
00134     def _create_mime_binary(cls, audio_data):
00135         """ create mime binary part
00136 
00137         @param audio_data:
00138         @param return: mime message
00139         """
00140         message = "Content-Type: application/octet-stream\r\n"
00141         message += "Content-Transfer-Encoding: binary\r\n\r\n"
00142         message += audio_data
00143         message += "\r\n"
00144         return message


rospeex_core
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:10