Skip to content

Commit ddac5af

Browse files
committed
Added shortcuts
1 parent 9afd34b commit ddac5af

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pycaching/geocaching.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
import logging
44
import requests
5+
from urllib.parse import urljoin
56
from mechanicalsoup import Browser
67
from bs4 import BeautifulSoup
78
from pycaching.cache import Cache, Type, Size
89
from pycaching.geo import Point
10+
from pycaching.trackable import Trackable
911
from pycaching.errors import Error, NotLoggedInException, LoginFailedException
1012
from pycaching.util import parse_date
1113

1214

1315
class Geocaching(object):
1416

15-
_baseurl = "http://www.geocaching.com"
17+
_baseurl = "https://www.geocaching.com"
1618
_urls = {
1719
"login_page": "login/default.aspx",
1820
"search": "play/search",
@@ -28,8 +30,7 @@ def _request(self, url, *, expect="soup", method="GET", login_check=True, **kwar
2830
if login_check and self._logged_in is False:
2931
raise NotLoggedInException("Login is needed.")
3032

31-
# TODO maybe use urljoin()
32-
url = url if "//" in url else "/".join([self._baseurl, url])
33+
url = url if "//" in url else urljoin(self._baseurl, url)
3334

3435
try:
3536
res = self._browser.request(method, url, **kwargs)
@@ -206,3 +207,16 @@ def search_quick(self, area, *, strict=False, zoom=None):
206207
else:
207208
# can yield more caches (which are not exactly in desired area)
208209
yield cache
210+
211+
# add some shortcuts ensure backwards compatibility
212+
213+
def geocode(self, location):
214+
return Point.from_location(self, location)
215+
216+
def load_cache(self, wp):
217+
"""Return a cache by its WP."""
218+
return Cache(self, wp)
219+
220+
def load_trackable(self, tid):
221+
"""Return a cache by its TID."""
222+
return Trackable(self, tid)

test/test_geocaching.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
import pycaching
66
import itertools
7+
from geopy.distance import great_circle
78
from pycaching import Geocaching, Point, Rectangle
89
from pycaching.errors import NotLoggedInException, LoginFailedException, PMOnlyException
910

@@ -105,7 +106,24 @@ def test_logout(self):
105106
self.assertIsNone(self.g.get_logged_user())
106107

107108

108-
class TestPackage(unittest.TestCase):
109+
class TestShortcuts(unittest.TestCase):
110+
111+
@classmethod
112+
def setUpClass(cls):
113+
cls.g = Geocaching()
114+
cls.g.login(_username, _password)
109115

110116
def test_login(self):
111117
pycaching.login(_username, _password)
118+
119+
def test_load_cache(self):
120+
c = self.g.load_cache("GC4808G")
121+
self.assertEqual("Nekonecne ticho", c.name)
122+
123+
def test_load_trackable(self):
124+
t = self.g.load_trackable("TB1KEZ9")
125+
self.assertEqual("Lilagul #2: SwedenHawk Geocoin", t.name)
126+
127+
def test_geocode(self):
128+
ref_point = Point(49.74774, 13.37752)
129+
self.assertLess(great_circle(self.g.geocode("Pilsen"), ref_point).miles, 10)

0 commit comments

Comments
 (0)