Exemple #1
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 = 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)
Exemple #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)
Exemple #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)
Exemple #4
0
 def _get_score(self, nick):
     score = utils.read_file(self.name, nick)
     if score is None:
         return None
     try:
         return int(score)
     except ValueError:
         return None
Exemple #5
0
 def _get_score(self, nick):
     score = utils.read_file(self.name, nick)
     if score is None:
         return None
     try:
         return int(score)
     except ValueError:
         return None
Exemple #6
0
    def weather(self, message, params=None, **kwargs):
        """Display current weather report (ex: .w [set] [<location>])"""
        wunderground = utils.get_config("Wunderground")
        api_key = wunderground.get("key")

        if params:
            location = params
            if location.startswith("set "):
                location = location[4:]
                utils.write_file(self.name, message.source, location)
                message.dispatch("Location information saved")
        else:
            location = utils.read_file(self.name, message.source)
            if not location:
                message.dispatch(self.weather.__doc__)
                return

        try:
            w = pywunderground.request(api_key, ["conditions"], location)
        except Exception:
            message.dispatch("Unable to fetch weather data")
            return

        if w.get("current_observation"):
            w = w["current_observation"]

            city = w["display_location"]["full"]
            zip_code = w["display_location"]["zip"]
            temp = w["temperature_string"]
            humidity = w["relative_humidity"]
            wind = w["wind_string"]
            condition = w["weather"]

            zip_code = "" if zip_code == "00000" else " %s" % zip_code
            humidity = "N/A%" if len(humidity) > 3 else humidity

            result = ("%s%s: %s   Humidity: %s   Wind: %s   %s") % (city,
                                                                    zip_code,
                                                                    temp,
                                                                    humidity,
                                                                    wind,
                                                                    condition)

            message.dispatch(result)
        else:
            message.dispatch("Location not found: '%s'" % location)
Exemple #7
0
    def wunderground(self, message, params=None, **kwargs):
        """Display current weather report (ex: .w [set] [<location>])."""
        wunderground = utils.get_config("Wunderground")
        api_key = wunderground.get("key")

        if params:
            location = params
            if location.startswith("set "):
                location = location[4:]
                utils.write_file(self.name, message.source, location)
                message.dispatch("Location information saved.")
        else:
            location = utils.read_file(self.name, message.source)
            if not location:
                message.dispatch(self.wunderground.__doc__)
                return

        try:
            w = pywunderground.request(api_key, ["conditions"], location)
        except Exception:
            message.dispatch("Unable to fetch Wunderground data.")
            return

        if "current_observation" in w:
            w = w["current_observation"]

            city = w["display_location"]["full"]
            zip_code = w["display_location"]["zip"]
            temp = w["temperature_string"]
            humidity = w["relative_humidity"]
            wind = w["wind_string"]
            condition = w["weather"]

            zip_code = "" if zip_code == "00000" else " %s" % zip_code
            humidity = "N/A%" if len(humidity) > 3 else humidity

            result = "%s%s: %s   Humidity: %s   Wind: %s   %s" % (city,
                                                                  zip_code,
                                                                  temp,
                                                                  humidity,
                                                                  wind,
                                                                  condition)
            message.dispatch(result)
        else:
            message.dispatch("Location not found: '%s'" % location)
Exemple #8
0
def get_paste(paste_id, raw=None):
    """Fetch and return a paste."""
    stats = os.stat(utils.get_directory("pastes") + paste_id)
    st_mtime = time.ctime(stats.st_mtime)
    st_size = stats.st_size

    paste = utils.read_file("pastes", paste_id)

    if not paste:
        flask.abort(404)

    if raw:
        return flask.Response(paste, status=200, mimetype="text/plain")

    return flask.render_template(
        "paste.html",
        paste_id=paste_id,
        paste=cgi.escape(paste),
        st_mtime=st_mtime,
        st_size=st_size,
        version=version.version_string())
Exemple #9
0
 def test_read_file(self):
     self.assertEquals(utils.read_file("tests", "pyhole_test_file"), "foo")
Exemple #10
0
 def test_read_file(self):
     self.assertEquals(utils.read_file("tests", "pyhole_test_file"), "foo")
Exemple #11
0
 def _load_cached_xsa_data(self):
     data_json = utils.read_file(self.name, "xsas.json")
     if data_json is None:
         return {}
     return json.loads(data_json)
Exemple #12
0
 def _load_cached_xsa_data(self):
     data_json = utils.read_file(self.name, "xsas.json")
     if data_json is None:
         return {}
     return json.loads(data_json)