Пример #1
0
        public string GetChannelURI(Channel channel)
        {
            string streamURL        = string.Empty;
            string scriptXpath      = "/html/body/script[3]";
            string today            = DateTime.Now.ToString("yyyyMMdd");
            string pathToCachedFile = Path.Combine(Path.GetTempPath(), "MagyarTV-StreamURL-" + channel.Name + "-" + DateTime.Now.ToString("yyyy-dd-M-HH") + ".html"); // Web page is fetched at the very least on top of each hour

            HtmlAgilityPack.HtmlDocument htmlDocument = HtmlAgilityPackEx.LoadFromCachedHtmlFile(pathToCachedFile);

            if (htmlDocument == null) // If there is no cached file, load it from the given URL
            {
                HtmlWeb browser = new HtmlWeb();
                htmlDocument = browser.Load(channel.IndexFeed);
                FileStream sw = new FileStream(pathToCachedFile, FileMode.Create);
                htmlDocument.Save(sw);
                sw.Close();
            }

            HtmlAgilityPack.HtmlNodeCollection script = htmlDocument.DocumentNode.SelectNodes(scriptXpath);

            try
            {
                string   currentline = String.Empty;
                var      x           = script.ToList();
                string[] lines       = x[0].InnerText.Split(new string[] { "\n" }, System.StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < lines.Length; i++)
                {
                    currentline = lines[i].Trim();
                    if (currentline.Contains("index.m3u8"))
                    {
                        break;
                    }
                }
                string[] url_parts = currentline.Split('"');
                streamURL = string.Format("https:{0}", url_parts[3].Replace("\\", ""));
            }
            catch (Exception ex)
            {
                throw;
            }

            return(streamURL);
        }
Пример #2
0
 private static List<HtmlNode> NodesToList(HtmlNodeCollection nodes)
 {
     return (nodes != null && nodes.Any()) ? nodes.ToList() : new List<HtmlNode>();
 }