/
xmltvparser.cpp
733 lines (653 loc) · 23.2 KB
/
xmltvparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "xmltvparser.h"
// Qt headers
#include <QFile>
#include <QStringList>
#include <QDateTime>
#include <QDomDocument>
#include <QUrl>
// C++ headers
#include <iostream>
#include <cstdlib>
// libmyth headers
#include "exitcodes.h"
#include "mythcorecontext.h"
#include "mythmiscutil.h"
// libmythtv headers
#include "programinfo.h"
#include "programdata.h"
#include "dvbdescriptors.h"
// filldata headers
#include "channeldata.h"
#include "fillutil.h"
XMLTVParser::XMLTVParser() : isJapan(false), current_year(0)
{
current_year = MythDate::current().date().toString("yyyy").toUInt();
}
static unsigned int ELFHash(const char *s)
{
/* ELF hash uses unsigned chars and unsigned arithmetic for portability */
const unsigned char *name = (const unsigned char *)s;
unsigned long h = 0, g;
while (*name)
{
h = (h << 4) + (unsigned long)(*name++);
if ((g = (h & 0xF0000000UL))!=0)
h ^= (g >> 24);
h &= ~g;
}
return (int)h;
}
static QString getFirstText(QDomElement element)
{
for (QDomNode dname = element.firstChild(); !dname.isNull();
dname = dname.nextSibling())
{
QDomText t = dname.toText();
if (!t.isNull())
return t.data();
}
return QString();
}
ChanInfo *XMLTVParser::parseChannel(QDomElement &element, QUrl &baseUrl)
{
ChanInfo *chaninfo = new ChanInfo;
QString xmltvid = element.attribute("id", "");
QStringList split = xmltvid.simplified().split(" ");
chaninfo->xmltvid = xmltvid;
chaninfo->tvformat = "Default";
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement info = child.toElement();
if (!info.isNull())
{
if (info.tagName() == "icon")
{
QString path = info.attribute("src", "");
if (!path.isEmpty() && !path.contains("://"))
{
QString base = baseUrl.toString(QUrl::StripTrailingSlash);
chaninfo->iconpath = base +
((path.left(1) == "/") ? path : QString("/") + path);
}
else if (!path.isEmpty())
{
QUrl url(path);
if (url.isValid())
chaninfo->iconpath = url.toString();
}
}
else if (info.tagName() == "display-name")
{
if (chaninfo->name.isEmpty())
{
chaninfo->name = info.text();
}
else if (isJapan && chaninfo->callsign.isEmpty())
{
chaninfo->callsign = info.text();
}
else if (chaninfo->chanstr.isEmpty())
{
chaninfo->chanstr = info.text();
}
}
}
}
chaninfo->freqid = chaninfo->chanstr;
return chaninfo;
}
static int TimezoneToInt (QString timezone)
{
// we signal an error by setting it invalid (> 840min = 14hr)
int result = 841;
if (timezone.toUpper() == "UTC" || timezone.toUpper() == "GMT")
return 0;
if (timezone.length() == 5)
{
bool ok;
result = timezone.mid(1,2).toInt(&ok, 10);
if (!ok)
result = 841;
else
{
result *= 60;
int min = timezone.right(2).toInt(&ok, 10);
if (!ok)
result = 841;
else
{
result += min;
if (timezone.left(1) == "-")
result *= -1;
}
}
}
return result;
}
// localTimezoneOffset: 841 == "None", -841 == "Auto", other == fixed offset
static void fromXMLTVDate(QString ×tr, QDateTime &dt, int localTimezoneOffset = 841)
{
if (timestr.isEmpty())
{
LOG(VB_XMLTV, LOG_ERR, "Found empty Date/Time in XMLTV data, ignoring");
return;
}
QStringList split = timestr.split(" ");
QString ts = split[0];
bool ok;
int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
if (ts.length() == 14)
{
year = ts.left(4).toInt(&ok, 10);
month = ts.mid(4,2).toInt(&ok, 10);
day = ts.mid(6,2).toInt(&ok, 10);
hour = ts.mid(8,2).toInt(&ok, 10);
min = ts.mid(10,2).toInt(&ok, 10);
sec = ts.mid(12,2).toInt(&ok, 10);
}
else if (ts.length() == 12)
{
year = ts.left(4).toInt(&ok, 10);
month = ts.mid(4,2).toInt(&ok, 10);
day = ts.mid(6,2).toInt(&ok, 10);
hour = ts.mid(8,2).toInt(&ok, 10);
min = ts.mid(10,2).toInt(&ok, 10);
sec = 0;
}
else
{
LOG(VB_GENERAL, LOG_ERR,
QString("Ignoring unknown timestamp format: %1")
.arg(ts));
return;
}
dt = QDateTime(QDate(year, month, day),QTime(hour, min, sec),
Qt::LocalTime);
if ((split.size() > 1) && (localTimezoneOffset <= 840))
{
QString tmp = split[1].trimmed();
int ts_offset = TimezoneToInt(tmp);
if (abs(ts_offset) > 840)
{
ts_offset = 0;
localTimezoneOffset = 841;
}
dt = dt.addSecs(-ts_offset * 60);
}
if (localTimezoneOffset < -840)
{
dt.setTimeSpec(Qt::UTC);
}
else if (abs(localTimezoneOffset) <= 840)
{
dt = dt.addSecs(localTimezoneOffset * 60 );
}
dt = dt.toUTC();
timestr = MythDate::toString(dt, MythDate::kFilename);
}
static void parseCredits(QDomElement &element, ProgInfo *pginfo)
{
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement info = child.toElement();
if (!info.isNull())
pginfo->AddPerson(info.tagName(), getFirstText(info));
}
}
static void parseVideo(QDomElement &element, ProgInfo *pginfo)
{
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement info = child.toElement();
if (!info.isNull())
{
if (info.tagName() == "quality")
{
if (getFirstText(info) == "HDTV")
pginfo->videoProps |= VID_HDTV;
}
else if (info.tagName() == "aspect")
{
if (getFirstText(info) == "16:9")
pginfo->videoProps |= VID_WIDESCREEN;
}
}
}
}
static void parseAudio(QDomElement &element, ProgInfo *pginfo)
{
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement info = child.toElement();
if (!info.isNull())
{
if (info.tagName() == "stereo")
{
if (getFirstText(info) == "mono")
{
pginfo->audioProps |= AUD_MONO;
}
else if (getFirstText(info) == "stereo")
{
pginfo->audioProps |= AUD_STEREO;
}
else if (getFirstText(info) == "dolby" ||
getFirstText(info) == "dolby digital")
{
pginfo->audioProps |= AUD_DOLBY;
}
else if (getFirstText(info) == "surround")
{
pginfo->audioProps |= AUD_SURROUND;
}
}
}
}
}
ProgInfo *XMLTVParser::parseProgram(
QDomElement &element, int localTimezoneOffset)
{
QString uniqueid, season, episode;
int dd_progid_done = 0;
ProgInfo *pginfo = new ProgInfo();
QString text = element.attribute("start", "");
fromXMLTVDate(text, pginfo->starttime, localTimezoneOffset);
pginfo->startts = text;
text = element.attribute("stop", "");
fromXMLTVDate(text, pginfo->endtime, localTimezoneOffset);
pginfo->endts = text;
text = element.attribute("channel", "");
QStringList split = text.split(" ");
pginfo->channel = split[0];
text = element.attribute("clumpidx", "");
if (!text.isEmpty())
{
split = text.split('/');
pginfo->clumpidx = split[0];
pginfo->clumpmax = split[1];
}
for (QDomNode child = element.firstChild(); !child.isNull();
child = child.nextSibling())
{
QDomElement info = child.toElement();
if (!info.isNull())
{
if (info.tagName() == "title")
{
if (isJapan)
{
if (info.attribute("lang") == "ja_JP")
{
pginfo->title = getFirstText(info);
}
else if (info.attribute("lang") == "ja_JP@kana")
{
pginfo->title_pronounce = getFirstText(info);
}
}
else if (pginfo->title.isEmpty())
{
pginfo->title = getFirstText(info);
}
}
else if (info.tagName() == "sub-title" &&
pginfo->subtitle.isEmpty())
{
pginfo->subtitle = getFirstText(info);
}
else if (info.tagName() == "desc" && pginfo->description.isEmpty())
{
pginfo->description = getFirstText(info);
}
else if (info.tagName() == "category")
{
const QString cat = getFirstText(info).toLower();
if (kCategoryNone == pginfo->categoryType &&
string_to_myth_category_type(cat) != kCategoryNone)
{
pginfo->categoryType = string_to_myth_category_type(cat);
}
else if (pginfo->category.isEmpty())
{
pginfo->category = cat;
}
if (cat == "film")
{
// Hack for tv_grab_uk_rt
pginfo->categoryType = kCategoryMovie;
}
}
else if (info.tagName() == "date" && !pginfo->airdate)
{
// Movie production year
QString date = getFirstText(info);
pginfo->airdate = date.left(4).toUInt();
}
else if (info.tagName() == "star-rating" && pginfo->stars.isEmpty())
{
QDomNodeList values = info.elementsByTagName("value");
QDomElement item;
QString stars, num, den;
float rating = 0.0;
// Use the first rating to appear in the xml, this should be
// the most important one.
//
// Averaging is not a good idea here, any subsequent ratings
// are likely to represent that days recommended programmes
// which on a bad night could given to an average programme.
// In the case of uk_rt it's not unknown for a recommendation
// to be given to programmes which are 'so bad, you have to
// watch!'
item = values.item(0).toElement();
if (!item.isNull())
{
stars = getFirstText(item);
num = stars.section('/', 0, 0);
den = stars.section('/', 1, 1);
if (0.0 < den.toFloat())
rating = num.toFloat()/den.toFloat();
}
pginfo->stars.setNum(rating);
}
else if (info.tagName() == "rating")
{
// again, the structure of ratings seems poorly represented
// in the XML. no idea what we'd do with multiple values.
QDomNodeList values = info.elementsByTagName("value");
QDomElement item = values.item(0).toElement();
if (item.isNull())
continue;
EventRating rating;
rating.system = info.attribute("system", "");
rating.rating = getFirstText(item);
pginfo->ratings.append(rating);
}
else if (info.tagName() == "previously-shown")
{
pginfo->previouslyshown = true;
QString prevdate = info.attribute("start");
if (!prevdate.isEmpty())
{
QDateTime date;
fromXMLTVDate(prevdate, date,
localTimezoneOffset);
pginfo->originalairdate = date.date();
}
}
else if (info.tagName() == "credits")
{
parseCredits(info, pginfo);
}
else if (info.tagName() == "subtitles")
{
if (info.attribute("type") == "teletext")
pginfo->subtitleType |= SUB_NORMAL;
else if (info.attribute("type") == "onscreen")
pginfo->subtitleType |= SUB_ONSCREEN;
else if (info.attribute("type") == "deaf-signed")
pginfo->subtitleType |= SUB_SIGNED;
}
else if (info.tagName() == "audio")
{
parseAudio(info, pginfo);
}
else if (info.tagName() == "video")
{
parseVideo(info, pginfo);
}
else if (info.tagName() == "episode-num")
{
if (info.attribute("system") == "dd_progid")
{
QString episodenum(getFirstText(info));
// if this field includes a dot, strip it out
int idx = episodenum.indexOf('.');
if (idx != -1)
episodenum.remove(idx, 1);
pginfo->programId = episodenum;
dd_progid_done = 1;
}
else if (info.attribute("system") == "xmltv_ns")
{
int tmp;
QString episodenum(getFirstText(info));
episode = episodenum.section('.',1,1);
episode = episode.section('/',0,0).trimmed();
season = episodenum.section('.',0,0).trimmed();
QString part(episodenum.section('.',2,2));
QString partnumber(part.section('/',0,0).trimmed());
QString parttotal(part.section('/',1,1).trimmed());
pginfo->categoryType = kCategorySeries;
if (!episode.isEmpty())
{
tmp = episode.toInt() + 1;
episode = QString::number(tmp);
pginfo->syndicatedepisodenumber = QString('E' + episode);
}
if (!season.isEmpty())
{
tmp = season.toInt() + 1;
season = QString::number(tmp);
pginfo->syndicatedepisodenumber.append(QString('S' + season));
}
uint partno = 0;
if (!partnumber.isEmpty())
{
bool ok;
partno = partnumber.toUInt(&ok) + 1;
partno = (ok) ? partno : 0;
}
if (!parttotal.isEmpty() && partno > 0)
{
bool ok;
uint partto = parttotal.toUInt(&ok) + 1;
if (ok && partnumber <= parttotal)
{
pginfo->parttotal = partto;
pginfo->partnumber = partno;
}
}
}
else if (info.attribute("system") == "onscreen" &&
pginfo->subtitle.isEmpty())
{
pginfo->categoryType = kCategorySeries;
pginfo->subtitle = getFirstText(info);
}
}
}
}
if (pginfo->category.isEmpty() && pginfo->categoryType != kCategoryNone)
pginfo->category = myth_category_type_to_string(pginfo->categoryType);
if (!pginfo->airdate)
pginfo->airdate = current_year;
/* Let's build ourself a programid */
QString programid;
if (kCategoryMovie == pginfo->categoryType)
programid = "MV";
else if (kCategorySeries == pginfo->categoryType)
programid = "EP";
else if (kCategorySports == pginfo->categoryType)
programid = "SP";
else
programid = "SH";
if (!uniqueid.isEmpty()) // we already have a unique id ready for use
programid.append(uniqueid);
else
{
QString seriesid = QString::number(ELFHash(pginfo->title.toLocal8Bit()
.constData()));
pginfo->seriesId = seriesid;
programid.append(seriesid);
if (!episode.isEmpty() && !season.isEmpty())
{
/* Append unpadded episode and season number to the seriesid (to
maintain consistency with historical encoding), but limit the
season number representation to a single base-36 character to
ensure unique programid generation. */
int season_int = season.toInt();
if (season_int > 35)
{
// Cannot represent season as a single base-36 character, so
// remove the programid and fall back to normal dup matching.
if (kCategoryMovie != pginfo->categoryType)
programid.clear();
}
else
{
programid.append(episode);
programid.append(QString::number(season_int, 36));
if (pginfo->partnumber && pginfo->parttotal)
{
programid += QString::number(pginfo->partnumber);
programid += QString::number(pginfo->parttotal);
}
}
}
else
{
/* No ep/season info? Well then remove the programid and rely on
normal dupchecking methods instead. */
if (kCategoryMovie != pginfo->categoryType)
programid.clear();
}
}
if (dd_progid_done == 0)
pginfo->programId = programid;
return pginfo;
}
bool XMLTVParser::parseFile(
QString filename, QList<ChanInfo> *chanlist,
QMap<QString, QList<ProgInfo> > *proglist)
{
QDomDocument doc;
QFile f;
if (!dash_open(f, filename, QIODevice::ReadOnly))
{
LOG(VB_GENERAL, LOG_ERR,
QString("Error unable to open '%1' for reading.") .arg(filename));
return false;
}
QString errorMsg = "unknown";
int errorLine = 0;
int errorColumn = 0;
if (!doc.setContent(&f, &errorMsg, &errorLine, &errorColumn))
{
LOG(VB_GENERAL, LOG_ERR, QString("Error in %1:%2: %3")
.arg(errorLine).arg(errorColumn).arg(errorMsg));
f.close();
return true;
}
f.close();
// now we calculate the localTimezoneOffset, so that we can fix
// the programdata if needed
QString config_offset = gCoreContext->GetSetting("TimeOffset", "None");
// we disable this feature by setting it invalid (> 840min = 14hr)
int localTimezoneOffset = 841;
if (config_offset == "Auto")
{
// we mark auto with the -ve of the disable magic number
localTimezoneOffset = -841;
}
else if (config_offset != "None")
{
localTimezoneOffset = TimezoneToInt(config_offset);
if (abs(localTimezoneOffset) > 840)
{
LOG(VB_XMLTV, LOG_ERR, QString("Ignoring invalid TimeOffset %1")
.arg(config_offset));
localTimezoneOffset = 841;
}
}
QDomElement docElem = doc.documentElement();
QUrl baseUrl(docElem.attribute("source-data-url", ""));
QUrl sourceUrl(docElem.attribute("source-info-url", ""));
if (sourceUrl.toString() == "http://labs.zap2it.com/")
{
LOG(VB_GENERAL, LOG_ERR, "Don't use tv_grab_na_dd, use the"
"internal datadirect grabber.");
exit(GENERIC_EXIT_SETUP_ERROR);
}
QString aggregatedTitle;
QString aggregatedDesc;
QString groupingTitle;
QString groupingDesc;
QDomNode n = docElem.firstChild();
while (!n.isNull())
{
QDomElement e = n.toElement();
if (!e.isNull())
{
if (e.tagName() == "channel")
{
ChanInfo *chinfo = parseChannel(e, baseUrl);
chanlist->push_back(*chinfo);
delete chinfo;
}
else if (e.tagName() == "programme")
{
ProgInfo *pginfo = parseProgram(e, localTimezoneOffset);
if (pginfo->startts == pginfo->endts)
{
/* Not a real program : just a grouping marker */
if (!pginfo->title.isEmpty())
groupingTitle = pginfo->title + " : ";
if (!pginfo->description.isEmpty())
groupingDesc = pginfo->description + " : ";
}
else
{
if (pginfo->clumpidx.isEmpty())
{
if (!groupingTitle.isEmpty())
{
pginfo->title.prepend(groupingTitle);
groupingTitle.clear();
}
if (!groupingDesc.isEmpty())
{
pginfo->description.prepend(groupingDesc);
groupingDesc.clear();
}
(*proglist)[pginfo->channel].push_back(*pginfo);
}
else
{
/* append all titles/descriptions from one clump */
if (pginfo->clumpidx.toInt() == 0)
{
aggregatedTitle.clear();
aggregatedDesc.clear();
}
if (!pginfo->title.isEmpty())
{
if (!aggregatedTitle.isEmpty())
aggregatedTitle.append(" | ");
aggregatedTitle.append(pginfo->title);
}
if (!pginfo->description.isEmpty())
{
if (!aggregatedDesc.isEmpty())
aggregatedDesc.append(" | ");
aggregatedDesc.append(pginfo->description);
}
if (pginfo->clumpidx.toInt() ==
pginfo->clumpmax.toInt() - 1)
{
pginfo->title = aggregatedTitle;
pginfo->description = aggregatedDesc;
(*proglist)[pginfo->channel].push_back(*pginfo);
}
}
}
delete pginfo;
}
}
n = n.nextSibling();
}
return true;
}