Skip to content

Commit 50ca344

Browse files
committed
Fixed #14 (changed cache type and size detection)
1 parent a055fad commit 50ca344

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

pycaching/cache.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,36 @@ class Cache(object):
9797
}
9898

9999
_possible_types = {
100-
"Traditional Cache",
101-
"Multi-Cache",
102-
"Unknown Cache",
103-
"Mystery Cache",
104-
"Letterbox Hybrid",
105-
"Event Cache",
106-
"Mega-Event Cache",
107-
"Giga-Event Cache",
108-
"Earthcache",
109-
"Cache In Trash Out Event",
110-
"Webcam Cache",
111-
"Virtual Cache",
112-
"Wherigo Cache",
113-
"Lost And Found Event Cache",
114-
"Project Ape Cache",
115-
"Groundspeak Hq",
116-
"Gps Adventures Exhibit",
117-
"Groundspeak Block Party",
118-
"Locationless (Reverse) Cache",
100+
# key is cache image url, used for parsing: http://www.geocaching.com/images/WptTypes/[KEY].gif
101+
"2": "Traditional Cache",
102+
"3": "Multi-cache",
103+
"8": "Mystery Cache",
104+
"__8": "Unknown Cache", # same as Mystery, key not used
105+
"5": "Letterbox hybrid",
106+
"6": "Event Cache",
107+
"mega": "Mega-Event Cache",
108+
"giga": "Giga-Event Cache",
109+
"earthcache": "Earthcache",
110+
"13": "Cache in Trash out Event",
111+
"11": "Webcam Cache",
112+
"4": "Virtual Cache",
113+
"1858": "Wherigo Cache",
114+
"10Years_32": "Lost and Found Event Cache",
115+
"ape_32": "Project Ape Cache",
116+
"HQ_32": "Groundspeak HQ",
117+
"1304": "GPS Adventures Exhibit",
118+
"4738": "Groundspeak Block Party",
119+
"12": "Locationless (Reverse) Cache",
119120
}
120121

121122
_possible_sizes = {
122-
"nano",
123-
"micro",
124-
"small",
125-
"regular",
126-
"large",
127-
"very large",
128-
"not chosen",
129-
"virtual",
130-
"other"
123+
"micro": "micro",
124+
"small": "small",
125+
"regular": "regular",
126+
"large": "large",
127+
"not_chosen": "not chosen",
128+
"virtual": "virtual",
129+
"other": "other",
131130
}
132131

133132
def __init__(self, wp, geocaching, *, name=None, cache_type=None, location=None, state=None,
@@ -225,11 +224,14 @@ def cache_type(self):
225224

226225
@cache_type.setter
227226
def cache_type(self, cache_type):
228-
cache_type = cache_type.strip().title()
227+
cache_type = cache_type.strip()
229228
cache_type = cache_type.replace("Geocache", "Cache")
230-
if cache_type not in self._possible_types:
229+
if cache_type in self._possible_types.values(): # try to search in values
230+
self._cache_type = cache_type
231+
elif cache_type in self._possible_types.keys(): # not in values => it must be a key
232+
self._cache_type = self._possible_types[cache_type]
233+
else:
231234
raise ValueError("Cache type '{}' is not possible.".format(cache_type))
232-
self._cache_type = cache_type
233235

234236
@property
235237
@lazy_loaded
@@ -257,9 +259,12 @@ def size(self):
257259
@size.setter
258260
def size(self, size):
259261
size = size.strip().lower()
260-
if size not in self._possible_sizes:
262+
if size in self._possible_sizes.values(): # try to search in values
263+
self._size = size
264+
elif size in self._possible_sizes.keys(): # not in values => it must be a key
265+
self._size = self._possible_sizes[size]
266+
else:
261267
raise ValueError("Size '{}' is not possible.".format(size))
262-
self._size = size
263268

264269
@property
265270
@lazy_loaded

pycaching/geocaching.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Geocaching(object):
3434

3535
_urls = {
3636
"login_page": _baseurl + "login/default.aspx",
37-
"cache_details": _baseurl + "seek/cache_details.aspx",
37+
"cache_details": _baseurl + "geocache/{wp}",
3838
"caches_nearest": _baseurl + "seek/nearest.aspx",
3939
"geocode": _baseurl + "api/geocode",
4040
"map": _tile_url + "map.details",
@@ -223,11 +223,12 @@ def _search_parse_cache(self, root):
223223
c = Cache(wp, self)
224224

225225
# prettify data
226-
c.cache_type = typeLink.find("img").get("alt")
226+
c.cache_type = typeLink.find("img").get(
227+
"src").split("/")[-1].rsplit(".", 1)[0] # filename of img[src]
227228
c.name = nameLink.span.text.strip()
228229
c.found = found
229230
c.state = "Strike" not in nameLink.get("class")
230-
c.size = " ".join(size.get("alt").split()[1:])
231+
c.size = size.get("src").split("/")[-1].rsplit(".", 1)[0] # filename of img[src]
231232
c.difficulty, c.terrain = list(map(float, D_T.text.split("/")))
232233
c.hidden = Util.parse_date(placed.text)
233234
c.author = author[3:] # delete "by "
@@ -454,10 +455,7 @@ def load_cache(self, wp, destination=None):
454455
assert type(wp) is str and wp.startswith("GC")
455456
logging.info("Loading details about %s...", wp)
456457

457-
# assemble request
458-
params = urlencode({"wp": wp})
459-
url = self._urls["cache_details"] + "?" + params
460-
458+
url = self._urls["cache_details"].format(wp=wp)
461459
try:
462460
root = self._browser.get(url).soup
463461
except requests.exceptions.ConnectionError as e:
@@ -472,7 +470,7 @@ def load_cache(self, wp, destination=None):
472470

473471
# parse raw data
474472
name = cache_details.find("h2")
475-
cache_type = cache_details.find("img").get("alt")
473+
cache_type = cache_details.find("img").get("src")
476474
author = cache_details("a")[1]
477475
hidden = cache_details.find("div", "minorCacheDetails").find_all("div")[1]
478476
location = root.find(id="uxLatLon")
@@ -491,14 +489,14 @@ def load_cache(self, wp, destination=None):
491489

492490
# prettify data
493491
c.name = name.text
494-
c.cache_type = cache_type
492+
c.cache_type = cache_type.split("/")[-1].rsplit(".", 1)[0]
495493
c.author = author.text
496-
c.hidden = Util.parse_date(hidden.text.split()[2])
494+
c.hidden = Util.parse_date(hidden.text.split(":")[-1])
497495
c.location = Point.from_string(location.text)
498496
c.state = state is None
499497
c.found = found and "Found It!" in found.text or False
500498
c.difficulty, c.terrain = [float(_.get("alt").split()[0]) for _ in D_T]
501-
c.size = " ".join(size.get("alt").split()[1:])
499+
c.size = size.get("src").split("/")[-1].rsplit(".", 1)[0] # filename of img[src]
502500
attributes_raw = [_.get("src").split('/')[-1].rsplit("-", 1) for _ in attributes_raw]
503501
c.attributes = {attribute_name: appendix.startswith("yes")
504502
for attribute_name, appendix in attributes_raw if not appendix.startswith("blank")}

pycaching/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def to_mindec(decimal):
3333
@staticmethod
3434
def parse_date(raw):
3535
"""Returns parsed date."""
36+
raw = raw.strip()
3637
patterns = ("%Y-%m-%d", "%Y/%m/%d", "%m/%d/%Y", "%d/%m/%Y",
3738
"%d.%m.%Y", "%d/%b/%Y", "%d.%b.%Y", "%b/%d/%Y", "%d %b %y")
3839

@@ -42,7 +43,7 @@ def parse_date(raw):
4243
except ValueError:
4344
pass
4445

45-
raise errors.ValueError("Unknown date format.")
46+
raise errors.ValueError("Unknown date format - '{}'.".format(raw))
4647

4748
@classmethod
4849
def get_possible_attributes(cls):

0 commit comments

Comments
 (0)