def parseLocation(self, strLocation): print('[parserForAngellist] Parsing location : ' + strLocation) if(strLocation.strip() == ''): dicEmptyGeoname = {} dicEmptyGeoname['strCity'] = '' dicEmptyGeoname['strCountry'] = '' dicEmptyGeoname['strContinent'] = '' return dicEmptyGeoname if(self.__geonamesCache.get(strLocation) == None): dicGeonameCache = {} dicSearchResult = geonames.search(q=strLocation, maxRows=10, featureClass='P') #countryBias='US' lstStrLocation = strLocation.split() while (dicSearchResult == None or len(dicSearchResult) == 0) and len(lstStrLocation) > 1: del lstStrLocation[-1] strSubLocation = ' '.join(lstStrLocation) print('[parserForAngellist] Parsing location : ' + strSubLocation) dicSearchResult = geonames.search(q=strSubLocation, maxRows=10, featureClass='P') #countryBias='US' if(dicSearchResult == None or len(dicSearchResult) == 0): dicNotFoundGeoname = {} dicNotFoundGeoname['strCity'] = '' dicNotFoundGeoname['strCountry'] = '' dicNotFoundGeoname['strContinent'] = '' return dicNotFoundGeoname strGeonameId = dicSearchResult[0]['geonameId'] # import pdb; pdb.set_trace() dicGeoname = geonames.get(strGeonameId) strFclName = dicGeoname['fclName'] strCountry = dicGeoname['countryCode'] strContinent = dicGeoname['continentCode'] # import pdb; pdb.set_trace() strCity = '' if 'city' in strFclName: strCity = dicGeoname['name'] elif dicGeoname.get('bbox') != None: bbox = dicGeoname['bbox'] dicCity = geonames.findCity(north=bbox['north'], south=bbox['south'], east=bbox['east'], west=bbox['west'])[0] strCity = dicCity['name'] else: strCity = strLocation import pdb; pdb.set_trace() dicGeonameCache['strCity'] = strCity dicGeonameCache['strCountry'] = strCountry dicGeonameCache['strContinent'] = strContinent self.__geonamesCache[strLocation] = dicGeonameCache return self.__geonamesCache[strLocation]
def getbylocation(location): '''Returns a Weather object for the given location name.''' if iszip(location): lat, lon = noaa.ziplatlon(int(location)) return Weather(lat, lon) else: place = gn.search(location)[0] return Weather(place['lat'], place['lng'])
def test_search_yes_results(self): assert_greater(len(gn.search('turlock')), 0) assert_greater(len(gn.search('los banos, ca')), 0) assert_greater(len(gn.search('San Francisco, CA')), 0) assert_greater(len(gn.search('kansas city')), 0) assert_greater(len(gn.search('miami')), 0) assert_greater(len(gn.search('90210')), 0)
def get_latlng(name_arg): result = geonames.search( q=name_arg, username=GEONAMES_USERNAME, isNameRequired='true' ) matches = result.get('geonames', []) if len(matches) > 0: return matches[0]['lat'], matches[0]['lng'] return None
def test_search_no_results(self): assert_list_equal(gn.search('blahblah123'), []) assert_list_equal(gn.search('Ghubaysh'), []) assert_list_equal(gn.search('Dushanbe'), []) assert_list_equal(gn.search('Yeruham'), [])