def recommendations(): data = request.args try: access_key = str(dict(data)['access_key'][0]) except: return accessKeyRequired() agent = Agent.query.filter_by(access_key=access_key).first() if agent is None: return accessKeyRequired(not_in_db=True) try: agent_user_id = str(data['user_id']) except: return badRequest() user = User.query.filter_by(agent_user_id=agent_user_id, agent_id=agent.id).first() if user is None: return badRequest(user=True) user_movies = get_user_movies(user.id) if not user_movies: return 'User has no movies' #, Response(403) jsonify([]) if agent.agent_name == 'vkino': user_tmdb_movies = [] for movie in user_movies: tmdbid = Vkino.query.filter_by(vkino_id=movie).first().tmdb_fk_id if tmdbid is None: continue else: user_tmdb_movies.append(tmdbid) if not user_tmdb_movies: return jsonify(recommendations=[]) min_num_of_recs = 3 rs = Recommender(user_tmdb_movies, agent.agent_name + '_movies', agent.agent_name + '_id', user.id) rs.tmdb_input_info() rs.form_characteristics() result = rs.get_recommendations() if not result: return jsonify(recommendations=result) agent_premiere_idx = [movie['pk_id'] for movie in result[:min_num_of_recs]] if type(agent_premiere_idx) is list: r = Recommendation(user.id, agent.id, datetime.now()) db.session.add(r) db.session.commit() for movie in agent_premiere_idx: mr = MovieRecommendation(r.id, movie, datetime.now()) db.session.add(mr) db.session.commit() return jsonify(recommendations=agent_premiere_idx, recommendation_id=r.id)
def recommendations(): print('2') # print(request.args) data = request.args try: access_key = str(dict(data)['access_key'][0]) except: return accessKeyRequired() agent = Agent.query.filter_by(access_key=access_key).first() if agent is None: return accessKeyRequired(not_in_db=True) try: agent_user_id = str(data['user_id']) except: return badRequest() user = User.query.filter_by(agent_user_id=agent_user_id, agent_id=agent.id).first() if user is None: return badRequest(user=True) user_movies = get_user_movies(user.id) logger.info(user_movies) print(user_movies) if not user_movies: return 'User has no movies' #, Response(403) jsonify([]) if not Recommendation.query.filter_by(user_id=user.id).all(): print('No recommendations before') else: print('Some movies were recommender before') prev_rec_dates = [] for rec in Recommendation.query.filter_by(user_id=user.id).all(): prev_rec_dates.append( (datetime.now() - rec.created_at).total_seconds() / 3600) last_rec_date = min(prev_rec_dates) print(last_rec_date) if last_rec_date <= 24: print('Latest recommendation was today. Return empty list') return jsonify(recommendations=[]) if agent.agent_name == 'vkino': user_tmdb_movies = [] for movie in user_movies: tmdbid = Vkino.query.filter_by(vkino_id=movie).first().tmdb_fk_id if tmdbid is None: continue else: user_tmdb_movies.append(tmdbid) if not user_tmdb_movies: return jsonify(recommendations=[]) print(user_tmdb_movies) min_num_of_recs = 3 rs = Recommender(user_tmdb_movies, agent.agent_name + '_movies', agent.agent_name + '_id', user.id) rs.tmdb_input_info() rs.form_characteristics() result = rs.get_recommendations() #print(result[0]) if not result: return jsonify(recommendations=result) agent_premiere_idx = [movie['pk_id'] for movie in result[:min_num_of_recs]] print(agent_premiere_idx) if type(agent_premiere_idx) is list: r = Recommendation(user.id, agent.id, datetime.now()) db.session.add(r) db.session.commit() for movie in agent_premiere_idx: mr = MovieRecommendation(r.id, movie, datetime.now()) db.session.add(mr) db.session.commit() return jsonify(recommendations=agent_premiere_idx, recommendation_id=r.id)