GetSimilarPlaylistsOrdered(Entities.Application.Playlist compared, List <Place> places, string spotifyToken)
        {
            var client = new HttpClient();


            OccurrenceSearcher occurrences = await FindPlaylistOccurences(compared, places, spotifyToken, client);

            var ponderedTags   = PonderOccurences(occurrences.Tags);
            var ponderedGenres = PonderOccurences(occurrences.Genres);

            var fmSucc  = double.TryParse(Configuration["FmPonderation"], out var fmCoeff);
            var spoSucc = double.TryParse(Configuration["SpoPonderation"], out var spoCoeff);
            // ordenar según getSimilitud según la ponderación que se quiera
            var result = places.Select(p =>
            {
                var similitude = GetSimilitude(ponderedTags[compared.Id], ponderedTags[p.MainPlaylist.Id])
                                 * (fmSucc ? fmCoeff : 0.5)
                                 + GetSimilitude(ponderedGenres[compared.Id], ponderedGenres[p.MainPlaylist.Id])
                                 * (spoSucc ? spoCoeff : 0.5);
                var res        = new ComparedPlace(p);
                res.Similitude = similitude;
                return(res);
            }).OrderByDescending(c => c.Similitude);

            return(result);
        }
Esempio n. 2
0
        public async Task <Entities.Application.Playlist> SetPlaylist(string place, Entities.Application.Playlist playlist, bool withSongs)
        {
            var converted = _mapper.ToDbEntity(playlist, place);
            var dbResult  = await _dao.UpdatePlaylist(converted);

            await _songService.OverrideSongs(withSongs?playlist.Songs : new List <Entities.Application.Song>(), place, "mainPlaylist");

            return(_mapper.ToApplicationEntity(dbResult));
        }
Esempio n. 3
0
        public DataAccess.Firestore.Model.Playlist ToDbEntity(Entities.Application.Playlist entity, string placeId)
        {
            DocumentReference reference = _db.Collection(PARENT_REF).Document(placeId).Collection(COLLECTION_REF).Document(MAIN_REF);

            return(new DataAccess.Firestore.Model.Playlist
            {
                Reference = reference,
                Id = entity.Id,
                Name = entity.Name,
                SnapshotId = entity.SnapshotVersion,
                Url = entity.Url,
                ImageUrl = entity.ImageUrl,
                LastFmOcurrenceDictionary = entity.Tags,
                SpotifyOccurrenceDictionary = entity.Genres
            });
        }
Esempio n. 4
0
        public void SetUp()
        {
            string place      = "test";
            var    dbplaylist = new Playlist();
            var    playlist   = new Entities.Application.Playlist();

            dao             = new Mock <IPlaylistDao <Playlist> >();
            mapper          = new Mock <IPlaylistMapper <Playlist> >();
            song            = new Mock <ISongService>();
            playlistService = new PlaylistService(dao.Object, mapper.Object, song.Object, null);

            dao.Setup(a => a.GetPlaylist(place)).Returns(Task.FromResult(dbplaylist));
            dao.Setup(a => a.GetPlaylist("")).Returns(Task.FromResult <Playlist>(null));
            mapper.Setup(a => a.ToApplicationEntity(dbplaylist)).Returns(playlist);
            song.Setup(a => a.GetSongsFromPlaylist(place, "mainPlaylist", 1, int.MaxValue)).Returns(() => Task.FromResult(new List <Entities.Application.Song> {
                new Entities.Application.Song()
            }));
        }
        private async Task <OccurrenceSearcher> FindPlaylistOccurences(Entities.Application.Playlist compared,
                                                                       List <Place> places, string spotifyToken, HttpClient client)
        {
            var occurrences = new OccurrenceSearcher();

            await places.ToAsyncEnumerable().ForEachAwaitAsync(async(Place place) =>
            {
                var header = await _spotify.GetPlaylistHeader(client, spotifyToken, place.MainPlaylist.Id);
                if (header != null)
                {
                    if (header.Snapshot_id == place.MainPlaylist.SnapshotVersion &&
                        place.MainPlaylist.Tags != null &&
                        place.MainPlaylist.Tags.Any() &&
                        place.MainPlaylist.Genres != null &&
                        place.MainPlaylist.Genres.Any())
                    {
                        occurrences.UpdateTags(place.MainPlaylist.Tags, place.MainPlaylist.Id);
                        occurrences.UpdateGenres(place.MainPlaylist.Genres, place.MainPlaylist.Id);
                    }
                    else
                    {
                        var playlistSongs =
                            (await _spotify.GetSongsFromPlaylist(client, spotifyToken, place.MainPlaylist.Id))
                            .Items.Select(s => new Entities.Application.Song(s)).ToList();

                        await GetTagsAndGenresFromSongs(spotifyToken, place.MainPlaylist.Id, playlistSongs, occurrences);

                        place.MainPlaylist.SnapshotVersion = header.Snapshot_id;
                        place.MainPlaylist.Songs           = playlistSongs;
                        place.MainPlaylist.Tags            = occurrences.GetTags(place.MainPlaylist.Id);
                        place.MainPlaylist.Genres          = occurrences.GetGenres(place.MainPlaylist.Id);
                    }
                }
            });

            await GetTagsAndGenresFromSongs(spotifyToken, compared.Id, compared.Songs, occurrences);

            compared.Tags   = occurrences.GetTags(compared.Id);
            compared.Genres = occurrences.GetGenres(compared.Id);

            return(occurrences);
        }
Esempio n. 6
0
        public async Task <Entities.Application.Playlist> CreatePlaylist(string place, Entities.Application.Playlist playlist)
        {
            var converted = _mapper.ToDbEntity(playlist, place);
            var dbResult  = await _dao.CreatePlaylist(converted);

            var result = _mapper.ToApplicationEntity(dbResult);

            if (playlist.Songs.Any())
            {
                result.Songs = await _songService.AddSongs(playlist.Songs, place, "mainPlaylist");
            }


            return(result);
        }
Esempio n. 7
0
 public DataAccess.Firestore.Model.Playlist ToDbEntity(Entities.Application.Playlist entity)
 {
     throw new NotImplementedException("Refer to the other overload");
 }
Esempio n. 8
0
        public async Task <IEnumerable <ComparedPlace> > GetRecommendedPlaces(int offset, int quantity, Geolocation location, double distance, Entities.Application.Playlist playlist, string spotifyToken)
        {
            var places = (await GetPlaces(1, int.MaxValue, location, distance, true)).Where(p => p.MainPlaylist != null).ToList();

            var compared = (await recommendationService.GetSimilarPlaylistsOrdered(playlist, places, spotifyToken)).Take(quantity).Skip((offset - 1) * quantity).ToList();

            await MassUpdatePlaylists(places.Where(p => p.MainPlaylist.Changed).ToList());

            compared.ForEach(c =>
            {
                if (double.IsNaN(c.Similitude))
                {
                    c.Similitude = 0;
                }
            });
            return(compared);
        }