Skip to content

Get country and state from a cache #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 120
exclude = .venv,dist,docs,build,*.egg,.git,__pycache__
exclude = .venv,dist,docs,build,*.egg,.git,__pycache__,.eggs,venv
count = true
show-source = true
statistics = true
24 changes: 21 additions & 3 deletions pycaching/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from bs4.element import Script

from pycaching import errors
from pycaching.country import CountryState
from pycaching.geo import Point
from pycaching.i18nhelper import I18NHelperFactory
from pycaching.log import Log
from pycaching.log import Type as LogType
from pycaching.trackable import Trackable
Expand Down Expand Up @@ -233,6 +235,7 @@ def __init__(self, geocaching, wp, **kwargs):
"guid",
"visited",
"log_counts",
"country",
}

for name in known_kwargs:
Expand Down Expand Up @@ -680,6 +683,15 @@ def _trackable_page_url(self):
def _trackable_page_url(self, trackable_page_url):
self.__trackable_page_url = trackable_page_url

@property
@lazy_loaded
def country(self):
return self._country

@country.setter
def country(self, country):
self._country = country

def load(self):
"""Load all possible cache details.

Expand Down Expand Up @@ -710,6 +722,9 @@ def load(self):
# probably 404 during cache loading - cache does not exist
raise errors.LoadError("Error in loading cache") from e

language = self.geocaching.get_website_language()
i18nhelper = I18NHelperFactory.create(language)

# check for PM only caches if using free account
self.pm_only = root.find("section", "premium-upgrade-widget") is not None

Expand All @@ -722,8 +737,7 @@ def load(self):

self.name = cache_details.find("h1").text.strip()

author = cache_details.find(id="ctl00_ContentBody_uxCacheBy").text
self.author = author[11:] # 11 = len("A cache by ")
Comment on lines -725 to -726
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed an obsolete piece of code, replaced by self.author = cache_details("a")[1].text. Thus it can be deleted without replacement.

self.author = i18nhelper.author(cache_details.find(id="ctl00_ContentBody_uxCacheBy").text.strip())

# parse cache detail list into a python list
details = cache_details.find("ul", "ul__hide-details").text.split("\n")
Expand All @@ -743,7 +757,8 @@ def load(self):
raise errors.LoadError()
self.name = cache_details.find("h2").text

self.author = cache_details("a")[1].text
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works correctly (for all languages) and shouldn't be removed.

# TODO self.author = i18nhelper.author().text.strip())
self.author = cache_details.find(id="ctl00_ContentBody_mcd1").find("a").text.strip()

D_and_T_img = root.find("div", "CacheStarLabels").find_all("img")
self.difficulty, self.terrain = [float(img.get("alt").split()[0]) for img in D_and_T_img]
Expand Down Expand Up @@ -772,6 +787,9 @@ def load(self):

self.location = Point.from_string(root.find(id="uxLatLon").text)

country = root.find(id="ctl00_ContentBody_Location")
self.country = CountryState.from_string(i18nhelper.country(country.text))

self.state = root.find("ul", "OldWarning") is None

log_image = root.find(id="ctl00_ContentBody_GeoNav_logTypeImage")
Expand Down
Loading