Skip to content

Commit 74dd10d

Browse files
committed
Merge pull request #50 from tomasbedrich/premium-cache-some-details
PM only caches details
2 parents 1451f01 + 17709e1 commit 74dd10d

File tree

1 file changed

+80
-48
lines changed

1 file changed

+80
-48
lines changed

pycaching/cache.py

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ def load(self):
488488
Use full cache details page. Therefore all possible properties are filled in, but the
489489
loading is a bit slow.
490490
491+
If you want to load basic details about a PM only cache, the :class:`.PMOnlyException` is
492+
still thrown, but avaliable details are filled in. If you know, that the cache you are
493+
loading is PM only, please consider using :meth:`load_quick` as it will load the same
494+
details, but quicker.
495+
491496
.. note::
492497
This method is called automatically when you access a property which isn't yet filled in
493498
(so-called "lazy loading"). You don't have to call it explicitly.
@@ -509,65 +514,92 @@ def load(self):
509514
raise errors.LoadError("Error in loading cache") from e
510515

511516
# check for PM only caches if using free account
512-
if root.find("p", "PMOWarning") is not None:
513-
raise errors.PMOnlyException("Premium Members only.")
514-
515-
cache_details = root.find(id="cacheDetails")
516-
attributes_widget, inventory_widget, * \
517-
bookmarks_widget = root.find_all("div", "CacheDetailNavigationWidget")
518-
519-
# parse raw data
520-
wp = root.title.string.split(" ")[0]
521-
name = cache_details.find("h2")
522-
type = cache_details.find("img").get("src").split(
523-
"/")[-1].rsplit(".", 1)[0] # filename w/o extension
524-
author = cache_details("a")[1]
525-
hidden = cache_details.find("div", "minorCacheDetails").find_all("div")[1]
526-
location = root.find(id="uxLatLon")
527-
state = root.find("ul", "OldWarning")
528-
found = root.find("div", "FoundStatus")
529-
D_T = root.find("div", "CacheStarLabels").find_all("img")
530-
size = root.find("div", "CacheSize").find("img")
531-
attributes_raw = attributes_widget.find_all("img")
532-
user_content = root.find_all("div", "UserSuppliedContent")
533-
hint = root.find(id="div_hint")
534-
favorites = root.find("span", "favorite-value")
517+
self.pm_only = root.find("p", "PMOWarning") is not None
535518

536-
# load logbook_token
537-
js_content = "\n".join(map(lambda i: i.text, root.find_all("script")))
538-
logbook_token = re.findall("userToken\\s*=\\s*'([^']+)'", js_content)[0]
519+
cache_details = root.find(id="ctl00_divContentMain") if self.pm_only else root.find(id="cacheDetails")
520+
521+
# details also avaliable for basic members for PM only caches -----------------------------
522+
523+
if self.pm_only:
524+
# parse from title in format: "cache name (WAYPOINT)"
525+
self.wp = cache_details.find("h2").text.split("(")[-1].strip(")")
526+
527+
name = cache_details.find("h2").text
528+
self.name = name.rsplit("(", 1)[0] if self.pm_only else name # everything before last bracket
529+
530+
author = root.find(id="ctl00_ContentBody_uxCacheType").text
531+
self.author = author[len("A cache by "):]
532+
533+
size = root.find("p", "PMCacheInfoSpacing")
534+
535+
# skip first, which is size <img>
536+
D_and_T_img = root.find("p", "PMCacheInfoSpacing").find_all("img")[1:]
539537

540-
# if there are some trackables
541-
if len(inventory_widget.find_all("a")) >= 3:
542-
trackable_page_url = inventory_widget.find(
543-
id="ctl00_ContentBody_uxTravelBugList_uxViewAllTrackableItems").get("href")[3:] # has "../" on start
544538
else:
545-
trackable_page_url = None
539+
# parse from <title> - get first word
540+
self.wp = root.title.string.split(" ")[0]
546541

547-
log_page_url = root.find(id="ctl00_ContentBody_GeoNav_logButton")["href"]
548-
# prettify data
549-
self.wp = wp
550-
self.name = name.text
542+
self.name = cache_details.find("h2").text
543+
544+
self.author = cache_details("a")[1].text
545+
546+
size = root.find("div", "CacheSize")
547+
548+
D_and_T_img = root.find("div", "CacheStarLabels").find_all("img")
549+
550+
size = size.find("img").get("src") # size img src
551+
size = size.split("/")[-1].rsplit(".", 1)[0] # filename w/o extension
552+
self.size = Size.from_filename(size)
553+
554+
self.difficulty, self.terrain = [float(img.get("alt").split()[0]) for img in D_and_T_img]
555+
556+
type = cache_details.find("img").get("src") # type img src
557+
type = type.split("/")[-1].rsplit(".", 1)[0] # filename w/o extension
551558
self.type = Type.from_filename(type)
552-
self.author = author.text
553-
self.hidden = parse_date(hidden.text.split(":")[-1])
554-
self.location = Point.from_string(location.text)
555-
self.state = state is None
559+
560+
if self.pm_only:
561+
raise errors.PMOnlyException()
562+
563+
# details not avaliable for basic members for PM only caches ------------------------------
564+
565+
attributes_widget, inventory_widget, *_ = root.find_all("div", "CacheDetailNavigationWidget")
566+
567+
hidden = cache_details.find("div", "minorCacheDetails").find_all("div")[1].text
568+
self.hidden = parse_date(hidden.split(":")[-1])
569+
570+
self.location = Point.from_string(root.find(id="uxLatLon").text)
571+
572+
self.state = root.find("ul", "OldWarning") is None
573+
574+
found = root.find("div", "FoundStatus")
556575
self.found = found and ("Found It!" or "Attended" in found.text) or False
557-
self.difficulty, self.terrain = [float(_.get("alt").split()[0]) for _ in D_T]
558-
self.size = Size.from_filename(size.get("src").split(
559-
"/")[-1].rsplit(".", 1)[0]) # filename w/o extension
576+
577+
attributes_raw = attributes_widget.find_all("img")
560578
attributes_raw = [_.get("src").split("/")[-1].rsplit("-", 1) for _ in attributes_raw]
561-
self.attributes = {attribute_name: appendix.startswith("yes")
562-
for attribute_name, appendix in attributes_raw if not appendix.startswith("blank")}
579+
580+
self.attributes = {attribute_name: appendix.startswith("yes") for attribute_name, appendix
581+
in attributes_raw if not appendix.startswith("blank")}
582+
583+
user_content = root.find_all("div", "UserSuppliedContent")
563584
self.summary = user_content[0].text
564585
self.description = str(user_content[1])
565-
self.hint = rot13(hint.text.strip())
586+
587+
self.hint = rot13(root.find(id="div_hint").text.strip())
588+
589+
favorites = root.find("span", "favorite-value")
566590
self.favorites = 0 if favorites is None else int(favorites.text)
567591

568-
self._logbook_token = logbook_token
569-
self._trackable_page_url = trackable_page_url
570-
self._log_page_url = log_page_url
592+
self._log_page_url = root.find(id="ctl00_ContentBody_GeoNav_logButton")["href"]
593+
594+
js_content = "\n".join(map(lambda i: i.text, root.find_all("script")))
595+
self._logbook_token = re.findall("userToken\\s*=\\s*'([^']+)'", js_content)[0]
596+
597+
# if there are some trackables
598+
if len(inventory_widget.find_all("a")) >= 3:
599+
trackable_page_url = inventory_widget.find(id="ctl00_ContentBody_uxTravelBugList_uxViewAllTrackableItems")
600+
self._trackable_page_url = trackable_page_url.get("href")[3:] # has "../" on start
601+
else:
602+
self._trackable_page_url = None
571603

572604
logging.debug("Cache loaded: {}".format(self))
573605

0 commit comments

Comments
 (0)