1313from pycaching .log import Log , Type as LogType
1414from pycaching .geo import Point
1515from pycaching .trackable import Trackable
16- from pycaching .errors import Error , NotLoggedInException , LoginFailedException
16+ from pycaching .errors import Error , NotLoggedInException , LoginFailedException , PMOnlyException
1717
1818
1919class 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' )):
0 commit comments