Wiki source code of TV Program

Last modified by Ludovic Dubost on 2010/03/29 22:55

Show last authors
1 {{html clean=false}}
2 <style type="text/css">
3 .starting {
4 background-color: lightgreen;
5 }
6 .running {
7 background-color: orange;
8 }
9 .finished {
10 color: lightgrey;
11 }
12 </style>
13 {{/html}}
14 {{groovy}}
15 import java.text.*;
16
17 def maxprogs = 40;
18 def maxarticles = 5;
19 def hoursBefore = 0.2;
20 def hoursAfter = 24;
21 def all = false;
22 def subjects = ["sport", "information","politique", "actualité", "économique", "film", "drame", "comédie"]
23 def rssType = "rss_2.0"
24 def split = false;
25 def textmode = false;
26
27 if ("1".equals(request.textmode))
28 textmode = true;
29
30 if (request.maxarticles)
31 maxarticles = Integer.parseInt(request.maxarticles)
32 if (request.maxprogs)
33 maxprogs = Integer.parseInt(request.maxprogs)
34 if (request.hoursbefore)
35 hoursBefore = Integer.parseInt(request.hoursbefore)
36 if (request.hoursafter)
37 hoursAfter = Integer.parseInt(request.hoursafter)
38
39 if (request.all=="1")
40 all = true;
41 if (request.subject) {
42 subjects = new ArrayList();
43 for(subject in request.getParameterValues("subject")) {
44 subjects.add(subject);
45 }
46 }
47 def getChannels(xml) {
48 def map = new HashMap();
49 for(channel in xml.channel) {
50 def key = channel.'@id';
51 def value = channel.'display-name'.text()
52 map.put(key,value)
53 }
54 return map;
55 }
56
57 if (request.code)
58 code = request.code
59 else
60 code = "phsx83v6uqhr";
61
62 def cont = xwiki.getURLContent("http://www.kazer.org/gen_xml.php?u=${code}")
63 def xml;
64 try {
65 xml = new XmlParser().parseText(cont.replaceAll("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">", ""));
66 def nb = 0;
67 def channels = getChannels(xml);
68 def datef = new SimpleDateFormat("yyyyMMddHHmmss Z")
69 def currentTime = (new Date()).getTime();
70 def proglist = new ArrayList();
71 def titlelist = new ArrayList();
72
73 for(prog in xml.programme) {
74 def isCatOk = false;
75 def lastCategory = "";
76 for (category in prog.category) {
77 def cattitle = category.text().toLowerCase();
78 for (subject in subjects) {
79 if (cattitle.contains(subject)) {
80 isCatOk = true
81 lastCategory = cattitle;
82 }
83 }
84 }
85
86 if (all)
87 isCatOk = true;
88
89 if (isCatOk) {
90 def start = prog.'@start';
91 def stop = prog.'@stop';
92 def startDate = datef.parse(start)
93 def stopDate = datef.parse(stop)
94 def startTime = startDate.getTime()
95 def endTime = stopDate.getTime()
96 def tit = prog.title.text();
97
98 if ((tit.length()>10 || request.nolimit) && (endTime > currentTime - hoursBefore * 3600000 && startTime < currentTime + hoursAfter * 3600000)) {
99 def sprog = [ "programme" : prog, "startDate" : startDate, "stopDate" : stopDate, "lastCategory" : lastCategory, "startTime" : startTime, "stopTime" : stopDate.getTime() ]
100 def added = false;
101 for (i=0;i<proglist.size();i++) {
102 if (startTime<proglist.get(i).startTime) {
103 proglist.add(i, sprog);
104 added = true;
105 break;
106 }
107 }
108 if (added==false)
109 proglist.add(sprog);
110 }
111 }
112 }
113
114 if (request.reverse=="1") {
115 def proglist2 = new ArrayList()
116 for(i=proglist.size()-1;i>0;i--) {
117 proglist2.add(proglist.get(i))
118 }
119 proglist = proglist2;
120 }
121 for(sprog in proglist) {
122 def prog = sprog.programme;
123 def startDate = xwiki.formatDate(sprog.startDate);
124 def stopDate = xwiki.formatDate(sprog.stopDate);
125 def lastCategory = sprog.lastCategory;
126
127 def tit = prog.title.text();
128
129 if (!titlelist.contains(tit)) {
130 // titlelist.add(tit)
131
132 def channel = channels.get(prog.'@channel')
133 def desc = prog.'@desc';
134 def descTitles = "";
135 def descDescs = "";
136
137 def ctime = (new Date()).getTime();
138 def cl = "future"
139 if (ctime > sprog.stopTime) {
140 cl = "finished"
141 }
142 else if (ctime > sprog.startTime - 900000 && ctime < sprog.startTime + 900000 && (ctime <= sprog.startTime || ctime < sprog.stopTime - 900000)) {
143 cl = "starting"
144 } else if (ctime >= sprog.startTime && ctime <= sprog.stopTime) {
145 cl = "running"
146 }
147
148 println "* (% class=${cl} %)${channel} ${startDate} ${stopDate} ${tit}";
149
150 if (desc!=null)
151 println desc;
152
153 nb++;
154 if (nb>=maxprogs)
155 break;
156 }
157 }
158 } catch (Exception e) {
159 println "{{{ ${cont} }}}";
160 }
161
162 {{/groovy}}