Exemplo n.º 1
0
class AudioFetcher(object):
    logger = logging.getLogger("wechat")

    def __init__(self, cfg):
        super(AudioFetcher, self).__init__()
        self.ekho = None
        if cfg.enable_ekho:
            from ekho import Ekho
            self.ekho = Ekho(cfg.words_audio_folder)
        self.qiniq = None
        if cfg.enable_qiniu:
            self.qiniu = QiniuStorage(cfg)

        # wav_folder="wav",mp3_folder="mp3",words_audio_folder="words_audio"
        #_url_prefix = "http://www.l2china.com/yueyu/sounds/"
        self.url_prefixs = [
            "http://www.yueyv.cn/sound/",
            "http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/"
        ]
        self.wav_folder = cfg.wav_folder
        self.mp3_folder = cfg.mp3_folder
        self.words_audio_folder = cfg.words_audio_folder
        self.audio_file_url_prefix = cfg.audio_file_url_prefix

    def get_result_audio_url(self, result):
        if self.qiniu:
            url = self.qiniu.check_existance_and_get_url(result.get_filename())
            if url:
                return url
        if self.ekho:
            audio_filepath = self.ekho.get_pronounces_mp3(result)
        else:
            audio_filepath = self.get_pronounces_mp3(result.pronounce_list)

        if self.qiniu:
            return self.qiniu.upload_and_get_url(audio_filepath)
        else:
            name = os.path.basename(audio_filepath)
            return self.audio_file_url_prefix + name

    def get_pronounces_audio_url(self, pronounce_list):
        audio_filepath = self.get_pronounces_mp3(pronounce_list)
        if self.qiniu:
            return self.qiniu.upload_and_get_url(audio_filepath)
        else:
            name = os.path.basename(audio_filepath)
            return cfg.audio_file_url_prefix + name

    def download_pronounce_file(self, pronounce):
        for url_prefix in self.url_prefixs:
            try:
                filename = pronounce + ".wav"
                url = url_prefix + filename
                urlretrieve(url, os.path.join(self.wav_folder, filename))
                self.logger.info("{0} downloaded".format(filename))
                return
            except Exception, e:
                self.logger.exception("downloaded pronounce %s from %s error" %
                                      (url_prefix, pronounce))
        raise Exception("downloaded pronounce %s error" % pronounce)
Exemplo n.º 2
0
class AudioFetcher(object):
	logger = logging.getLogger("wechat")

	def __init__(self, cfg):
		super(AudioFetcher, self).__init__()
		self.ekho = None
		if cfg.enable_ekho:
			from ekho import Ekho
			self.ekho = Ekho(cfg.words_audio_folder)
		self.qiniq = None
		if cfg.enable_qiniu:
			self.qiniu = QiniuStorage(cfg)

		# wav_folder="wav",mp3_folder="mp3",words_audio_folder="words_audio"
		#_url_prefix = "http://www.l2china.com/yueyu/sounds/"
		self.url_prefixs = ["http://www.yueyv.cn/sound/","http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/"]
		self.wav_folder = cfg.wav_folder
		self.mp3_folder = cfg.mp3_folder
		self.words_audio_folder = cfg.words_audio_folder
		self.audio_file_url_prefix = cfg.audio_file_url_prefix


	def get_result_audio_url(self, result):
		if self.qiniu:
			url = self.qiniu.check_existance_and_get_url(result.get_filename())
			if url:
				return url
		if self.ekho:
			audio_filepath = self.ekho.get_pronounces_mp3(result)
		else:
			audio_filepath = self.get_pronounces_mp3(result.pronounce_list)
		
		if self.qiniu:
			return self.qiniu.upload_and_get_url(audio_filepath)
		else:
			name = os.path.basename(audio_filepath)	
			return self.audio_file_url_prefix + name
	
	def get_pronounces_audio_url(self, pronounce_list):
		audio_filepath = self.get_pronounces_mp3(pronounce_list)
		if self.qiniu:
			return self.qiniu.upload_and_get_url(audio_filepath)
		else:
			name = os.path.basename(audio_filepath)
			return cfg.audio_file_url_prefix + name

	def download_pronounce_file(self, pronounce):
		for url_prefix in self.url_prefixs:
			try:
				filename = pronounce + ".wav"
				url = url_prefix + filename
				urlretrieve(url,os.path.join(self.wav_folder,filename))
				self.logger.info("{0} downloaded".format(filename))
				return
			except Exception, e:
				self.logger.exception("downloaded pronounce %s from %s error" % (url_prefix, pronounce))
		raise Exception("downloaded pronounce %s error" % pronounce)
Exemplo n.º 3
0
    def __init__(self, cfg):
        super(AudioFetcher, self).__init__()
        self.ekho = None
        if cfg.enable_ekho:
            from ekho import Ekho
            self.ekho = Ekho(cfg.words_audio_folder)
        self.qiniq = None
        if cfg.enable_qiniu:
            self.qiniu = QiniuStorage(cfg)

        # wav_folder="wav",mp3_folder="mp3",words_audio_folder="words_audio"
        #_url_prefix = "http://www.l2china.com/yueyu/sounds/"
        self.url_prefixs = [
            "http://www.yueyv.cn/sound/",
            "http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/"
        ]
        self.wav_folder = cfg.wav_folder
        self.mp3_folder = cfg.mp3_folder
        self.words_audio_folder = cfg.words_audio_folder
        self.audio_file_url_prefix = cfg.audio_file_url_prefix
Exemplo n.º 4
0
	def __init__(self, cfg):
		super(AudioFetcher, self).__init__()
		self.ekho = None
		if cfg.enable_ekho:
			from ekho import Ekho
			self.ekho = Ekho(cfg.words_audio_folder)
		self.qiniq = None
		if cfg.enable_qiniu:
			self.qiniu = QiniuStorage(cfg)

		# wav_folder="wav",mp3_folder="mp3",words_audio_folder="words_audio"
		#_url_prefix = "http://www.l2china.com/yueyu/sounds/"
		self.url_prefixs = ["http://www.yueyv.cn/sound/","http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/"]
		self.wav_folder = cfg.wav_folder
		self.mp3_folder = cfg.mp3_folder
		self.words_audio_folder = cfg.words_audio_folder
		self.audio_file_url_prefix = cfg.audio_file_url_prefix