def fetch_image(self, data): img_url = None try: img_url = self.picture_url.format_map(data) if img_url == self.oldimg_url and self.image: return data = fetchurl(img_url) self.image = cairo.ImageSurface.create_from_png(BytesIO(data)) self.oldimg_url = img_url except Exception as e: log.exception("Error fetching picture %r", img_url, exc_info=e) self.image = None
def fetch(self): try: data = fetchurl(self.uri) xml = ET.fromstring(data.decode('ascii')) except Exception as e: log.exception("Error fetching weather info", exc_info=e) return None data = dict() for tag in self.tags_to_fetch: elem = xml.find('.//{%s}%s' % (WEATHER_NS, tag)) for attr, val in elem.attrib.items(): data['{0}_{1}'.format(tag, attr)] = val return data
def fetch_woeid(self): woeid = None while woeid is None: try: data = fetchurl(QUERY_URL, query={ 'q': "select woeid from geo.places " "where text='{0}'".format(self.location), 'format': 'json' }) data = json.loads(data.decode('ascii'))['query'] if data['count'] > 1: woeid = data['results']['place'][0]['woeid'] else: woeid = data['results']['place']['woeid'] except Exception as e: log.exception("Error fetching woeid", exc_info=e) sleep(60) else: if woeid is None: sleep(60) return woeid