Esempio n. 1
0
    def _request_query(self, text, index):
        """
		Requests a query to the google translate API and 
		returns the .mp3 data. 
		"""
        try:
            q = self._query(text)
            l = len(text)

            url = "http://translate.google.com/translate_tts?tl=en&q=%s&total=%s&idx=%s&client=t&prev=input" % (
                q, l, index)
            logger.info("query reqeust url: %s" % url)

            # headers taken from https://github.com/hungtruong/Google-Translate-TTS
            headers = {
                "Host":
                "translate.google.com",
                "User-Agent":
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) "
                "AppleWebKit/535.19 (KHTML, like Gecko) "
                "Chrome/18.0.1025.163 Safari/535.19"
            }

            req = urllib2.Request(url, "", headers)
            resp = urllib2.urlopen(req)

            return resp.read()

        except urllib2.HTTPError as e:
            logger.error("http error requesting query(index: %s): %s" %
                         (str(index), str(e)))
            return None
Esempio n. 2
0
	def prepare(self):
		"""
		Prepares an alarm before it is set off.
		Queries and music is downloaded, converted and prepared, this usually
		takes around 15 to 30 seconds.
		"""
		self.mp = Player()
		self.qh = QueryHandler()
		self.sc = Soundloader()

		# temp
		# weather goes here
		q = [ "F**k you f****t, have a nice day.",
			"Today has a 97 percent chance of raining.",
			"Good morning." ]

		self.qh.load(q)

		self.query_delay = 1
		self.query_start_delay = 4
		self.query_db_sink = 10
		self.query_duration = self._get_query_duration(self.qh.sound_list)

		logger.info("query duration: %d seconds" % self.query_duration)

		song_path = self.get_song()
		new_song_path = self._prepare_segment_fade(song_path)
		self.mp.load(new_song_path)

		self.prepared = True
Esempio n. 3
0
	def set_volume(percent):
		"""
		Set's the volume of system, duuh.
		"""
		mixer = aa.Mixer(control='PCM')
		mixer.setvolume(percent)
		
		volume = mixer.getvolume()
		logger.info("volume set to: %s" % str(volume))

		return volume
		
Esempio n. 4
0
    def play(self, delay=0):
        """
		Plays a prepared list of queries.
		"""
        logger.info("playing query list...")

        if not self.loaded:
            logger.warning("the query handler must be loaded to continue")
            return

        self._play_queries(delay, self.sound_list)

        logger.debug("played queries successfully...")
Esempio n. 5
0
    def run(self):
        """
		Start the main loop.
		"""
        self.select_next_pending()
        delta_t = self.next_pending.time_pending() - get_date()
        sec_to_next = delta_t.total_seconds()
        sleep_time = sec_to_next - self.pre_wake_time

        if sec_to_next >= self.pre_wake_time:
            logger.info("Sleeping for %d seconds..." % sleep_time)
            time.sleep(sec_to_next - self.pre_wake_time)

        logger.info("alarm going of in %d seconds... preparing..." %
                    self.pre_wake_time)

        # prepare the alarm and shit
        self.next_pending.prepare()
        logger.debug("waiting...")
        while self.next_pending.time_pending() > get_date():
            time.sleep(1)

        logger.info("pling pling pling")
        self.next_pending.set_off()

        logger.info("starting over..")
        self.run()
Esempio n. 6
0
	def set_off(self):
		"""
		Set's off the alarm.
		"""
		if not self.prepared:
			logger.warning("alarm is not prepared... default fallback...")
			# play darude sandstorm

		logger.info("alarm '%s' going off @ %s" % (self.name, get_time()))

		self.mp.play(0, False)
		time.sleep(self.query_start_delay)
		self.qh.play(self.query_delay)

		self.prepared = False
Esempio n. 7
0
    def _get_query(self, save_name, text):
        """
		Takes a given string and converts it into a .mp3 file,
		which can later be converted.
		"""
        try:
            li = text.split(" ")
            q = []
            maxlen = 100
            index = 0

            with open(save_name, "wb") as f:
                for i, w in enumerate(li):
                    neww = w + " "
                    newlist = list(q)
                    newlist.append(neww)
                    newlen = len(" ".join(newlist))

                    if newlen >= maxlen:
                        s = " ".join(q)
                        logger.debug("query string: %s" % s)

                        r = self._request_query(s, index)
                        f.write(r)

                        index += 1
                        q = []
                        q.append(w)
                    else:
                        q.append(neww)

                s = " ".join(q)
                logger.debug("query string: %s" % s)

                r = self._request_query(s, index)
                f.write(r)

            logger.info("successfully retrieved query translation...")
            return True

        except IOError as e:
            err = str(e)
            logger.error("ioerror saving query: %s" % err)

            return False
Esempio n. 8
0
    def _queue_queries(self, queries):
        """
		Retrieves and converts a list of queries 
		and returns a list of the paths to the converted .wav files.
		"""
        sounds = []

        for i, q in enumerate(queries):
            template = "query/query-%d.%s"
            name = template % (i, "mp3")
            ret = self._get_query(name, q)

            if ret:
                wav = convert_mp3(name, True)
                sounds.append(wav)

                logger.info("queued up sound: %s" % wav)
                logger.debug(wav)
            else:
                logger.warning("failed to retreieve query: %s" % wav)

        return sounds
Esempio n. 9
0
    def select_next_pending(self):
        """
		Select and set the next pending alarm.
		"""
        if not (len(self.pool) > 0):
            logger.warning("found no alarms... exiting")
            sys.exit(1)

        date = get_date()
        lowest = self.pool[0]

        for a in self.pool:
            logger.debug("] %s" % str(a.time_pending()))

        for alarm in self.pool:
            if alarm.time_pending() < lowest.time_pending():
                lowest = alarm

        logger.info("next pending alarm (%s) @%s" %
                    (lowest.name, str(lowest.time_pending())))
        self.next_pending = lowest
        return lowest