Skip to content

Commit 9fec66a

Browse files
S0S-90tomasbedrich
authored andcommitted
make it possible to read my_logs even if a PMonly cache is among them
1 parent b4cb385 commit 9fec66a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

pycaching/geocaching.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pycaching.log import Log, Type as LogType
1414
from pycaching.geo import Point
1515
from pycaching.trackable import Trackable
16-
from pycaching.errors import Error, NotLoggedInException, LoginFailedException
16+
from pycaching.errors import Error, NotLoggedInException, LoginFailedException, PMOnlyException
1717

1818

1919
class Geocaching(object):
@@ -382,6 +382,27 @@ def _cache_from_guid(self, guid):
382382
print_page = self._request(Cache._urls["print_page"], params={"guid": guid})
383383
return Cache._from_print_page(self, guid, print_page)
384384

385+
def _try_getting_cache_from_guid(self, guid):
386+
"""tries to read a geocache from GUID page
387+
if this is not possible (because it's a premium only cache) it reads the cache from the GC code"""
388+
try:
389+
return self.get_cache(guid=guid)
390+
except PMOnlyException:
391+
url = "https://www.geocaching.com/seek/cache_details.aspx?guid={}".format(guid)
392+
wp = self._get_gccode_from_guidpage(url)
393+
return self.get_cache(wp)
394+
395+
@staticmethod
396+
def _get_gccode_from_guidpage(url):
397+
data = requests.get(url).text
398+
soup = bs4.BeautifulSoup(data, features="html.parser")
399+
gc_element = soup.find("li", class_="li__gccode")
400+
if gc_element is not None: # PMonly caches, this it what this function is for
401+
gccode = gc_element.text.strip()
402+
else:
403+
gccode = soup.find("span", class_="CoordInfoCode").text.strip()
404+
return gccode
405+
385406
def my_logs(self, log_type=None, limit=float('inf')):
386407
"""Get an iterable of the logged-in user's logs.
387408
@@ -408,7 +429,7 @@ def my_logs(self, log_type=None, limit=float('inf')):
408429
if yielded >= limit:
409430
break
410431

411-
yield self.get_cache(guid=guid)
432+
yield self._try_getting_cache_from_guid(guid)
412433
yielded += 1
413434

414435
def my_finds(self, limit=float('inf')):

test/test_geocaching.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ def test_search_quick_match_load(self):
9696
except PMOnlyException:
9797
pass
9898

99+
def test__get_gccode_from_guidpage(self):
100+
# for a "normal" cache (not really relevant for program)
101+
url_normal = "https://www.geocaching.com/seek/cache_details.aspx?guid={}".format(
102+
"15ad3a3d-92c1-4f7c-b273-60937bcc2072")
103+
wp_normal = self.gc._get_gccode_from_guidpage(url_normal)
104+
self.assertEqual(wp_normal, "GC4808G")
105+
106+
# for PMonly cache
107+
url = "https://www.geocaching.com/seek/cache_details.aspx?guid={}".format(
108+
"328927c1-aa8c-4e0d-bf59-31f1ce44d990")
109+
wp = self.gc._get_gccode_from_guidpage(url)
110+
self.assertEqual(wp, "GC74HEV")
111+
112+
def test__try_getting_cache_from_guid(self):
113+
# get "normal" cache from guidpage
114+
with self.recorder.use_cassette('geocaching_shortcut_getcache__by_guid'): # is a replacement for login
115+
cache = self.gc._try_getting_cache_from_guid('15ad3a3d-92c1-4f7c-b273-60937bcc2072')
116+
self.assertEqual("Nekonecne ticho", cache.name)
117+
118+
# get PMonly cache from GC code (doesn't load any information)
119+
cache_pm = self.gc._try_getting_cache_from_guid('328927c1-aa8c-4e0d-bf59-31f1ce44d990')
120+
cache_pm.load_quick() # necessary to get name for PMonly cache
121+
self.assertEqual("Nidda: jenseits der Rennstrecke Reloaded", cache_pm.name)
122+
99123

100124
class TestLoginOperations(NetworkedTest):
101125
def setUp(self):

0 commit comments

Comments
 (0)