def external_ip(query_url='http://ipv4.icanhazip.com/'): '''Return external IP address. Suggested URIs: * http://ipv4.icanhazip.com/ * http://ipv6.icanhazip.com/ * http://icanhazip.com/ (returns IPv6 address if available, else IPv4) :param str query_url: URI to query for IP address, should return only the IP address as a text string ''' return urllib_read(query_url).strip()
def weather(unit='c', location_query=None, icons=None): '''Return weather from Yahoo! Weather. Uses GeoIP lookup from http://freegeoip.net/ to automatically determine your current location. This should be changed if you're in a VPN or if your IP address is registered at another location. Returns a list of colorized icon and temperature segments depending on weather conditions. :param str unit: temperature unit, can be one of ``F``, ``C`` or ``K`` :param str location_query: location query for your current location, e.g. ``oslo, norway`` :param dict icons: dict for overriding default icons, e.g. ``{'heavy_snow' : u'❆'}`` ''' import json if not location_query: try: location = json.loads(urllib_read('http://freegeoip.net/json/' + _external_ip())) location_query = ','.join([location['city'], location['region_name'], location['country_name']]) except (TypeError, ValueError): return None query_data = { 'q': 'use "http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml" as we;' 'select * from we where location="{0}" and unit="{1}"'.format(location_query, unit).encode('utf-8'), 'format': 'json' } try: url = 'http://query.yahooapis.com/v1/public/yql?' + urllib_urlencode(query_data) response = json.loads(urllib_read(url)) condition = response['query']['results']['weather']['rss']['channel']['item']['condition'] condition_code = int(condition['code']) except (KeyError, TypeError, ValueError): return None try: icon_names = weather_conditions_codes[condition_code] except IndexError: icon_names = (('not_available' if condition_code == 3200 else 'unknown'),) for icon_name in icon_names: if icons: if icon_name in icons: icon = icons[icon_name] break else: icon = weather_conditions_icons[icon_names[-1]] groups = ['weather_condition_' + icon_name for icon_name in icon_names] + ['weather_conditions', 'weather'] return [ { 'contents': icon + ' ', 'highlight_group': groups, 'divider_highlight_group': 'background:divider', }, { 'contents': '{0}°{1}'.format(condition['temp'], unit.upper()), 'highlight_group': ['weather_temp_cold' if int(condition['temp']) < 0 else 'weather_temp_hot', 'weather_temp', 'weather'], 'draw_divider': False, 'divider_highlight_group': 'background:divider', }, ]
def _external_ip(query_url='http://ipv4.icanhazip.com/'): return urllib_read(query_url).strip()