Exemplo n.º 1
0
    def loadCache(self, wp):
        """Loads details from cache page.

        Loads all cache details and return fully populated cache object."""

        if not self.loggedIn or not (isinstance(wp, StringTypes) and wp.startswith("GC")):
            return

        logging.info("Loading details about %s...", wp)

        # assemble request
        params = urlencode({"wp": wp})
        url = self._getURL("cacheDetails") + "?" + params

        # make request
        rawPage = Util.urlopen(url)
        if not rawPage:
            logging.error("Cannot load search page.")
            return
        soup = BeautifulSoup(rawPage)

        # parse raw data
        cacheDetails = soup.find(id="cacheDetails")
        name = cacheDetails.find("h2")
        cacheType = cacheDetails.find("img").get("alt")
        author = cacheDetails("a")[1]
        hidden = cacheDetails.find("div", "minorCacheDetails").findAll("div")[1]
        location = soup.find(id="uxLatLon")
        state = soup.find("ul", "OldWarning")
        found = soup.find("div", "FoundStatus")
        DandT = soup.find("div", "CacheStarLabels").findAll("img")
        size = soup.find("div", "CacheSize").find("img")
        attributesRaw = soup("div", "CacheDetailNavigationWidget")[0].findAll("img")
        userContent = soup("div", "UserSuppliedContent")
        hint = soup.find(id="div_hint")
        favorites = soup.find("span", "favorite-value")

        # prettify data
        name = name.text.encode("utf-8", "xmlcharrefreplace")
        author = author.text.encode("utf-8", "xmlcharrefreplace")
        hidden = datetime.strptime(hidden.text.split()[2], '%m/%d/%Y').date()
        try:
            lat, lon = Util.parseRaw(location.text)
            location = geo.Point(Util.toDecimal(*lat), Util.toDecimal(*lon))
        except ValueError:
            loggin.debug("Could not parse coordinates")
        state = state is None
        found = found and "Found It!" in found.text or False
        dif, ter = map(lambda e: float(e.get("alt").split()[0]), DandT)
        size = " ".join(size.get("alt").split()[1:]).lower()
        attributesRaw = map(lambda e: e.get("src").split('/')[-1].rsplit("-", 1), attributesRaw)
        attributes = {} # parse attributes by src to know yes/no
        for attribute_name, appendix in attributesRaw:
            if appendix.startswith("blank"):
                continue
            attributes[attribute_name] = appendix.startswith("yes")
        summary = userContent[0].text.encode("utf-8", "xmlcharrefreplace")
        description = userContent[1]
        hint = Util.rot13(hint.text.strip().encode('utf-8'))
        favorites = int(favorites.text)

        # assemble cache object
        c = Cache(wp, name, cacheType, location, state, found,
            size, dif, ter, author, hidden, attributes,
            summary, description, hint, favorites)
        return c