python Weather API location id
Question: 
    a=pywapi.get_loc_id_from_weather_com("pune")
    
    
    {0: (u'TTXX0257', u'Pune, OE, Timor-leste'), 
     1: (u'INXX0102', u'Pune, MH, India'), 
     2: (u'BRPA0444', u'Pune, PA, Brazil'),
     3: (u'FRBR2203', u'Punel, 29, France'), 
     4: (u'IDVV9705', u'Punen, JT, Indonesia'),
     5: (u'IRGA2787', u'Punel, 19, Iran'),
     6: (u'IRGA2788', u'Punes, 19, Iran'),
     7: (u'IDYY7030', u'Punen, JI, Indonesia'), 
     8: (u'RSUD1221', u'Punem, UD, Russia'),
     9: (u'BUXX2256', u'Punevo, 09, Bulgaria'),    
    'count': 10}
    

For the above command, I'm getting 10 results. I want a specific location like
Pune,MH,India. How do I get it?


Answer: I look the source code of pywapi, and found that the searchstring would be
quoted(url encode, e.g. ',' will be quoted to "%2C") in
`get_loc_id_from_waather_com`.

So when you call `pywapi.get_loc_id_from_weather_com(" Pune,MH,India")` it
will request the
url:`http://xml.weather.com/search/search?where=Pune%2CMH%2CIndia` but not
`http://wxdata.weather.com/wxdata/search/search?where=Pune,MH,India`. And the
formmer is certain no results.

A solution is that you can modify(hack) the pywapi. Just edit the pywapi.py
and find the `get_loc_id_from_weather_com` function. replace the line `url =
LOCID_SEARCH_URL % quote(search_string)` to `url = LOCID_SEARCH_URL %
quote(search_string, ',')` And now you can:

    
    
    In [2]: import pywapi
    
    In [3]: pywapi.get_loc_id_from_weather_com("Pune,MH,India") # no spaces
    Out[3]: {0: (u'INXX0102', u'Pune, MH, India'), 'count': 1}
    

  
  
  
PS: The source code of pywapi:

    
    
    def get_loc_id_from_weather_com(search_string):
        """Get location IDs for place names matching a specified string.
        Same as get_location_ids() but different return format.
    
        Parameters:
          search_string: Plaintext string to match to available place names.
          For example, a search for 'Los Angeles' will return matches for the
          city of that name in California, Chile, Cuba, Nicaragua, etc as well
          as 'East Los Angeles, CA', 'Lake Los Angeles, CA', etc.
    
        Returns:
          loc_id_data: A dictionary of tuples in the following format:
          {'count': 2, 0: (LOCID1, Placename1), 1: (LOCID2, Placename2)}
    
        """
        # Weather.com stores place names as ascii-only, so convert if possible
        try: 
            # search_string = unidecode(search_string.encode('utf-8'))
            search_string = unidecode(search_string)
        except NameError:
            pass 
    
        url = LOCID_SEARCH_URL % quote(search_string) 
        # change to:url = LOCID_SEARCH_URL % quote(search_string, ',') 
        try: 
            handler = urlopen(url)
        except URLError:
            return {'error': 'Could not connect to server'}
        if sys.version > '3': 
            # Python 3
            content_type = dict(handler.getheaders())['Content-Type']
        else:
            # Python 2
            content_type = handler.info().dict['content-type']
        try: 
            charset = re.search('charset\=(.*)', content_type).group(1)
        except AttributeError:
            charset = 'utf-8'
        if charset.lower() != 'utf-8':
            xml_response = handler.read().decode(charset).encode('utf-8')
        else:
            xml_response = handler.read()
        dom = minidom.parseString(xml_response)
        handler.close()
    
        loc_id_data = {} 
        try: 
            num_locs = 0
            for loc in dom.getElementsByTagName('search')[0].getElementsByTagName('loc'):
                loc_id = loc.getAttribute('id')  # loc id
                place_name = loc.firstChild.data  # place name
                loc_id_data[num_locs] = (loc_id, place_name)
                num_locs += 1 
            loc_id_data['count'] = num_locs
        except IndexError:
            error_data = {'error': 'No matching Location IDs found'}
            return error_data
        finally:
            dom.unlink()
    
        return loc_id_data
    

