示例#1
0
 def action(self, query):
     query_text = query.get_text()
     query_text = str(
         TextBlob(query_text).translate(from_lang="de", to="en"))
     communicator = query.get_communicator()
     if "gro" in query.get_text() and "big" in query_text:
         # quick fix because translate always translates gross to big instead of tall and I somehow often ask how
         # tall people are
         query_text = query_text.replace("big", "tall")
     try:
         answer = self.main_module.get_wolfram_alpha_answer(query_text)
         answer = str(TextBlob(answer).translate(from_lang="en", to="de"))
         communicator.say(answer)
     except ImpossibleActionError:
         # try Wikipedia
         query_text = self.convert_query(query.get_text())
         if query_text is None:
             raise ImpossibleActionError(
                 "da kann ich dir leider nicht weiterhelfen.")
         if query_text:
             query_text = str(query_text.title())
             communicator.say(
                 self.main_module.get_wikipedia_description(
                     query_text, self.language))
         else:
             raise ImpossibleActionError(
                 "da kann ich dir leider nicht weiterhelfen.")
示例#2
0
    def continue_playback():
        spotify = Spotify()
        mpd_controller = MpdController.get_instance()
        song = spotify.get_currently_playing()
        if song is None:
            # TODO localize
            raise ImpossibleActionError("There is currently no song playing!")
        else:
            time = song["progress_ms"] // 1000
            mpd_controller.play_tid(song["item"]["uri"], time)
            if song["context"] is not None:
                if song["context"]["type"] == "playlist":
                    # fetch rest of the playlist
                    mpd_controller.add_to_current(spotify.read_playlist(song["context"]["uri"], song["item"]["uri"]))
                    return

                if song["context"]["type"] == "album":
                    # fetch rest of the album
                    mpd_controller.add_to_current(spotify.get_album_tracks(song["context"]["uri"], song["item"]["uri"]))
                    return

                if song["context"]["type"] == "artist":
                    # fetch rest of the artist songs
                    mpd_controller.add_to_current(spotify.get_artist_songs(song["context"]["uri"], song["item"]["uri"]))
                    return

                raise ModuleError("Spotify", "Cannot process current context: " + song["context"]["type"])
                    # TODO implement other contexts
            else:
                mpd_controller.add_to_current(spotify.get_saved(10, song["item"]["uri"]))
示例#3
0
 def fan_off(self):
     try:
         self.bridge.set_group(3, 'on', False)
     except phue.PhueRequestTimeout:
         raise ImpossibleActionError("Connection timed out.")
     except Exception as e:
         raise ModuleError(self.module_name, str(e))
示例#4
0
 def action(self, query):
     query_text = query.get_text()
     communicator = query.get_communicator()
     try:
         communicator.say(
             self.main_module.get_wolfram_alpha_answer(query_text))
     except ImpossibleActionError as e:
         # try Wikipedia
         query_text = self.convert_query(query.get_text())
         if query_text is None:
             raise ImpossibleActionError("I can't help you")
         if query_text:
             query_text = str(query_text.title())
             communicator.say(
                 self.main_module.get_wikipedia_description(
                     query_text, self.language))
         else:
             raise ImpossibleActionError("I can't help you")
示例#5
0
    def get_answer(self, query):
        query = self.convert_query(query)
        try:
            url = "http://api.wolframalpha.com/v1/result?appid=" + self.api_key + "&i=" + query

            response = urllib.request.urlopen(url).read().decode('utf-8')
        except:
            # TODO localize
            raise ImpossibleActionError("nope.")
        return response
示例#6
0
 def configure(self):
     try:
         bridge_ip = Config.get_instance().get("hue", "bridge_ip")
         self.bridge = Bridge(bridge_ip)
     except phue.PhueRegistrationException:
         # TODO figure out how to localize error message here
         raise ImpossibleActionError(
             "You have to press the button on your bridge first!")
     except Exception as e:
         raise ModuleError(self.module_name, str(e))
示例#7
0
 def get_wolfram_alpha_answer(self, query):
     query = self.convert_query(query)
     if self.conversationID is None:
         url = self.url + "?appid=" + self.api_key + "&i=" + query
     else:
         url = self.url + "?appid=" + self.api_key + "&conversationid=" + self.conversationID + "&i=" + query
         if self.s is not None:
             url = url + "&s=" + self.s
     response = urllib.request.urlopen(url).read().decode('utf-8')
     result = json.loads(response)
     if "error" in result.keys():
         # reset values
         self.url = "http://api.wolframalpha.com/v1/conversation.jsp"
         self.conversationID = None
         self.s = None
         raise ImpossibleActionError("even Wolfram Alpha cannot help")
     self.url = "http://" + result["host"] + "/api/v1/conversation.jsp"
     self.conversationID = result["conversationID"]
     if "s" in result.keys():
         self.s = result["s"]
     else:
         self.s = None
     return Co.get_sentences(result["result"], 1)