Exemple #1
0
    def load_trackables(self, limit=float("inf")):
        """Return a generator of trackables in this cache.

        Yield instances of :class:`.Trackable` filled with trackable data.

        :param int limit: Maximum number of trackables to generate.
        """
        logging.info("Loading trackables for {}...".format(self))
        self.trackables = []

        url = self._trackable_page_url  # will trigger lazy_loading if needed
        if not url:
            # no link to all trackables = no trackables in cache
            raise StopIteration()
        res = self.geocaching._request(url)

        trackable_table = res.find_all("table")[1]
        links = trackable_table.find_all("a")
        # filter out all urls for trackables
        urls = [link.get("href") for link in links if "track" in link.get("href")]
        # find the names matching the trackble urls
        names = [re.split("[\<\>]", str(link))[2] for link in links if "track" in link.get("href")]

        for name, url in zip(names, urls):

            limit -= 1  # handle limit
            if limit < 0:
                raise StopIteration()

            # create and fill trackable object
            t = Trackable(self.geocaching, None)
            t.name = name
            t.url = url
            self.trackables.append(t)
            yield t
Exemple #2
0
    def load_trackable_by_url(self, url, destination=None):
        try:
            root = self._browser.get(url).soup
        except requests.exceptions.ConnectionError as e:
            raise Error("Cannot load cache details page.") from e
        title_tuple = re.split("[\(\)-]", root.title.string)
        tid = title_tuple[1]
        trackable_type = title_tuple[2]

        name = ''
        for n in title_tuple[3:]:
            name += n + '-'
        name = name.rstrip('-')

        owner_raw = root.findAll(
            "a", {"id": "ctl00_ContentBody_BugDetails_BugOwner"})
        #return owner_raw
        owner = re.split("[\<\>]", str(owner_raw))[2]

        location_raw = root.findAll(
            "a", {"id": "ctl00_ContentBody_BugDetails_BugLocation"})
        #return owner_raw
        location_url = location_raw[0].get('href')
        if 'cache_details' in location_url:
            location = self.load_cache_by_url(location_url).location
        else:
            location = re.split("[\<\>]", str(location_raw))[2]

        description_raw = root.findAll("div", {"id": "TrackableDetails"})
        description = description_raw[0].text

        goal_raw = root.findAll("div", {"id": "TrackableGoal"})
        goal = goal_raw[0].text

        # create trackable object
        t = destination or Trackable(tid, self)
        assert isinstance(t, Trackable)
        t.tid = tid
        t.name = name
        t.owner = owner
        t.location = location
        t.type = trackable_type
        t.description = description
        t.goal = goal
        return t
Exemple #3
0
    def load_trackable_list(self, url):
        try:
            root = self._browser.get(url).soup
        except requests.exceptions.ConnectionError as e:
            raise Error("Cannot load cache details page.") from e

        trackable_table = root.find_all("table")[1]
        urls_raw = trackable_table.find_all("a")
        # filter out all urls for trackables
        urls = [
            url.get("href") for url in urls_raw if "track" in url.get("href")
        ]
        # find the names matching the trackble urls
        names = [
            re.split("[\<\>]", str(url))[2] for url in urls_raw
            if "track" in url.get("href")
        ]
        # create trackables and build list to return
        trackables = []
        for n, u in zip(names, urls):
            trackables.append(Trackable(None, self, name=n, trackable_page=u))
        return trackables
Exemple #4
0
    def get_trackable(self, tid):
        """Return a :class:`.Trackable` object by its trackable ID.

        :param str tid: Trackable ID.
        """
        return Trackable(self, tid)