예제 #1
0
def geocode(q, api_key):
    find = make_nsfind({'ns': 'urn:yahoo:maps'})
    args = {'location': q, 'appid': api_key}
    url = 'http://local.yahooapis.com/MapsService/V1/geocode?%s' % urllib.urlencode(args)
    et = ET.parse(urllib.urlopen(url))
    
    result = find(et, '//ns:Result')
    if not result:
        return (None, (None, None))
    else:
        namebits = {}
        for field in ('Address', 'City', 'State', 'Zip', 'Country'):
            bit = find(result, 'ns:%s' % field)
            if bit is not None and bit.text:
                namebits[field] = bit.text.decode('utf8')

        if 'Address' in namebits:
            name = '%(Address)s, %(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
        elif 'Zip' in namebits:
            name = '%(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
        elif 'City' in namebits:
            name = '%(City)s, %(State)s, %(Country)s' % namebits
        elif 'State' in namebits:
            name = '%(State)s, %(Country)s' % namebits
        elif 'Country' in namebits:
            name = namebits['Country']
        else:
            return (None, (None, None))

        lat = float(find(result, 'ns:Latitude').text)
        lon = float(find(result, 'ns:Longitude').text)
        
        return (name, (lat, lon))
예제 #2
0
def geocode(q, api_key):
    find = make_nsfind({'ns': 'urn:yahoo:maps'})
    args = {'location': q, 'appid': api_key}
    url = 'http://local.yahooapis.com/MapsService/V1/geocode?%s' % urllib.urlencode(args)
    et = ET.parse(urllib.urlopen(url))
    
    result = find(et, '//ns:Result')
    if not result:
        return (None, (None, None))
    else:
        namebits = {}
        for field in ('Address', 'City', 'State', 'Zip', 'Country'):
            bit = find(result, 'ns:%s' % field)
            if bit is not None and bit.text:
                namebits[field] = bit.text.decode('utf8')

        if 'Address' in namebits:
            name = '%(Address)s, %(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
        elif 'Zip' in namebits:
            name = '%(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
        elif 'City' in namebits:
            name = '%(City)s, %(State)s, %(Country)s' % namebits
        elif 'State' in namebits:
            name = '%(State)s, %(Country)s' % namebits
        elif 'Country' in namebits:
            name = namebits['Country']
        else:
            return (None, (None, None))

        lat = float(find(result, 'ns:Latitude').text)
        lon = float(find(result, 'ns:Longitude').text)
        
        return (name, (lat, lon))
예제 #3
0
def geocode(q, api_key):
    find = make_nsfind({'ns': 'http://wherein.yahooapis.com/v1/schema'})
    args = {
        'documentContent': q,
        'documentType': 'text/plain',
        'appid': api_key,
    }
    et = ET.parse(
        urllib.urlopen('http://wherein.yahooapis.com/v1/document',
                       urlencode(args)))
    place = find(et, 'ns:document/ns:placeDetails/ns:place')
    if place is None:
        return None, (None, None)
    else:
        name = find(place, 'ns:name').text.decode('utf8')
        lat = float(find(place, 'ns:centroid/ns:latitude').text)
        lon = float(find(place, 'ns:centroid/ns:longitude').text)
        return name, (lat, lon)
예제 #4
0
def geocode(q, api_key):
    find = make_nsfind({
        'ns': 'http://wherein.yahooapis.com/v1/schema'
    })
    args = {
        'documentContent': q,
        'documentType': 'text/plain',
        'appid': api_key,
    }
    et = ET.parse(urllib.urlopen(
        'http://wherein.yahooapis.com/v1/document', urllib.urlencode(args))
    )
    place = find(et, 'ns:document/ns:placeDetails/ns:place')
    if place is None:
        return None, (None, None)
    else:
        name = find(place, 'ns:name').text.decode('utf8')
        lat = float(find(place, 'ns:centroid/ns:latitude').text)
        lon = float(find(place, 'ns:centroid/ns:longitude').text)
        return name, (lat, lon)
예제 #5
0
def get_embed_code(url, maxwidth=None, maxheight=None):
    embed = {}
    header = cache.get_headers(url)
    if header.get('content-type', '').startswith('text/html'):
        html = cache.read_url(url)
        json_oembed = filter(lambda l: 'json+oembed' in l, re.compile('<link.*?>').findall(html))
        xml_oembed = filter(lambda l: 'xml+oembed' in l, re.compile('<link.*?>').findall(html))
        if json_oembed:
            oembed_url = find_re(json_oembed[0], 'href="(.*?)"')
            if maxwidth:
                oembed_url += '&maxwidth=%d' % maxwidth
            if maxheight:
                oembed_url += '&maxheight=%d' % maxheight
            embed = json.loads(cache.read_url(oembed_url))
        elif xml_oembed:
            oembed_url = find_re(json_oembed[0], 'href="(.*?)"')
            if maxwidth:
                oembed_url += '&maxwidth=%d' % maxwidth
            if maxheight:
                oembed_url += '&maxheight=%d' % maxheight
            data = cache.read_url(oembed_url)
            for e in ET.fromstring(data):
                embed[e.tag] = e.text
    return embed