def test_location(self): name, countryName, lat, lng = location('24060') self.assertEqual(name, "Blacksburg") self.assertEqual(countryName, "United States") self.assertEqual(lat, 37.2295733) self.assertEqual(lng, -80.4139393)
def test_locations(self): def check_places(*args): def validate(actual_name, actual_lat, actual_lon): names = [n.strip() for n in actual_name.split(',')] for arg in args: self.assertIn(arg, names) return validate locations = [ ('92121', check_places("San Diego", "California")), ('94110', check_places("SF", "California")), ('94041', check_places("Mountain View", "California")), ('27959', check_places("Dare County", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('15213', check_places("Allegheny County", "Pennsylvania")), ('90210', check_places("Los Angeles County", "California")), ('33109', check_places("Miami-Dade County", "Florida")), ('80201', check_places("Denver", "Colorado")), ("Berlin", check_places("Berlin", "Deutschland")), ("Paris", check_places("Paris", "France métropolitaine")), ("Vilnius", check_places("Vilnius", "Lietuva")), ('Blacksburg, VA', check_places("Blacksburg", "Virginia")), ('Granger, IN', check_places("Granger", "Indiana")), ] for loc, validator in locations: names, lat, lon = location(loc) validator(names, lat, lon)
def test_locations(self): def check_location(result, expected): self.assertAlmostEqual(result[0], expected[0], delta=0.07) self.assertAlmostEqual(result[1], expected[1], delta=0.07) locations = [ ('92121', (32.9, -117.2)), ('94110', (37.8, -122.4)), ('94041', (37.4, -122.1)), ('27959', (36.0, -75.6)), ('48067', (42.5, -83.1)), ('23606', (37.1, -76.5)), ('23113', (37.5, -77.6)), ('27517', (42.6, -7.8)), ('15213', (40.4, -80.0)), ('90210', (34.1, -118.3)), ('33109', (25.8, -80.1)), ('80201', (22.6, 120.3)), ("Berlin", (52.5, 13.4)), ("Paris", (48.9, 2.4)), ("Vilnius", (54.7, 25.3)), ('Blacksburg, VA', (37.2, -80.4)), ('Granger, IN', (41.7, -86.1)), ] for query, expected in locations: result = weather.location(query) check_location(result, expected)
def test_locations(self): def check_places(*args): def validate(actual_name, actual_lat, actual_lon): names = [n.strip() for n in actual_name.split(',')] for arg in args: self.assertIn(arg, names) return validate locations = [ ('92121', check_places("San Diego", "California")), ('94110', check_places("SF", "California")), ('94041', check_places("Mountain View", "California")), ('27959', check_places("Dare County", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('15213', check_places("Allegheny County", "Pennsylvania")), ('90210', check_places("Los Angeles County", "California")), ('33109', check_places("Miami-Dade County", "Florida")), ('80201', check_places("Denver", "Colorado")), ("Berlin", check_places("Berlin", "Deutschland")), ("Paris", check_places("Paris", "Île-de-France")), ("Vilnius", check_places("Vilnius", "Lietuva")), ('Blacksburg, VA', check_places("Blacksburg", "Virginia")), ('Granger, IN', check_places("Granger", "Indiana")), ] for loc, validator in locations: names, lat, lon = location(loc) validator(names, lat, lon)
def test_locations(self): def check_places(*args): def validate(actual_name, actual_lat, actual_lon): names = [n.strip() for n in actual_name.split(',')] for arg in args: self.assertIn(arg, names) return validate locations = [ ('24060', check_places("Blacksburg", "Virginia")), ('92121', check_places("San Diego", "California")), ('94110', check_places("San Francisco", "California")), ('94041', check_places("Mountain View", "California")), ('27959', check_places("Nags Head", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('46530', check_places("Granger", "Indiana")), ('15213', check_places("Pittsburgh", "Pennsylvania")), ('90210', check_places("Beverly Hills", "California")), ('12144', check_places("Clinton Park", "New York")), ('33109', check_places("Homestead", "Florida")), ('80201', check_places("Denver", "Colorado")), ("Berlin", check_places("Berlin", "Deutschland")), ("Paris", check_places("Paris", "European Union")), ("Vilnius", check_places("Vilnius", "European Union")), ] for loc, validator in locations: names, lat, lon = location(loc) validator(names, lat, lon)
def test_locations(self): def check_places(*args): def validate(actual_name, actual_lat, actual_lon): names = [n.strip() for n in actual_name.split(',')] for arg in args: self.assertIn(arg, names) return validate locations = [ ('24060', check_places("Blacksburg", "Virginia")), ('92121', check_places("San Diego", "California")), ('94110', check_places("San Francisco", "California")), ('94041', check_places("Mountain View", "California")), ('27959', check_places("Nags Head", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('46530', check_places("Granger", "Indiana")), ('15213', check_places("Pittsburgh", "Pennsylvania")), ('90210', check_places("Beverly Hills", "California")), ('12144', check_places("Clinton Park", "New York")), ('33109', check_places("Homestead", "Florida")), ('80201', check_places("Denver", "Colorado")), ("Berlin", check_places("Berlin", "Deutschland")), ("Paris", check_places("Paris", "France métropolitaine")), ("Vilnius", check_places("Vilnius", "Lietuva")), ] for loc, validator in locations: names, lat, lon = location(loc) validator(names, lat, lon)
def test_locations(self): def check_location(result, expected): self.assertAlmostEqual(result[0], expected[0], places=1) self.assertAlmostEqual(result[1], expected[1], places=1) locations = [ ('92121', (32.9, -117.2)), ('94110', (37.8, -122.4)), ('94041', (37.4, -122.1)), ('27959', (36.0, -75.6)), ('48067', (42.5, -83.1)), ('23606', (37.1, -76.5)), ('23113', (37.5, -77.6)), ('27517', (42.6, -7.8)), ('15213', (40.4, -80.0)), ('90210', (34.1, -118.3)), ('33109', (25.8, -80.1)), ('80201', (22.6, 120.3)), ("Berlin", (52.5, 13.4)), ("Paris", (48.9, 2.4)), ("Vilnius", (54.7, 25.3)), ('Blacksburg, VA', (37.2, -80.4)), ('Granger, IN', (41.7, -86.1)), ] for query, expected in locations: result = weather.location(query) check_location(result, expected)
def nws_lookup(jenni, input): ''' Look up weather watches, warnings, and advisories. ''' text = input.group(2) if not text: return jenni.reply('You need to provide some input.') bits = text.split(',') master_url = False if len(bits) == 2: ## county given name, county, state, countryName, lat, lng = weather.location(text) url_part1 = 'http://alerts.weather.gov' state = (state).strip().lower() county = (county).strip().lower() reverse_lookup = list() if len(state) == 2: reverse_lookup = [k for k, v in states.iteritems() if v == state] if reverse_lookup: state = reverse_lookup[0] if state not in states and len(reverse_lookup) < 1: jenni.reply('State not found.') return url1 = county_list.format(states[state]) page1 = web.get(url1).split('\n') prev1 = str() prev2 = str() url_part2 = str() for line in page1: mystr = '>' + unicode(county) + '<' if mystr in line.lower(): url_part2 = prev2[9:40] break prev2 = prev1 prev1 = line if not url_part2: return jenni.reply('Could not find county.') master_url = 'http://alerts.weather.gov/cap/' + url_part2 location = text elif len(bits) == 1: ## zip code if bits[0]: zip_code = bits[0] zips = re_zip.findall(zip_code) if not zips: return jenni.reply('ZIP is invalid.') else: try: zip_code = zips[0][0] except: return jenni.reply('ZIP could not be validated.') urlz = zip_code_lookup.format(zip_code) pagez = web.get(urlz) fips = re_fips.findall(pagez) if fips: state = re_state.findall(pagez) city = re_city.findall(pagez) if not state and not city: return jenni.reply('Could not match ZIP code to a state') try: state = state[0].lower() state = states[state].upper() location = city[0] + ', ' + state fips_combo = unicode(state) + 'C' + unicode(fips[0]) master_url = alerts.format(fips_combo) except: return jenni.reply( 'Could not parse state or city from database.') else: return jenni.reply('ZIP code does not exist.') if not master_url: return jenni.reply( 'Invalid input. Please enter a ZIP code or a county and state pairing, such as \'Franklin, Ohio\'' ) feed = feedparser.parse(master_url) warnings_dict = dict() for item in feed.entries: if nomsg[:51] == colourize(item['title']): return jenni.say(nomsg.format(location)) else: warnings_dict[colourize(unicode(item['title']))] = unicode( item['summary']) if len(warnings_dict) > 0: ## if we have any alerts... ## let us sort it so the most recent thing is first, then second, etc... warn_keys = warnings_dict.keys() find_issue = re.compile('issued (\S+) (\S+) at (\S+):(\S+)(\S)M') warn_keys_dt = dict() for warn in warn_keys: warn_dt = find_issue.findall(warn) if len(warn_dt) > 0: warn_dt = warn_dt[0] month = months[warn_dt[0]] day = int(warn_dt[1]) hour = int(warn_dt[2]) minute = int(warn_dt[3]) if warn_dt[-1] == 'P': if hour < 12: hour += 12 year = datetime.datetime.now().year hour -= 1 warn_keys_dt[warn] = datetime.datetime(year, month, day, hour, minute) warn_list_dt = sorted(warn_keys_dt, key=warn_keys_dt.get, reverse=True) #print 'warn_list_dt', warn_list_dt if input.sender.startswith('#'): ## if queried in channel for key in warn_list_dt: jenni.say(key) jenni.say(more_info.format(location, master_url)) else: ## if queried in private message for key in warn_list_dt: jenni.say(key) jenni.say(warnings_dict[key]) jenni.say(more_info.format(location, master_url))
def nws_lookup(jenni, input): ''' Look up weather watches, warnings, and advisories. ''' text = input.group(2) if not text: return jenni.reply('You need to provide some input.') bits = text.split(',') master_url = False if len(bits) == 2: ## county given name, county, state, countryName, lat, lng = weather.location(text) url_part1 = 'http://alerts.weather.gov' state = (state).strip().lower() county = (county).strip().lower() reverse_lookup = list() if len(state) == 2: reverse_lookup = [k for k, v in states.iteritems() if v == state] if reverse_lookup: state = reverse_lookup[0] if state not in states and len(reverse_lookup) < 1: jenni.reply('State not found.') return url1 = county_list.format(states[state]) page1 = web.get(url1).split('\n') prev1 = str() prev2 = str() url_part2 = str() for line in page1: mystr = '>' + unicode(county) + '<' if mystr in line.lower(): url_part2 = prev2[9:40] break prev2 = prev1 prev1 = line if not url_part2: return jenni.reply('Could not find county.') master_url = 'http://alerts.weather.gov/cap/' + url_part2 location = text elif len(bits) == 1: ## zip code if bits[0]: zip_code = bits[0] zips = re_zip.findall(zip_code) if not zips: return jenni.reply('ZIP is invalid.') else: try: zip_code = zips[0][0] except: return jenni.reply('ZIP could not be validated.') urlz = zip_code_lookup.format(zip_code) pagez = web.get(urlz) fips = re_fips.findall(pagez) if fips: state = re_state.findall(pagez) city = re_city.findall(pagez) if not state and not city: return jenni.reply('Could not match ZIP code to a state') try: state = state[0].lower() state = states[state].upper() location = city[0] + ', ' + state fips_combo = unicode(state) + 'C' + unicode(fips[0]) master_url = alerts.format(fips_combo) except: return jenni.reply('Could not parse state or city from database.') else: return jenni.reply('ZIP code does not exist.') if not master_url: return jenni.reply('Invalid input. Please enter a ZIP code or a county and state pairing, such as \'Franklin, Ohio\'') feed = feedparser.parse(master_url) warnings_dict = dict() for item in feed.entries: if nomsg[:51] == colourize(item['title']): return jenni.say(nomsg.format(location)) else: warnings_dict[colourize(unicode(item['title']))] = unicode(item['summary']) if len(warnings_dict) > 0: ## if we have any alerts... ## let us sort it so the most recent thing is first, then second, etc... warn_keys = warnings_dict.keys() find_issue = re.compile('issued (\S+) (\S+) at (\S+):(\S+)(\S)M') warn_keys_dt = dict() for warn in warn_keys: warn_dt = find_issue.findall(warn) if len(warn_dt) > 0: warn_dt = warn_dt[0] month = months[warn_dt[0]] day = int(warn_dt[1]) hour = int(warn_dt[2]) minute = int(warn_dt[3]) if warn_dt[-1] == 'P': if hour < 12: hour += 12 year = datetime.datetime.now().year hour -= 1 warn_keys_dt[warn] = datetime.datetime(year, month, day, hour, minute) warn_list_dt = sorted(warn_keys_dt, key=warn_keys_dt.get, reverse=True) #print 'warn_list_dt', warn_list_dt if input.sender.startswith('#') and not (input.group(1)).startswith('nws-more'): ## if queried in channel for key in warn_list_dt: jenni.say(key) jenni.say(more_info.format(location, master_url)) else: ## if queried in private message for key in warn_list_dt: jenni.say(key) jenni.say(warnings_dict[key]) jenni.say(more_info.format(location, master_url))