Exemplo n.º 1
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'])
Exemplo n.º 2
0
 def get_by_slug(self, slug):
     city = City.find_one({"slug": slug})
     country = city.relation_set.country
     return {
         "city": city,
         "country": country,
     }
Exemplo n.º 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'])
Exemplo n.º 4
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
Exemplo n.º 5
0
 def get_by_slug(self, slug):
     city = City.find_one({"slug": slug})
     country = city.relation_set.country
     return {
         "city": city,
         "country": country,
     }
Exemplo n.º 6
0
 def _get_cities_by_prefix(self, prefix):
     for city in City.find_prefix('name', prefix):
         country_code = city.relation_set.country.code.lower()
         yield {"name": city.name,
                "slug": city.slug,
                "country_code": country_code,
                "label": self._get_html_label(city.name, country_code), 
                "ranking": city.total_ranking}
Exemplo n.º 7
0
 def _get_cities_by_prefix(self, prefix):
     for city in City.find_prefix('name', prefix):
         country_code = city.relation_set.country.code.lower()
         yield {
             "name": city.name,
             "slug": city.slug,
             "country_code": country_code,
             "label": self._get_html_label(city.name, country_code),
             "ranking": city.total_ranking
         }
Exemplo n.º 8
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']))
Exemplo n.º 9
0
    def get_by_coordinates(self, obj, latlng):
        lat, lng = map(float, latlng.split(";"))
        resp = []
        for city in City.find_nearby('coordinates', [lat, lng], 100):
            resp.append({"name": city.name,
                         "slug": city.slug,
                         "country": city.relation_set.country.name,
                         "distance": get_distance([lat, lng],
                                                  city.coordinates)})

        return sorted(resp, key=lambda k: k["distance"])
Exemplo n.º 10
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']))
Exemplo n.º 11
0
    def get_by_coordinates(self, obj, latlng):
        lat, lng = map(float, latlng.split(";"))
        resp = []
        for city in City.find_nearby('coordinates', [lat, lng], 100):
            resp.append({
                "name": city.name,
                "slug": city.slug,
                "country": city.relation_set.country.name,
                "distance": get_distance([lat, lng], city.coordinates)
            })

        return sorted(resp, key=lambda k: k["distance"])
Exemplo n.º 12
0
def post_migrate():
    """
    Post-migration phase: index generations, cross-document relationships.
    """
    for country in Country.find():
        country['capital_city'] = City.find_one({
                      'old_pk': country['old_capitalcity_pk']})._id

        del country['old_capitalcity_pk']
        country.save()

    for cls in (City, Country, Region):
        cls.generate_index(cls.collection)
Exemplo n.º 13
0
def post_migrate():
    """
    Post-migration phase: index generations, cross-document relationships.
    """
    for country in Country.find():
        country['capital_city'] = City.find_one({
            'old_pk':
            country['old_capitalcity_pk']
        })._id

        del country['old_capitalcity_pk']
        country.save()

    for cls in (City, Country, Region):
        cls.generate_index(cls.collection)
Exemplo n.º 14
0
def migrate_city(entry):
    """
    Migration of a JSON-represented city.
    """
    corresp_map = {
        'wikiname':
        entry['fields']['wikiname'],
        'slug':
        entry['fields']['slug'],
        'name':
        entry['fields']['name'],
        'rankings': [
            float(entry['fields']['rating_factor_%d' % idx])
            for idx in xrange(1, 4)
        ],
        'timezone':
        u'UTC%s%d' % (u'' if entry['fields']['timezone'] < 0 else u'+',
                      entry['fields']['timezone']),
        'total_ranking':
        float(entry['fields']['total_rating']),
        'coordinates': [
            float(entry['fields']['latitude']),
            float(entry['fields']['longitude'])
        ],
        'country':
        Country.find_one({
            'old_pk': entry['fields']['country']
        })._id,
    }
    additional_corresp_map = {
        'old_pk': entry['pk'],
    }
    city = City.find_one({'old_pk': entry['pk']})
    if not city:
        city = City()

    city.set_lang("en")
    for k, v in corresp_map.iteritems():
        setattr(city, k, v)

    for k, v in additional_corresp_map.iteritems():
        city[k] = v

    city.save()

    return city
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
0
def migrate_city(entry):
    """
    Migration of a JSON-represented city.
    """
    corresp_map = {
        'wikiname': entry['fields']['wikiname'],
        'slug': entry['fields']['slug'],
        'name': entry['fields']['name'],
        'rankings': [float(entry['fields']['rating_factor_%d' % idx])
                     for idx in xrange(1, 4)],
        'timezone': u'UTC%s%d' % (
                         u'' if entry['fields']['timezone'] < 0 else u'+',
                         entry['fields']['timezone']),
        'total_ranking': float(entry['fields']['total_rating']),
        'coordinates': [float(entry['fields']['latitude']),
                        float(entry['fields']['longitude'])],
        'country': Country.find_one({
                         'old_pk': entry['fields']['country']})._id,
    }
    additional_corresp_map = {
        'old_pk': entry['pk'],
    }
    city = City.find_one({'old_pk': entry['pk']})
    if not city:
        city = City()

    city.set_lang("en")
    for k, v in corresp_map.iteritems():
        setattr(city, k, v)

    for k, v in additional_corresp_map.iteritems():
        city[k] = v

    city.save()

    return city
Exemplo n.º 18
0
from triplander.models import City

city = City.find_one({"slug": "groningen"})
point = city.coordinates
distance = 20

City.set_lang("en")
for c in City.find_nearby("coordinates", point, distance):
    print "1", c.name

for c in City.find_prefix("name", "gro"):
    print "2", c.name
Exemplo n.º 19
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
Exemplo n.º 20
0
def add_new_city(name, country):
    # we need a name and a country at first!
    std_error_obj = {'error': 2, 'city': None}
    if(country is None): return std_error_obj
    
    # by name
    coords = gmaplocal_coordinates(name,country.name)
    if(coords is None): return std_error_obj
    # let's see if this city already exists
    new_city = None
    try:
        new_city = City.objects.get(name__iexact=coords['city'])
        if(new_city is None): new_city = City.objects.get(local_name__iexact=coords['city'])
    except ObjectDoesNotExist:
        new_city = None
    
    if new_city is not None: return {'id': new_city.id, 'slug': new_city.slug, 'error': 1}
    
    new_city = City()
    new_city.name = coords['city']
    new_city.coordinates = Point((float(coords['latitude']),float(coords['longitude'])))
    new_city.latitude = Decimal(coords['latitude'])
    new_city.longitude = Decimal(coords['longitude'])
    new_city.country = country
    
    new_city.save()
    
    # now it has an ID
    new_city_id = new_city.id
    # take timezone info
    timezone = city_timezone(new_city_id)
    new_city.timezone = timezone['offset']
    # wikipedia tentative article name & rankings
    wname = _wikipedia_article(new_city_id)
    if (wname is not None): 
        new_city.wikiname = wname['wikiname']
    
    new_city.save()
    # rank, and it's done
    rank_city(new_city)
    
    return {'id': new_city_id, 'slug': new_city.slug, 'error': 0}