public List<Track> Search(string q, long maxResults = 5)
        {
            var result = new List<Track>();

            var searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.Q = q;
            searchListRequest.MaxResults = 20;

            var queryResult = searchListRequest.Execute();

            foreach (var video in queryResult.Items.Where(v => v.Id.Kind == "youtube#video" && v.Snippet.Title.Contains(" - ")))
            {
                if (result.Count >= maxResults) continue;

                var track = new Track
                {
                    Title = video.Snippet.Title.Split(new[] { " - " }, StringSplitOptions.None)[1],
                    Artist = video.Snippet.Title.Split(new[] { " - " }, StringSplitOptions.None)[0],
                };

                Thumbnail[] thumbnails = { video.Snippet.Thumbnails.Maxres, video.Snippet.Thumbnails.High, video.Snippet.Thumbnails.Medium, video.Snippet.Thumbnails.Default__ };
                track.CoverArtUrl = thumbnails.First(t => t != null).Url;
                track.TrackSource = new TrackSource()
                {
                    SourceType = SourceType.YouTube,
                    Url = $"https://www.youtube.com/watch?v={video.Id.VideoId}",
                    TrackId = video.Id.VideoId
                };

                result.Add(track);
            }

            return result;
        }
        public void Initialize() {
            playlistManager = new PlaylistManager(new PlaylistRepository(new EFDbContext(ContextEnum.BeatBuddyTest)), new UserRepository(new EFDbContext(ContextEnum.BeatBuddyTest)));
            userManager = new UserManager(new UserRepository(new EFDbContext(ContextEnum.BeatBuddyTest)));
            user = userManager.CreateUser("*****@*****.**", "matthias", "test", "acidshards", "");
            playlist = playlistManager.CreatePlaylistForUser("testplaylist", "gekke playlist om te testen", "125", 5, true, "", user);

            trackProvider = new YouTubeTrackProvider();
            albumArtProvider = new BingAlbumArtProvider();

            playlistControllerWithAuthenticatedUser = MyWebApi.Controller<PlaylistController>()
               .WithResolvedDependencyFor<IPlaylistManager>(playlistManager)
               .WithResolvedDependencyFor<IUserManager>(userManager)
               .WithResolvedDependencyFor<ITrackProvider>(trackProvider)
               .WithResolvedDependencyFor<IAlbumArtProvider>(albumArtProvider)
               .WithAuthenticatedUser(
                u => u.WithIdentifier("NewId")
                      .WithUsername(user.Email)
                      .WithClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Email, user.Email))
                      .WithClaim(new System.Security.Claims.Claim("sub", user.Email))
               );
            Track metallicaTrack = new Track()
            {
                Artist = "Metallica",
                Title = "Master of Puppets (live)",
                CoverArtUrl = "",
                Duration = 800,
                TrackSource = new TrackSource
                {
                    SourceType = SourceType.YouTube,
                    Url = "https://www.youtube.com/watch?v=kV-2Q8QtCY4",
                    TrackId = "kV-2Q8QtCY4"
                }
            };
            Track nickelbackTrack = new Track()
            {
                Artist = "Nickelback",
                Title = "How You Remind Me",
                CoverArtUrl = "",
                Duration = 400,
                TrackSource = new TrackSource
                {
                    SourceType = SourceType.YouTube,
                    Url = "https://www.youtube.com/watch?v=1cQh1ccqu8M",
                    TrackId = "1cQh1ccqu8M"
                }
            };

            _addedMetallicaTrack = playlistManager.AddTrackToPlaylist(playlist.Id, metallicaTrack);
            _addedNickelbackTrack = playlistManager.AddTrackToPlaylist(playlist.Id, nickelbackTrack);
            
        }
        public void Initialize()
        {
           
            playlistManager = new PlaylistManager(new PlaylistRepository(new EFDbContext(ContextEnum.BeatBuddyTest)), new UserRepository(new EFDbContext(ContextEnum.BeatBuddyTest)));
            userManager = new UserManager(new UserRepository(new EFDbContext(ContextEnum.BeatBuddyTest)));
            organisationManager = new OrganisationManager(new OrganisationRepository(new EFDbContext(ContextEnum.BeatBuddyTest)));
            _userWithOrganisation = userManager.CreateUser("*****@*****.**", "matthias", "test", "acidshards", "");
            _userWithoutOrganisation = userManager.CreateUser("*****@*****.**", "heylen", "jos", "acidshards", "");

            playlist = playlistManager.CreatePlaylistForUser("testplaylist", "gekke playlist om te testen", "125", 5, true, "", _userWithOrganisation);
            _userControllerWithAuthenticatedUserWithOrganisation = MyWebApi.Controller<UserController>()
                .WithResolvedDependencyFor<PlaylistManager>(playlistManager)
                .WithResolvedDependencyFor<UserManager>(userManager)
                .WithResolvedDependencyFor<OrganisationManager>(organisationManager)
                .WithAuthenticatedUser(
                 u => u.WithIdentifier("NewId")
                       .WithUsername(_userWithOrganisation.Email)
                       .WithClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Email, _userWithOrganisation.Email))
                       .WithClaim(new System.Security.Claims.Claim("sub", _userWithOrganisation.Email))
                );

            _userControllerWithAuthenticatedUserWithoutOrganisation = MyWebApi.Controller<UserController>()
                .WithResolvedDependencyFor<PlaylistManager>(playlistManager)
                .WithResolvedDependencyFor<UserManager>(userManager)
                .WithResolvedDependencyFor<OrganisationManager>(organisationManager)
                .WithAuthenticatedUser(
                 u => u.WithIdentifier("NewId")
                       .WithUsername(_userWithOrganisation.Email)
                       .WithClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Email, _userWithoutOrganisation.Email))
                       .WithClaim(new System.Security.Claims.Claim("sub", _userWithoutOrganisation.Email))
                );

            Track track = new Track()
            {
                Artist = "Metallica",
                Title = "Master of Puppets (live)",
                CoverArtUrl = "",
                Duration = 800,
                TrackSource = new TrackSource
                {
                    SourceType = SourceType.YouTube,
                    Url = "https://www.youtube.com/watch?v=kV-2Q8QtCY4"
                }
            };
            Track addedtrack = playlistManager.AddTrackToPlaylist(playlist.Id, track);


            organisation = organisationManager.CreateOrganisation("gek organisatie test","",_userWithOrganisation);
        }
        public Track CreateTrack(long playlistId, Track track)
        {
            var playlist = ReadPlaylist(playlistId);
            if (playlist == null) return null;

            var playlistTrack = new PlaylistTrack {Track = track};
            if(playlist.PlaylistTracks == null) playlist.PlaylistTracks = new Collection<PlaylistTrack>();
            
            playlist.PlaylistTracks.Add(playlistTrack);
           
            context.SaveChanges();
            
            return playlistTrack.Track;
        }
Ejemplo n.º 5
0
 public Track AddTrackToPlaylist(long playlistId, Track track)
 {
     
     return repo.CreateTrack(playlistId, track);
 }
        private Track GetTrackWithFreshYoutubeUrl(Track originalTrack)
        {
            IEnumerable<Track> tracks = playlistManager.ReadTracks().Where(t => t.Artist == originalTrack.Artist && t.Title == originalTrack.Title);

            DateTime timeTrackRequested = new DateTime(1970, 1, 1, 0, 0, 0);
            Track trackWithLatestYoutubeUrl = null;
            string youTubeVideo = null;

            foreach (Track track in tracks)
            {
                if (track.Url != null)
                {
                    Match match = Regex.Match(track.Url, @"lmt(\=[^&]*)?(?=&|$)|^lmt(\=[^&]*)?(&|$)");
                    if (match.Success)
                    {
                        string timestamp;
                        timestamp = match.Groups[1].Value;
                        timestamp.Replace("lmt=", "");
                        DateTime datetime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(timestamp));
                        if (datetime > timeTrackRequested)
                        {
                            timeTrackRequested = datetime;
                            trackWithLatestYoutubeUrl = track;
                        }
                    }

                }

            }
            if (timeTrackRequested.AddHours(6) >= DateTime.UtcNow || trackWithLatestYoutubeUrl == null)
            {
                var youTube = YouTube.Default; // starting point for YouTube actions
                youTubeVideo = youTube.GetVideo(originalTrack.TrackSource.Url).Uri; // gets a Video object with info about the video
            }
            else {
                youTubeVideo = trackWithLatestYoutubeUrl.Url;
            }

            Track newTrack = new Track()
            {
                Id = originalTrack.Id,
                Artist = originalTrack.Artist,
                CoverArtUrl = originalTrack.CoverArtUrl,
                Duration = originalTrack.Duration,
                TrackSource = new TrackSource()
                {
                    Id = originalTrack.TrackSource.Id,
                    SourceType = originalTrack.TrackSource.SourceType,
                    TrackId = originalTrack.TrackSource.TrackId,
                    Url = originalTrack.TrackSource.Url
                },
                Title = originalTrack.Title,
                Url = youTubeVideo
            };

            return newTrack;
        }
        public Track LookupTrack(string TrackId)
        {
            var lookupRequest = youtubeService.Videos.List("contentDetails,snippet");
            lookupRequest.Id = TrackId;

            var queryResult = lookupRequest.Execute();
            foreach (var video in queryResult.Items)
            {
                var span = XmlConvert.ToTimeSpan(video.ContentDetails.Duration);
                try {
                    var track = new Track
                    {
                        Title = video.Snippet.Title.Split(new[] { " - " }, StringSplitOptions.None)[1],
                        Artist = video.Snippet.Title.Split(new[] { " - " }, StringSplitOptions.None)[0],
                        Duration = (int)span.TotalSeconds
                    };

                    Thumbnail[] thumbnails = { video.Snippet.Thumbnails.Maxres, video.Snippet.Thumbnails.High, video.Snippet.Thumbnails.Medium, video.Snippet.Thumbnails.Default__ };
                    track.CoverArtUrl = thumbnails.First(t => t != null).Url;
                    track.TrackSource = new TrackSource()
                    {
                        SourceType = SourceType.YouTube,
                        Url = $"https://www.youtube.com/watch?v={video.Id}",
                        TrackId = video.Id
                    };

                    return track;
                }
                catch 
                {
                    return null;
                }
            }

            return null;
        }