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));
        }
Example #2
0
        public async Task <IActionResult> GetLiveStatus(Guid channelId, [FromServices] YouTubeCacheService youTubeCacheService)
        {
            Channel           channel;
            ChannelLiveStatus status = new ChannelLiveStatus();
            PlayHistory       currentMedia;
            ChannelPlaylist   playlist;

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

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

                currentMedia = await ctx.PlayHistory.Where(p => p.ChannelId == channel.Id).OrderByDescending(p => p.StartDateUTC).FirstOrDefaultAsync();

                playlist = await ctx.ChannelPlaylist.FirstOrDefaultAsync(p => p.ChannelId == channel.Id);
            }

            status.channelStatus = channel.Status;

            string nextMediaId = null;

            if (!string.IsNullOrEmpty(playlist?.Playlist))
            {
                var list      = JsonConvert.DeserializeObject <List <VideoInfo> >(playlist.Playlist);
                var firstItem = list.FirstOrDefault();
                nextMediaId = firstItem?.Id;

                if (list != null)
                {
                    status.mediaCountInPlaylist = list.Count;
                }
            }

            status.currentMediaId       = currentMedia?.MediaId;
            status.startDateUTC         = currentMedia?.StartDateUTC;
            status.currentMediaDuration = currentMedia?.Duration;
            status.nextMediaId          = nextMediaId;

            if (status.currentMediaId != null)
            {
                var info = await youTubeCacheService.GetVideoInfo(status.currentMediaId);

                status.currentMediaTitle = info?.Snippet?.Title;
            }

            if (status.nextMediaId != null)
            {
                var info = await youTubeCacheService.GetVideoInfo(status.nextMediaId);

                status.nextMediaTitle = info?.Snippet?.Title;
            }

            return(Ok(status));
        }
Example #3
0
        public YoutubeClientService(YouTubeCacheService youtubeCacheService, IConfiguration config)
        {
            _youtubeCacheService = youtubeCacheService;
            var key = config["YouTubeApiKey"];

            _youTubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey                = key,
                ApplicationName       = this.GetType().ToString(),
                HttpClientInitializer = new TestClient()
            });
        }
        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> GetChannelInfo([FromQuery] Guid channelId, [FromServices] YouTubeCacheService youTubeCacheService)
        {
            using (var ctx = DataFactory.GetDataContext())
            {
                var channel = await ctx.Channel.FirstOrDefaultAsync(c => c.Id == channelId);

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

                string lastMediaTitle = null;

                try
                {
                    var lastMedia = await ctx.PlayHistory
                                    .Where(c => c.ChannelId == channelId)
                                    .OrderByDescending(c => c.StartDateUTC)
                                    .FirstOrDefaultAsync();

                    if (lastMedia != null)
                    {
                        var mediaInfo = await youTubeCacheService.GetVideoInfo(lastMedia.MediaId);

                        lastMediaTitle = mediaInfo?.Snippet?.Title;
                    }
                }
                catch (Exception e)
                {
                    _log.Information(e, "[{0}] Failed to load channel last media", channelId);
                }

                return(Ok(new
                {
                    title = channel.Title,
                    lastMediaTitle
                }));
            }
        }
        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));
        }