ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/xmltv_parser/trunk/libxmltv/Core/XMLTVProgramCollection.cs
Revision: 30
Committed: Thu Mar 7 13:36:42 2013 UTC (10 years, 2 months ago) by william
File size: 5840 byte(s)
Log Message:

File Contents

# User Rev Content
1 william 27 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6     using System.Xml.Linq;
7 william 28 using System.Globalization;
8 william 27
9     namespace libxmltv.Core
10     {
11     internal class XMLTVProgramCollection
12     {
13     private Dictionary<int, IXMLTVProgram> entries = new Dictionary<int, IXMLTVProgram>();
14     public XMLTVProgramCollection(object xmltv)
15     {
16 william 28 XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection");
17 william 27 IXMLTV_PARSER _xmltv;
18     if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return; }
19     XMLTV_PARSER = _xmltv;
20     Create();
21     }
22    
23     #region IXMLTVSource
24     private IXMLTV_PARSER XMLTV_PARSER { get; set; }
25     public Dictionary<int, IXMLTVProgram> Collection
26     {
27     get { return entries; }
28     }
29     #endregion
30    
31 william 28 //
32     private DateTime ParseDate(string timeStamp)
33     {
34     DateTime dt = new DateTime();
35     try
36     {
37     dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture);
38     }
39     catch (Exception ex) { throw ex; }
40     return dt;
41     }
42 william 27 private void Create()
43     {
44     var doc = XMLTV_PARSER.XMLTV_LOADER.XmlDoc;
45     int index = 0;
46     foreach (var c in doc.Descendants(XMLTV_CONSTANTS.PROGRAM_ELEMENT))
47     {
48 william 28 Program program = new Program();
49    
50     program.Id = index;
51 william 30 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("program_Id: {0}", program.Id);
52 william 28 if (c.HasAttributes)
53     {
54     var start = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStart).Value;
55     program.Start = ParseDate(start);
56 william 30 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_start: {0}", start);
57 william 28 var stop = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStop).Value;
58     program.Stop = ParseDate(stop);
59 william 30 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop);
60 william 28 var channelid = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramChannelId).Value;
61 william 30 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid);
62 william 28 IXMLTVChannel channel = new Channel();
63     try
64     {
65     channel = XMLTV_PARSER.Channels[channelid];
66 william 30
67 william 28 }
68     catch (KeyNotFoundException)
69     {
70     XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid));
71     }
72     program.Channel = channel;
73 william 30 //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString());
74 william 28 }
75 william 30 try
76     {
77     var title = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramTitle).FirstOrDefault().Value;
78     program.Title = title;
79     }
80     catch (Exception) { program.Title = string.Empty; }
81     //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title);
82     try
83     {
84     var subtitle = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramSubTitle).FirstOrDefault().Value;
85     program.SubTitle = subtitle;
86     }
87     catch (Exception) { program.SubTitle = string.Empty; }
88     //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle);
89     try
90     {
91     var description = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramDescription).FirstOrDefault().Value;
92     program.Description = description;
93     }
94     catch (Exception) { program.Description = string.Empty; }
95     //XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description);
96 william 28
97     entries.Add(program.Id, program);
98 william 27 index++;
99     }
100     }
101     public override string ToString()
102     {
103     //return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl);
104     return string.Empty;
105     }
106     }
107    
108     internal class Program : IXMLTVProgram
109     {
110     public Program()
111     {
112 william 28 Id = 0;
113     Start = new DateTime();
114     Stop = new DateTime();
115     Channel = new Channel();
116     Title = string.Empty;
117     SubTitle = string.Empty;
118     Description = string.Empty;
119 william 27 }
120 william 28 #region IXMLTVProgram members
121     public int Id { get; set; }
122     public DateTime Start { get; set; }
123     public DateTime Stop { get; set; }
124     public IXMLTVChannel Channel { get; set; }
125     public string Title { get; set; }
126     public string SubTitle { get; set; }
127     public string Description { get; set; }
128     #endregion
129     public override string ToString()
130 william 27 {
131 william 28 return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt"));
132 william 27 }
133     }
134     }