<commit_before>import urllib
import requests
from xml.etree import ElementTree

class NetVocaloidRequestLib(object):
    URL = "http://192.168.127.12/vws.php"
    KEY = "<KEY>"
    VER = "12010101"

    @classmethod
    def getUrl(cls):
        return cls.URL

    @classmethod
    def getParamDict(cls):
        return {'ver':cls.VER, 'key':cls.KEY}

    @classmethod
    def startCreateVocal(cls, xmlFile):
        files = {'vxml': open(xmlFile, 'rb')}
        params = params=cls.getParamDict()
        params['ope'] = 'request'
        try:
            r = requests.post(cls.getUrl(), params=params, files=files)
            e = ElementTree.fromstring(r.text)
        except Exception as e:
            raise NetVocaloidRequestException(e)
        
        if e.find('status').text == 'error':
            raise NetVocaloidRequestException(e.find('message').text)

        try:
            ticketId = e.find('ticketId').text
            print ticketId
            retryAfter = e.find('retryAfter').text
        
            return ticketId, retryAfter
        except Exception as e:
            raise NetVocaloidRequestException(e)


    @classmethod
    def isDoneCreateVocal(cls, ticketId):
        params = cls.getParamDict()
        params['id'] = ticketId
        params['ope'] = 'query'
        try:
            r = requests.get(cls.getUrl(), params=params)
            e = ElementTree.fromstring(r.text)
            status = e.find('status').text
            if status == 'done':
                return True
            elif status == 'working':
                return False
            else:
                raise NetVocaloidRequestException('create vocal has error')
        except Exception as e:
            raise NetVocaloidRequestException(e)

    @classmethod
    def downloadVocalFile(cls, ticketId, output):
        params = cls.getParamDict()
        params['id'] = ticketId
        params['ope'] = 'query'
        try:
            r = requests.get(cls.getUrl(), params=params)
            print r.text
            e = ElementTree.fromstring(r.text)
            status = e.find('status').text
            if status != 'done':
                raise NetVocaloidRequestException('vocal data is not ready')
            urllib.urlretrieve(e.find('url').text, output)

            params['ope'] = 'done'
            r = requests.get(cls.getUrl(), params=params)
            status = e.find('status').text
            
            if status != 'ok':
                raise NetVocaloidRequestException('download vocal file error')
        except Exception as e:
            raise NetVocaloidRequestException(e)

     
class NetVocaloidRequestException(BaseException):
    pass

    
id =  NetVocaloidRequestLib.startCreateVocal('test.xml')
print id
<commit_msg>add test code<commit_after>import urllib
import requests
from xml.etree import ElementTree

class NetVocaloidRequestLib(object):
    URL = "http://192.168.127.12/vws.php"
    KEY = "<KEY>"
    VER = "12010101"

    @classmethod
    def getUrl(cls):
        return cls.URL

    @classmethod
    def getParamDict(cls):
        return {'ver':cls.VER, 'key':cls.KEY}

    @classmethod
    def startCreateVocal(cls, xmlFile):
        files = {'vxml': open(xmlFile, 'rb')}
        params = params=cls.getParamDict()
        params['ope'] = 'request'
        try:
            r = requests.post(cls.getUrl(), params=params, files=files)
            e = ElementTree.fromstring(r.text)
        except Exception as e:
            raise NetVocaloidRequestException(e)
        
        if e.find('status').text == 'error':
            raise NetVocaloidRequestException(e.find('message').text)

        try:
            ticketId = e.find('ticketId').text
        
            return ticketId
        except Exception as e:
            raise NetVocaloidRequestException(e)


    @classmethod
    def isDoneCreateVocal(cls, ticketId):
        params = cls.getParamDict()
        params['id'] = ticketId
        params['ope'] = 'query'
        try:
            r = requests.get(cls.getUrl(), params=params)
            e = ElementTree.fromstring(r.text)
            status = e.find('status').text
            if status == 'done':
                return True
            elif status == 'working':
                return False
            else:
                raise NetVocaloidRequestException(e.find('message').text)
        except Exception as e:
            raise NetVocaloidRequestException(e)

    @classmethod
    def downloadVocalFile(cls, ticketId, output):
        params = cls.getParamDict()
        params['id'] = ticketId
        params['ope'] = 'query'
        try:
            r = requests.get(cls.getUrl(), params=params)
            e = ElementTree.fromstring(r.text)
            status = e.find('status').text
            if status != 'done':
                raise NetVocaloidRequestException('vocal data is not ready')
            urllib.urlretrieve(e.find('url').text, output)

            params['ope'] = 'done'
            r = requests.get(cls.getUrl(), params=params)
            status = e.find('status').text
            if status != 'done':
                raise NetVocaloidRequestException('download vocal file error')
            else:
                print 'download succeed'
        except Exception as e:
            raise NetVocaloidRequestException(e)

     
class NetVocaloidRequestException(BaseException):
    pass

    
if __name__ == "__main__":
    import time
    id = NetVocaloidRequestLib.startCreateVocal('test.xml')
    while (NetVocaloidRequestLib.isDoneCreateVocal(id) is False):
        print 'createing'
        time.sleep(3)
    NetVocaloidRequestLib.downloadVocalFile(id, "tmp.mp3")

{'fraction_non_alphanumeric': 0.10989977140847547, 'fraction_numerical': 0.00703358537014243, 'mean_word_length': 2.0352187833511204, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4537672', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1590, 'n_words': 354}