예제 #1
0
def weather_conditions(city_slug): # weather conditions by Google
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return

    weather_url = ((u"http://www.google.com/ig/api?weather=%s,%s&hl=en-gb") %
                   (urllib.quote(cobj.name.encode('utf-8')),
                    urllib.quote(cobj.country.name.encode('utf-8'))))

    httpresp = urllib2.urlopen(weather_url)
    httpcont = httpresp.read()
    # the usual unicode mess...
    httpwhat = httpcont.replace(
                    '<?xml version="1.0"?>',
                    '<?xml version="1.0" encoding="ISO-8859-15"?>').decode(
                                                     'latin1').encode('latin1')
    
    xmlresp = minidom.parseString(httpwhat)
    success = xmlresp.getElementsByTagName('problem_cause')
    dataresp = {} 
    if len(success) == 1:
        return  # unsuccessful response (city not found)
    current_cond = xmlresp.getElementsByTagName('current_conditions')
    forecast_cond = xmlresp.getElementsByTagName('forecast_conditions')
    if (len(current_cond) == 0 or len(forecast_cond) == 0):
        return  # unsuccessful response (expected data not found)

    base_icon_url = 'http://www.google.com'
    # current conditions
    current_cond = current_cond[0]

    dataresp['current'] = {
       'temp': current_cond.getElementsByTagName(
                                     'temp_c')[0].getAttribute('data'),
       'condition': current_cond.getElementsByTagName(
                                  'condition')[0].getAttribute('data'),
       'icon': base_icon_url+current_cond.getElementsByTagName(
                                       'icon')[0].getAttribute('data'),
    }

    forecast_resp = []
    # forecast conditions
    for fc in forecast_cond:
        this_entry = {
          'temp_low': fc.getElementsByTagName(
                          'low')[0].getAttribute('data'),
          'temp_high': fc.getElementsByTagName(
                           'high')[0].getAttribute('data'),
          'condition': fc.getElementsByTagName(
                               'condition')[0].getAttribute('data'),
          'icon': base_icon_url+fc.getElementsByTagName(
                                'icon')[0].getAttribute('data'),
          'day': fc.getElementsByTagName(
                             'day_of_week')[0].getAttribute('data'),
        }
        forecast_resp.append(this_entry)

    dataresp['forecast'] = forecast_resp
    
    return dataresp
예제 #2
0
def yahootravel_ranking(city_slug):
    cobj = City.find(slug=city_slug)

    query_array = cobj.name.lower().split(" ")
    if (cobj.region is not None):
        query_array.append("\"" + cobj.region.name.lower() + "\"")

    query_array.append("\"" + cobj.country.name.lower() + "\"")
    my_wikiname = re.sub(ur"[,\.:();]", " ", cobj.wikiname)
    my_wikiname = re.sub(ur" {2,}", " ", my_wikiname)
    query_array = query_array + my_wikiname.split(" ")

    yt_query = " ".join(set(query_array))

    yt_url = (("http://travel.yahooapis.com/TripService/V1.1/" +
               "tripSearch?appid=%s&query=%s&results=%d&output=json") %
              (YAHOO_DEVAPP_ID, urllib.quote(yt_query.encode('utf-8')), 10))
    httpsched = get_http_scheduler()
    httpwhat = httpsched.urlopen(service="yahootravel",
                                 url=yt_url,
                                 json=True,
                                 alt_delta=2)

    if ('ResultSet' not in httpwhat):
        return
    if ('totalResultsAvailable' not in httpwhat['ResultSet']):
        return

    return int(httpwhat['ResultSet']['totalResultsAvailable'])
예제 #3
0
def yahootravel_ranking(city_slug):
    cobj = City.find(slug=city_slug)

    query_array = cobj.name.lower().split(" ")
    if(cobj.region is not None):
        query_array.append("\"" + cobj.region.name.lower() + "\"")

    query_array.append("\"" + cobj.country.name.lower() + "\"")
    my_wikiname = re.sub(ur"[,\.:();]", " ", cobj.wikiname)
    my_wikiname = re.sub(ur" {2,}", " ", my_wikiname)
    query_array = query_array + my_wikiname.split(" ")

    yt_query = " ".join(set(query_array))

    yt_url = (("http://travel.yahooapis.com/TripService/V1.1/" + 
               "tripSearch?appid=%s&query=%s&results=%d&output=json") %
              (YAHOO_DEVAPP_ID, urllib.quote(yt_query.encode('utf-8')), 10))
    httpsched = get_http_scheduler()
    httpwhat = httpsched.urlopen(service="yahootravel", url=yt_url,
                                 json=True, alt_delta=2)

    if ('ResultSet' not in httpwhat):
        return
    if ('totalResultsAvailable' not in httpwhat['ResultSet']):
        return

    return int(httpwhat['ResultSet']['totalResultsAvailable'])
예제 #4
0
def panoramio_ranking(city_slug):
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return

    panoramio_resp = _open_panoramio_conn(
        (cobj.coordinates.x, cobj.coordinates.y), 1)
    if (not panoramio_resp):
        return 0

    return decimal.Decimal(str(panoramio_resp['count']))
예제 #5
0
def panoramio_ranking(city_slug):
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return
    
    panoramio_resp = _open_panoramio_conn((cobj.coordinates.x,
                                           cobj.coordinates.y), 1)
    if (not panoramio_resp):
        return 0

    return decimal.Decimal(str(panoramio_resp['count']))
예제 #6
0
def panoramio_set(city_slug):
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return

    panoramio_resp = _open_panoramio_conn((cobj.coordinates.x,
                                           cobj.coordinates.y))
    if (not panoramio_resp):
        return

    response = []
    for p in panoramio_resp['photos']:
        this_entry = {'href': p['photo_url'],
                      'src': p['photo_file_url'],
                      'alt': p['photo_title'],
                      }
        response.append(this_entry)
    
    return response
예제 #7
0
def panoramio_set(city_slug):
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return

    panoramio_resp = _open_panoramio_conn(
        (cobj.coordinates.x, cobj.coordinates.y))
    if (not panoramio_resp):
        return

    response = []
    for p in panoramio_resp['photos']:
        this_entry = {
            'href': p['photo_url'],
            'src': p['photo_file_url'],
            'alt': p['photo_title'],
        }
        response.append(this_entry)

    return response
예제 #8
0
def weather_conditions(city_slug):  # weather conditions by Google
    cobj = City.find({"slug": city_slug})
    if not cobj:
        return

    weather_url = ((u"http://www.google.com/ig/api?weather=%s,%s&hl=en-gb") %
                   (urllib.quote(cobj.name.encode('utf-8')),
                    urllib.quote(cobj.country.name.encode('utf-8'))))

    httpresp = urllib2.urlopen(weather_url)
    httpcont = httpresp.read()
    # the usual unicode mess...
    httpwhat = httpcont.replace(
        '<?xml version="1.0"?>',
        '<?xml version="1.0" encoding="ISO-8859-15"?>').decode(
            'latin1').encode('latin1')

    xmlresp = minidom.parseString(httpwhat)
    success = xmlresp.getElementsByTagName('problem_cause')
    dataresp = {}
    if len(success) == 1:
        return  # unsuccessful response (city not found)
    current_cond = xmlresp.getElementsByTagName('current_conditions')
    forecast_cond = xmlresp.getElementsByTagName('forecast_conditions')
    if (len(current_cond) == 0 or len(forecast_cond) == 0):
        return  # unsuccessful response (expected data not found)

    base_icon_url = 'http://www.google.com'
    # current conditions
    current_cond = current_cond[0]

    dataresp['current'] = {
        'temp':
        current_cond.getElementsByTagName('temp_c')[0].getAttribute('data'),
        'condition':
        current_cond.getElementsByTagName('condition')[0].getAttribute('data'),
        'icon':
        base_icon_url +
        current_cond.getElementsByTagName('icon')[0].getAttribute('data'),
    }

    forecast_resp = []
    # forecast conditions
    for fc in forecast_cond:
        this_entry = {
            'temp_low':
            fc.getElementsByTagName('low')[0].getAttribute('data'),
            'temp_high':
            fc.getElementsByTagName('high')[0].getAttribute('data'),
            'condition':
            fc.getElementsByTagName('condition')[0].getAttribute('data'),
            'icon':
            base_icon_url +
            fc.getElementsByTagName('icon')[0].getAttribute('data'),
            'day':
            fc.getElementsByTagName('day_of_week')[0].getAttribute('data'),
        }
        forecast_resp.append(this_entry)

    dataresp['forecast'] = forecast_resp

    return dataresp