예제 #1
0
        public async Task <IActionResult> ImportPlaylist([FromServices] YoutubeClientService youtubeService, [FromServices] ChannelService channelService,
                                                         [FromBody] ImportLibraryForm data)
        {
            using (var ctx = DataFactory.GetDataContext())
            {
                var channel = await ctx.Channel.FirstOrDefaultAsync(c => c.OwnerId == User.GetUserId() && c.Id == data.channelId && !c.IsDisabled);

                if (channel == null)
                {
                    return(NotFound());
                }
            }

            string playlistId = null;

            try
            {
                Uri playlistUri = new Uri(data.playlistUrl);
                playlistId = HttpUtility.ParseQueryString(playlistUri.Query).Get("list");
            }
            catch (Exception e)
            {
                _log.Information(e, "Invalid playlist url [{0}]", data.playlistUrl);
                return(BadRequest("Invalid playlist"));
            }

            var videos = await youtubeService.ImportPlaylist(playlistId);

            var videoIds = videos.Select(v => v.Id).ToList();

            var libraryItems = await channelService.AddVideosToLibrary(data.channelId, videoIds);

            if (data.addToPlaylist)
            {
                await channelService.AddVideosToPlaylist(data.channelId, libraryItems);
            }

            return(Ok(new { imported = libraryItems.Count }));
        }