示例#1
0
文件: distance.py 项目: jk0/pyhole
    def distance(self, message, params=None, **kwargs):
        """Display distances (ex: .dist <nick|loc> [to <nick|loc>])."""
        maps_api = utils.get_config("GoogleMaps")
        try:
            key = maps_api.get("key")
        except Exception:
            message.dispatch("No Google Maps API key set.")
            return

        parts = params.split(" to ")
        if not parts:
            message.dispatch(self.distance.__doc__)
            return

        dest_nick = parts[0].strip()

        if len(parts) > 1:
            origin_nick = parts[1].strip()
        else:
            origin_nick = message.source.split("!")[0]

        dest = None
        origin = None
        for filename in utils.list_files("Wunderground"):
            nick = filename.split("!")[0]
            if nick == dest_nick:
                dest = utils.read_file("Wunderground", filename)
            if nick == origin_nick:
                origin = utils.read_file("Wunderground", filename)

        if not dest:
            # They passed in a location
            dest = dest_nick

        if not origin:
            # They passed in a location
            origin = origin_nick

        origin = _resolve_pws(origin)
        dest = _resolve_pws(dest)

        resp = request.get("https://maps.googleapis.com/maps/api"
                           "/directions/json?origin=%s&destination=%s"
                           "&key=%s" % (origin, dest, key))

        msg = None
        if resp.status_code == 200:
            try:
                msg = resp.json()["routes"][0]["legs"][0]["distance"]["text"]
            except IndexError:
                pass

        if not msg:
            msg = "Unable to fetch data from Google Maps."

        message.dispatch(msg)
示例#2
0
    def distance(self, message, params=None, **kwargs):
        """Display distances (ex: .dist <nick|loc> [to <nick|loc>])."""
        maps_api = utils.get_config("GoogleMaps")
        try:
            key = maps_api.get("key")
        except Exception:
            message.dispatch("No Google Maps API key set.")
            return

        parts = params.split(" to ")
        if not parts:
            message.dispatch(self.distance.__doc__)
            return

        dest_nick = parts[0].strip()

        if len(parts) > 1:
            origin_nick = parts[1].strip()
        else:
            origin_nick = message.source.split("!")[0]

        dest = None
        origin = None
        for filename in utils.list_files("Wunderground"):
            nick = filename.split("!")[0]
            if nick == dest_nick:
                dest = utils.read_file("Wunderground", filename)
            if nick == origin_nick:
                origin = utils.read_file("Wunderground", filename)

        if not dest:
            # They passed in a location
            dest = dest_nick

        if not origin:
            # They passed in a location
            origin = origin_nick

        origin = _resolve_pws(origin)
        dest = _resolve_pws(dest)

        resp = utils.fetch_url("https://maps.googleapis.com/maps/api"
                               "/directions/json?origin=%s&destination=%s"
                               "&key=%s" % (origin, dest, key))

        msg = None
        if resp.status_code == 200:
            try:
                msg = resp.json()["routes"][0]["legs"][0]["distance"]["text"]
            except IndexError:
                pass

        if not msg:
            msg = "Unable to fetch data from Google Maps."

        message.dispatch(msg)
示例#3
0
    def distance(self, message, params=None, **kwargs):
        """Display distance between users (ex: .dist <nick|location> [to <nick|location>])"""
        maps_api = utils.get_config("Googlemaps")
        try:
            key = maps_api.get("key")
        except Exception:
            message.dispatch("No Google Maps API key set")
            return

        parts = params.split(' to ')
        if not parts:
            message.dispatch(self.distance.__doc__)
            return

        dest_nick = parts[0].strip()

        if len(parts) > 1:
            origin_nick = parts[1].strip()
        else:
            origin_nick = message.source.split('!')[0]

        dest = None
        origin = None
        for filename in utils.list_files('Weather'):
            nick, ident = filename.split('!')
            if nick == dest_nick:
                dest = utils.read_file('Weather', filename)
            if nick == origin_nick:
                origin = utils.read_file('Weather', filename)

        if not dest:
            # They passed in a location
            dest = dest_nick

        if not origin:
            # They passed in a location
            origin = origin_nick

        resp =  requests.get('https://maps.googleapis.com/maps/api'
                             '/directions/json?origin=%s&destination=%s'
                             '&key=%s' % (origin, dest, key))

        msg = None
        if resp.status_code == 200:
            try:
                msg = resp.json()['routes'][0]['legs'][0]['distance']['text']
            except IndexError:
                pass

        if not msg:
            msg = "Unable to fetch data from Google Maps"

        message.dispatch(msg)
示例#4
0
    def _display_highscores(self, message):
        scores = []
        for nick in utils.list_files(self.name):
            score = self._get_score(nick)
            scores.append((score, nick))

        if not scores:
            message.dispatch("No scores yet.")
            return

        message.dispatch("Highscores (Top 5)")
        message.dispatch("==================")
        scores.sort(reverse=True)
        for score, nick in scores[:5]:
            message.dispatch("%s %s" % (str(score).rjust(4), nick.rjust(13)))
示例#5
0
    def _display_highscores(self, message):
        scores = []
        for nick in utils.list_files(self.name):
            score = self._get_score(nick)
            scores.append((score, nick))

        if not scores:
            message.dispatch("No scores yet")
            return

        message.dispatch("Highscores (Top 5)")
        message.dispatch("==================")
        scores.sort(reverse=True)
        for score, nick in scores[:5]:
            message.dispatch("%s %s" % (str(score).rjust(4),
                                        nick.rjust(13)))