Skip to content

Commit 78a8fe2

Browse files
authored
Cleanup code for loading cache data from print page (#247)
1 parent 994d122 commit 78a8fe2

2 files changed

Lines changed: 28 additions & 56 deletions

File tree

pycaching/cache.py

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -119,49 +119,12 @@ class Cache(object):
119119
}
120120

121121
@classmethod
122+
@deprecated
122123
def _from_print_page(cls, geocaching, guid, soup):
123124
"""Create a cache instance from a souped print-page and a GUID."""
124-
if soup.find("p", "Warning") is not None:
125-
raise errors.PMOnlyException()
126-
127-
cache_info = dict()
128-
cache_info["guid"] = guid
129-
cache_info["wp"] = soup.find(class_="HalfRight").find("h1").text.strip()
130-
content = soup.find(id="Content")
131-
cache_info["name"] = content.find("h2").text.strip()
132-
cache_info["type"] = Type.from_filename(content.h2.img["src"].split("/")[-1].partition(".")[0])
133-
cache_info["author"] = content.find(class_="Meta").text.partition(":")[2].strip()
134-
diff_terr = content.find(class_="DiffTerr").find_all("img")
135-
assert len(diff_terr) == 2
136-
cache_info["difficulty"] = float(diff_terr[0]["alt"].split()[0])
137-
cache_info["terrain"] = float(diff_terr[1]["alt"].split()[0])
138-
cache_info["size"] = Size.from_string(content.find(class_="Third AlignCenter").p.img["alt"].partition(":")[2])
139-
fav_text = content.find(class_="Third AlignRight").p.contents[2]
140-
try:
141-
cache_info["favorites"] = int(fav_text)
142-
except ValueError: # element not present when 0 favorites
143-
cache_info["favorites"] = 0
144-
cache_info["hidden"] = parse_date(
145-
content.find(class_="HalfRight AlignRight").p.text.strip().partition(":")[2].strip()
146-
)
147-
cache_info["location"] = Point.from_string(content.find(class_="LatLong").text.strip())
148-
attributes = [
149-
img["src"].split("/")[-1].partition(".")[0].rpartition("-")
150-
for img in content.find(class_="sortables").find_all("img")
151-
if img.get("src") and img["src"].startswith("/images/attributes/")
152-
]
153-
cache_info["attributes"] = {attr_name: attr_setting == "yes" for attr_name, _, attr_setting in attributes}
154-
if "attribute" in cache_info["attributes"]: # 'blank' attribute
155-
del cache_info["attributes"]["attribute"]
156-
cache_info["summary"] = content.find("h2", string="Short Description").find_next("div").text
157-
raw_description = content.find("h2", string="Long Description").find_next("div")
158-
cache_info["description"] = raw_description.text
159-
cache_info["description_html"] = str(raw_description)
160-
hint = content.find(id="uxEncryptedHint")
161-
cache_info["hint"] = hint.get_text(separator="\n") if hint else None
162-
cache_info["waypoints"] = Waypoint.from_html(content, table_id="Waypoints")
163-
cache_info["log_counts"] = Cache._get_log_counts_from_print_page(soup)
164-
return Cache(geocaching, **cache_info)
125+
cache = cls(geocaching, None, guid=guid)
126+
cache.load_by_guid()
127+
return cache
165128

166129
@classmethod
167130
def _from_api_record(cls, geocaching, record):
@@ -928,26 +891,28 @@ def load_by_guid(self):
928891

929892
self.name = content.find("h2").text
930893

931-
self.location = Point.from_string(content.find("p", "LatLong Meta").text)
894+
self.location = Point.from_string(content.find(class_="LatLong").text.strip())
932895

933896
type_img = os.path.basename(content.find("img").get("src"))
934897
self.type = Type.from_filename(os.path.splitext(type_img)[0])
935898

936899
size_img = content.find("img", src=re.compile(r"\/icons\/container\/"))
937900
self.size = Size.from_string(size_img.get("alt").split(": ")[1])
938901

939-
D_and_T_img = content.find("p", "Meta DiffTerr").find_all("img")
940-
self.difficulty, self.terrain = [float(img.get("alt").split()[0]) for img in D_and_T_img]
941-
942-
# TODO do NOT use English phrases like "Placed by" to search for attributes
943-
944-
self.author = content.find("p", string=re.compile("Placed by:")).text.split("\r\n")[2].strip()
902+
diff_terr = content.find(class_="DiffTerr").find_all("img")
903+
assert len(diff_terr) == 2
904+
self.difficulty = float(diff_terr[0]["alt"].split()[0])
905+
self.terrain = float(diff_terr[1]["alt"].split()[0])
945906

946-
hidden_p = content.find("p", string=re.compile("Placed Date:"))
947-
self.hidden = hidden_p.text.replace("Placed Date:", "").strip()
907+
self.author = content.find(class_="Meta").text.partition(":")[2].strip()
908+
self.hidden = content.find(class_="HalfRight AlignRight").p.text.strip().partition(":")[2].strip()
948909

949-
attr_img = content.find_all("img", src=re.compile(r"\/attributes\/"))
950-
attributes_raw = [os.path.basename(_.get("src")).rsplit("-", 1) for _ in attr_img]
910+
attr_img = content.find(class_="sortables").find_all("img")
911+
attributes_raw = [
912+
os.path.basename(_.get("src")).rsplit("-", 1)
913+
for _ in attr_img
914+
if _.get("src") and _.get("src").startswith("/images/attributes/")
915+
]
951916
self.attributes = {
952917
name: appendix.startswith("yes") for name, appendix in attributes_raw if not appendix.startswith("blank")
953918
}
@@ -958,13 +923,19 @@ def load_by_guid(self):
958923
self.description = raw_description.text
959924
self.description_html = str(raw_description)
960925

961-
self.hint = content.find(id="uxEncryptedHint").get_text(separator="\n")
926+
hint = content.find(id="uxEncryptedHint")
927+
self.hint = hint.get_text(separator="\n") if hint else None
962928

963-
self.favorites = content.find("strong", string=re.compile("Favorites:")).parent.text.split()[-1]
929+
fav_text = content.find(class_="Third AlignRight").p.contents[2]
930+
try:
931+
self.favorites = int(fav_text)
932+
except ValueError: # element not present when 0 favorites
933+
self.favorites = 0
964934

965935
self.waypoints = Waypoint.from_html(content, "Waypoints")
966936

967937
self.log_counts = Cache._get_log_counts_from_print_page(res)
938+
self.wp = res.find(class_="HalfRight").find("h1").text.strip()
968939

969940
@staticmethod
970941
def _get_log_counts_from_cache_details(soup):

pycaching/geocaching.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,9 @@ def post_log(self, wp, text, type=LogType.found_it, date=None):
488488

489489
def _cache_from_guid(self, guid):
490490
logging.info("Loading cache with GUID {!r}".format(guid))
491-
print_page = self._request(Cache._urls["print_page"], params={"guid": guid})
492-
return Cache._from_print_page(self, guid, print_page)
491+
cache = Cache(self, None, guid=guid)
492+
cache.load_by_guid()
493+
return cache
493494

494495
def _try_getting_cache_from_guid(self, guid):
495496
"""Try to get a cache from guid page if possible, otherwise from gccode.

0 commit comments

Comments
 (0)