def checkOutLocation(request): location = None params = None if (request.method == 'GET'): params = request.GET elif request.method == 'POST': params = request.POST consumer_service = ConsumerService() user_id = params.get('user_id', None) api_token = params.get('api_key', None) consumer = consumer_service.isValidUser(user_id, api_token) if not consumer: raise InvalidUserError(user_id) location_service = LocationService() location_service.checkOut(consumer) response_data = {} response_data['status'] = 200 return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json'))
def checkInLocation(request): location = None params = None if (request.method == 'GET'): params = request.GET elif request.method == 'POST': params = request.POST consumer_service = ConsumerService() user_id = params.get('user_id', None) api_token = params.get('api_key', None) location_id = params.get('location_id', None) if not location_id: raise InvalidLocationError(location_id) consumer = consumer_service.isValidUser(user_id, api_token) if not consumer: raise InvalidUserError(user_id) try: location = Location.objects.get(pk=location_id) except (KeyError, Location.DoesNotExist): raise InvalidLocationError(location_id) if not location.is_active: raise InvalidLocationError(location_id) location_service = LocationService() location_service.checkIn(consumer, location) return __refreshPlaylistHelper__(consumer, location_id)
def addToPlaylist(request): params = None if (request.method == 'GET'): params = request.GET elif request.method == 'POST': params = request.POST location_service = LocationService() consumer_service = ConsumerService() user_id = params.get('user_id', None) api_token = params.get('api_key', None) location_id = params.get('location_id', None) music_track_id = params.get('music_track_id', None) if not location_id: raise InvalidLocationError(location_id) if not music_track_id: raise MusicTrackNotFoundError(music_track_id) consumer = consumer_service.isValidUser(user_id, api_token) if not consumer: raise InvalidUserError(user_id) if not location_service.isActive(location_id): response_data = {} location_service.checkOut(consumer) response_data['message'] = 'Location is not active' response_data['checked_out'] = True response_data['status'] = 200 return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json')) logger.info("Incoming request- add to playlist with parameters device_id " + str(user_id) + ", music_track_id " + str(music_track_id) + ", location_id " + str(location_id)) playlist_service = PlaylistService() try: playlist_service.addToPlaylist(consumer, location_id, music_track_id) logger.info("added music track " + str(music_track_id) + " to playlist for location " + str(location_id)) # Improve exception handling. except (KeyError, PlaylistNotFoundError, UnableToAddMusicError) as exception: error = utils.internalServerErrorResponse(exception.value) logger.error(exception.value) return HttpResponse(simplejson.dumps(error), mimetype='application/json') return __refreshPlaylistHelper__(consumer, location_id)
def voteUp(request): params = None if (request.method == 'GET'): params = request.GET elif request.method == 'POST': params = request.POST location_service = LocationService() consumer_service = ConsumerService() user_id = params.get('user_id', None) api_token = params.get('api_key', None) location_id = params.get('location_id', None) music_track_id = params.get('music_track_id', None) if not location_id: raise InvalidLocationError(location_id) if not music_track_id: raise MusicTrackNotFoundError(music_track_id) consumer = consumer_service.isValidUser(user_id, api_token) if not consumer: raise InvalidUserError(user_id) if not location_service.isActive(location_id): response_data = {} location_service.checkOut(consumer) response_data['message'] = 'Location is not active' response_data['checked_out'] = True response_data['status'] = 200 return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json')) logger.info("Incoming request- vote up with parameters user_id " + str(user_id) + ", location_id " + str(location_id) + ", music_track_id " + str(music_track_id)) voting_service = VotingService() try: voting_service.voteUp(consumer, location_id, music_track_id) logger.info("Updated playlist after vote up for music track " + str(music_track_id) + " at location " + str(location_id) + " from user " + str(user_id)) except UnableToVoteError as utv: error = utils.internalServerErrorResponse(utv.value) logger.error(utv.value) return HttpResponse(simplejson.dumps(error), mimetype='application/json') return __refreshPlaylistHelper__(consumer, location_id)
def start(self): location_service = LocationService() locations = location_service.get3_indonesia() for i, location in enumerate(locations): print("\n ", location['name']) hotels = self.hotel_service.get_hotels_by_locationid( location['location_id']) for j, hotel in enumerate(hotels): print("[", self.now, "]", j+1, ") ", hotel['location_id'], " - ", hotel['name']) self.calculate_sentiment_score(hotel) self.count += 1 j += 1
def refreshPlaylist(request): params = None if (request.method == 'GET'): params = request.GET elif request.method == 'POST': params = request.POST location_service = LocationService() consumer_service = ConsumerService() user_id = params.get('user_id', None) api_token = params.get('api_key', None) location_id = params.get('location_id', None) if not location_id: raise InvalidLocationError(location_id) consumer = consumer_service.isValidUser(user_id, api_token) if not consumer: raise InvalidUserError(user_id) if not location_service.isActive(location_id): response_data = {} location_service.checkOut(consumer) response_data['message'] = 'Location is not active' response_data['checked_out'] = True response_data['status'] = 200 return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json')) location_map = location_service.getLocationMap(consumer) if not location_map: logger.warn("user with user_id" + str(user_id) + " not checked in") if location_map and str(location_map.location.pk) != location_id: logger.warn("user with user_id" + str(user_id) + " not checked in to correct location: " + location_id) logger.info("Incoming request- refresh playlist with parameters device_id " + str(user_id) + ", location_id " + str(location_id)) # Use location id to fetch current playlist return __refreshPlaylistHelper__(consumer, location_id)
from controllers.ride_controller import RideBookingController from controllers.user_controller import UserController from services.user_service import UserService from services.ride_service import RideService from services.location_service import LocationService from services.vehicle_service import VehicleService userController = UserController(UserService()) rideController = RideBookingController(RideService()) location1 = LocationService().addLocation(5, 6) location2 = LocationService().addLocation(10, 9) location3 = LocationService().addLocation(11, 4) location4 = LocationService().addLocation(2, 6) vehicle1 = VehicleService().addVehicle('prime', 123, 'sedan', location3) vehicle2 = VehicleService().addVehicle('play', 124, 'sedan', location4) driver1 = userController.addDriver('d1', 'XXX', '999', vehicle1) driver2 = userController.addDriver('d2', 'YYY', '456', vehicle2) customer1 = userController.addCustomer('c1', 'AAA', '342', location1) rideController.bookRide('r1', 'c1', location1, location2, userController.userService)
def start(self): datenow = datetime.datetime.now() reviewtranslate_service = ReviewTranslatedService() hotel_service = HotelService() review_service = ReviewService() location_service = LocationService() # locations = location_service.get_all_locations() locations = location_service.get_locations_indonesia() for i, location in enumerate(locations): hotels = hotel_service.get_hotels_by_locationid( location['location_id']) for j, hotel in enumerate(hotels): reviews = review_service.get_review_by_hotel_locationid( hotel['location_id']) for r, review in enumerate(reviews): text_to_translate = review['text'] try: isexist_review = reviewtranslate_service.isexist_review_by_hotel_locid( hotel['location_id'], review['id']) # print(isexist_review) if isexist_review.count() == 0: print( "[", datetime.datetime.now(), "] Review (", review['id'], ") on table Translated Review is not exist. Saving Review ..." ) # gTranslator = Translator() language_translator = LanguageTranslator() text_translated = language_translator.translate_yandex( text_to_translate) data = { "hotel": hotel, "review": review, "location_id": location['location_id'], "hotel_id": hotel['location_id'], "review_id": review['id'], "text_to_translate": text_to_translate, "text_translated": text_translated, "created_at": datenow } if text_translated != None: reviewtranslate_service.create(data) else: print( str("-----> Err : Failed to translate review !" )) else: print( "[", datetime.datetime.now(), "] Review (", review['id'], ") on table Translated Review is already exist" ) except Exception as err: print(str("Err : ", err)) continue
from config import MetricHandler, MetricHandlerException # noqa from helpers.logging import get_logger # noqa from helpers.util import abspath, required_keys # noqa from services.location_service import LocationService # noqa sns = boto3.client("sns") SENTRY_DSN = os.environ["SENTRY_DSN"] SNS_RESULT_TOPIC_ARN = os.environ["SNS_RESULT_TOPIC_ARN"] sentry_sdk.init(dsn=SENTRY_DSN, integrations=[AwsLambdaIntegration() ]) # AWS Lambda integration logger = get_logger("worker-handler") fetch_service = LocationService() def lambda_handler(event, context): """ :param event: AWS Lambda uses this parameter to pass in event data to the handler. For details, see: https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html :param context: AWS Lambda uses this parameter to provide runtime information to your handler. For details, see: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html :return: """ logger.debug(f"Received event: {event['Records'][0]['Sns']['MessageId']}") message = event["Records"][0]["Sns"]["Message"] decoded = json.loads(message)
def start(self): datenow = datetime.now() sentimentreview_service = SentimentReviewService() sentiment_analyzer = SentimentAnalyzer() location_service = LocationService() hotel_service = HotelService() reviewtranslated_service = ReviewTranslatedService() review_service = ReviewService() language_translator = LanguageTranslator() locations = location_service.get_locations_indonesia() for i, location in enumerate(locations): hotels = hotel_service.get_hotels_by_locationid( location['location_id']) for j, hotel in enumerate(hotels): reviews_translated = reviewtranslated_service.get_review_by_hotel_locid( hotel['location_id']) sentimentreviews_on_hotel = sentimentreview_service.get_review_by_hotel_locid( hotel['location_id']) print(reviews_translated.count()) if reviews_translated.count() > 0: print("[", datetime.now(), "] Data translated") for r, review_translated in enumerate(reviews_translated): text_to_sentiment = review_translated[ 'text_translated'] try: isexist_review = any( x['review_id'] == review_translated['review_id'] for x in sentimentreviews_on_hotel) if not isexist_review: vader = sentiment_analyzer.get_vader( text_to_sentiment) wordnet = sentiment_analyzer.get_sentiwordnet( text_to_sentiment) subratings = self.map_subratings( review_translated['review']) subratings_normalized = self.normalize_subratings( subratings) date_publish = dateutil.parser.parse( review_translated['review'] ['published_date']) data = { "hotel": hotel, "review_translated": review_translated, "publish_date": review_translated['review'] ['published_date'], "month": date_publish.month, "year": date_publish.year, "location_id": location['location_id'], "hotel_id": hotel['location_id'], "review_id": review_translated['review_id'], "subratings": subratings, "subratings_normalized": subratings_normalized, "text_review": review['text'], "text_to_sentiment": text_to_sentiment, "vader_sentiment": vader, "wordnet_sentiment": wordnet, "wordnet_normalized": (wordnet - (-1)) / 2, "created_at": datenow } # pprint.pprint(data) sentimentreview_service.create(data) else: print( "[", datetime.now(), "] Review (", review_translated['review_id'], ") on table Sentiment Review is already exist" ) except Exception as err: print(str("[", datetime.now(), "] Err : ", err)) continue else: print("[", datetime.now(), "] No translated data") reviews = review_service.get_review_by_hotel_locationid( hotel['location_id']) for r, review in enumerate(reviews): try: isexist_review = any( x['review_id'] == review['id'] for x in sentimentreviews_on_hotel) if not isexist_review: subratings = self.map_subratings(review) subratings_normalized = self.normalize_subratings( subratings) date_publish = dateutil.parser.parse( review['published_date']) data = { "hotel": hotel, "review": review, "publish_date": review['published_date'], "month": date_publish.month, "year": date_publish.year, "location_id": location['location_id'], "hotel_id": hotel['location_id'], "review_id": review['id'], "subratings": subratings, "subratings_normalized": subratings_normalized, "text_to_sentiment": "", "vader_sentiment": { 'neg': 0, 'pos': 0, 'neu': 0, 'compound': 0.5 }, "wordnet_sentiment": 0, "wordnet_normalized": 0.5, "created_at": datenow } # pprint.pprint(data) sentimentreview_service.create(data) else: print( "[", datetime.now(), "] Review (", review['id'], ") on table Sentiment Review is already exist" ) except Exception as err: print(str("[", datetime.now(), "] Err : ", err)) continue