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

File Contents

# Content
1 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 using System.Globalization;
8
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 XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection");
17 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 //
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 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 Program program = new Program();
49
50 program.Id = index;
51
52 if (c.HasAttributes)
53 {
54 var start = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStart).Value;
55 program.Start = ParseDate(start);
56
57 var stop = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramStop).Value;
58 program.Stop = ParseDate(stop);
59
60 var channelid = c.Attribute(XMLTV_CONSTANTS.Programs.ProgramChannelId).Value;
61 IXMLTVChannel channel = new Channel();
62 try
63 {
64 channel = XMLTV_PARSER.Channels[channelid];
65 }
66 catch (KeyNotFoundException)
67 {
68 XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid));
69 }
70 program.Channel = channel;
71 }
72 var title = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramTitle).FirstOrDefault().Value;
73 program.Title = title;
74 var subtitle = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramSubTitle).FirstOrDefault().Value;
75 program.SubTitle = subtitle;
76 var description = c.Descendants(XMLTV_CONSTANTS.Programs.ProgramDescription).FirstOrDefault().Value;
77 program.Description = description;
78
79 entries.Add(program.Id, program);
80 index++;
81 }
82 }
83 public override string ToString()
84 {
85 //return string.Format("XmlTv Source: '{0}' (Generated by: '{1}') (support: '{2}')", SourceName, GeneratorName, GeneratorUrl);
86 return string.Empty;
87 }
88 }
89
90 internal class Program : IXMLTVProgram
91 {
92 public Program()
93 {
94 Id = 0;
95 Start = new DateTime();
96 Stop = new DateTime();
97 Channel = new Channel();
98 Title = string.Empty;
99 SubTitle = string.Empty;
100 Description = string.Empty;
101 }
102 #region IXMLTVProgram members
103 public int Id { get; set; }
104 public DateTime Start { get; set; }
105 public DateTime Stop { get; set; }
106 public IXMLTVChannel Channel { get; set; }
107 public string Title { get; set; }
108 public string SubTitle { get; set; }
109 public string Description { get; set; }
110 #endregion
111 public override string ToString()
112 {
113 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"));
114 }
115 }
116 }