Skip to content

Commit e05075b

Browse files
Merge pull request #218 from DarkOutcast6/master
Adding html_description attribute to the Cache object
2 parents 1824668 + e470634 commit e05075b

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

pycaching/cache.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def _from_print_page(cls, geocaching, guid, soup):
154154
if "attribute" in cache_info["attributes"]: # 'blank' attribute
155155
del cache_info["attributes"]["attribute"]
156156
cache_info["summary"] = content.find("h2", string="Short Description").find_next("div").text
157-
cache_info["description"] = content.find("h2", string="Long 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)
158160
hint = content.find(id="uxEncryptedHint")
159161
cache_info["hint"] = hint.get_text(separator="\n") if hint else None
160162
cache_info["waypoints"] = Waypoint.from_html(content, table_id="Waypoints")
@@ -222,6 +224,7 @@ def __init__(self, geocaching, wp, **kwargs):
222224
"attributes",
223225
"summary",
224226
"description",
227+
"description_html",
225228
"hint",
226229
"favorites",
227230
"pm_only",
@@ -613,6 +616,20 @@ def description(self, description):
613616
description = str(description).strip()
614617
self._description = description
615618

619+
@property
620+
@lazy_loaded
621+
def description_html(self):
622+
"""The cache long description in raw HTML.
623+
624+
:type: :class:`str`
625+
"""
626+
return self._description_html
627+
628+
@description_html.setter
629+
def description_html(self, description):
630+
description = str(description).strip()
631+
self._description_html = description
632+
616633
@property
617634
@lazy_loaded
618635
def hint(self):
@@ -811,7 +828,9 @@ def load(self):
811828
}
812829

813830
self.summary = root.find(id="ctl00_ContentBody_ShortDescription").text
814-
self.description = root.find(id="ctl00_ContentBody_LongDescription").text
831+
raw_description = root.find(id="ctl00_ContentBody_LongDescription")
832+
self.description = raw_description.text
833+
self.description_html = str(raw_description)
815834

816835
self.hint = rot13(root.find(id="div_hint").get_text(separator="\n"))
817836

@@ -935,7 +954,9 @@ def load_by_guid(self):
935954

936955
self.summary = content.find("h2", string="Short Description").find_next("div").text
937956

938-
self.description = content.find("h2", string="Long Description").find_next("div").text
957+
raw_description = content.find("h2", string="Long Description").find_next("div")
958+
self.description = raw_description.text
959+
self.description_html = str(raw_description)
939960

940961
self.hint = content.find(id="uxEncryptedHint").get_text(separator="\n")
941962

@@ -1078,7 +1099,6 @@ def load_logbook(self, limit=float("inf")):
10781099
return
10791100

10801101
for log_data in logbook_page:
1081-
10821102
limit -= 1 # handle limit
10831103
if limit < 0:
10841104
return
@@ -1120,7 +1140,6 @@ def load_trackables(self, limit=float("inf")):
11201140
names = [re.split(r"[\<\>]", str(link))[2] for link in links if "track" in link.get("href")]
11211141

11221142
for name, url in zip(names, urls):
1123-
11241143
limit -= 1 # handle limit
11251144
if limit < 0:
11261145
return

test/test_cache.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def setUp(self):
3333
hidden=date(2000, 1, 1),
3434
attributes={"onehour": True, "kids": False, "available": True},
3535
summary="text",
36-
description="long text",
36+
description="Luftballon",
37+
description_html="<b>Luftballon</b>",
3738
hint="rot13",
3839
favorites=0,
3940
pm_only=False,
@@ -164,7 +165,6 @@ def test_hidden(self):
164165
self.c.hidden = None
165166

166167
def test_visited(self):
167-
168168
with self.subTest("automatic str conversion"):
169169
self.c.visited = "1/30/2000"
170170
self.assertEqual(self.c.visited, date(2000, 1, 30))
@@ -188,7 +188,10 @@ def test_summary(self):
188188
self.assertEqual(self.c.summary, "text")
189189

190190
def test_description(self):
191-
self.assertEqual(self.c.description, "long text")
191+
self.assertEqual(self.c.description, "Luftballon")
192+
193+
def test_description_html(self):
194+
self.assertEqual(self.c.description_html, "<b>Luftballon</b>")
192195

193196
def test_hint(self):
194197
self.assertEqual(self.c.hint, "rot13")
@@ -237,6 +240,18 @@ def test_load(self):
237240
cache = Cache(self.gc, "GC123456")
238241
cache.load()
239242

243+
with self.subTest("description"):
244+
with self.recorder.use_cassette("cache_normal_normal"):
245+
cache = Cache(self.gc, "GC4808G")
246+
self.assertIn("Tuhle zprávu ti nepíšu proto, abych ti řekl, že ", cache.description)
247+
self.assertIn(
248+
(
249+
';background-color:black;border:1px solid black;padding:20px;">'
250+
"Tuhle zprávu ti nepíšu proto, abych ti řekl, že "
251+
),
252+
cache.description_html,
253+
)
254+
240255
def test_load_quick(self):
241256
with self.subTest("normal"):
242257
with self.recorder.use_cassette("cache_quick_normal"):
@@ -279,7 +294,17 @@ def test_load_by_guid(self, mock_load_quick, mock_load):
279294
},
280295
)
281296
self.assertEqual(cache.summary, "Gibt es das Luftschloss wirklich?")
282-
self.assertIn("Seit dem 16.", cache.description)
297+
self.assertIn(
298+
" funktioniert, der kann gerne eine Mail schreiben.\r\nSeit dem 16. Jahrhundert steht die ",
299+
cache.description,
300+
)
301+
self.assertIn(
302+
(
303+
" funktioniert, der kann gerne eine Mail schreiben.</b></p>\r\n"
304+
"Seit dem 16. Jahrhundert steht die "
305+
),
306+
cache.description_html,
307+
)
283308
self.assertEqual(cache.hint, "Das ist nicht nötig")
284309
self.assertGreater(cache.favorites, 350)
285310
self.assertEqual(len(cache.waypoints), 2)

0 commit comments

Comments
 (0)