Пример #1
0
        public async Task <bool> IsSourcePlayableAsync(string url)
        {
            string resolvedUrl = dnsResolver.ResolveUrl(url);
            string urlToUse    = url;

            if (string.IsNullOrWhiteSpace(resolvedUrl))
            {
                return(false);
            }

            MediaStreamStatus status = cache.GetStreamStatus(url);

            if (!(status is null))
            {
                return(status.IsAlive);
            }

            logger.Verbose(MyOperation.MediaSourceCheck, OperationStatus.Started, new LogInfo(MyLogInfoKey.Url, url));

            Uri uri = new Uri(url);

            if (uri.Scheme == "http")
            {
                urlToUse = resolvedUrl;
            }

            StreamState state;

            if (urlToUse.Contains(".m3u") || urlToUse.Contains(".m3u8"))
            {
                state = await GetPlaylistStateAsync(urlToUse);
            }
            else if (!urlToUse.EndsWith(".ts"))
            {
                state = await GetStreamStateAsync(urlToUse);
            }
            else
            {
                state = StreamState.Dead;
            }

            if (state == StreamState.Alive)
            {
                logger.Verbose(MyOperation.MediaSourceCheck, OperationStatus.Success, new LogInfo(MyLogInfoKey.Url, url));
            }
            else
            {
                logger.Verbose(MyOperation.MediaSourceCheck, OperationStatus.Failure, new LogInfo(MyLogInfoKey.Url, url), new LogInfo(MyLogInfoKey.StreamState, state));
            }

            SaveToCache(url, state);
            return(state == StreamState.Alive);
        }
Пример #2
0
        IEnumerable <Channel> FilterProviderChannels(
            IList <Channel> channels,
            IEnumerable <ChannelDefinition> channelDefinitions)
        {
            logger.Info(
                MyOperation.ProviderChannelsFiltering,
                OperationStatus.Started,
                new LogInfo(MyLogInfoKey.ChannelsCount, channels.Count()));

            List <Task>             tasks            = new List <Task>();
            ConcurrentBag <Channel> filteredChannels = new ConcurrentBag <Channel>();
            IEnumerable <Channel>   uniqueChannels   = channels
                                                       .GroupBy(x => dnsResolver.ResolveUrl(x.Url))
                                                       .Select(g => g.First());

            foreach (Channel channel in uniqueChannels)
            {
                Task task = Task.Run(async() =>
                {
                    bool isPlayable = await mediaSourceChecker.IsSourcePlayableAsync(channel.Url);

                    if (isPlayable)
                    {
                        filteredChannels.Add(channel);
                    }
                });

                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());

            logger.Info(
                MyOperation.ProviderChannelsFiltering,
                OperationStatus.Success);

            return(filteredChannels.OrderBy(x => channels.IndexOf(x)));
        }
Пример #3
0
        public async Task <string> TryDownloadStringAsync(string url)
        {
            string content = cache.GetWebDownload(url);

            if (!(content is null))
            {
                return(content);
            }

            string resolvedUrl = dnsResolver.ResolveUrl(url);

            if (!(resolvedUrl is null))
            {
                try
                {
                    content = await GetAsync(url);
                }
                catch { }
            }

            cache.StoreWebDownload(url, content);
            return(content);
        }
Пример #4
0
        IEnumerable <Channel> GetProvicerChannels(
            IList <Channel> channels,
            IEnumerable <ChannelDefinition> channelDefinitions)
        {
            logger.Info(
                MyOperation.ProviderChannelsFiltering,
                OperationStatus.Started,
                new LogInfo(MyLogInfoKey.ChannelsCount, channels.Count()));

            List <Task>           tasks            = new List <Task>();
            IEnumerable <Channel> filteredChannels = channels
                                                     .Where(x => !string.IsNullOrWhiteSpace(dnsResolver.ResolveUrl(x.Url)))
                                                     .GroupBy(x => dnsResolver.ResolveUrl(x.Url))
                                                     .Select(g => g.First())
                                                     .OrderBy(x => channels.IndexOf(x))
                                                     .ToList();

            logger.Info(
                MyOperation.ProviderChannelsFiltering,
                OperationStatus.Success);

            return(filteredChannels);
        }
        async Task <bool> IsStreamPlayableAsync(string url)
        {
            string resolvedUrl = dnsResolver.ResolveUrl(url);

            if (string.IsNullOrWhiteSpace(resolvedUrl))
            {
                return(false);
            }

            HttpWebRequest request = CreateWebRequest(resolvedUrl);

            try
            {
                HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync());

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(true);
                }
            }
            catch { }

            return(false);
        }
Пример #6
0
        async Task <string> GetAsync(string url)
        {
            Uri    uri         = new Uri(url);
            string resolvedUrl = dnsResolver.ResolveUrl(url);

            if (string.IsNullOrWhiteSpace(resolvedUrl))
            {
                return(null);
            }

            string content  = null;
            string urlToUse = url;

            if (uri.Scheme == "http")
            {
                urlToUse = resolvedUrl;
            }

            content = await SendGetRequestAsync(urlToUse);

            cache.StoreWebDownload(url, content);

            return(content);
        }