OSDN Git Service

add dist
[rec10/rec10-git.git] / dist / trunk / tstools / epgdump / epgdump.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <iconv.h>
7 #include <time.h>
8
9 #include "ts.h"
10 #include "sdt.h"
11 #include "eit.h"
12 #include "ts_ctl.h"
13
14 typedef         struct  _ContentTYPE{
15         char    *japanese ;
16         char    *english ;
17 }CONTENT_TYPE;
18
19 #define         CAT_COUNT               16
20 static  CONTENT_TYPE    ContentCatList[CAT_COUNT] = {
21         { "¥Ë¥å¡¼¥¹¡¦ÊóÆ»", "news" },
22         { "¥¹¥Ý¡¼¥Ä", "sports" },
23         { "¾ðÊó", "information" },
24         { "¥É¥é¥Þ", "drama" },
25         { "²»³Ú", "music" },
26         { "¥Ð¥é¥¨¥Æ¥£", "variety" },
27         { "±Ç²è", "cinema" },
28         { "¥¢¥Ë¥á¡¦Æû£", "anime" },
29         { "¥É¥­¥å¥á¥ó¥¿¥ê¡¼¡¦¶µÍÜ", "documentary" },
30         { "±é·à", "stage" },
31         { "¼ñÌ£¡¦¼ÂÍÑ", "hobby" },
32         { "Ê¡»ã", "etc" },                      //Ê¡»ã
33         { "ͽÈ÷", "etc" }, //ͽÈ÷
34         { "ͽÈ÷", "etc" }, //ͽÈ÷
35         { "ͽÈ÷", "etc" }, //ͽÈ÷
36         { "¤½¤Î¾", "etc" } //¤½¤Î¾
37 };
38
39 SVT_CONTROL     *svttop = NULL;
40 #define         SECCOUNT        4
41 char    title[1024];
42 char    subtitle[1024];
43 char    desc[102400] = {0};
44 char    Category[1024];
45 char    ServiceName[1024];
46 iconv_t cd ;
47
48 void    xmlspecialchars(char *str)
49 {
50         strrep(str, "&", "&amp;");
51         strrep(str, "'", "&apos;");
52         strrep(str, "\"", "&quot;");
53         strrep(str, "<", "&lt;");
54         strrep(str, ">", "&gt;");
55 }
56
57
58
59 void    GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs, int count)
60 {
61         SECcache  *bsecs;
62
63         while((bsecs = readTS(infile, secs, count)) != NULL) {
64                 /* SDT */
65                 if((bsecs->pid & 0xFF) == 0x11) {
66                         dumpSDT(bsecs->buf, svttop);
67                 }
68         }
69 }
70 void    GetSDT_chout(FILE *infile, SVT_CONTROL *svttop, SECcache *secs, int count,STATION *station, int * station_count,char *header)
71 {
72         SECcache  *bsecs;
73
74         while((bsecs = readTS(infile, secs, count)) != NULL) {
75                 /* SDT */
76                 if((bsecs->pid & 0xFF) == 0x11) {
77                         dumpSDT_chout(bsecs->buf,svttop,station,station_count,header);
78                 }
79         }
80 }
81 void    GetEIT(FILE *infile, FILE *outfile, STATION *psta, SECcache *secs, int count)
82 {
83         SECcache  *bsecs;
84         EIT_CONTROL     *eitcur ;
85         EIT_CONTROL     *eitnext ;
86         EIT_CONTROL     *eittop = NULL;
87         char    *outptr ;
88         char    *inptr ;
89         size_t  ilen;
90         size_t  olen;
91         time_t  l_time ;
92         time_t  end_time ;
93         struct  tm      tl ;
94         struct  tm      *endtl ;
95         char    cendtime[32];
96         char    cstarttime[32];
97
98         eittop = calloc(1, sizeof(EIT_CONTROL));
99         eitcur = eittop ;
100         fseek(infile, 0, SEEK_SET);
101         while((bsecs = readTS(infile, secs, SECCOUNT)) != NULL) {
102                 /* EIT */
103                 if((bsecs->pid & 0xFF) == 0x12) {
104                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
105                 }else if((bsecs->pid & 0xFF) == 0x26) {
106                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
107                 }else if((bsecs->pid & 0xFF) == 0x27) {
108                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
109                 }
110         }
111         eitcur = eittop ;
112         while(eitcur != NULL){
113                 if(!eitcur->servid){
114                         eitcur = eitcur->next ;
115                         continue ;
116                 }
117                 if(eitcur->content_type > CAT_COUNT){
118                         eitcur->content_type = CAT_COUNT -1 ;
119                 }
120                 outptr = title ;
121                 memset(title, '\0', sizeof(title));
122                 ilen = strlen(eitcur->title);
123                 olen = sizeof(title);
124                 inptr = eitcur->title;
125                 iconv(cd, &inptr, &ilen, &outptr, &olen);
126                 xmlspecialchars(title);
127
128                 memset(subtitle, '\0', sizeof(subtitle));
129                 ilen = strlen(eitcur->subtitle);
130                 olen = sizeof(subtitle);
131                 outptr = subtitle ;
132                 inptr = eitcur->subtitle;
133                 iconv(cd, &inptr, &ilen, &outptr, &olen);
134                 xmlspecialchars(subtitle);
135
136                 memset(desc, '\0', sizeof(desc));
137                 if ( eitcur->desc ) {
138                         ilen = strlen(eitcur->desc);
139                         olen = sizeof(desc);
140                         outptr = desc ;
141                         inptr = eitcur->desc;
142                         iconv(cd, &inptr, &ilen, &outptr, &olen);
143                         xmlspecialchars(desc);
144                 }
145
146                 memset(Category, '\0', sizeof(Category));
147                 ilen = strlen(ContentCatList[eitcur->content_type].japanese);
148                 olen = sizeof(Category);
149                 outptr = Category ;
150                 inptr = ContentCatList[eitcur->content_type].japanese;
151                 iconv(cd, &inptr, &ilen, &outptr, &olen);
152                 xmlspecialchars(Category);
153
154                 tl.tm_sec = eitcur->ss ;
155                 tl.tm_min = eitcur->hm ;
156                 tl.tm_hour = eitcur->hh ;
157                 tl.tm_mday = eitcur->dd ;
158                 tl.tm_mon = (eitcur->mm - 1);
159                 tl.tm_year = (eitcur->yy - 1900);
160                 tl.tm_wday = 0;
161                 tl.tm_isdst = 0;
162                 tl.tm_yday = 0;
163                 l_time = mktime(&tl);
164                 if((eitcur->ehh == 0) && (eitcur->emm == 0) && (eitcur->ess == 0)){
165                         (void)time(&l_time);
166                         end_time = l_time + (60 * 5);           // £µÊ¬¸å¤ËÀßÄê
167                 endtl = localtime(&end_time);
168                 }else{
169                         end_time = l_time + eitcur->ehh * 3600 + eitcur->emm * 60 + eitcur->ess;
170                         endtl = localtime(&end_time);
171                 }
172                 memset(cendtime, '\0', sizeof(cendtime));
173                 memset(cstarttime, '\0', sizeof(cstarttime));
174                 strftime(cendtime, (sizeof(cendtime) - 1), "%Y%m%d%H%M%S", endtl);
175                 strftime(cstarttime, (sizeof(cstarttime) - 1), "%Y%m%d%H%M%S", &tl);
176 #if 1
177                 fprintf(outfile, "  <programme start=\"%s +0900\" stop=\"%s +0900\" channel=\"%s\">\n", 
178                                 cstarttime, cendtime, psta->ontv);
179                 fprintf(outfile, "    <title lang=\"ja_JP\">%s</title>\n", title);
180                 fprintf(outfile, "    <desc lang=\"ja_JP\">%s</desc>\n", subtitle);
181                 fprintf(outfile, "    <longdesc lang=\"ja_JP\">%s</longdesc>\n", desc);
182                 fprintf(outfile, "    <category lang=\"ja_JP\">%s</category>\n", Category);
183 //              fprintf(outfile, "    <category lang=\"en\">%s</category>\n", ContentCatList[eitcur->content_type].english);
184                 fprintf(outfile, "  </programme>\n");
185 #else
186                 fprintf(outfile, "(%x:%x:%x)%s,%s,%s,%s,%s,%s\n",
187                                         eitcur->servid, eitcur->table_id, eitcur->event_id,
188                                         cstarttime, cendtime,
189                                         title, subtitle,
190                                         Category,
191                                         ContentCatList[eitcur->content_type].english);
192 #endif
193 #if 0
194                 fprintf(outfile, "(%x:%x)%04d/%02d/%02d,%02d:%02d:%02d,%02d:%02d:%02d,%s,%s,%s,%s\n",
195                                         eitcur->table_id, eitcur->event_id,
196                                         eitcur->yy, eitcur->mm, eitcur->dd,
197                                         eitcur->hh, eitcur->hm, eitcur->ss,
198                                         eitcur->ehh, eitcur->emm, eitcur->ess,
199                                         eitcur->title, eitcur->subtitle,
200                                         ContentCatList[eitcur->content_type].japanese,
201                                         ContentCatList[eitcur->content_type].english);
202 #endif
203                 eitnext = eitcur->next ;
204                 free(eitcur->title);
205                 free(eitcur->subtitle);
206                 free(eitcur);
207                 eitcur = eitnext ;
208         }
209         free(eittop);
210         eittop = NULL;
211 }
212 void checkSta_BS(STATION **station,int *stalength){
213         STATION *statmp;
214         int chl[90];
215         int chlt=0;
216         int stal=0;
217         STATION * statin= *station;
218         statmp=malloc(sizeof(STATION)*2);
219         for (int i=0;i<*stalength;i++){
220                 int noidinchl=1;
221                 for (int j=0;j<chlt;j++){
222                         if (chl[j]==statin[i].svId || statin[i].svId >= 290){
223                                 noidinchl=0;
224                         }
225                 }
226                 if (noidinchl==1){
227                         statmp=realloc(statmp,(stal+1)*sizeof(STATION));
228                         statmp[stal]=statin[i];
229                         //memcpy(statmp[stal-1],station[i],sizeof(STATION));
230                         chl[chlt]=statin[i].svId;
231                         chlt++;
232                         stal++;
233                 }
234         }
235         //*station=realloc(station,(stal+1)*sizeof(STATION));
236         //memcpy(*station,statmp,(stal+1)*sizeof(STATION));
237         *station=statmp;
238         *stalength=stal;
239         //memcpy(statin,statmp,chlt*sizeof(STATION));
240         //free(statmp);
241         return;
242 }
243
244 void checkSta(STATION **station,int *stalength){
245         STATION *statmp;
246         int chl[90];
247         int chlt=0;
248         int stal=0;
249         STATION * statin= *station;
250         statmp=malloc(sizeof(STATION)*2);
251         for (int i=0;i<*stalength;i++){
252                 int noidinchl=1;
253                 for (int j=0;j<chlt;j++){
254                         if (chl[j]==statin[i].svId){
255                                 noidinchl=0;
256                         }
257                 }
258                 if (noidinchl==1){
259                         statmp=realloc(statmp,(stal+1)*sizeof(STATION));
260                         statmp[stal]=statin[i];
261                         //memcpy(statmp[stal-1],station[i],sizeof(STATION));
262                         chl[chlt]=statin[i].svId;
263                         chlt++;
264                         stal++;
265                 }       
266         }
267         sleep(1);
268         *station=statmp;
269         *stalength=stal;//¤³¤³¤¤¤é¤¬²ø¤·¤¤
270         //memcpy(statin,statmp,chlt*sizeof(STATION));
271         //free(statmp);
272         return;
273 }
274 int main(int argc, char *argv[])
275 {
276
277         FILE *infile = stdin;
278         FILE *outfile = stdout;
279         int             arg_maxcount = 1 ;
280         char    *arg_onTV ;
281         int   mode = 1;
282         int             staCount ;
283         int   eitcnt;
284         char *file;
285         int   inclose = 0;
286         int   outclose = 0;
287         int             flag = 0 ;
288         int     outputhtml = 1;
289         SVT_CONTROL     *svtcur ;
290         SVT_CONTROL     *svtsave ;
291         char    *outptr ;
292         char    *inptr ;
293         size_t  ilen;
294         size_t  olen;
295         SECcache   secs[SECCOUNT];
296         int rtn;
297         int             lp ;
298         STATION *pStas ;
299         int             act ;
300
301         /* ¶½Ì£¤Î¤¢¤ëpid¤ò»ØÄê */
302         memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
303         secs[0].pid = 0x11;
304         secs[1].pid = 0x12;
305         secs[2].pid = 0x26;
306         secs[3].pid = 0x27;
307
308         if(argc == 4){
309                 arg_onTV = argv[1];
310                 file = argv[2];
311                 if(strcmp(file, "-")) {
312                         infile = fopen(file, "r");
313                         inclose = 1;
314                 }
315                 if(strcmp(argv[3], "-")) {
316                         outfile = fopen(argv[3], "w+");
317                         outclose = 1;
318                 }
319         }else{
320                 fprintf(stdout, "Usage : %s /BS <tsFile> <outfile>\n", argv[0]);
321                 fprintf(stdout, "Usage : %s <ontvcode> <tsFile> <outfile>\n", argv[0]);
322                 fprintf(stdout, "ontvcode ¥Á¥ã¥ó¥Í¥ë¼±Ê̻ҡ£****.ontvjapan.com ¤Ê¤É\n");
323                 fprintf(stdout, "/BS      BS¥â¡¼¥É¡£°ì¤Ä¤ÎTS¤«¤éBSÁ´¶É¤Î¥Ç¡¼¥¿¤òÆɤ߹þ¤ß¤Þ¤¹¡£\n");
324                 fprintf(stdout, "/CS      CS¥â¡¼¥É¡£°ì¤Ä¤ÎTS¤«¤éÊ£¿ô¶É¤Î¥Ç¡¼¥¿¤òÆɤ߹þ¤ß¤Þ¤¹¡£\n");
325                 return 0;
326         }
327
328         if(strcmp(arg_onTV, "/BS") == 0){
329                 STATION *sta=malloc(sizeof(STATION)*2);
330                 int sta_count = 0;
331                 svttop = calloc(1, sizeof(SVT_CONTROL));
332                 char *head="BS";
333                 GetSDT_chout(infile, svttop, secs, SECCOUNT,&sta, &sta_count,head);
334                 checkSta_BS(&sta,&sta_count);
335                 //if (sta_count) 
336                 //printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
337                 pStas = sta;
338                 staCount = sta_count;// sizeof(pStas) / sizeof (STATION);
339                 act = 0 ;
340                 outputhtml=1;
341         }else if(strcmp(arg_onTV, "/CS") == 0){
342                 STATION *sta=NULL;
343                 int sta_count = 0;
344                 svttop = calloc(1, sizeof(SVT_CONTROL));
345                 char *head="CS";
346                 GetSDT_chout(infile, svttop, secs, SECCOUNT,&sta, &sta_count,head);
347                 checkSta(&sta,&sta_count);
348                 //if (sta_count) 
349                 //printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
350                 pStas = sta;
351                 staCount = sta_count;
352                 //staCount=sizeof(pStas) / sizeof (STATION);
353                 act = 0 ;
354                 outputhtml=1;
355         }else if(strcmp(arg_onTV, "/TEST") == 0){
356                 STATION *sta=NULL;
357                 int sta_count = 0;
358                 svttop = calloc(1, sizeof(SVT_CONTROL));
359                 char *head="TEST";
360                 GetSDT_chout(infile, svttop, secs, SECCOUNT,&sta, &sta_count,head);
361                 checkSta(&sta,&sta_count);
362                 //if (sta_count) 
363                 //printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
364                 pStas = sta;
365                 staCount = sta_count;// sizeof(pStas) / sizeof (STATION);
366                 act = 0 ;
367                 outputhtml=1;
368         }else{
369                 /*act = 1 ;
370                 svttop = calloc(1, sizeof(SVT_CONTROL));
371                 GetSDT(infile, svttop, secs, SECCOUNT);
372                 svtcur = svttop->next ; //ÀèƬ
373                 if(svtcur == NULL){
374                         free(svttop);
375                         return ;
376                 }
377
378                 pStas = calloc(1, sizeof(STATION));
379                 pStas->tsId = svtcur->transport_stream_id ;
380                 pStas->onId = svtcur->original_network_id ;
381                 pStas->svId = svtcur->event_id ;
382                 pStas->ontv = arg_onTV ;
383                 pStas->name = svtcur->servicename ;
384                 staCount = 1;*/
385                 ///Êѹ¹¸å
386                 STATION *sta=NULL;
387                 int sta_count = 0;
388                 svttop = calloc(1, sizeof(SVT_CONTROL));
389                 //char *head=arg_onTV;
390                 GetSDT_chout(infile, svttop, secs, SECCOUNT,&sta, &sta_count,arg_onTV);
391                 checkSta(&sta,&sta_count);
392                 //if (sta_count) 
393                 //printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
394                 pStas = sta;
395                 staCount = sta_count;// sizeof(pStas) / sizeof (STATION);
396                 act = 0 ;
397                 outputhtml=1;
398         }
399         if (outputhtml == 1){
400                 fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
401                 fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
402                 fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
403
404                 cd = iconv_open("UTF-8", "EUC-JP");
405                 for(lp = 0 ; lp < staCount ; lp++){
406                         memset(ServiceName, '\0', sizeof(ServiceName));
407                         ilen = strlen(pStas[lp].name);
408                         olen = sizeof(ServiceName);
409                         outptr = ServiceName ;
410                         inptr = pStas[lp].name ;
411                         iconv(cd, &inptr, &ilen, &outptr, &olen);
412                         xmlspecialchars(ServiceName);
413
414                         fprintf(outfile, "  <channel id=\"%s\">\n", pStas[lp].ontv);
415                         fprintf(outfile, "    <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
416                         fprintf(outfile, "  </channel>\n");
417                 }
418                 for(lp = 0 ; lp < staCount ; lp++){
419                         GetEIT(infile, outfile, &pStas[lp], secs, SECCOUNT);
420                 }
421                 fprintf(outfile, "</tv>\n");
422                 if(inclose) {
423                         fclose(infile);
424                 }
425
426                 if(outclose) {
427                         fclose(outfile);
428                 }
429                 iconv_close(cd);
430                 if(act){
431                         free(pStas);
432                         svtcur = svttop ;       //ÀèƬ
433                         while(svtcur != NULL){
434                                 svtsave = svtcur->next ;
435                                 free(svtcur);
436                                 svtcur = svtsave ;
437                         }
438                 }
439         }else{
440                 
441         }
442         return 0;
443 }