Esempio n. 1
0
        /// <summary>
        /// Generates main output xml.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        private void GenerateOutputXml(string sourceXml,long srno, string newChannelName=null, int newOffset=0)
        {
            var document = XDocument.Load(sourceXml);

            var _tv = new tv();
            var channelNode1 = document.Descendants("channel");

            var xElements = channelNode1 as XElement[] ?? channelNode1.ToArray();
            for (var i=0;i<=xElements.Length-1;i++)
            {
                var elementChannel = xElements[i];
                var elementchannelId = elementChannel.Attributes("id").First().Value;

                var progArray = document.Descendants("programme")
                    .Where(x => (string)x.Attribute("channel").Value == elementchannelId);

                #region Channel...
                string id = string.Empty;
                string channelName = string.Empty;
                const string formatString = "yyyyMMddHHmmss";
                var outputFileName = string.Empty;

                #region Channel Name...

                if (!string.IsNullOrEmpty(newChannelName))
                {
                    channelName = newChannelName;
                    try
                    {
                        id = xElements[i].Attributes("id").First().Value;
                        outputFileName = xElements[i].Value;

                        if (string.IsNullOrEmpty(channelName))
                        {
                            outputFileName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                    catch (Exception)
                    {
                        var channelNode = document.Descendants("channel").First();
                        outputFileName = channelNode.Value;
                        if (string.IsNullOrEmpty(channelName))
                        {
                            outputFileName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                }
                else
                {
                    try
                    {
                        id = xElements[i].Attributes("id").First().Value;

                        channelName = xElements[i].Value;

                        //Nov-19
                        if (channelName.Contains("http://"))
                        {
                            channelName = channelName.Substring(0, channelName.IndexOf("http:", StringComparison.Ordinal));
                        }

                        if (string.IsNullOrEmpty(channelName))
                        {
                            channelName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                    catch (Exception)
                    {
                        var channelNode = document.Descendants("channel").First();
                        channelName = channelNode.Value;
                        if (string.IsNullOrEmpty(channelName))
                        {
                            channelName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }

                    outputFileName = channelName;

                }

                #endregion Channel Name...

                 //1.> Nov-19 - Remove any Illegeal characters from filename.
                 //2.> Nov-25 - Task ID:  1647 (duplicate channel bug ).
                if (outputFileName.Contains("http://"))
                {
                    outputFileName = outputFileName.Substring(0, outputFileName.IndexOf("http://", StringComparison.Ordinal));
                }

                outputFileName = GetSafeFilename(outputFileName);

                //Check if current channe is active.
                using (var dataContext=new EPGDataModel())
                {
                    var channelList =
                        dataContext.SourceURLs.Find(srno)
                            .ActiveChannels.FirstOrDefault(c => c.ChannelName.Equals(outputFileName) && c.IsActive);

                    if (channelList == null)
                        continue;
                }
                //End

                var tvChannel = new tvChannel
                {
                   id = id,
                   displayname = channelName,
                };

                _tv.channel = tvChannel;

                #endregion Channel...

                #region Programme Nodes...

                var sourceXElements = progArray.ToList();

                //If no programe nodes move to next channel.
                if (sourceXElements.Count == 0)
                    continue;

                tvProgramme[] _tvProgrammes = new tvProgramme[sourceXElements.Count];

                var iNodeCount = 0;

                foreach (var item in sourceXElements)
                {
                    #region Minutes...

                    var startTime = item.Attributes("start").First().Value;
                    var epgStartOffset = startTime;

                    if (startTime.Contains("+"))
                        startTime = startTime.Substring(0, startTime.IndexOf('+'));

                    var stopTime = item.Attributes("stop").First().Value;

                    if (stopTime.Contains("+"))
                        stopTime = stopTime.Substring(0, stopTime.IndexOf('+'));

                    var duration = DateTime.ParseExact(stopTime.Trim(), formatString, CultureInfo.InvariantCulture).Subtract(DateTime.ParseExact(startTime.Trim(), formatString, CultureInfo.InvariantCulture)).TotalMinutes;

                    #endregion Minutes...

                    #region Credits...

                    tvProgrammeName[] tvProgrammeNames = null;

                    if (item.Element("credits") != null)
                    {

                        XElement creditElement = item.Element("credits");

                        if (creditElement != null)
                        {
                            var  creditsArray = creditElement.Nodes().ToList();

                            tvProgrammeNames = new tvProgrammeName[creditsArray.Count];

                            for (var x=0;x<= creditsArray.Count-1;x++)
                            {
                                XNode node = creditsArray[x];
                                XElement element = (XElement) node;

                                tvProgrammeNames[x] = new tvProgrammeName
                                {

                                    function = element.Name.LocalName,
                                    Value = element.Value
                                };
                            }

                        }
                    }

                    #endregion Credits...

                    #region Image...

                    var imageSrc = "";

                    if (item.Element("icon") != null)
                    {
                        var imgElement = item.Element("icon");

                        if (imgElement != null && imgElement.Attribute("src") != null)
                        {
                            imageSrc = imgElement.Attribute("src").Value;
                        }
                    }

                    #endregion Image...

                    _tvProgrammes[iNodeCount] = new tvProgramme
                    {
                        channel = channelName,
                        start = CommonFunctions.GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop = CommonFunctions.GetDateAddingOffset(item.Attributes("stop").First().Value, newOffset),

                        length = new tvProgrammeLength
                        {
                            Value = ushort.Parse(Convert.ToString(duration, CultureInfo.InvariantCulture)),
                            units = "minutes"
                        },
                        title = item.Element("title") != null ? item.Element("title").Value : "",
                        category = item.Element("category") != null ? item.Element("category").Value : "",
                        desc = item.Element("desc") != null ? item.Element("desc").Value : "",
                        credits = tvProgrammeNames,
                        image = imageSrc
                    };

                    iNodeCount++;
                }

                _tv.programme = _tvProgrammes;

                var startDt = _tvProgrammes.First().start;

                if (startDt.Contains("+"))
                    startDt = startDt.Substring(0, startDt.IndexOf('+')).Trim();

                var stopDt = _tvProgrammes.Reverse().First().stop;

                if (stopDt.Contains("+"))
                    stopDt = stopDt.Substring(0, stopDt.IndexOf('+')).Trim();

                try
                {
                    _startDate = DateTime.ParseExact(startDt, formatString, CultureInfo.InvariantCulture);
                    _stopDate = DateTime.ParseExact(stopDt, formatString, CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                }

                #endregion Programme Nodes...

                _tv.Save(Server.MapPath("../Output/" + outputFileName + ".xml"));

                #region Generate Additional Xml...

                var _tv2 = new Code.SecondOutput.tv();
                Code.SecondOutput.tvProgramme[] _tvProgrammes2 = new Code.SecondOutput.tvProgramme[sourceXElements.Count];

                iNodeCount = 0;
                foreach (var item in sourceXElements)
                {
                    #region Minutes...

                    var startTime = item.Attributes("start").First().Value;
                    var epgStartOffset = startTime;

                    if (startTime.Contains("+"))
                        startTime = startTime.Substring(0, startTime.IndexOf('+'));

                    var stopTime = item.Attributes("stop").First().Value;

                    if (stopTime.Contains("+"))
                        stopTime = stopTime.Substring(0, stopTime.IndexOf('+'));

                    var duration = DateTime.ParseExact(stopTime.Trim(), formatString, CultureInfo.InvariantCulture).Subtract(DateTime.ParseExact(startTime.Trim(), formatString, CultureInfo.InvariantCulture)).TotalMinutes;

                   // var date = DateTime.ParseExact(startTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

                    #endregion Minutes...

                    _tvProgrammes2[iNodeCount] = new Code.SecondOutput.tvProgramme()
                    {
                        channel = channelName,
                        start = CommonFunctions.GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop = CommonFunctions.GetDateAddingOffset(item.Attributes("stop").First().Value, newOffset),
                        title = item.Element("title") != null ? item.Element("title").Value : "",
                        desc = item.Element("desc") != null ? item.Element("desc").Value : "",
                        subTitle = item.Element("sub-title") != null ? item.Element("sub-title").Value : "",
                        date = DateTime.Today,
                        originid = "",//Find
                        reserve = "no"
                    };

                    iNodeCount++;
                }

                _tv2.programme = _tvProgrammes2;

                _tv2.Save(Server.MapPath("../Output/" + outputFileName +"_additional"+ ".xml"));

                #endregion Generate Additional Xml...

                 _outputXmlList.Add("../Output/" + outputFileName + ".xml" + ","
                     + _startDate.Date.ToShortDateString() + ","
                     + _stopDate.Date.ToShortDateString() + ","
                     + "../Output/" + outputFileName + "_additional" + ".xml" + ","
                     + "../SourceXmlFiles/"+ Path.GetFileName(sourceXml)
                     );
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generates main output xml.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        private void GenerateOutputXml(string sourceXml, long srno, string newChannelName = null, int newOffset = 0)
        {
            var document = XDocument.Load(sourceXml);

            var _tv          = new tv();
            var channelNode1 = document.Descendants("channel");

            var xElements = channelNode1 as XElement[] ?? channelNode1.ToArray();

            for (var i = 0; i <= xElements.Length - 1; i++)
            {
                var elementChannel   = xElements[i];
                var elementchannelId = elementChannel.Attributes("id").First().Value;


                var progArray = document.Descendants("programme")
                                .Where(x => (string)x.Attribute("channel").Value == elementchannelId);

                #region Channel...
                string       id             = string.Empty;
                string       channelName    = string.Empty;
                const string formatString   = "yyyyMMddHHmmss";
                var          outputFileName = string.Empty;

                #region Channel Name...

                if (!string.IsNullOrEmpty(newChannelName))
                {
                    channelName = newChannelName;
                    try
                    {
                        id             = xElements[i].Attributes("id").First().Value;
                        outputFileName = xElements[i].Value;

                        if (string.IsNullOrEmpty(channelName))
                        {
                            outputFileName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                    catch (Exception)
                    {
                        var channelNode = document.Descendants("channel").First();
                        outputFileName = channelNode.Value;
                        if (string.IsNullOrEmpty(channelName))
                        {
                            outputFileName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                }
                else
                {
                    try
                    {
                        id = xElements[i].Attributes("id").First().Value;

                        channelName = xElements[i].Value;

                        //Nov-19
                        if (channelName.Contains("http://"))
                        {
                            channelName = channelName.Substring(0, channelName.IndexOf("http:", StringComparison.Ordinal));
                        }

                        if (string.IsNullOrEmpty(channelName))
                        {
                            channelName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }
                    catch (Exception)
                    {
                        var channelNode = document.Descendants("channel").First();
                        channelName = channelNode.Value;
                        if (string.IsNullOrEmpty(channelName))
                        {
                            channelName = xElements[i].Attributes("display-name").First().Value;
                        }
                    }

                    outputFileName = channelName;
                }

                #endregion Channel Name...

                //1.> Nov-19 - Remove any Illegeal characters from filename.
                //2.> Nov-25 - Task ID:  1647 (duplicate channel bug ).
                if (outputFileName.Contains("http://"))
                {
                    outputFileName = outputFileName.Substring(0, outputFileName.IndexOf("http://", StringComparison.Ordinal));
                }

                outputFileName = GetSafeFilename(outputFileName);

                //Check if current channe is active.
                using (var dataContext = new EPGDataModel())
                {
                    var channelList =
                        dataContext.SourceURLs.Find(srno)
                        .ActiveChannels.FirstOrDefault(c => c.ChannelName.Equals(outputFileName) && c.IsActive);

                    if (channelList == null)
                    {
                        continue;
                    }
                }
                //End

                var tvChannel = new tvChannel
                {
                    id          = id,
                    displayname = channelName,
                };

                _tv.channel = tvChannel;

                #endregion Channel...

                #region Programme Nodes...

                var sourceXElements = progArray.ToList();

                //If no programe nodes move to next channel.
                if (sourceXElements.Count == 0)
                {
                    continue;
                }


                tvProgramme[] _tvProgrammes = new tvProgramme[sourceXElements.Count];

                var iNodeCount = 0;

                foreach (var item in sourceXElements)
                {
                    #region Minutes...

                    var startTime      = item.Attributes("start").First().Value;
                    var epgStartOffset = startTime;


                    if (startTime.Contains("+"))
                    {
                        startTime = startTime.Substring(0, startTime.IndexOf('+'));
                    }

                    var stopTime = item.Attributes("stop").First().Value;

                    if (stopTime.Contains("+"))
                    {
                        stopTime = stopTime.Substring(0, stopTime.IndexOf('+'));
                    }

                    var duration = DateTime.ParseExact(stopTime.Trim(), formatString, CultureInfo.InvariantCulture).Subtract(DateTime.ParseExact(startTime.Trim(), formatString, CultureInfo.InvariantCulture)).TotalMinutes;

                    #endregion Minutes...

                    #region Credits...

                    tvProgrammeName[] tvProgrammeNames = null;

                    if (item.Element("credits") != null)
                    {
                        XElement creditElement = item.Element("credits");

                        if (creditElement != null)
                        {
                            var creditsArray = creditElement.Nodes().ToList();

                            tvProgrammeNames = new tvProgrammeName[creditsArray.Count];


                            for (var x = 0; x <= creditsArray.Count - 1; x++)
                            {
                                XNode    node    = creditsArray[x];
                                XElement element = (XElement)node;

                                tvProgrammeNames[x] = new tvProgrammeName
                                {
                                    function = element.Name.LocalName,
                                    Value    = element.Value
                                };
                            }
                        }
                    }

                    #endregion Credits...


                    #region Image...

                    var imageSrc = "";

                    if (item.Element("icon") != null)
                    {
                        var imgElement = item.Element("icon");

                        if (imgElement != null && imgElement.Attribute("src") != null)
                        {
                            imageSrc = imgElement.Attribute("src").Value;
                        }
                    }


                    #endregion Image...

                    _tvProgrammes[iNodeCount] = new tvProgramme
                    {
                        channel = channelName,
                        start   = CommonFunctions.GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop    = CommonFunctions.GetDateAddingOffset(item.Attributes("stop").First().Value, newOffset),

                        length = new tvProgrammeLength
                        {
                            Value = ushort.Parse(Convert.ToString(duration, CultureInfo.InvariantCulture)),
                            units = "minutes"
                        },
                        title    = item.Element("title") != null?item.Element("title").Value       : "",
                        category = item.Element("category") != null?item.Element("category").Value : "",
                        desc     = item.Element("desc") != null?item.Element("desc").Value         : "",
                        credits  = tvProgrammeNames,
                        image    = imageSrc
                    };

                    iNodeCount++;
                }


                _tv.programme = _tvProgrammes;


                var startDt = _tvProgrammes.First().start;

                if (startDt.Contains("+"))
                {
                    startDt = startDt.Substring(0, startDt.IndexOf('+')).Trim();
                }

                var stopDt = _tvProgrammes.Reverse().First().stop;

                if (stopDt.Contains("+"))
                {
                    stopDt = stopDt.Substring(0, stopDt.IndexOf('+')).Trim();
                }

                try
                {
                    _startDate = DateTime.ParseExact(startDt, formatString, CultureInfo.InvariantCulture);
                    _stopDate  = DateTime.ParseExact(stopDt, formatString, CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                }

                #endregion Programme Nodes...

                _tv.Save(Server.MapPath("../Output/" + outputFileName + ".xml"));

                #region Generate Additional Xml...

                var _tv2 = new Code.SecondOutput.tv();
                Code.SecondOutput.tvProgramme[] _tvProgrammes2 = new Code.SecondOutput.tvProgramme[sourceXElements.Count];

                iNodeCount = 0;
                foreach (var item in sourceXElements)
                {
                    #region Minutes...

                    var startTime      = item.Attributes("start").First().Value;
                    var epgStartOffset = startTime;


                    if (startTime.Contains("+"))
                    {
                        startTime = startTime.Substring(0, startTime.IndexOf('+'));
                    }

                    var stopTime = item.Attributes("stop").First().Value;

                    if (stopTime.Contains("+"))
                    {
                        stopTime = stopTime.Substring(0, stopTime.IndexOf('+'));
                    }

                    var duration = DateTime.ParseExact(stopTime.Trim(), formatString, CultureInfo.InvariantCulture).Subtract(DateTime.ParseExact(startTime.Trim(), formatString, CultureInfo.InvariantCulture)).TotalMinutes;


                    // var date = DateTime.ParseExact(startTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

                    #endregion Minutes...

                    _tvProgrammes2[iNodeCount] = new Code.SecondOutput.tvProgramme()
                    {
                        channel  = channelName,
                        start    = CommonFunctions.GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop     = CommonFunctions.GetDateAddingOffset(item.Attributes("stop").First().Value, newOffset),
                        title    = item.Element("title") != null?item.Element("title").Value         : "",
                        desc     = item.Element("desc") != null?item.Element("desc").Value           : "",
                        subTitle = item.Element("sub-title") != null?item.Element("sub-title").Value : "",
                        date     = DateTime.Today,
                        originid = "",//Find
                        reserve  = "no"
                    };

                    iNodeCount++;
                }


                _tv2.programme = _tvProgrammes2;



                _tv2.Save(Server.MapPath("../Output/" + outputFileName + "_additional" + ".xml"));

                #endregion Generate Additional Xml...

                _outputXmlList.Add("../Output/" + outputFileName + ".xml" + ","
                                   + _startDate.Date.ToShortDateString() + ","
                                   + _stopDate.Date.ToShortDateString() + ","
                                   + "../Output/" + outputFileName + "_additional" + ".xml" + ","
                                   + "../SourceXmlFiles/" + Path.GetFileName(sourceXml)
                                   );
            }
        }