1
1
#!/usr/bin/env python3
2
2
3
3
import logging
4
- import requests
5
4
import datetime
5
+ import requests
6
+ import bs4
6
7
from urllib .parse import urljoin
7
- from mechanicalsoup import Browser
8
- from bs4 import BeautifulSoup
9
8
from pycaching .cache import Cache , Type , Size
10
9
from pycaching .log import Log , Type as LogType
11
10
from pycaching .geo import Point
@@ -25,7 +24,7 @@ class Geocaching(object):
25
24
26
25
def __init__ (self ):
27
26
self ._logged_in = False
28
- self ._browser = Browser ()
27
+ self ._session = requests . Session ()
29
28
30
29
def _request (self , url , * , expect = "soup" , method = "GET" , login_check = True , ** kwargs ):
31
30
# check login unless explicitly turned off
@@ -35,12 +34,12 @@ def _request(self, url, *, expect="soup", method="GET", login_check=True, **kwar
35
34
url = url if "//" in url else urljoin (self ._baseurl , url )
36
35
37
36
try :
38
- res = self ._browser .request (method , url , ** kwargs )
37
+ res = self ._session .request (method , url , ** kwargs )
39
38
res .raise_for_status ()
40
39
41
40
# return bs4.BeautifulSoup, JSON dict or raw requests.Response
42
41
if expect == "soup" :
43
- return res .soup
42
+ return bs4 . BeautifulSoup ( res .text , "html.parser" )
44
43
elif expect == "json" :
45
44
return res .json ()
46
45
elif expect == "raw" :
@@ -99,17 +98,17 @@ def login(self, username, password):
99
98
def logout (self ):
100
99
"""Logs out the user.
101
100
102
- Logs out the user by creating new browser ."""
101
+ Logs out the user by creating new session ."""
103
102
104
103
logging .info ("Logging out." )
105
104
self ._logged_in = False
106
- self ._browser = Browser ()
105
+ self ._session = requests . Session ()
107
106
108
107
def get_logged_user (self , login_page = None ):
109
108
"""Returns the name of curently logged user or None, if no user is logged in."""
110
109
111
110
login_page = login_page or self ._request (self ._urls ["login_page" ], login_check = False )
112
- assert isinstance (login_page , BeautifulSoup )
111
+ assert isinstance (login_page , bs4 . BeautifulSoup )
113
112
114
113
logging .debug ("Checking for already logged user." )
115
114
try :
@@ -193,7 +192,7 @@ def _search_get_page(self, point, start_index):
193
192
"originTreatment" : 0
194
193
}, expect = "json" )
195
194
196
- return BeautifulSoup (res ["HtmlString" ].strip ())
195
+ return bs4 . BeautifulSoup (res ["HtmlString" ].strip (), "html.parser" )
197
196
198
197
def search_quick (self , area , * , strict = False , zoom = None ):
199
198
logging .info ("Searching quick in {}" .format (area ))
@@ -228,7 +227,8 @@ def post_log(self, wp, text, type=LogType.found_it, date=datetime.date.today()):
228
227
l = Log (type = type , text = text , visited = date )
229
228
self .get_cache (wp ).post_log (l )
230
229
231
- # ensure backwards compatibility -----------------------------------------
230
+ # ensure backwards compatibility ------------------------------------------
231
+ # deprecated methods will be removed in next version!
232
232
233
233
@deprecated
234
234
def load_cache (self , wp ):
0 commit comments