pts: fix and reenable eitsave module, use implemented logging helper
[enigma2-plugins.git] / permanenttimeshift / src / eitsave / eitsave.cpp
1 /*
2   eEITSave E2
3   (c) 2010 by Dr. Best  <dr.best@dreambox-tools.info>
4 */
5
6 using namespace std;
7 #include <lib/dvb/dvb.h>
8 #include <lib/dvb/epgcache.h>
9 #include <lib/service/event.h>
10 #include <fcntl.h>
11
12 static void SaveEIT(const char *ref, const char *filename, int  eit_event_id, time_t begTime, time_t endTime)
13 {
14         eEPGCache::getInstance()->Lock();
15         ePtr<eServiceEvent> event = 0;
16         eServiceReference mref = eServiceReference(ref);
17         std::string sref = ref;
18         if ( eit_event_id != -1 )
19         {
20                 eDebug("[EITSave] query epg event id %d, %s", eit_event_id, sref.c_str());
21                 eEPGCache::getInstance()->lookupEventId(mref, eit_event_id, event);
22         }
23         
24         if ( !event && (begTime != -1 && endTime != -1) )
25         {
26                 time_t queryTime = begTime + ((endTime-begTime)/2);
27                 tm beg, end, query;
28                 localtime_r(&begTime, &beg);
29                 localtime_r(&endTime, &end);
30                 localtime_r(&queryTime, &query);
31                 eDebug("[EITSave] query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
32                         beg.tm_hour, beg.tm_min, beg.tm_sec,
33                         end.tm_hour, end.tm_min, end.tm_sec,
34                         query.tm_hour, query.tm_min, query.tm_sec);
35                 eEPGCache::getInstance()->lookupEventTime(mref, queryTime, event);
36         }
37         if ( event )
38         {
39                 eDebug("[EITSave] found event.. store to disc");
40                 uint8_t eit_raw[4096];
41                 size_t eit_len = event->writeToEITBuffer(eit_raw);
42                 eDebug("found event.. store to disc");
43                 int fd = open(filename, O_CREAT | O_WRONLY | O_CLOEXEC, 0777);
44                 if(fd > -1)
45                         {
46                         ssize_t wr = ::write(fd, (unsigned char*)eit_raw, eit_len);
47                         if(wr != (ssize_t)eit_len)
48                                 eDebug("eit write error (%m)");
49                         ::close(fd);
50                 }
51         }
52         else
53                 eDebug("[EITSave] no event found...");
54         eEPGCache::getInstance()->Unlock();
55 }
56
57 extern "C" {
58
59
60 static PyObject *
61 SaveEIT(PyObject *self, PyObject *args)
62 {
63         char* var1;
64         char* var2;
65         int var3;
66         time_t var4;
67         time_t var5;
68         if (PyTuple_Size(args) != 5 || !PyArg_ParseTuple(args, "ssiii", &var1, &var2, &var3, &var4, &var5))
69                 Py_RETURN_NONE;
70         else
71                 SaveEIT(var1,var2, var3, var4, var5);
72         Py_RETURN_NONE;
73 }
74
75
76 static PyMethodDef module_methods[] = {
77         {"SaveEIT", (PyCFunction)SaveEIT,  METH_VARARGS,
78          "SaveEIT"
79         },
80         {NULL, NULL, 0, NULL} 
81 };
82
83 PyMODINIT_FUNC
84 initeitsave(void)
85 {
86         Py_InitModule3("eitsave", module_methods,
87                 "EIT Saver");
88 }
89 };
90