<s> import xml . dom . minidom <EOL> from BeautifulSoup import BeautifulSoup <EOL> from geopy . geocoders . base import Geocoder <EOL> from geopy . point import Point <EOL> from geopy . location import Location <EOL> from geopy import util <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> class SemanticMediaWiki ( Geocoder ) : <EOL> def __init__ ( self , format_url , attributes = None , relations = None , <EOL> prefer_semantic = False , transform_string = None ) : <EOL> self . format_url = format_url <EOL> self . attributes = attributes <EOL> self . relations = relations <EOL> self . prefer_semantic = prefer_semantic <EOL> self . transform_string = transform_string <EOL> def get_url ( self , string ) : <EOL> return self . format_url % self . transform_string ( string ) <EOL> def parse_rdf_link ( self , page , mime_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> soup = BeautifulSoup ( page ) <EOL> link = soup . head . find ( '<STR_LIT>' , rel = '<STR_LIT>' , type = mime_type ) <EOL> return link and link [ '<STR_LIT>' ] or None <EOL> def parse_rdf_things ( self , data ) : <EOL> dom = xml . dom . minidom . parseString ( data ) <EOL> thing_map = { } <EOL> things = dom . getElementsByTagName ( '<STR_LIT>' ) <EOL> things . reverse ( ) <EOL> for thing in things : <EOL> name = thing . attributes [ '<STR_LIT>' ] . value <EOL> articles = thing . getElementsByTagName ( '<STR_LIT>' ) <EOL> things [ name ] = articles [ <NUM_LIT:0> ] . attributes [ '<STR_LIT>' ] . value <EOL> return ( things , thing ) <EOL> def transform_semantic ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return string . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) . capitalize ( ) <EOL> def get_relations ( self , thing , relations = None ) : <EOL> if relations is None : <EOL> relations = self . relations <EOL> for relation in relations : <EOL> relation = self . transform_semantic ( relation ) <EOL> for node in thing . getElementsByTagName ( '<STR_LIT>' + relation ) : <EOL> resource = node . attributes [ '<STR_LIT>' ] . value <EOL> yield ( relation , resource ) <EOL> def get_attributes ( self , thing , attributes = None ) : <EOL> if attributes is None : <EOL> attributes = self . attributes <EOL> for attribute in attributes : <EOL> attribute = self . transform_semantic ( attribute ) <EOL> for node in thing . getElementsByTagName ( '<STR_LIT>' + attribute ) : <EOL> value = node . firstChild . nodeValue . strip ( ) <EOL> yield ( attribute , value ) <EOL> def get_thing_label ( self , thing ) : <EOL> return util . get_first_text ( thing , '<STR_LIT>' ) <EOL> def geocode_url ( self , url , attempted = None ) : <EOL> if attempted is None : <EOL> attempted = set ( ) <EOL> print "<STR_LIT>" % url <EOL> page = urlopen ( url ) <EOL> soup = BeautifulSoup ( page ) <EOL> rdf_url = self . parse_rdf_link ( soup ) <EOL> print "<STR_LIT>" % rdf_url <EOL> page = urlopen ( rdf_url ) <EOL> things , thing = self . parse_rdf ( page ) <EOL> name = self . get_label ( thing ) <EOL> attributes = self . get_attributes ( thing ) <EOL> for attribute , value in attributes : <EOL> latitude , longitude = util . parse_geo ( value ) <EOL> if None not in ( latitude , longitude ) : <EOL> break <EOL> if None in ( latitude , longitude ) : <EOL> relations = self . get_relations ( thing ) <EOL> for relation , resource in relations : <EOL> url = things . get ( resource , resource ) <EOL> if url in tried : <EOL> continue <EOL> tried . add ( url ) <EOL> name , ( latitude , longitude ) = self . geocode_url ( url , tried ) <EOL> if None not in ( name , latitude , longitude ) : <EOL> break <EOL> return ( name , ( latitude , longitude ) ) </s>
