Merge branch 'master' of git+ssh://aholst@scm.schwerkraft.elitedvb.net/scmrepos/git...
[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 <fcntl.h>
10
11 static void SaveEIT(const char *ref, const char *filename, int  eit_event_id, time_t begTime, time_t endTime)
12 {
13         eEPGCache::getInstance()->Lock();
14         const eit_event_struct *event = 0;
15         eServiceReference mref = eServiceReference(ref);
16         std::string sref = ref;
17         if ( eit_event_id != -1 )
18         {
19                 eDebug("[EITSave] query epg event id %d, %s", eit_event_id, sref.c_str());
20                 eEPGCache::getInstance()->lookupEventId(mref, eit_event_id, event);
21         }
22         
23         if ( !event && (begTime != -1 && endTime != -1) )
24         {
25                 time_t queryTime = begTime + ((endTime-begTime)/2);
26                 tm beg, end, query;
27                 localtime_r(&begTime, &beg);
28                 localtime_r(&endTime, &end);
29                 localtime_r(&queryTime, &query);
30                 eDebug("[EITSave] query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
31                         beg.tm_hour, beg.tm_min, beg.tm_sec,
32                         end.tm_hour, end.tm_min, end.tm_sec,
33                         query.tm_hour, query.tm_min, query.tm_sec);
34                 eEPGCache::getInstance()->lookupEventTime(mref, queryTime, event);
35         }
36         if ( event )
37         {
38                 eDebug("[EITSave] found event.. store to disc");
39                 std::string fname = filename;
40                 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
41                 if (fd>-1)
42                 {
43                         int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
44                         int wr = ::write( fd, (unsigned char*)event, evLen );
45                         if ( wr != evLen )
46                                 eDebug("eit write error (%m)");
47                         ::close(fd);
48                 }
49         }
50         else
51                 eDebug("[EITSave] no event found...");
52         eEPGCache::getInstance()->Unlock();
53 }
54
55 extern "C" {
56
57
58 static PyObject *
59 SaveEIT(PyObject *self, PyObject *args)
60 {
61         char* var1;
62         char* var2;
63         int var3;
64         time_t var4;
65         time_t var5;
66         if (PyTuple_Size(args) != 5 || !PyArg_ParseTuple(args, "ssiii", &var1, &var2, &var3, &var4, &var5))
67                 Py_RETURN_NONE;
68         else
69                 SaveEIT(var1,var2, var3, var4, var5);
70         Py_RETURN_NONE;
71 }
72
73
74 static PyMethodDef module_methods[] = {
75         {"SaveEIT", (PyCFunction)SaveEIT,  METH_VARARGS,
76          "SaveEIT"
77         },
78         {NULL, NULL, 0, NULL} 
79 };
80
81 PyMODINIT_FUNC
82 initeitsave(void)
83 {
84         Py_InitModule3("eitsave", module_methods,
85                 "EIT Saver");
86 }
87 };
88