Ejemplo n.º 1
0
        /// <summary>
        /// Generates main output xml.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="srno"></param>
        /// <param name="epgEnd"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        /// <param name="epgStart"></param>
        public static List <string> GenerateOutputXml(string sourceXml, long srno, out DateTime epgStart, out DateTime epgEnd, string newChannelName = null, int newOffset = 0)
        {
            OutputXmlList.Clear();
            epgStart = new DateTime();
            epgEnd   = new DateTime();

            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...
                var          id               = string.Empty;
                var          channelName      = string.Empty;
                var          queryChannelName = string.Empty;
                const string formatString     = "yyyyMMddHHmmss";
                var          outputFileName   = string.Empty;

                #region Channel Name...

                if (!string.IsNullOrEmpty(newChannelName))
                {
                    channelName = newChannelName;

                    id = xElements[i].Attributes("id").First().Value;
                    queryChannelName = xElements[i].Value;
                    if (string.IsNullOrEmpty(queryChannelName))
                    {
                        queryChannelName = xElements[i].Attributes("display-name").First().Value;
                    }

                    try
                    {
                        id             = xElements[i].Attributes("id").First().Value;
                        outputFileName = xElements[i].Value;

                        if (string.IsNullOrEmpty(outputFileName))
                        {
                            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;
                        queryChannelName = channelName;
                        //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);
                queryChannelName = outputFileName;
                //Check if current channe is active.
                using (var dataContext = new EPGDataModel())
                {
                    var channelList =
                        dataContext.SourceURLs.Find(srno)
                        .ActiveChannels.FirstOrDefault(c => c.ChannelName.Equals(queryChannelName) && 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;
                }


                var _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   = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop    = 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);

                    epgStart = _startDate;
                    epgEnd   = _stopDate;
                }
                catch (Exception)
                {
                }

                #endregion Programme Nodes...


                _tv.Save(OutputFolderPath + outputFileName + ".xml");


                #region Generate Additional Xml...

                var _tv2 = new XmlParser.Core.XmlSchema.SecondOutput.tv();
                XmlParser.Core.XmlSchema.SecondOutput.tvProgramme[] _tvProgrammes2 = new XmlParser.Core.XmlSchema.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 XmlParser.Core.XmlSchema.SecondOutput.tvProgramme()
                    {
                        channel  = channelName,
                        start    = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop     = 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(OutputFolderPath + 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)
                                  );
            }

            return(OutputXmlList);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        private static void GenerateOutputXml(string sourceXml, string newChannelName = null, int newOffset = 0)
        {
            Console.WriteLine("GenerateOutputXml > Source-" + sourceXml + ", Channel Name-" + newChannelName + ", Offset-" + newOffset);

            var document = XDocument.Load(sourceXml);

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

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

            Console.WriteLine("Total Channels in current File - " + xElements.Length);

            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;
                }

                Console.WriteLine("Processing Channel - " + channelName);

                #endregion Channel Name...

                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   = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop    = 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 generateException)
                {
                    logger.Error("Error occured while processing xml - " + sourceXml + "| Channel name-" + channelName + " | Error message-" + generateException.Message);
                    if (generateException.InnerException != null)
                    {
                        logger.Error("Function Generate Xml Inner exception - " +
                                     generateException.InnerException.Message);
                    }
                }

                #endregion Programme Nodes...



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

                _tv.Save(OutputFolderPath + outputFileName + ".xml");
                logger.Info("Generate Output xml -" + outputFileName + ".xml");


                #region Generate Additional Xml...

                var _tv2 = new XmlParser.Core.XmlSchema.SecondOutput.tv();
                XmlParser.Core.XmlSchema.SecondOutput.tvProgramme[] _tvProgrammes2 = new XmlParser.Core.XmlSchema.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 XmlParser.Core.XmlSchema.SecondOutput.tvProgramme()
                    {
                        channel  = channelName,
                        start    = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop     = 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(OutputFolderPath + outputFileName + "_additional" + ".xml");

                #endregion Generate Additional Xml...

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

                Console.WriteLine("Done Processing Channel - " + channelName);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates main output xml.
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="srno"></param>
        /// <param name="epgEnd"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        /// <param name="epgStart"></param>
        public static List<string> GenerateOutputXml(string sourceXml, long srno,out DateTime epgStart,out DateTime epgEnd, string newChannelName = null, int newOffset = 0)
        {
            OutputXmlList.Clear();
            epgStart=new DateTime();
            epgEnd = new DateTime();

               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...
               var id = string.Empty;
               var channelName = string.Empty;
               var queryChannelName = string.Empty;
               const string formatString = "yyyyMMddHHmmss";
               var outputFileName = string.Empty;

               #region Channel Name...

               if (!string.IsNullOrEmpty(newChannelName))
               {
                   channelName = newChannelName;

                   id = xElements[i].Attributes("id").First().Value;
                   queryChannelName = xElements[i].Value;
                   if (string.IsNullOrEmpty(queryChannelName))
                   {
                       queryChannelName = xElements[i].Attributes("display-name").First().Value;
                   }

                   try
                   {
                       id = xElements[i].Attributes("id").First().Value;
                       outputFileName = xElements[i].Value;

                       if (string.IsNullOrEmpty(outputFileName))
                       {
                           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;
                       queryChannelName = channelName;
                       //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);
               queryChannelName = outputFileName;
               //Check if current channe is active.
               using (var dataContext = new EPGDataModel())
               {
                   var channelList =
                       dataContext.SourceURLs.Find(srno)
                           .ActiveChannels.FirstOrDefault(c => c.ChannelName.Equals(queryChannelName) && 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;

               var _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 = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                       stop = 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);

                   epgStart = _startDate;
                   epgEnd = _stopDate;
               }
               catch (Exception)
               {
               }

               #endregion Programme Nodes...

               _tv.Save( OutputFolderPath + outputFileName + ".xml");

               #region Generate Additional Xml...

               var _tv2 = new XmlParser.Core.XmlSchema.SecondOutput.tv();
               XmlParser.Core.XmlSchema.SecondOutput.tvProgramme[] _tvProgrammes2 = new XmlParser.Core.XmlSchema.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 XmlParser.Core.XmlSchema.SecondOutput.tvProgramme()
                   {
                       channel = channelName,
                       start = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                       stop = 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(OutputFolderPath + 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)
                   );
               }

            return OutputXmlList;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="newChannelName"></param>
        /// <param name="newOffset"></param>
        private static void GenerateOutputXml(string sourceXml, string newChannelName = null, int newOffset = 0)
        {
            Console.WriteLine("GenerateOutputXml > Source-"+sourceXml + ", Channel Name-"+newChannelName +", Offset-"+newOffset);

            var document = XDocument.Load(sourceXml);

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

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

            Console.WriteLine("Total Channels in current File - " + xElements.Length);

            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;
                }

                Console.WriteLine("Processing Channel - " + channelName);

                #endregion Channel Name...

                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 = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop = 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 generateException)
                {
                    logger.Error("Error occured while processing xml - " + sourceXml +"| Channel name-"+channelName+ " | Error message-"+generateException.Message);
                    if (generateException.InnerException != null)
                        logger.Error("Function Generate Xml Inner exception - " +
                                     generateException.InnerException.Message);
                }

                #endregion Programme Nodes...

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

                _tv.Save(OutputFolderPath + outputFileName + ".xml");
                logger.Info("Generate Output xml -" + outputFileName + ".xml");

                #region Generate Additional Xml...

                var _tv2 = new XmlParser.Core.XmlSchema.SecondOutput.tv();
                XmlParser.Core.XmlSchema.SecondOutput.tvProgramme[] _tvProgrammes2 = new XmlParser.Core.XmlSchema.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 XmlParser.Core.XmlSchema.SecondOutput.tvProgramme()
                    {
                        channel = channelName,
                        start = GetDateAddingOffset(item.Attributes("start").First().Value, newOffset),
                        stop = 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(OutputFolderPath + outputFileName + "_additional" + ".xml");

                #endregion Generate Additional Xml...

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

                Console.WriteLine("Done Processing Channel - " + channelName);
            }
        }