Ejemplo n.º 1
0
    def _getWeatherForDay(self, day):
        now = datetime.datetime.now()
        if datetime.datetime(day.year, day.month, day.day) > now:
            raise KeyError

        if day.year == now.year and day.month == now.month and day.day == now.day:
            return json.loads(
                cache.urlopen(
                    self.API.API_BASE_URL
                    + self.API.API_KEY
                    + "/history_"
                    + datetime.datetime.strftime(day, "%Y%m%d")
                    + self.l
                    + "."
                    + self.API.API_FORMAT
                )
            )["history"]
        else:
            return json.loads(
                cache.urlopen(
                    self.API.API_BASE_URL
                    + self.API.API_KEY
                    + "/history_"
                    + datetime.datetime.strftime(day, "%Y%m%d")
                    + self.l
                    + "."
                    + self.API.API_FORMAT,
                    self.API.CACHE_PATH,
                )
            )["history"]
Ejemplo n.º 2
0
 def __getattr__(self, key):
     if key in [
         "alerts",
         "almanac",
         "astronomy",
         "conditions",
         "forecast",
         "forecast10day",
         "hourly",
         "hourly10day",
         "rawtide",
         "satellite",
         "tide",
         "webcams",
     ]:
         data = json.loads(
             cache.urlopen(self.API.API_BASE_URL + self.API.API_KEY + "/" + key + self.l + "." + self.API.API_FORMAT)
         )
         if (
             "response" in data
             and "error" in data["response"]
             and data["response"]["error"]["type"] == "keynotfound"
         ):
             raise KeyError("keynotfound")
         if key in ["forecast10day"]:
             return data["forecast"]
         if key in ["astronomy"]:
             return data
         if key in ["conditions"]:
             return data["current_observation"]
         if key in ["hourly", "hourly10day"]:
             return data["hourly_forecast"]
         return data[key]
     else:
         raise AttributeError
Ejemplo n.º 3
0
 def search(self, q):
     data = json.loads(cache.urlopen(self.API_AUTOCOMPLETE_URL + "?query=" + urllib.quote(q), self.CACHE_PATH))[
         "RESULTS"
     ]
     result = []
     for d in data:
         result.append(d["name"])
     return result
Ejemplo n.º 4
0
    def __getitem__(self, key):
        if type(key) is str:
            data = json.loads(
                cache.urlopen(self.API_AUTOCOMPLETE_URL + "?query=" + urllib.quote(key), self.CACHE_PATH)
            )["RESULTS"]
            if len(data) != 1:
                raise KeyError
            return Location(data[0], self)

        elif type(key) is tuple:
            results = []
            for city in key:
                data = json.loads(
                    cache.urlopen(self.API_AUTOCOMPLETE_URL + "?query=" + urllib.quote(city), self.CACHE_PATH)
                )["RESULTS"]
                if len(data) != 1:
                    raise KeyError
                results.append(Location(data[0], self))
            return results
 def test_cached_content(self):
     self.assertEqual(stupidcache.urlopen(self.url, self.cacheFolder), urllib2.urlopen(self.url).read())
 def test_folder_creation(self):
     self.assertFalse(os.path.exists(self.cacheFolder))
     stupidcache.urlopen(self.url, self.cacheFolder)
     self.assertTrue(os.path.exists(self.cacheFolder))
 def test_not_cached_content(self):
     self.assertFalse(os.path.exists(self.cacheFolder))
     self.assertEqual(stupidcache.urlopen(self.url), urllib2.urlopen(self.url).read())
     self.assertFalse(os.path.exists(self.cacheFolder))