forked from 3PO/epgd-plugin-tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvm.c
793 lines (593 loc) · 21.4 KB
/
tvm.c
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
* tvm.c
*
* See the README file for copyright information
*
*/
#include <stdint.h>
#include <dirent.h>
#include <map>
#include "tvm.h"
//***************************************************************************
// Tvm
//***************************************************************************
Tvm2::Tvm2()
: Plugin()
{
pxsltStylesheet = 0;
stmtMarkOldEvents = 0;
stmtSetUpdFlgByFileref = 0;
selectDistBySource = 0;
selectId = 0;
valueFileRef = 0;
// config
timeout = 30;
}
Tvm2::~Tvm2()
{
delete stmtMarkOldEvents;
delete stmtSetUpdFlgByFileref;
delete selectDistBySource;
delete valueFileRef;
if (pxsltStylesheet)
xsltFreeStylesheet(pxsltStylesheet);
}
int Tvm2::init(cEpgd* aObject, int aUtf8)
{
Plugin::init(aObject, aUtf8);
pxsltStylesheet = loadXSLT("tvmovie", confDir, utf8);
return done;
}
int Tvm2::initDb()
{
int status = success;
// // --------
// // update events set updflg = ?
// // where fileref = ?
// // and source = ?
// // and updflg in (....)
// stmtSetUpdFlgByFileref = new cDbStatement(obj->eventsDb);
// stmtSetUpdFlgByFileref->build("update %s set ", obj->eventsDb->TableName());
// stmtSetUpdFlgByFileref->bind("UpdFlg", cDbService::bndIn |cDbService:: bndSet);
// stmtSetUpdFlgByFileref->build( " where ");
// stmtSetUpdFlgByFileref->bind(valueFileRef, cDbService::bndIn |cDbService:: bndSet);
// stmtSetUpdFlgByFileref->bind("Source", cDbService::bndIn | cDbService::bndSet, " and ");
// stmtSetUpdFlgByFileref->build(" and updflg in (%s)", Us::getDeletable());
// status += stmtSetUpdFlgByFileref->prepare();
// // --------
// // update events set delflg = ?, fileref = ?, updsp = ?
// // where fileref = ?
// // and source = ?
// valueFileRef = new cDbValue(obj->eventsDb->getField("FileRef"));
// stmtSetDelByFileref = new cDbStatement(obj->eventsDb);
// stmtSetDelByFileref->build("update %s set ", obj->eventsDb->TableName());
// stmtSetDelByFileref->bind("DelFlg", cDbService::bndIn |cDbService:: bndSet);
// stmtSetDelByFileref->bind("UpdFlg", cDbService::bndIn |cDbService:: bndSet, ", ");
// stmtSetDelByFileref->bind("FileRef", cDbService::bndIn | cDbService::bndSet, ", ");
// stmtSetDelByFileref->bind("UpdSp", cDbService::bndIn | cDbService::bndSet, ", ");
// stmtSetDelByFileref->build( " where ");
// stmtSetDelByFileref->bind(valueFileRef, cDbService::bndIn |cDbService:: bndSet);
// stmtSetDelByFileref->bind("Source", cDbService::bndIn | cDbService::bndSet, " and ");
// status += stmtSetDelByFileref->prepare();
// ----------
// update events
// set updflg = case when updflg in (...) then 'D' else updflg end,
// delflg = 'Y',
// updsp = unix_timestamp()
// where source = '...'
// and (source, fileref) not in (select source,fileref from fileref)
stmtMarkOldEvents = new cDbStatement(obj->eventsDb);
stmtMarkOldEvents->build("update %s set ", obj->eventsDb->TableName());
stmtMarkOldEvents->build("updflg = case when updflg in (%s) then 'D' else updflg end, ", cEventState::getDeletable());
stmtMarkOldEvents->build("delflg = 'Y', updsp = unix_timestamp()");
stmtMarkOldEvents->build(" where source = '%s'", getSource());
stmtMarkOldEvents->build(" and (source, fileref) not in (select source,fileref from fileref)");
status += stmtMarkOldEvents->prepare();
// ---------
// select channelid, mergesp from channelmap
// where source = ? and extid = ?
selectId = new cDbStatement(obj->mapDb);
selectId->build("select ");
selectId->bind("ChannelId", cDBS::bndOut);
selectId->bind("MergeSp", cDBS::bndOut, ", ");
selectId->bind("Merge", cDBS::bndOut, ", ");
selectId->build(" from %s where ", obj->mapDb->TableName());
selectId->bind("Source", cDBS::bndIn | cDBS::bndSet);
selectId->bind("ExternalId", cDBS::bndIn | cDBS::bndSet, " and ");
status += selectId->prepare();
// --------
// select distinct extid from channelmap
// where source = ?
selectDistBySource = new cDbStatement(obj->mapDb);
selectDistBySource->build("select ");
selectDistBySource->bind("ExternalId", cDBS::bndOut, "distinct ");
selectDistBySource->build(" from %s where ", obj->mapDb->TableName());
selectDistBySource->bind("Source", cDBS::bndIn | cDBS::bndSet);
status += selectDistBySource->prepare();
return status;
}
int Tvm2::exitDb()
{
delete valueFileRef; valueFileRef = 0;
delete stmtMarkOldEvents; stmtMarkOldEvents = 0;
delete stmtSetUpdFlgByFileref; stmtSetUpdFlgByFileref = 0;
delete selectDistBySource; selectDistBySource = 0;
delete selectId; selectId = 0;
return success;
}
//***************************************************************************
// At Config Item
//***************************************************************************
int Tvm2::atConfigItem(const char* Name, const char* Value)
{
if (!strcasecmp(Name, "Timeout"))
timeout = atoi(Value);
else
return fail;
return success;
}
//***************************************************************************
// Ready
//***************************************************************************
int Tvm2::ready()
{
static int count = na;
if (count == na)
{
char* where;
asprintf(&where, "source = '%s'", getSource());
if (obj->mapDb->countWhere(where, count) != success)
count = na;
free(where);
}
return count > 0;
}
//***************************************************************************
// Process
// - Datei von TVM holen (sofern aktualisiert, noch nicht geholt oder fullupdate)
// Erkennung der Aktualisierung erfolgt über die 'fileref' Tabelle
// - XSLT Konvertierung durchführen
// - Resultat je zugeordnetem Kanal in der 'events'
// Tabelle speichern/ aktualisieren incl. der 'fileref'
// - Neue 'fileref' in die 'fileref' Tabelle übernehmen
// -> damit ist die Verarbeitung quittiert
//***************************************************************************
int Tvm2::processDay(int day, int fullupdate, Statistic* stat)
{
int status;
MemoryStruct data;
if (day != 0)
{
tell(1, "Skipping day %d for TVM plugin, since all days ar performed on day 0", day);
return success;
}
obj->connection->startTransaction();
// loop over all extid's of channelmap
obj->mapDb->clear();
obj->mapDb->setValue("Source", getSource());
for (int res = selectDistBySource->find(); res && !obj->doShutDown();
res = selectDistBySource->fetch())
{
char* extid = strdup(obj->mapDb->getStrValue("ExternalId"));
int found;
char* url;
char* filename; // the file to be downloaded
int fileSize = 0;
tell(0, "Checking tvm id %s", extid);
// URL - Aufbau
// http://www.clickfinder.de/daten/onlinedata/cftv520/tvdaten-premium-EXTID.cftv
asprintf(&filename, "tvdaten-premium-%s.cftv", extid);
asprintf(&url, "http://www.clickfinder.de/daten/onlinedata/cftv520/%s", filename);
// lookup file information
obj->fileDb->setValue("Name", filename);
obj->fileDb->setValue("Source", getSource());
found = obj->fileDb->find();
// Don't check the day or the EPG2VDRConfig.upddays
// since TVM deliver all days in one file ...
char* fileRef = 0;
// first get the http header
data.clear();
data.headerOnly = yes;
status = obj->downloadFile(url, fileSize, &data, 30, userAgent());
if (status != success)
{
tell(0, "Download header of '%s' failed", url);
free(filename);
free(url);
obj->fileDb->reset();
free(extid);
continue;
}
asprintf(&fileRef, "%s-%s", filename, data.tag);
if (found && obj->fileDb->hasValue("FileRef", fileRef))
{
tell(2, "Skipping download of id %s due to non-update", extid);
stat->nonUpdates++;
free(filename);
free(url);
free(fileRef);
obj->fileDb->reset();
free(extid);
continue;
}
data.clear();
if (obj->downloadFile(url, fileSize, &data, timeout, userAgent()) != success || !zipValid(&data))
{
if (!zipValid(&data))
tell(1, "Error, zip file '%s' not valid, check the coresponding tvm id in youre configuration", url);
else
tell(1, "Error, download of '%s' failed", url);
free(filename);
free(url);
free(fileRef);
obj->fileDb->reset();
free(extid);
continue;
}
free(fileRef);
downloadImageFile(extid);
if (fileSize > 0)
{
char* fileRef = 0;
stat->bytes += fileSize;
stat->files++;
tell(0, "Downloaded file '%s' with (%d) Bytes", url, fileSize);
// store to FS
{
char* tmp = 0;
asprintf(&tmp, "%s.zip", filename);
obj->storeToFs(&data, tmp, getSource());
free(tmp);
}
asprintf(&fileRef, "%s-%s", obj->fileDb->getStrValue("Name"), data.tag);
if ((status = processFile(extid, &data, filename, fileRef)) != success)
{
tell(0, "Processing of '%s' failed", filename);
status = fail;
stat->rejected++;
}
if (status == success)
{
// we can use this code instead of "stmtMarkOldEvents" !!
// if (found)
// {
// // mark 'old' entrys in events table as deleted
// // and 'fake' fileref to new to avoid deletion at cleanup
// // first - set updflg to 'D' (only ift updflg in Us::getDeletable())
// obj->eventsDb->clear();
// obj->eventsDb->setValue("UpdFlg", "D");
// // where ..
// obj->eventsDb->setValue("Source", getSource());
// valueFileRef->setValue(obj->fileDb->getStrValue("FileRef")); // old fileref
// stmtSetUpdFlgByFileref->execute();
// // second - set delflg and fileref ..
// obj->eventsDb->clear();
// obj->eventsDb->setValue("DelFlg", "Y");
// obj->eventsDb->setValue("FileRef", fileRef); // new fileref
// obj->eventsDb->setValue("UpdSp", time(0));
// // where ..
// obj->eventsDb->setValue("Source", getSource());
// valueFileRef->setValue(obj->fileDb->getStrValue("FileRef")); // old fileref
// stmtSetDelByFileref->execute();
// }
// Confirm processing of file
obj->fileDb->setValue("ExternalId", extid);
obj->fileDb->setValue("Tag", data.tag);
obj->fileDb->setValue("FileRef", fileRef);
obj->fileDb->store();
obj->connection->commit();
usleep(100000);
obj->connection->startTransaction();
}
free(fileRef);
}
obj->fileDb->reset();
free(url);
free(filename);
free(extid);
data.clear();
if (!obj->dbConnected())
return fail;
}
selectDistBySource->freeResult();
obj->connection->commit();
return success;
}
//***************************************************************************
// Zip Valid
//***************************************************************************
int Tvm2::zipValid(MemoryStruct* data)
{
char head[100+TB];
// check content
if (!data || !data->memory)
return no;
strncpy(head, data->memory, 100);
head[100] = 0;
if (strcasestr(head, "DOCTYPE HTML"))
return no;
return yes;
}
//***************************************************************************
// Process Tvm File
//***************************************************************************
int Tvm2::processFile(const char* extid, MemoryStruct* data,
const char* fileName, const char* fileRef)
{
xmlDocPtr transformedDoc;
xmlNodePtr xmlRoot;
MemoryStruct xmlData;
int count = 0;
char* command;
char password[7+TB];
char* tmp = 0;
memset(password, 0xb7, 7);
password[7] = 0;
tell(0, "Extracting '%s'", fileName);
asprintf(&command, "unzip -o -qq -P %s -d %s/%s %s/%s/%s",
password, EpgdConfig.cachePath, getSource(),
EpgdConfig.cachePath, getSource(), fileName);
asprintf(&tmp, "%s/%s/%s.tv1", EpgdConfig.cachePath, getSource(), extid);
if (system(command) < 0 || !fileExists(tmp))
{
tell(0, "Extracting of '%s' failed, missing at least '%s'", fileName, tmp);
free(tmp);
free(command);
return fail;
}
free(tmp);
free(command);
if (createXml(extid, &xmlData) != success)
return fail;
if ((transformedDoc = obj->transformXml(xmlData.memory, xmlData.size, pxsltStylesheet, fileName)) == 0)
{
xmlData.clear();
tell(0, "XSLT transformation for '%s' failed, ignoring", fileName);
return fail;
}
xmlData.clear();
if (!(xmlRoot = xmlDocGetRootElement(transformedDoc)))
{
tell(0, "Invalid xml document returned from xslt for '%s', ignoring", fileName);
return fail;
}
// process 'all' events for 'all' configured channles of this extid
obj->mapDb->clear();
obj->mapDb->setValue("ExternalId", extid);
obj->mapDb->setValue("Source", getSource());
for (int f = selectId->find(); f && obj->dbConnected(); f = selectId->fetch())
{
const char* channelId = obj->mapDb->getStrValue("ChannelId");
for (xmlNodePtr node = xmlRoot->xmlChildrenNode; node && obj->dbConnected(); node = node->next)
{
int insert;
char* prop = 0;
tEventId id;
// skip all unexpected elements
if (node->type != XML_ELEMENT_NODE || strcmp((char*)node->name, "event") != 0)
continue;
// get/check id
if (!(prop = (char*)xmlGetProp(node, (xmlChar*)"id")) || !*prop || !(id = atoi(prop)))
{
xmlFree(prop);
tell(0, "Missing event id, ignoring!");
continue;
}
xmlFree(prop);
// create event ..
obj->eventsDb->clear();
obj->eventsDb->setBigintValue("EventId", id);
obj->eventsDb->setValue("ChannelId", channelId);
insert = !obj->eventsDb->find();
obj->eventsDb->setValue("Source", getSource());
obj->eventsDb->setValue("FileRef", fileRef);
// auto parse and set other fields
obj->parseEvent(obj->eventsDb->getRow(), node);
// ...
time_t mergesp = obj->mapDb->getIntValue("MERGESP");
long starttime = obj->eventsDb->getIntValue("STARTTIME");
int merge = obj->mapDb->getIntValue("MERGE");
// store ..
if (insert)
{
// handle insert
obj->eventsDb->setValue("VERSION", 0xFF);
obj->eventsDb->setValue("TABLEID", 0L);
obj->eventsDb->setValue("USEID", 0L);
if (starttime <= mergesp)
obj->eventsDb->setCharValue("UPDFLG", cEventState::usInactive);
else
obj->eventsDb->setCharValue("UPDFLG", merge > 1 ? cEventState::usMergeSpare : cEventState::usActive);
obj->eventsDb->insert();
}
else
{
if (obj->eventsDb->hasValue("DELFLG", "Y"))
obj->eventsDb->setValue("DELFLG", "N");
if (obj->eventsDb->hasValue("UPDFLG", "D"))
{
if (starttime <= mergesp)
obj->eventsDb->setCharValue("UPDFLG", cEventState::usInactive);
else
obj->eventsDb->setCharValue("UPDFLG", merge > 1 ? cEventState::usMergeSpare : cEventState::usActive);
}
obj->eventsDb->update();
}
obj->eventsDb->reset();
count++;
}
}
selectId->freeResult();
xmlFreeDoc(transformedDoc);
tell(2, "XML File '%s' processed, updated %d events", fileName, count);
return success;
}
//***************************************************************************
// Cleanup After
//***************************************************************************
int Tvm2::cleanupAfter()
{
// mark wasted events (delflg, ...)
stmtMarkOldEvents->execute();
// remove remaining (unused) pictures from filesysten
DIR* dir;
dirent* dp;
char* path = 0;
asprintf(&path, "%s/%s", EpgdConfig.cachePath, getSource());
if (!(dir = opendir(path)))
{
tell(0, "Error: Opening plugin directory '%s' failed, %s",
path, strerror(errno));
return fail;
}
while ((dp = readdir(dir)))
{
if (strstr(dp->d_name, ".jpg"))
{
char* fileName;
asprintf(&fileName, "%s/%s/%s", EpgdConfig.cachePath, getSource(), dp->d_name);
removeFile(fileName);
free(fileName);
}
}
closedir(dir);
free(path);
return success;
}
//***************************************************************************
// Download Image File
//***************************************************************************
int Tvm2::downloadImageFile(const char* extid)
{
char* filename;
int fileSize = 0;
char* url;
MemoryStruct data;
char* command;
char password[7+TB];
int status;
asprintf(&filename, "tvbilder-premium-%s.cftv", extid);
asprintf(&url, "http://www.clickfinder.de/daten/onlinedata/cftv520/%s", filename);
if (obj->downloadFile(url, fileSize, &data, 30, userAgent()) != success)
{
tell(0, "Download at '%s' failed", url);
free(url);
free(filename);
return fail;
}
free(url);
status = obj->storeToFs(&data, filename, getSource());
memset(password, 0xb7, 7);
password[7] = 0;
asprintf(&command, "unzip -o -qq -P %s -d %s/%s %s/%s/%s",
password, EpgdConfig.cachePath, getSource(),
EpgdConfig.cachePath, getSource(), filename);
if (system(command) < 0)
{
tell(0, "Extracting of '%s' failed", filename);
free(command);
free(filename);
return fail;
}
// remove zipped file
char* tmp = 0;
asprintf(&tmp, "%s/%s/%s", EpgdConfig.cachePath, getSource(), filename);
removeFile(tmp);
free(tmp);
free(command);
free(filename);
return status;
}
//***************************************************************************
// Get Picture
//***************************************************************************
int Tvm2::getPicture(const char* imagename, const char* fileRef, MemoryStruct* data)
{
data->clear();
obj->loadFromFs(data, imagename, getSource());
// remove image after epgd has requested it (stored in table)
char* tmp = 0;
asprintf(&tmp, "%s/%s/%s", EpgdConfig.cachePath, getSource(), imagename);
removeFile(tmp);
free(tmp);
return data->size;
}
//***************************************************************************
// Create Xml
//***************************************************************************
int Tvm2::createXml(const char* extid, MemoryStruct* xmlData)
{
char* filename;
map<int, map<string, string> > table;
map<int, map<string, string> >::iterator iter;
tell(0, "Creating xml of %s", extid);
// -----------------------------------------------
// read data into memory table
for (int num = 0; num < 2; num++)
{
FILE* file;
int ch;
int row = 0, col = 0;
asprintf(&filename, "/%s/%s/%s.tv%d", EpgdConfig.cachePath, getSource(), extid, num+1);
if (!(file = fopen(filename, "r")))
{
tell(0, "Error: Opening '%s' failed, status was '%s'", filename, strerror(errno));
free(filename);
return fail;
}
fseek(file, 0, SEEK_SET);
for (int i = 0; i < 10; i++)
{
row *= 10;
row += fgetc(file) - '0';
}
while ((ch = fgetc(file)) != '\f')
{
col *= 10;
col += ch - '0';
}
fseek(file, 40, SEEK_SET); // seek to data section
for (int c = 0; c < col; c++)
{
Field header(file);
const char* name = header.getString().c_str();
for (int r = 0; r < row; r++)
{
Field field(file);
if (field.isNumeric())
table[r][name] = num2Str(field.getNumeric());
else if (field.isString())
table[r][name] = field.getXmlString();
}
}
fclose(file);
removeFile(filename);
free(filename);
}
// -----------------------------------------------
// flush data to xml file
string xmlBuf;
xmlBuf += "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
xmlBuf += "<!DOCTYPE Export>\n";
xmlBuf += "<Export>\n";
for (iter = table.begin(); iter != table.end(); iter++)
{
xmlBuf += " <Sendung>\n";
for (map<string, string>::iterator i = iter->second.begin(); i != iter->second.end(); i++)
xmlBuf += " <" + i->first + ">" + i->second + "</" + i->first + ">\n";
xmlBuf += " </Sendung>\n";
}
xmlBuf += "</Export>\n";
xmlData->size = xmlBuf.length()+TB;
xmlData->memory = (char*)malloc(xmlData->size);
strcpy(xmlData->memory, xmlBuf.c_str());
if (EpgdConfig.storeXmlToFs)
{
asprintf(&filename, "%s.xml", extid);
obj->storeToFs(xmlData, filename, getSource());
free(filename);
}
return success;
}
//***************************************************************************
extern "C" void* EPGPluginCreator () { return new Tvm2(); }