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