/usr/lib/python3/dist-packages/geopy/geocoders/wiki_gis.py is in python3-geopy 0.95.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | from urllib.parse import urlencode
from urllib.request import urlopen
import xml
from xml.parsers.expat import ExpatError
from geopy import util
import collections
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
util.logger.warn("BeautifulSoup was not found. "
"The MediaWiki geocoder will not work.")
from geopy.geocoders.base import Geocoder,GeocoderError,GeocoderResultError
from geopy import Point, Location, util
class MediaWiki(Geocoder):
def __init__(self, format_url, transform_string=None):
"""Initialize a geocoder that can parse MediaWiki pages with the GIS
extension enabled.
``format_url`` is a URL string containing '%s' where the page name to
request will be interpolated. For example: 'http://www.wiki.com/wiki/%s'
``transform_string`` is a callable that will make appropriate
replacements to the input string before requesting the page. If None is
given, the default transform_string which replaces ' ' with '_' will be
used. It is recommended that you consider this argument keyword-only,
since subclasses will likely place it last.
"""
self.format_url = format_url
if isinstance(transform_string, collections.Callable):
self.transform_string = transform_string
@classmethod
def transform_string(cls, string):
"""Do the WikiMedia dance: replace spaces with underscores."""
return string.replace(' ', '_')
def geocode(self, string):
if isinstance(string, str):
string = string.encode('utf-8')
wiki_string = self.transform_string(string)
url = self.format_url % wiki_string
return self.geocode_url(url)
def geocode_url(self, url):
util.logger.debug("Fetching %s..." % url)
page = urlopen(url)
name, (latitude, longitude) = self.parse_xhtml(page)
return (name, (latitude, longitude))
def parse_xhtml(self, page):
soup = isinstance(page, BeautifulSoup) and page or BeautifulSoup(page)
meta = soup.head.find('meta', {'name': 'geo.placename'})
name = meta and meta['content'] or None
meta = soup.head.find('meta', {'name': 'geo.position'})
if meta:
position = meta['content']
latitude, longitude = util.parse_geo(position)
if latitude == 0 or longitude == 0:
latitude = longitude = None
else:
latitude = longitude = None
return (name, (latitude, longitude))
|