def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() city = self.request.get('city').encode('utf8') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') params = { 'format': 'json', 'action': 'query', 'prop': 'extracts', 'titles': city, 'redirects': 1, 'exsentences': 4, 'explaintext': 1 } params = urllib.urlencode(params) details = json.loads(urllib.urlopen(wikipedia['WIKI_BASE_URL'] % params).read()) if 'query' in details and 'pages' in details['query'] and details['query']['pages']: page_ids = details['query']['pages'].keys() page_id = page_ids.pop() page = None if page_id: page = details['query']['pages'][page_id] summary = None if page and 'extract' in page: summary = page['extract'] summary = [i for i in summary.split('.')] if len(summary) > 1: summary = summary[1:] summary = '.'.join(summary) if summary: query = ("UPDATE cities SET summary = %s WHERE id = %s") cursor.execute(query, (summary, city_id)) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(summary))
def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() photo_id = self.request.get('photo_id') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') params = { 'api_key': flickr['FLICKR_API_KEY'], 'method': flickr['FLICKR_GET_SIZE'], 'photo_id': photo_id, 'format': 'json', 'nojsoncallback': 1 } params = urllib.urlencode(params) photo_details = json.loads(urllib2.urlopen(flickr['FLICKR_BASE_URL'] % params, timeout=10).read()) if 'sizes' in photo_details and 'size' in photo_details['sizes']: for size in photo_details['sizes']['size']: if size and 'label' in size and 'source' in size and 'width' in size and 'height' in size: if size['label'] in ['Small', 'Small 320', 'Square', 'Large Square', 'Thumbnail']: continue details = ( photo_id, city_id, size['label'], size['source'], int(size['width']), int(size['height']) ) cursor.execute("INSERT IGNORE INTO photos (photo_id, city_id, size, url, width, height) VALUES (%s, %s, %s, %s, %s, %s)", details) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(photo_details))
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() city = self.request.get('city').encode('utf8') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') # Process query string query = [item.replace(',', '') for item in city.split()] query.append('travel') query = '+'.join(query) params = { 'key': config['API_KEY'], 'q': query, 'maxResults': 30, 'orderBy': 'relevance', 'country': 'US' } params = urllib.urlencode(params) books = json.loads(urllib.urlopen(gapi['GOOGLE_BOOKS_SEARCH'] % params).read()) if 'items' in books and books['items']: for item in books['items']: book_db = { 'book_id': None, 'title': None, 'description': None, 'thumbnail': None, 'url': None } if 'id' in item and item['id']: book_db['book_id'] = item['id'] if 'volumeInfo' in item and item['volumeInfo'] and 'title' in item['volumeInfo'] and item['volumeInfo']['title']: book_db['title'] = item['volumeInfo']['title'] if 'volumeInfo' in item and item['volumeInfo'] and 'description' in item['volumeInfo'] and item['volumeInfo']['description']: book_db['description'] = item['volumeInfo']['description'] if 'volumeInfo' in item and item['volumeInfo'] and 'imageLinks' in item['volumeInfo'] and item['volumeInfo']['imageLinks']: img = item['volumeInfo']['imageLinks'] if 'thumbnail' in img and img['thumbnail']: book_db['thumbnail'] = img['thumbnail'] if 'volumeInfo' in item and item['volumeInfo'] and 'infoLink' in item['volumeInfo'] and item['volumeInfo']['infoLink']: book_db['url'] = item['volumeInfo']['infoLink'] if not all(book_db.values()): continue cursor.execute( "INSERT IGNORE INTO cities_books (city_id, book_id, title, description, thumbnail, url) VALUES (%s, %s, %s, %s, %s, %s)", (city_id, book_db['book_id'], book_db['title'], book_db['description'], book_db['thumbnail'], book_db['url']) ) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(books))
def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() city = self.request.get('city').encode('utf8') city_id = self.request.get('city_id') plan_id = self.request.get('plan_id') # Process query string query = [item.replace(',', '') for item in city.split()] query.append('travel') query = '+'.join(query) params = { 'part': 'snippet', 'key': config['API_KEY'], 'type': 'video', 'order': 'relevance', 'maxResults': 30, 'videoDefinition': 'high', 'q': query } params = urllib.urlencode(params) videos = json.loads(urllib.urlopen(gapi['GOOGLE_VIDEOS_SEARCH'] % params).read()) if 'items' in videos and videos['items']: for item in videos['items']: video_db = { 'video_id': None, 'title': None, 'thumbnail': None } if 'id' in item and item['id'] and 'videoId' in item['id'] and item['id']['videoId']: video_db['video_id'] = item['id']['videoId'] if 'snippet' in item and item['snippet'] and 'title' in item['snippet'] and item['snippet']['title']: video_db['title'] = item['snippet']['title'] if 'snippet' in item and item['snippet'] and 'thumbnails' in item['snippet'] and item['snippet']['thumbnails']: thumb = item['snippet']['thumbnails'] if 'high' in thumb and thumb['high'] and 'url' in thumb['high'] and thumb['high']['url']: video_db['thumbnail'] = thumb['high']['url'] if not all(video_db.values()): continue cursor.execute( "INSERT IGNORE INTO cities_videos (city_id, video_id, title, thumbnail) VALUES (%s, %s, %s, %s)", (city_id, video_db['video_id'], video_db['title'], video_db['thumbnail']) ) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(videos))
def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() place_id = self.request.get('place_id') reference = self.request.get('reference') plan_id = self.request.get('plan_id') if not place_id: # Sync completed Task s.update_count(plan_id) return False params = { 'sensor': 'true', 'reference': reference, 'language': 'en', 'key': config['API_KEY'] } params = urllib.urlencode(params) details = json.loads(urllib.urlopen(gapi['GOOGLE_PLACES_DETAILS'] % params).read()) if 'result' in details and details['result'] and 'photos' in details['result'] and details['result']['photos']: for photo in details['result']['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 'result' in details and details['result'] and 'reviews' in details['result'] and details['result']['reviews']: for review in details['result']['reviews']: place_review = { 'author_id': None, 'author_name': None, 'review': None, 'rating': None, 'reviewed_on': None } if 'text' in review and review['text']: place_review['review'] = review['text'] if 'author_url' in review and review['author_url']: place_review['author_id'] = urlparse.urlparse(review['author_url']).path[1:] if 'author_name' in review and review['author_name']: place_review['author_name'] = review['author_name'] if 'rating' in review and review['rating']: place_review['rating'] = review['rating'] if 'time' in review and review['time']: place_review['reviewed_on'] = datetime.datetime.fromtimestamp(review['time']) if not all(place_review.values()): continue place_review['aspects'] = None if 'aspects' in review and review['aspects']: place_review['aspects'] = json.dumps(review['aspects']) review_db = ( place_id, place_review['author_id'], place_review['author_name'], place_review['review'], place_review['aspects'], place_review['rating'], place_review['reviewed_on'], httpExists(gapi['GOOGLE_PROFILE_PHOTO'] % place_review['author_id']) ) cursor.execute( """INSERT INTO places_reviews (place_id, author_id, author_name, review, aspects, rating, reviewed_on, has_photo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""", review_db ) if 'result' in details and details['result'] and 'events' in details['result'] and details['result']['events']: for event in details['result']['events']: place_event = { 'event_id': None, 'start_time': None, 'summary': None, 'url': None } if 'event_id' in event and event['event_id']: place_event['event_id'] = event['event_id'] if 'start_time' in event and event['start_time']: place_event['start_time'] = event['start_time'] if 'summary' in event and event['summary']: place_event['summary'] = event['summary'] if 'url' in event and event['url']: place_event['url'] = event['url'] if not all(place_event.values()): continue event_db = ( place_id, place_event['event_id'], place_event['start_time'], place_event['summary'], place_event['url'] ) cursor.execute( """INSERT INTO places_events (place_id, event_id, start_time, summary, url) VALUES (%s, %s, %s, %s, %s)""", event_db ) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(details))
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 get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() # Get task specifics city_id = self.request.get('city_id') hotel_id = self.request.get('hotel_id') plan_id = self.request.get('plan_id') expedia_hotel_id = self.request.get('expedia_hotel_id') # 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, 'hotelId': expedia_hotel_id, '_type': 'json' } params = urllib.urlencode(params) photos = json.loads(urllib2.urlopen(expedia['EXPEDIA_PHOTOS_SEARCH'] % params, timeout=20).read()) # Dear programmer, please remember to replace _s with _y and _z if 'HotelRoomImageResponse' in photos and photos['HotelRoomImageResponse']: photos = photos['HotelRoomImageResponse'] if 'RoomImages' in photos and photos['RoomImages']: photos = photos['RoomImages'] if 'RoomImage' in photos and photos['RoomImage']: photos = photos['RoomImage'] if not photos: # Sync completed Task s.update_count(plan_id) return if type(photos) is dict: photos = [photos] for photo in photos: if 'url' in photo and photo['url']: if not httpExists(photo['url']): continue if not httpExists(photo['url'].replace('_s', '_y')): continue photo_db = ( city_id, hotel_id, expedia_hotel_id, photo['url'], httpExists(photo['url'].replace('_s', '_z')), created ) query = ("""INSERT INTO hotels_photos (city_id, hotel_id, expedia_hotel_id, url, is_large, created) VALUES (%s, %s, %s, %s, %s, %s)""") cursor.execute(query, photo_db) # Save 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(photos))
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): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() # Every entity is created NOW created = str(datetime.datetime.now()) city = self.request.get('city') city_id = self.request.get('city_id') country = self.request.get('country') plan_id = self.request.get('plan_id') latitude = self.request.get('latitude') longitude = self.request.get('longitude') categories = self.request.get('categories') categories = categories.replace('|', ',') 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') params = { 'app_key': eventbrite['EVENTBRITE_API_KEY'], 'city': city, 'country': country, 'max': 16, 'category': categories, 'date': '%s %s' % (future_day.strftime('%Y-%m-%d'), (future_day + datetime.timedelta(days=30)).strftime('%Y-%m-%d')), 'display': 'repeat_schedule' } params = urllib.urlencode(params) events = json.loads(urllib.urlopen(eventbrite['EVENTBRITE_EVENT_SEARCH'] % params).read()) # Let the parsing begin if 'events' in events and events['events']: for item in events['events']: new_event = { 'eventbrite_id': None, 'title': None, 'url': None, 'start_date': None, 'end_date': None, 'venue_latitude': None, 'venue_longitude': None, 'organizer_name': None } if 'event' not in item: continue event = item['event'] if 'id' in event and event['id']: new_event['eventbrite_id'] = event['id'] if 'title' in event and event['title']: new_event['title'] = event['title'] if 'url' in event and event['url']: new_event['url'] = event['url'] if 'start_date' in event and event['start_date']: new_event['start_date'] = event['start_date'] if 'end_date' in event and event['end_date']: new_event['end_date'] = event['end_date'] if 'venue' in event and event['venue'] and 'latitude' in event['venue'] and event['venue']['latitude']: new_event['venue_latitude'] = event['venue']['latitude'] if 'venue' in event and event['venue'] and 'longitude' in event['venue'] and event['venue']['longitude']: new_event['venue_longitude'] = event['venue']['longitude'] if 'organizer' in event and event['organizer'] and 'name' in event['organizer'] and event['organizer']['name']: new_event['organizer_name'] = event['organizer']['name'] # If minimum requirements are not met we move on if not all(new_event.values()): continue new_event['logo'] = None if 'logo' in event and event['logo']: new_event['logo'] = event['logo'] new_event['organizer_url'] = None if 'organizer' in event and event['organizer'] and 'url' in event['organizer'] and event['organizer']['url']: new_event['organizer_url'] = event['organizer']['url'] new_event['venue_name'] = None if 'venue' in event and event['venue'] and 'name' in event['venue'] and event['venue']['name']: new_event['venue_name'] = event['venue']['name'] new_event['venue_address'] = None if 'venue' in event and event['venue'] and 'address' in event['venue'] and event['venue']['address']: new_event['venue_address'] = event['venue']['address'] new_event['venue_address_2'] = None if 'venue' in event and event['venue'] and 'address_2' in event['venue'] and event['venue']['address_2']: new_event['venue_address_2'] = event['venue']['address_2'] new_event['repeats'] = 0 if 'repeats' in event and event['repeats'] == 'yes': new_event['repeats'] = 1 new_event['repeat_schedule'] = None if 'repeat_schedule' in event and event['repeat_schedule']: new_event['repeat_schedule'] = event['repeat_schedule'] new_event['categories'] = None if 'category' in event and event['category']: new_event['categories'] = event['category'] # Insert the new event into our database event_db = ( city_id, new_event['eventbrite_id'], new_event['title'], new_event['url'], new_event['logo'], new_event['categories'], new_event['start_date'], new_event['end_date'], new_event['venue_latitude'], new_event['venue_longitude'], new_event['venue_address'], new_event['venue_address_2'], new_event['venue_name'], new_event['organizer_name'], new_event['organizer_url'], new_event['repeats'], json.dumps(new_event['repeat_schedule']), future_day, created ) query = ("""INSERT IGNORE INTO cities_events (city_id, eventbrite_id, title, url, logo, categories, start_date, end_date, venue_latitude, venue_longitude, venue_address, venue_address_2, venue_name, organizer_name, organizer_url, repeats, repeat_schedule, filter_date, created) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""") cursor.execute(query, event_db) # 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(events))
def get(self): # MySQL Cursor db_conn = self.connection cursor = db_conn.cursor(MySQLdb.cursors.DictCursor) # Sync Directive s = Sync() # Created Time created = datetime.datetime.now() # Get task specific data plan_id = self.request.get('plan_id') latitude = self.request.get('latitude') longitude = self.request.get('longitude') future_day = self.request.get('future_day') params = [latitude, longitude, future_day] params = ','.join(params) print forecast['FORECAST_FUTURE'] % (forecast['FORECAST_API_KEY'], params) weather = json.loads(urllib.urlopen(forecast['FORECAST_FUTURE'] % (forecast['FORECAST_API_KEY'], params)).read()) new_forecast = { 'offset': None, 'hourly_data': None } if 'offset' in weather and weather['offset'] is not None: new_forecast['offset'] = weather['offset'] if 'hourly' in weather and weather['hourly'] and 'data' in weather['hourly'] and weather['hourly']['data']: new_forecast['hourly_data'] = json.dumps(weather['hourly']['data']) if not new_forecast['hourly_data']: # Sync completed Task s.update_count(plan_id) return new_forecast['icon'] = None if 'hourly' in weather and weather['hourly'] and 'icon' in weather['hourly'] and weather['hourly']['icon']: new_forecast['icon'] = weather['hourly']['icon'] new_forecast['summary'] = None if 'hourly' in weather and weather['hourly'] and 'summary' in weather['hourly'] and weather['hourly']['summary']: new_forecast['summary'] = weather['hourly']['summary'] # Save everything in our DB forecast_db = ( plan_id, future_day, new_forecast['summary'], int(new_forecast['offset']), new_forecast['icon'], new_forecast['hourly_data'], created ) query = ("""INSERT IGNORE INTO plans_forecast (plan_id, future_day, summary, offset, icon, hourly_data, created) VALUES (%s, %s, %s, %s, %s, %s, %s)""") cursor.execute(query, forecast_db) db_conn.commit() # Sync completed Task s.update_count(plan_id) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(weather))