def create_paste(): """Create a new paste.""" try: paste = flask.request.get_json()["paste"] except KeyError: flask.abort(422) file_name = str(uuid.uuid4()).replace("-", "") utils.write_file("pastes", file_name, paste) response = "%s/%s" % (flask.request.url, file_name) try: # NOTE(jk0): If these items exist, write them to the queue. item = ( flask.request.get_json()["network"], flask.request.get_json()["target"], response ) QUEUE.put(item) except KeyError: pass return flask.redirect(response)
def _adjust_score(self, message, nick, delta): score = self._get_score(nick) if score is None: score = 0 score += delta utils.write_file(self.name, nick, str(score)) message.dispatch(score)
def poll_xsa_list(self, message, params=None, **kwargs): old_data = self._load_cached_xsa_data() new_data = self._fetch_xsa_data() if not new_data: return new_data_json = json.dumps(new_data) utils.write_file(self.name, 'xsas.json', new_data_json) # Notify subscribed channels on new XSAs old_xsa_ids = set(old_data.keys()) new_xsa_ids = set(new_data.keys()) added_xsa_ids = new_xsa_ids - old_xsa_ids if old_xsa_ids and added_xsa_ids: self._notify_new_xsas(added_xsa_ids, new_data)
def poll_xsa_list(self, message, params=None, **kwargs): old_data = self._load_cached_xsa_data() new_data = self._fetch_xsa_data() if not new_data: return new_data_json = json.dumps(new_data) utils.write_file(self.name, "xsas.json", new_data_json) # Notify subscribed channels on new XSAs old_xsa_ids = set(old_data.keys()) new_xsa_ids = set(new_data.keys()) added_xsa_ids = new_xsa_ids - old_xsa_ids if old_xsa_ids and added_xsa_ids: self._notify_new_xsas(added_xsa_ids, new_data)
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)
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)
def setUp(self): utils.write_file("tests", "pyhole_test_file", "foo") self.new_file = utils.get_home_directory() + "tests/pyhole_test_file" self.cpid = os.getpid()