Ejemplo n.º 1
0
    def run(self):
        # get the current weather for NYC from the National Weather Service feed
        weather_xml = get_url(
            'http://forecast.weather.gov/MapClick.php?lat=40.71980&lon=-73.99300&FcstType=dwml'
        )

        if weather_xml is None:
            # there was an error gettting the weather data
            send('NYC Weather',
                 'Sorry, this service is temporarily unavailable',
                 recipient_list=[self.sender],
                 sender=server_auto_email)
        else:
            # parse the report from the xml and auto-reply with it as the message body
            doc = etree.fromstring(weather_xml)

            # find the human-readable text report in the xml
            report = []
            for elem in doc.xpath('//wordedForecast'):
                for subelem in elem.getchildren():
                    if subelem.tag == 'text':
                        report.append(subelem.text)

            # send it back to the sender
            send('NYC Weather',
                 ' '.join(report),
                 recipient_list=[self.sender],
                 sender=server_auto_email)
    def run(self):
        # get the time as an html page result from the US Naval Observatory Master Clock
        time_html = get_url('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

        if time_html is None:
            # there was an error gettting the time data
            send('The Current Time', 'Sorry, this service is temporarily unavailable', recipient_list=[self.sender], sender=server_auto_email)
        else:
            # auto-reply with both the text and html versions of the time report
            time_txt = get_text_from_html(time_html.replace('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final"//EN>', ''))
            send('The Current Time', time_txt, recipient_list=[self.sender], html=time_html, sender=server_auto_email)
Ejemplo n.º 3
0
    def run(self):
        # determine the sender's ip address from the email headers
        ip_address = None
        try:
            headers = self.email_dict['headers']
            for hdr in [
                    'X-Originating-IP',  # preferred header order to use
                    'X-Source-IP',
                    'X-Source',
                    'Received'
            ]:
                if headers.has_key(hdr):
                    match = ip_pattern.search(headers[hdr])
                    if match is not None:
                        ip_address = match.group().strip().replace('[',
                                                                   '').replace(
                                                                       ']', '')
                        break

        except KeyError:
            pass

        if ip_address is not None:
            # use the ip address to get the geographic location
            location = get_location(ip_address)

            try:
                lat = location['Latitude']
                lng = location['Longitude']

                # use the latitude and longitude to get the current report from the forecast.io API
                weather_url = 'https://api.forecast.io/forecast/' + forecast_io_key + '/' + lat + ',' + lng
                weather_data = get_url(weather_url)
                if weather_data is not None:
                    data = json.loads(weather_data)
                    report = data["currently"]["summary"] + '\n\n' + data[
                        "hourly"]["summary"]
                    send('Current Weather',
                         report,
                         recipient_list=[self.sender],
                         sender=server_auto_email)
                    return

            except KeyError:
                pass

        # the default reply, in case the location or weather for that location can't be found
        send(
            'Current Weather',
            'Sorry, this service could not determine the weather for your geographic location',
            recipient_list=[self.sender],
            sender=server_auto_email)
def get_location(ip_address):
    """Call the geolocation API and return a dict of the results, if any,
    including the latitude and longitude"""

    location = {}

    query = get_url(_generate_url(ip_address))
    if query is not None:
        for line in query.splitlines():
            data = line.split(': ')
            if len(data) == 2 and len(data[1]) > 0:
                location[data[0]] = data[1]

    return location
def get_location (ip_address):
    """Call the geolocation API and return a dict of the results, if any,
    including the latitude and longitude"""

    location = {}

    query = get_url(_generate_url(ip_address))
    if query is not None:
        for line in query.splitlines():
            data = line.split(': ')
            if len(data) == 2 and len(data[1]) > 0:
                location[data[0]] = data[1]

    return location
    def run(self):
        # determine the sender's ip address from the email headers
        ip_address = None
        try:
            headers = self.email_dict['headers']
            for hdr in ['X-Originating-IP', # preferred header order to use
                        'X-Source-IP',
                        'X-Source',
                        'Received']:
                if headers.has_key(hdr):
                    match = ip_pattern.search(headers[hdr])
                    if match is not None:
                       ip_address = match.group().strip().replace('[','').replace(']', '')
                       break

        except KeyError:
            pass

        if ip_address is not None:
            # use the ip address to get the geographic location
            location = get_location(ip_address)

            try:
                lat = location['Latitude']
                lng = location['Longitude']

                # use the latitude and longitude to get the current report from the forecast.io API
                weather_url  = 'https://api.forecast.io/forecast/'+forecast_io_key+'/'+lat+','+lng
                weather_data = get_url(weather_url)
                if weather_data is not None:
                    data = json.loads(weather_data)
                    report = data["currently"]["summary"] + '\n\n' + data["hourly"]["summary"]
                    send('Current Weather', report, recipient_list=[self.sender], sender=server_auto_email)
                    return

            except KeyError:
                pass

        # the default reply, in case the location or weather for that location can't be found
        send('Current Weather',
             'Sorry, this service could not determine the weather for your geographic location',
             recipient_list=[self.sender],
             sender=server_auto_email)
    def run(self):
        # get the current weather for NYC from the National Weather Service feed
        weather_xml = get_url('http://forecast.weather.gov/MapClick.php?lat=40.71980&lon=-73.99300&FcstType=dwml')

        if weather_xml is None:
            # there was an error gettting the weather data
            send('NYC Weather', 'Sorry, this service is temporarily unavailable', recipient_list=[self.sender], sender=server_auto_email)
        else:
            # parse the report from the xml and auto-reply with it as the message body
            doc = etree.fromstring(weather_xml)

            # find the human-readable text report in the xml
            report = []
            for elem in doc.xpath('//wordedForecast'):
                for subelem in elem.getchildren():
                    if subelem.tag == 'text':
                        report.append(subelem.text)

            # send it back to the sender
            send('NYC Weather', ' '.join(report), recipient_list=[self.sender], sender=server_auto_email)
    def run(self):
        # get the time as an html page result from the US Naval Observatory Master Clock
        time_html = get_url('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

        if time_html is None:
            # there was an error gettting the time data
            send('The Current Time',
                 'Sorry, this service is temporarily unavailable',
                 recipient_list=[self.sender],
                 sender=server_auto_email)
        else:
            # auto-reply with both the text and html versions of the time report
            time_txt = get_text_from_html(
                time_html.replace(
                    '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final"//EN>',
                    ''))
            send('The Current Time',
                 time_txt,
                 recipient_list=[self.sender],
                 html=time_html,
                 sender=server_auto_email)