def add_saved_flight(self, flight_id, user_id, fares):
        saved_flights = arangodb.collection('saved_flights')
        flights_stats = arangodb.collection('flights_stats')

        if not saved_flights.has(str(flight_id)):
            self.init_flight(flight_id, user_id)
        else:
            saved_flight = saved_flights.get(str(flight_id))
            users = saved_flight['users']

            # надо ли?
            if user_id not in users:
                users.append(user_id)
                saved_flight['users'] = users
                saved_flights.update(saved_flight)

        if not flights_stats.has(str(flight_id)):
            stats = {
                "_key": str(flight_id),
                'prices': [{
                    'date': str(datetime.date.today()),
                    'fares': fares
                }],
            }
            flights_stats.insert(stats)
Esempio n. 2
0
 def get_user_history(self, user_id):
     user_activity_collection = arangodb.collection('user_activity')
     history_collection = arangodb.collection('history')
     user_document = user_activity_collection.get(str(user_id))
     search_ids = user_document['searches']
     list_of_searches = []
     for id in search_ids:
         search = history_collection.get(id)
         list_of_searches.append(search)
     return list_of_searches
    def increase_counter(self, airport, date):
        destinations_stats_collection = arangodb.collection('destinations_stats')

        # TODO: check in psql if there is such airport
        airports = Airport.query.order_by("country").all()
        if airport in airports:
            country_key = str(airport.country).replace(' ', '_')
            print(country_key)

            if country_key in destinations_stats_collection:
                country = destinations_stats_collection.get(country_key)
                if airport.city in country:
                    city = country[str(airport.city)]
                    date_parts = date.split('-')
                    year = "year_" + date_parts[0]
                    month = int(date_parts[1])
                    if year in city:
                        year_object = city[year]

                        months = year_object['counters']
                        months[month - 1] += 1
                        year_object['counters'] = months
                        city[year] = year_object
                        country[str(airport.city)] = city
                        destinations_stats_collection.update(country)
Esempio n. 4
0
    def send_update(flight_id, fares):
        saved_flights = arangodb.collection('saved_flights')

        users = []
        for saved_flight in saved_flights:
            if saved_flight['flight_id'] == flight_id:
                users = saved_flight['users']

        recipients = []
        for user_id in users:
            user = User.query.get(user_id)
            recipients.append(user.email)

        flight = Flight.query.get(flight_id)
        msg_subj = _("Price for your flight has changed!")
        with app.app_context():
            msg = Message(msg_subj, recipients=recipients)
            msg.html = "<p>" + flight.departure + "---->" + flight.arrival + "</p>" \
                    "<p>" + flight.airline + "</p>" \
                    "<p>" + str(flight.departureTime) + " - " + str(flight.arrivalTime) + "</p>"

            msg.html += _("<p> Current fares:")
            for fare in fares:
                msg.html += str(fare['amount']) + " " + str(
                    fare['currencyCode']) + "</p><p>"
            msg.html += "</p>"

            mail.send(msg)
    def create_stats_fields(self):
        airlines_data_collection = arangodb.collection('airlines_data')
        curr_year = datetime.datetime.now().year
        next_year = curr_year + 1
        curr_year = 'year_' + str(curr_year)
        next_year = 'year_' + str(next_year)

        for airline in list_of_airlines:
            airline_data = airlines_data_collection.get(airline)

            if 'stats' not in airline_data:
                airline_data['stats'] = {}
                stats = airline_data['stats']
                stats[curr_year] = {"counters": [0] * 12}
                stats[next_year] = {"counters": [0] * 12}
            else:
                stats = airline_data['stats']
                if curr_year not in stats:
                    stats[curr_year] = {"counters": [0] * 12}

                if next_year not in stats:
                    stats[next_year] = {"counters": [0] * 12}

            airline_data['stats'] = stats
            airlines_data_collection.update(airline_data)
Esempio n. 6
0
 def get_saved_flights(self, user_id):
     user_activity_collection = arangodb.collection('user_activity')
     user_document = user_activity_collection.get(str(user_id))
     flights_ids = user_document['saved_flights']
     list_of_flights = []
     for id in flights_ids:
         flight = Flight.query.filter_by(id=id).first()
         list_of_flights.append(flight)
     return list_of_flights
Esempio n. 7
0
 def remove_history(self, key, user_id):
     user_activity_collection = arangodb.collection('user_activity')
     # history_flights = arangodb.collection('history')
     user_document = user_activity_collection.get(str(user_id))
     search_ids = user_document['searches']
     for id in search_ids:
         if id == key:
             search_ids.remove(id)
             break
     user_document['searches'] = search_ids
     user_activity_collection.update(user_document)
    def init_flight(self, flight_id, user_id):

        saved_flights = arangodb.collection('saved_flights')
        # if not saved_flights.has(str(flight_id)):
        # first user who saved the flight
        flight = {
            "_key": str(flight_id),
            'flight_id': flight_id,
            "users": [user_id]
        }
        saved_flights.insert(flight)
Esempio n. 9
0
    def run(self):
        print('Flights Updater started')
        while self.isWorking:
            saved_flights = arangodb.collection('saved_flights')
            flights = []
            print(saved_flights)

            for saved_flight in saved_flights:
                flight = Flight.query.get(saved_flight["flight_id"])
                flights.append(flight)

            print(flights)

            for flight in flights:

                # with app.test_request_context():
                search_data = SearchRequest(flight.departure, flight.arrival,
                                            str(flight.departureTime.date()),
                                            1, 0, 0, 0, 0)

                airlines = []
                if flight.airline == 'Ryanair':
                    airlines.append('ryanair')
                else:
                    if flight.airline == 'Wizzair':
                        airlines.append('wizzair')
                    # if flight.airline == 'UIA':
                    #     airlines.append = 'uia'

                search_results = search_handler.handle(search_data, airlines)
                if len(search_results) > 0:
                    result = search_results[0]
                    current_fares = result.get("fares")

                    in_db = FlightsStatsManager.get_current_stats_for(
                        flight.id)

                    print(in_db['fares'])
                    print(current_fares)
                    if self.needs_update(in_db['fares'], current_fares):
                        FlightsStatsManager.update_stats(
                            flight.id, {
                                'date': str(datetime.date.today()),
                                'fares': current_fares
                            })
                        print("Update Found!")

                        MailSender.send_update(flight_id=flight.id,
                                               fares=current_fares)

            time.sleep(60 * 60)
Esempio n. 10
0
    def insert_search(self, key, user_id):
        user_activity_collection = arangodb.collection('user_activity')
        user_document = user_activity_collection.get(str(user_id))
        list_of_searches = user_document['searches']
        already_in_history = False

        # check if already in user's history
        for user_search in list_of_searches:
            if user_search == key:
                already_in_history = True
                break
        if not already_in_history:
            list_of_searches.append(key)
            user_document['searches'] = list_of_searches
            user_activity_collection.update(user_document)
Esempio n. 11
0
    def insert_flight(self, flight_id, user_id, fares):
        user_activity_collection = arangodb.collection('user_activity')
        activity = user_activity_collection.get(str(user_id))
        list_of_flights = activity['saved_flights']

        already_in_flights = False
        for flight in list_of_flights:
            if flight == flight_id:
                already_in_flights = True
                break
        if not already_in_flights:
            list_of_flights.append(flight_id)
            activity['saved_flights'] = list_of_flights
            user_activity_collection.update(activity)
            savedFlightsManager = SavedFlightsManager()
            savedFlightsManager.add_saved_flight(flight_id, user_id, fares)
    def increase_count(self, airline, date):
        airlines = arangodb.collection('airlines_data')
        if airlines.has(airline.lower()):
            airline_data = airlines.get(airline.lower())
            date_parts = date.split('-')
            if len(date_parts) == 3:
                year = "year_" + date_parts[0]
                month = int(date_parts[1])
                stats = airline_data['stats']
                if not year in stats:
                    stats[year] = {"counters": [0] * 12}
                    airline_data['stats'] = stats
                    airlines.update(airline_data)

                year_object = (airline_data['stats'])[year]
                months = year_object['counters']
                months[month - 1] = months[month - 1] + 1
                year_object['counters'] = months
                airlines.update(airline_data)
    def create_stats_fields(self):
        destinations_stats_collection = arangodb.collection('destinations_stats')
        airports = Airport.query.order_by("country").all()

        curr_year = datetime.datetime.now().year
        next_year = curr_year + 1
        curr_year = 'year_' + str(curr_year)
        next_year = 'year_' + str(next_year)

        counters = {"counters": [0] * 12}

        city = {curr_year: counters, next_year: counters}

        for airport in airports:
            country_key = str(airport.country).replace(' ', '_')
            if not destinations_stats_collection.has(country_key):

                country = {'_key': country_key, str(airport.city): city}
                destinations_stats_collection.insert(country)
            else:
                country = destinations_stats_collection.get(country_key)
                if str(airport.city) not in country:
                    country[str(airport.city)] = city
                    destinations_stats_collection.update(country)
Esempio n. 14
0
 def init_user(self, user_id):
     user_activity_collection = arangodb.collection('user_activity')
     activity = {'_key': str(user_id), 'saved_flights': [], 'searches': []}
     user_activity_collection.insert(activity)
Esempio n. 15
0
 def insert_history(self, key, search):
     history_collection = arangodb.collection('history')
     search_found = history_collection.get(key)
     if search_found is None:
         history_collection.insert(search)
Esempio n. 16
0
 def get_history(self, key):
     history_collection = arangodb.collection('history')
     search_found = history_collection.get(key)
     return search_found