示例#1
0
    def weather(self, params=None, **kwargs):
        """Display current weather report (ex: .w [set] [<location>])"""
        if params:
            location = params
            if location.startswith("set "):
                location = location[4:]
                utils.write_file(self.name, self.irc.source, location)
                self.irc.reply("Location information saved")
        else:
            location = utils.read_file(self.name, self.irc.source)
            if not location:
                self.irc.reply(self.weather.__doc__)
                return

        try:
            w = pywapi.get_weather_from_google(location)
        except Exception:
            self.irc.reply("Unable to fetch weather data")
            return

        if w["current_conditions"]:
            city = w["forecast_information"]["city"]
            temp_f = w["current_conditions"]["temp_f"]
            temp_c = w["current_conditions"]["temp_c"]
            humidity = w["current_conditions"]["humidity"]
            wind = w["current_conditions"]["wind_condition"]
            condition = w["current_conditions"]["condition"]

            result = "%s: %sF/%sC   %s   %s   %s" % (city, temp_f, temp_c,
                    humidity, wind, condition)
            self.irc.reply(result)
        else:
            self.irc.reply("Location not found: '%s'" % location)
示例#2
0
文件: tophat.py 项目: fattarsi/pyhole
    def __election(self):
        if self.running:
            LOG.error("ALREADY RUNNING")
            return

        while True:
            if not self.teatime_election.get('timer_expiry'):
                LOG.debug("No expiry, bailing.")
                break

            self.running = True
            time.sleep(30)

            if time.time() > self.teatime_election['timer_expiry']:
                votes = self.__sorted_votes()
                if votes:
                    loc, votes = votes[0]
                    self.irc.reply("Sir, your guests have chosen %s "
                                   "with %d votes" % (loc, len(votes)))
                break

        # End the election
        self.running = False
        utils.write_file(self.name, 'teatime', {'started': False,})

        return
示例#3
0
文件: tophat.py 项目: fattarsi/pyhole
 def load_teatime(self):
     try:
         self.teatime_election = json.loads(utils.read_file(self.name, 'teatime'))
     except (TypeError, ValueError), e:
         self.teatime_election = {'started': False,
                                  'locations': {}}
         utils.write_file(self.name, 'teatime',
                          json.dumps(self.teatime_election))
示例#4
0
    def weather(self, 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, self.irc.source, location)
                self.irc.reply("Location information saved")
        else:
            location = utils.read_file(self.name, self.irc.source)
            if not location:
                self.irc.reply(self.weather.__doc__)
                return

        try:
            w = pywunderground.request(api_key, ["conditions"], location)
        except Exception:
            self.irc.reply("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"]
            heat_index = "%s F (%s C)" % (w["heat_index_f"], w["heat_index_c"])
            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 / Feels Like: %s]   Humidity: %s   "
                    "Wind: %s   %s") % (city, zip_code, temp, heat_index,
                    humidity, wind, condition)

            self.irc.reply(result)
        else:
            self.irc.reply("Location not found: '%s'" % location)
示例#5
0
    def write_scrum(self, _date=None):
        if _date:
            _date = get_date(_date)
        else:
            _date = self.date

        scrums = utils.write_file(os.path.join(self.name, 'scrums'),
                                  formatted_date(_date),
                                  str.join('\n', self.scrum_data))
        return scrums
示例#6
0
 def write_clusters(self, clusters):
     utils.write_file(self.name, 'clusters',
                      json.dumps(clusters))
示例#7
0
 def setUp(self):
     utils.write_file("tests", "pyhole_test_file", "foo")
     self.new_file = utils.get_home_directory() + "tests/pyhole_test_file"
示例#8
0
 def setUp(self):
     utils.write_file("tests", "pyhole_test_file", "foo")
     self.new_file = utils.get_home_directory() + "tests/pyhole_test_file"
示例#9
0
文件: tophat.py 项目: fattarsi/pyhole
    def teatime(self, params=None, **kwargs):
        """Start a poll for 'teatime' locations.
            (ex: .teatime start [at] => start a teatime vote for [at] minutes
                                        defaults to 10 minutes.
                 .teatime suggest [location] => suggest a location
                 .teatime [location] => vote for a location)
        """
        self.load_teatime()

        res = []
        run = True
        if params is None:
            if self.teatime_election.get('started'):
                sorted_votes = self.__sorted_votes()
                if sorted_votes:
                    loc, votes = sorted_votes[0]
                    res.append('Currently, your guests would like tea at %s.' % loc)
                else:
                    res.append("Looks like you're taking tea alone, sir.")

            else:
                res.append("You haven't asked for tea, sir.")
        else:
            if 'start' in params:
                if not self.teatime_election.get('started'):
                    try:
                        timer = int(params[5:] if params[5:] else 10)
                    except ValueError:
                        timer = 10
                    timer *= 60

                    self.teatime_election['started'] = True
                    self.teatime_election['timer_expiry'] = time.time() + timer
                    self.teatime_election['locations'] = {}

                    res.append('The sir would like tea?')
                else:
                    res.append('Sir, please be reasonable... '
                               'two teatimes in one day? '
                               'Are you a hobbit?')
            elif 'stop' in params:
                # End the election
                self.irc.reply("My apologies, sir, perhaps it wasn't teatime after all.")

                self.running = False
                utils.write_file(self.name, 'teatime', {'started': False,})
            elif 'suggest' in params:
                params, loc = params.split('suggest')
                loc = loc.strip(' ')

                loc = self.__suggest(loc)

                res.append('Very good, sir, %s is an excellent choice.' % loc)
            elif 'help' in params:
                res.append(self.teatime.__doc__)
                run = False
            else:
                # Assume this is a location.
                loc = self.__suggest(params)

                res.append('Very good, sir, %s is an excellent choice.' % loc)


        utils.write_file(self.name, 'teatime',
                         json.dumps(self.teatime_election))
        self.irc.reply(str.join(' ', res))
        if run and not self.running:
            self.__election()  # Always make sure we have the elector running
示例#10
0
 def _write_cache(self, data):
     """Write RSS data to cache file."""
     json_data = json.dumps(data)
     utils.write_file(self.name, "rss_cache", json_data)