public string GatherPlaylist()
        {
            groups = groupRepository
                     .GetAll()
                     .ToServiceModels()
                     .ToDictionary(x => x.Id, x => x);

            channelDefinitions = channelRepository
                                 .GetAll()
                                 .OrderBy(x => groups[x.GroupId].Priority)
                                 .ThenBy(x => x.Name)
                                 .ToServiceModels();

            playlistProviders = playlistProviderRepository
                                .GetAll()
                                .Where(x => x.IsEnabled)
                                .ToServiceModels();

            IList <Channel> providerChannels = playlistFetcher
                                               .FetchProviderPlaylists(playlistProviders)
                                               .SelectMany(x => x.Channels)
                                               .ToList();

            Playlist playlist = new Playlist();

            IEnumerable <Channel> channels = GetChannels(providerChannels);

            foreach (Channel channel in channels)
            {
                playlist.Channels.Add(channel);
            }

            return(playlistFileBuilder.BuildFile(playlist));
        }
Beispiel #2
0
        public string GatherPlaylist()
        {
            groups = groupRepository
                     .GetAll()
                     .Where(x => x.IsEnabled)
                     .ToServiceModels()
                     .ToDictionary(x => x.Id, x => x);

            channelDefinitions = channelRepository
                                 .GetAll()
                                 .OrderBy(x => groups[x.GroupId].Priority)
                                 .ThenBy(x => x.Name)
                                 .ToServiceModels();

            playlistProviders = playlistProviderRepository
                                .GetAll()
                                .Where(x => x.IsEnabled)
                                .ToServiceModels();

            IList <Channel> providerChannels = playlistFetcher
                                               .FetchProviderPlaylists(playlistProviders)
                                               .SelectMany(x => x.Channels)
                                               .ToList();

            Playlist playlist = new Playlist();
            IEnumerable <Channel>           filteredProviderChannels  = FilterProviderChannels(providerChannels, channelDefinitions);
            IEnumerable <ChannelDefinition> enabledChannelDefinitions = channelDefinitions
                                                                        .Where(x => x.IsEnabled && groups[x.GroupId].IsEnabled);

            logger.Info(MyOperation.ChannelMatching, OperationStatus.Started);

            foreach (ChannelDefinition channelDef in enabledChannelDefinitions)
            {
                Channel matchedChannel = filteredProviderChannels
                                         .FirstOrDefault(x => channelMatcher.DoesMatch(channelDef.Name, x.Name));

                if (matchedChannel is null)
                {
                    continue;
                }

                Channel channel = new Channel();
                channel.Id      = channelDef.Id;
                channel.Name    = channelDef.Name.Value;
                channel.Group   = groups[channelDef.GroupId].Name;
                channel.LogoUrl = channelDef.LogoUrl;
                channel.Number  = playlist.Channels.Count + 1;
                channel.Url     = matchedChannel.Url;

                playlist.Channels.Add(channel);
            }

            if (settings.CanIncludeUnmatchedChannels)
            {
                logger.Info(MyOperation.ChannelMatching, OperationStatus.InProgress, $"Getting unmatched channels");

                IEnumerable <Channel> unmatchedChannels = filteredProviderChannels
                                                          .Where(x => channelDefinitions.All(y => !channelMatcher.DoesMatch(y.Name, x.Name)))
                                                          .GroupBy(x => x.Name)
                                                          .Select(g => g.First())
                                                          .OrderBy(x => x.Name);

                foreach (Channel unmatchedChannel in unmatchedChannels)
                {
                    logger.Warn(MyOperation.ChannelMatching, OperationStatus.Failure, new LogInfo(MyLogInfoKey.Channel, unmatchedChannel.Name));

                    unmatchedChannel.Number = playlist.Channels.Count + 1;
                    playlist.Channels.Add(unmatchedChannel);
                }
            }

            logger.Debug(
                MyOperation.ChannelMatching,
                OperationStatus.Success,
                new LogInfo(MyLogInfoKey.ChannelsCount, playlist.Channels.Count.ToString()));

            return(playlistFileBuilder.BuildFile(playlist));
        }