Example #1
0
        public async Task <IActionResult> Get(Guid channelId, [FromServices] YouTubeCacheService youTubeCacheService)
        {
            List <VideoInfo> library;

            using (var ctx = DataFactory.GetDataContext())
            {
                var channel = await ctx.Channel.FirstOrDefaultAsync(c => c.OwnerId == User.GetUserId() && c.Id == channelId && !c.IsDisabled);

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

                var channelLibrary = await ctx.ChannelLibrary.FirstOrDefaultAsync(l => l.ChannelId == channelId);

                library = JsonConvert.DeserializeObject <List <VideoInfo> >(channelLibrary.Library);
            }

            var videosInfo = await youTubeCacheService.GetVideos(library.Select(l => l.Id).ToList());

            var result = library.Select(v =>
            {
                var item = videosInfo.FirstOrDefault(i => i.Id == v.Id);
                return(new
                {
                    v.Id,
                    Title = (item?.Snippet?.Title != null) ? item?.Snippet?.Title : "Unknown Media"
                });
            }).ToList();

            return(Ok(result));
        }
        public async Task <IActionResult> Get(Guid channelId, [FromServices] YouTubeCacheService youTubeCacheService)
        {
            List <VideoInfo> playlist;
            string           playlistHash;

            using (var ctx = DataFactory.GetDataContext())
            {
                var channel = await ctx.Channel.FirstOrDefaultAsync(c => c.OwnerId == User.GetUserId() && c.Id == channelId && !c.IsDisabled);

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

                var channelPlaylist = await ctx.ChannelPlaylist.FirstOrDefaultAsync(l => l.ChannelId == channelId);

                playlist     = channelPlaylist.Playlist.FromJson <List <VideoInfo> >();
                playlistHash = channelPlaylist.Playlist.GetMD5Hash();
            }

            var videosInfo = await youTubeCacheService.GetVideos(playlist.Select(l => l.Id).ToList());

            var result = playlist.Select(v =>
            {
                var item = videosInfo.FirstOrDefault(i => i.Id == v.Id);
                return(new
                {
                    v.Id,
                    Title = (item?.Snippet?.Title != null) ? item?.Snippet?.Title : "Unknown Media"
                });
            }).ToList();

            var response = new PlaylistHashedData()
            {
                hash = playlistHash,
                data = result
            };

            return(Ok(response));
        }
        public async Task <IActionResult> Index([FromServices] YouTubeCacheService youTubeCacheService)
        {
            var sql = @"WITH mediaDates AS(
SELECT p.ChannelId, MAX(p.StartDateUTC) AS lastDate FROM play_history p
INNER JOIN channel c ON p.ChannelId = c.Id
WHERE c.Status = 'RUNNING'
GROUP BY p.ChannelId
LIMIT 50
)

SELECT c.Id AS ChannelId, c.Title AS ChannelTitle, p.MediaId FROM mediaDates i
INNER JOIN channel c ON c.Id = i.ChannelId
INNER JOIN play_history p ON i.lastDate = p.StartDateUTC AND p.ChannelId = c.Id";

            List <DbActiveChannel> channels;

            using (var conn = DataFactory.OpenConnection())
            {
                channels = (await conn.QueryAsync <DbActiveChannel>(sql)).ToList();
            }

            var ids        = channels.Select(c => c.MediaId).ToList();
            var videosInfo = await youTubeCacheService.GetVideos(ids);

            var data = channels.Select(c =>
            {
                var video = videosInfo.FirstOrDefault(v => v.Id == c.MediaId);
                return(new
                {
                    c.ChannelId,
                    c.ChannelTitle,
                    video?.Snippet?.Title,
                    c.MediaId
                });
            });

            return(Ok(data));
        }