Пример #1
0
        public async Task<TrackInfoDto> GetTrackInfo(string artist, string trackName)
        {

            const string regExp = @"[^\w\d\s\/\\\.\,\[\]!@#$%^&*\(\);:?№']";

            int artistLength = (artist.Length > 50) ? 50 : artist.Length;
            int trackNameLength = (trackName.Length > 50) ? 50 : trackName.Length;

            artist = Regex.Replace(artist, regExp, "").Substring(0, artistLength);
            trackName = Regex.Replace(trackName, regExp, "").Substring(0, trackNameLength);

            var trackData = new TrackInfoDto();

            var lastFmApi = _musicServiceFactory.Resolve<LastfmTrackData>();
            var lastfmData = await lastFmApi.GetTrackInfo(artist, trackName);

            var deezerApi = _musicServiceFactory.Resolve<DeezerTrackData.TrackData>();
            var deezerData = await deezerApi.GetTrackInfo(artist, trackName);

            var chartLyricsApi = _musicServiceFactory.Resolve<string[]>();
            var lyricData = await chartLyricsApi.GetTrackInfo(artist, trackName);

            if (lyricData == null || !lyricData.Any() || (lyricData.Count() == 1 && String.IsNullOrEmpty(lyricData[0])))
            {
                _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi("Vkontakte");
                UserSocialNetwork socialNetworkData = null;
                using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
                {
                    socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
                    unitOfWork.Commit();
                }
                if (socialNetworkData != null)
                {
                    lyricData =
                        await
                            _socialNetworkApi.GetTrackLyricByArtistAndName(artist, trackName,
                                socialNetworkData.AccessToken, socialNetworkData.ThirdPartId);
                }
            }

            if (lastfmData.Track != null)
            {
                Mapper.Map(lastfmData, trackData);
            }
            if (deezerData != null)
            {
                Mapper.Map(deezerData, trackData);
            }
            if (lyricData != null)
            {
                Mapper.Map(lyricData, trackData);
            }

            return trackData;
        }
Пример #2
0
        public async Task<List<TrackData.Audio>> GetFriendsTracks(string provider, string friendId)
        {
            _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi(provider);
            UserSocialNetwork socialNetworkData;

            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                socialNetworkData = GetSocialNetworkData(unitOfWork, provider);

                unitOfWork.Commit();
            }

            if (socialNetworkData == null)
            {
                throw new EndUserException("Can't get social network info with provider name = " + provider);
            }

            return await _socialNetworkApi.GetTracks(friendId, socialNetworkData.AccessToken);
        }
Пример #3
0
        public async Task<List<VkFriendData.Friend>> GetFriendsInfo(string provider, int offset, int count)
        {
            _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi(provider);
            UserSocialNetwork socialNetworkData;

            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                socialNetworkData = GetSocialNetworkData(unitOfWork, provider);

                unitOfWork.Commit();
            }

            if (socialNetworkData == null)
            {
                throw new EndUserException("Can't get social network info with provider name = " + provider);
            }

            return await _socialNetworkApi.GetFriendsInfo(socialNetworkData.ThirdPartId, socialNetworkData.AccessToken, offset, count);
        }
Пример #4
0
        public async Task SetPlaylist(DataForTrackSaving tracksInfo, string provider, int index)
        {
            _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi(provider);

            bool tracksToEnd = index == -1;

            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                var playlistTrackRepository = unitOfWork.GetRepository<PlaylistTrack>();
                var trackRepository = unitOfWork.GetRepository<Track>();

                List<VkTrackResponse.Audio> trackDatas = null;
                //get checked tracks
                var socialNetworkData = GetSocialNetworkData(unitOfWork, provider);
                trackDatas = await _socialNetworkApi.GetSelectedTracks(tracksInfo, socialNetworkData.AccessToken);

                if (trackDatas.Any())
                {
                    var playlist = unitOfWork.PlaylistRepository.GetOne(pl => pl.Id == tracksInfo.PlaylistId);
                    playlist.PlaylistTracks =
                        playlistTrackRepository.Get(s => s.Identifier.Playlist.Id == tracksInfo.PlaylistId).ToList();

                    int i = 0;
                    //create Track objects
                    var artistRepo = unitOfWork.GetRepository<Artist>();

                    try
                    {
                        foreach (var trackData in trackDatas)
                        {
                            if (trackData.Artist.Length > 50)
                            {
                                trackData.Artist = trackData.Artist.Substring(0, 50);
                            }
                            if (trackData.Title.Length > 50)
                            {
                                trackData.Title = trackData.Title.Substring(0, 50);
                            }

                            var artist = new Artist
                            {
                                Name = trackData.Artist
                            };
                            var album = new Album
                            {
                                Name = "",
                                Artist = artist,
                            };

                            artist.Albums.Add(album);
                            artistRepo.AddItem(artist);

                            var track = new Track
                            {
                                Duration = trackData.Duration.ToString(CultureInfo.InvariantCulture),
                                //Lyrics =
                                //    await _socialNetworkApi.GetLyricsById(socialNetworkData.ThirdPartId, trackData.Id,
                                //            socialNetworkData.AccessToken),
                                Name = trackData.Title,
                                Album = album,
                                ThirdPartId = Convert.ToString(trackData.Id),
                                OwnerId = Convert.ToString(trackData.OwnerId),
                                Genre = trackData.GenreId.ToString(),
                            };

                            track.Playlists.Add(playlist);
                            album.Tracks.Add(track);
                            trackRepository.AddItem(track);

                            if (index == -1)
                            {
                                index = (playlist.Tracks.Any()) ? playlist.Tracks.Count() : 0;
                            }

                            var playlistTrack = new PlaylistTrack
                            {
                                Identifier = new PlaylistTracksIdentifier
                                {
                                    Playlist = playlist,
                                    Track = track
                                },
                                TrackPosition = index + i++
                            };

                            playlistTrackRepository.AddItem(playlistTrack);

                            playlist.PlaylistTracks.Add(playlistTrack);
                        }
                    }
                    catch (Exception exp)
                    {
                        var s = exp;
                    }


                    if (tracksToEnd == false)
                    {
                        playlist.PlaylistTracks.ForEach((item, n) =>
                        {
                            if (n < playlist.PlaylistTracks.Count - i && item.TrackPosition >= index)
                            {
                                item.TrackPosition += i;
                            }
                        });
                    }
                }

                unitOfWork.Commit();
            }
        }
Пример #5
0
        public async Task<List<TrackData.Audio>> SearchTracksInSn(List<TrackSearchInfo.SearchData> tracksDescription,
            string provider)
        {
            _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi(provider);
            //List<TrackData.Audio> searchedTracks = new List<TrackData.Audio>();

            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                if (provider != "Vkontakte")
                {
                    var id = unitOfWork.UserRepository.Get(user =>
                           user.ScreenName == "id268940215" && user.Name.FirstName == "Azimuth" &&
                           user.Name.LastName == "Azimuth").FirstOrDefault().Id;
                    var admin =
                        unitOfWork.UserRepository.GetFullUserData(id);
                    var accessToken =
                        admin.SocialNetworks.FirstOrDefault(sn => sn.SocialNetwork.Name == "Vkontakte").AccessToken;
                }
                var socialNetworkData = GetSocialNetworkData(unitOfWork, provider);
                var searchedTracks =
                    await _socialNetworkApi.SearchTracksForLyric(tracksDescription, socialNetworkData.AccessToken);
                unitOfWork.Commit();
                return searchedTracks;
            }
        }
Пример #6
0
        public async Task<List<TracksDto>> MakeSearch(string searchText, string criteria)
        {
            var trackDtos = new List<TracksDto>();
            var tracks = new List<Track>();
            var findInVk = new List<TrackData.Audio>();
            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                //var socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
                UserSocialNetwork socialNetworkData;
                _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi("Vkontakte");
                var trackRepository = unitOfWork.GetRepository<Track>();

                switch (criteria)
                {
                    case "genre":
                        tracks = trackRepository.Get(track => track.Genre.ToLower().Contains(searchText)).ToList();
                        break;
                    case "artist":
                        tracks =
                            trackRepository.Get(track => track.Album.Artist.Name.ToLower().Contains(searchText))
                                .ToList();
                        break;
                    case "track":
                        tracks = trackRepository.Get(track => track.Name.ToLower().Contains(searchText)).ToList();
                        break;
                    case "vkontakte":
                        if (AzimuthIdentity.Current.UserCredential.SocialNetworkName != "Vkontakte")
                        {
                            var id = unitOfWork.UserRepository.Get(user =>
                                user.ScreenName == "id268940215" && user.Name.FirstName == "Azimuth" &&
                                user.Name.LastName == "Azimuth").FirstOrDefault().Id;

                            var admin =
                                unitOfWork.UserRepository.GetFullUserData(id);
                            socialNetworkData =
                                admin.SocialNetworks.FirstOrDefault(sn => sn.SocialNetwork.Name == "Vkontakte");
                        }
                        else
                        {
                            socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
                        }
                        findInVk =
                            await _socialNetworkApi.SearchTracks(searchText, socialNetworkData.AccessToken, 0, 0, 20);
                        if (findInVk.Count > 0)
                        {
                            findInVk.ForEach(item =>
                            {
                                var dto = new TracksDto();
                                Mapper.Map(item, dto);
                                if (!trackDtos.Any(track => (track.Artist == dto.Artist) && (track.Name == dto.Name)))
                                {
                                    trackDtos.Add(dto);
                                }
                            });
                        }
                        break;
                    case "myvktracks":
                        socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
                        findInVk =
                            await
                                _socialNetworkApi.GetTracks(socialNetworkData.ThirdPartId, socialNetworkData.AccessToken);
                        findInVk =
                            findInVk.Where(
                                s =>
                                    s.Title.ToLower().Contains(searchText) ||
                                    s.GenreId.ToString().ToLower().Contains(searchText) ||
                                    s.Artist.ToLower().Contains(searchText)).ToList();

                        if (findInVk.Count > 0)
                        {
                            findInVk.ForEach(item =>
                            {
                                var dto = new TracksDto();
                                Mapper.Map(item, dto);
                                if (!trackDtos.Any(track => (track.Artist == dto.Artist) && (track.Name == dto.Name)))
                                {
                                    trackDtos.Add(dto);
                                }
                            });
                        }
                        break;
                }

                if (tracks.Count > 0)
                {
                    tracks.ForEach(item =>
                    {
                        var dto = new TracksDto();
                        Mapper.Map(item, dto);
                        if (!trackDtos.Any(track => (track.Artist == dto.Artist) && (track.Name == dto.Name)))
                        {
                            trackDtos.Add(dto);
                        }
                    });
                }

                unitOfWork.Commit();
            }

            return trackDtos;
        }
Пример #7
0
        public void AddTrack(long ownerId, long trackId)
        {
            using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
            {
                var socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
                _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi("Vkontakte");
                _socialNetworkApi.AddTrack(ownerId.ToString(), trackId.ToString(), socialNetworkData.AccessToken);

                unitOfWork.Commit();
            }
        }
Пример #8
0
 public async Task<List<TracksDto>> VkontakteSearch(string searchText, int offset)
 {
     var trackDtos = new List<TracksDto>();
     var findInVk = new List<TrackData.Audio>();
     _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi("Vkontakte");
     UserSocialNetwork socialNetworkData;
     using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
     {
         socialNetworkData = GetSocialNetworkData(unitOfWork, "Vkontakte");
         unitOfWork.Commit();
     }
     findInVk = await _socialNetworkApi.SearchTracks(searchText, socialNetworkData.AccessToken, 0, offset, 20);
     if (findInVk.Count > 0)
     {
         findInVk.ForEach(item =>
         {
             var dto = new TracksDto();
             Mapper.Map(item, dto);
             if (!trackDtos.Any(track => (track.Artist == dto.Artist) && (track.Name == dto.Name)))
             {
                 trackDtos.Add(dto);
             }
         });
     }
     return trackDtos;
 }
Пример #9
0
        public async Task<List<string>> GetTrackUrl(TrackSocialInfo tracks, string provider)
        {
            string accessToken;
            if (AzimuthIdentity.Current == null || AzimuthIdentity.Current.UserCredential.SocialNetworkName != "Vkontakte")
            {
                using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
                {
                    var id = unitOfWork.UserRepository.Get(user =>
                            user.ScreenName == "id268940215" && user.Name.FirstName == "Azimuth" &&
                            user.Name.LastName == "Azimuth").FirstOrDefault().Id;

                    var admin =
                        unitOfWork.UserRepository.GetFullUserData(id);
                    accessToken =
                        admin.SocialNetworks.FirstOrDefault(sn => sn.SocialNetwork.Name == "Vkontakte").AccessToken;
                    unitOfWork.Commit();
                }
            }
            else
            {
                using (var unitOfWork = _unitOfWorkFactory.NewUnitOfWork())
                {
                    accessToken = GetSocialNetworkData(unitOfWork, provider).AccessToken;
                    unitOfWork.Commit();
                }
            }

            _socialNetworkApi = SocialNetworkApiFactory.GetSocialNetworkApi(provider);

            return await _socialNetworkApi.GetTrackUrl(tracks, accessToken);

        }