def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() location = self.request.get('location') types = self.request.get('types') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') # Check if we already have this state stored in the database params = { 'sensor': 'true', 'location': location, 'language': 'en', 'types': types, 'radius': 1000, 'rankby': 'prominence', 'key': config['API_KEY'] } params = urllib.urlencode(params) places = json.loads(urllib.urlopen(gapi['GOOGLE_PLACES_SEARCH'] % params).read()) places_queue = taskqueue.Queue('places') if 'status' in places and places['status'] == 'OK' and 'results' in places and places['results']: for place in places['results']: if 'reference' not in place: continue details = { 'id': None, 'name': None, 'reference': None, 'types': None, 'vicinity': None, 'latitude': None, 'longitude': None, 'icon': None } if 'id' in place and place['id']: details['id'] = place['id'] if 'reference' and place['reference']: details['reference'] = place['reference'] if 'name' in place and place['name']: details['name'] = place['name'] if 'geometry' in place and place['geometry'] and 'location' in place['geometry'] and place['geometry']['location']: details['latitude'] = place['geometry']['location']['lat'] details['longitude'] = place['geometry']['location']['lng'] if 'types' in place and place['types']: details['types'] = '|'.join(place['types']) if 'icon' in place and place['icon']: details['icon'] = place['icon'] if 'vicinity' in place and place['vicinity']: details['vicinity'] = place['vicinity'] if not all(details.values()): continue details['rating'] = None if 'rating' in place and place['rating']: details['rating'] = place['rating'] place_db = ( details['id'], city_id, details['reference'], details['types'], details['name'], details['vicinity'], details['latitude'], details['longitude'], details['rating'], details['icon'], str(datetime.datetime.now()) ) cursor.execute( """INSERT IGNORE INTO places (gapi_id, city_id, reference, types, name, vicinity, latitude, longitude, rating, icon, created) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", place_db ); place_id = cursor.lastrowid if 'photos' in place and place['photos'] and place_id: for photo in place['photos']: if 'photo_reference' in photo and photo['photo_reference']: cursor.execute("INSERT IGNORE INTO places_photos (place_id, reference) VALUES (%s, %s)", (place_id, photo['photo_reference'])) if place_id: t = taskqueue.Task( url='/api/google/places/details', params={ 'reference': details['reference'], 'place_id': place_id, 'plan_id': plan_id }, method='GET' ) places_queue.add(t) s.count_tasks(plan_id) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(places))
def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() # Get task specifics country = self.request.get('country') city = self.request.get('city') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') future_day = self.request.get('future_day') future_day = datetime.datetime.strptime(future_day, '%Y-%m-%d') # Filter Characters try: city= remove_accents(city).encode('utf8') except: city = city.encode('utf8') # Compose signature created = datetime.datetime.now() now = datetime.datetime.utcnow() now = calendar.timegm(now.utctimetuple()) sig = expedia['EXPEDIA_API_KEY'] + expedia['EXPEDIA_API_SECRET'] + str(now) # Generate signature hash m = hashlib.md5() m.update(sig.encode('utf-8')) signature = m.hexdigest() params = { 'cid': expedia['EXPEDIA_CID'], 'apiKey': expedia['EXPEDIA_API_KEY'], 'sig': signature, 'currencyCode': 'USD', 'city': city, 'countryCode': country, 'arrivalDate': future_day.strftime('%m/%d/%Y'), 'departureDate': (future_day + datetime.timedelta(days=10)).strftime('%m/%d/%Y'), 'numberOfResults': 16, '_type': 'json' } params = urllib.urlencode(params) hotels = json.loads(urllib2.urlopen(expedia['EXPEDIA_HOTELS_SEARCH'] % params, timeout=20).read()) # The Queue hotels_queue = taskqueue.Queue('hotels') if 'HotelListResponse' in hotels and hotels['HotelListResponse']: hotels = hotels['HotelListResponse'] if 'HotelList' in hotels and hotels['HotelList']: hotels = hotels['HotelList'] if 'HotelSummary' in hotels and hotels['HotelSummary']: hotels = hotels['HotelSummary'] if not hotels: # Sync completed Task s.update_count(plan_id) return if type(hotels) is dict: hotels = [hotels] for hotel in hotels: new_hotel = { 'hotel_id': None, 'name': None, 'thumbnail': None, 'description': None, 'location_description': None, 'url': None, 'latitude': None, 'longitude': None, 'address': None, 'high_rate': None, 'low_rate': None, 'rating': None } if 'hotelId' in hotel and hotel['hotelId']: new_hotel['hotel_id'] = hotel['hotelId'] if 'name' in hotel and hotel['name']: new_hotel['name'] = hotel['name'] if 'thumbNailUrl' in hotel and hotel['thumbNailUrl']: new_hotel['thumbnail'] = expedia['EXPEDIA_IMAGES_BASE_URL'] + hotel['thumbNailUrl'] if 'shortDescription' in hotel and hotel['shortDescription']: new_hotel['description'] = hotel['shortDescription'] if 'locationDescription' in hotel and hotel['locationDescription']: new_hotel['location_description'] = hotel['locationDescription'] if 'deepLink' in hotel and hotel['deepLink']: new_hotel['url'] = hotel['deepLink'] if 'latitude' in hotel and hotel['latitude']: new_hotel['latitude'] = hotel['latitude'] if 'longitude' in hotel and hotel['longitude']: new_hotel['longitude'] = hotel['longitude'] if 'address1' in hotel and hotel['address1']: new_hotel['address'] = hotel['address1'] if 'lowRate' in hotel and hotel['lowRate']: new_hotel['low_rate'] = hotel['lowRate'] if 'highRate' in hotel and hotel['highRate']: new_hotel['high_rate'] = hotel['highRate'] if 'hotelRating' in hotel and hotel['hotelRating']: new_hotel['rating'] = hotel['hotelRating'] if not all(new_hotel.values()): continue new_hotel['trip_advisor_rating'] = None if 'tripAdvisorRating' in hotel and hotel['tripAdvisorRating']: new_hotel['trip_advisor_rating'] = hotel['tripAdvisorRating'] new_hotel['amenity_mask'] = None if 'amenityMask' in hotel and hotel['amenityMask']: new_hotel['amenity_mask'] = hotel['amenityMask'] # Add new hotel to database hotel_db = ( city_id, new_hotel['hotel_id'], new_hotel['name'], new_hotel['thumbnail'], new_hotel['description'], new_hotel['location_description'], new_hotel['url'], new_hotel['latitude'], new_hotel['longitude'], new_hotel['address'], new_hotel['low_rate'], new_hotel['high_rate'], new_hotel['rating'], new_hotel['trip_advisor_rating'], new_hotel['amenity_mask'], future_day, created ) query = ("""INSERT INTO cities_hotels (city_id, hotel_id, name, thumbnail, description, location_description, url, latitude, longitude, address, low_rate, high_rate, rating, trip_advisor_rating, amenity_mask, filter_date, created) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""") cursor.execute(query, hotel_db) hotel_id = cursor.lastrowid # Let's grab some photos for this hotel t = taskqueue.Task( url='/api/expedia/photos/search', params={ 'plan_id': plan_id, 'city_id': city_id, 'hotel_id': hotel_id, 'expedia_hotel_id': new_hotel['hotel_id'] }, method='GET' ) hotels_queue.add(t) s.count_tasks(plan_id) # Write everything in DB db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(hotels))
def get(self): city = self.request.get('city') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') latitude = self.request.get('latitude') longitude = self.request.get('longitude') tag_mode = self.request.get('tag_mode') or 'all' # Sync Directive s = Sync() # Process query string city_query = [] try: city_query = [remove_accents(city).encode('utf8')] except: city_query = [city.encode('utf8')] query = city_query if tag_mode == 'all': query.extend(['travel', '-women', '-girl', '-woman', '-nude']) query = ' '.join(query) params = { 'api_key': flickr['FLICKR_API_KEY'], 'method': flickr['FLICKR_SEARCH'], 'text': query, 'per_page': 40, 'page': 1, 'privacy_filter': 1, 'content_type': 1, 'sort': 'interestingness-desc', 'media': 'photos', 'format': 'json', 'nojsoncallback': 1 } params = urllib.urlencode(params) photos = json.loads(urllib2.urlopen(flickr['FLICKR_BASE_URL'] % params, timeout=10).read()) flickr_queue = taskqueue.Queue('flickr') photo_count = 0 if 'photos' in photos and 'photo' in photos['photos']: for photo in photos['photos']['photo']: if 'id' in photo and photo['id']: t = taskqueue.Task( url='/api/flickr/get_size', params={ 'photo_id': photo['id'], 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) flickr_queue.add(t) s.count_tasks(plan_id) photo_count = photo_count + 1 # Safe Query for Flickr if photo_count < 5 and tag_mode == 'all': query = [] query.extend(['forest', 'hills', 'clouds', 'summer', 'spring', 'winter', 'autumn', 'sky', 'mountains', '-women', '-girl', '-woman', '-nude']) query = ' '.join(query) params = { 'api_key': flickr['FLICKR_API_KEY'], 'method': flickr['FLICKR_SEARCH'], 'text': query, 'per_page': 20, 'page': 1, 'privacy_filter': 1, 'content_type': 1, 'sort': 'interestingness-desc', 'media': 'photos', 'format': 'json', 'nojsoncallback': 1 } params = urllib.urlencode(params) photos = json.loads(urllib2.urlopen(flickr['FLICKR_BASE_URL'] % params, timeout=10).read()) if 'photos' in photos and 'photo' in photos['photos']: for photo in photos['photos']['photo']: if 'id' in photo and photo['id']: t = taskqueue.Task( url='/api/flickr/get_size', params={ 'photo_id': photo['id'], 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) flickr_queue.add(t) s.count_tasks(plan_id) # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(photos))
def post(self): # Check if User is already logged in if not self.session.get('google_id'): return self.redirect('/') # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Every entity is created NOW created = str(datetime.datetime.now()) # Get current user user_id = self.session.get('user_id') # Get address city = self.request.get('city') # Get geolocation data latitude = self.request.get('latitude') longitude = self.request.get('longitude') # Check to see if we have a valid location if not latitude or not longitude: self.session['tj__plan__error'] = 'The location you have selected is unknown. Please try again.' return self.redirect('/plan') country = self.request.get('country') short_city = self.request.get('short_city') future_day = self.request.get('future_day_submit') # Check to see if user filled in the future day if not future_day: self.session['tj__plan__error'] = 'Please select a future date around which you would like to plan your trip.' return self.redirect('/plan') # Extra Validation future_day = datetime.datetime.strptime(future_day, '%Y-%m-%d') future_tmp = calendar.timegm(future_day.utctimetuple()) future_day = future_day.strftime('%Y-%m-%d') # Check to see if we have short_city if not short_city: short_city = city.split(',')[0] # Get preferences places = self.request.get_all('places[]') # Check to see if the user has selected at least one type of place if not len(places): self.session['tj__plan__error'] = 'Please select at least one type of place that you would like to visit.' return self.redirect('/plan') places = '|'.join(places) events = self.request.get_all('events[]') # Check to see if the user has selected at least one type of event if not len(events): self.session['tj__plan__error'] = 'Please select at least one type of event that you would like to attend.' return self.redirect('/plan') events = '|'.join(events) # Do an extra call to Places API params = { 'sensor': 'true', 'location': str(latitude) + ',' + str(longitude), 'language': 'en', 'types': places, 'radius': 1000, 'rankby': 'prominence', 'key': config['API_KEY'] } params = urllib.urlencode(params) test_pl = json.loads(urllib.urlopen(gapi['GOOGLE_PLACES_SEARCH'] % params).read()) if 'status' not in test_pl or test_pl['status'] != 'OK' or 'results' not in test_pl or not test_pl['results'] or len(test_pl['results']) < 3: self.session['tj__plan__error'] = 'We couldn\'t find places that met your criteria. Please try to select more types.' return self.redirect('/plan') # Check if the city already exists in the Database query = ("""SELECT id, latitude, longitude, country FROM cities WHERE latitude = %s AND longitude = %s""") cursor.execute(query, (latitude, longitude)) city_db = cursor.fetchone() # Add city to database if it's not there if city_db is None: new_city = ( short_city, city, latitude, longitude, country, created ) query = ("""INSERT IGNORE INTO cities (name, address, latitude, longitude, country, created) VALUES (%s, %s, %s, %s, %s, %s)""") cursor.execute(query, new_city) city_id = cursor.lastrowid else: city_id = city_db['id'] # Write plan to database new_plan = ( user_id, city_id, places, events, datetime.datetime.fromtimestamp(future_tmp) ) query = ("""INSERT INTO plans (user_id, city_id, types, events, future_day) VALUES (%s, %s, %s, %s, %s)""") cursor.execute(query, new_plan) db_conn.commit() plan_id = cursor.lastrowid # Let's fire up the Main queue main_queue = taskqueue.Queue('main') # Sync Directive s = Sync() # Let's search for some cool places location = str(latitude + ',' + longitude) t = taskqueue.Task( url='/api/google/places/search', params={ 'location': location, 'types': places, 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # Let's find some awesome videos t = taskqueue.Task( url='/api/google/videos/search', params={ 'city': short_city, 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # Fancy reading a book? t = taskqueue.Task( url='/api/google/books/search', params={ 'city': short_city, 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # Photos from flickr t = taskqueue.Task( url='/api/flickr/search', params={ 'city': short_city, 'city_id': city_id, 'plan_id': plan_id, 'latitude': latitude, 'longitude': longitude }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # Let's grab some events so we don't get bored on our trip t = taskqueue.Task( url='/api/eventbrite/events/search', params={ 'city': short_city, 'city_id': city_id, 'country': country, 'latitude': latitude, 'longitude': longitude, 'categories': events, 'plan_id': plan_id, 'future_day': future_day }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # What about hotels? t = taskqueue.Task( url='/api/expedia/hotels/search', params={ 'city': short_city, 'city_id': city_id, 'plan_id': plan_id, 'country': country, 'future_day': future_day }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # How about a little wikipedia knowledge t = taskqueue.Task( url='/api/wikipedia/summary', params={ 'city': short_city, 'city_id': city_id, 'plan_id': plan_id }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # But what about the weather? t = taskqueue.Task( url='/api/forecast/future', params={ 'plan_id': plan_id, 'latitude': latitude, 'longitude': longitude, 'future_day': str(future_tmp) }, method='GET' ) main_queue.add(t) s.count_tasks(plan_id) # Write to DB db_conn.commit() # Let's go somewhere beautiful return self.redirect('/plans')