diff --git a/Makefile b/Makefile index 0d95e45..5626df3 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,10 @@ endif all: tags epg +install: epg + strip epg + cp epg /mod/bin/epg + epg: ${OBJS} @echo "Linking..." @-[ -f $@ ] && mv $@ $@~ || exit 0 @@ -42,7 +46,6 @@ epg: ${OBJS} ${CFLAGS} -o $@ \ ${OBJS} \ ${LIBS} - @strip epg @echo "Done..." clean: diff --git a/main.c b/main.c index 113b0f3..6963cb2 100644 --- a/main.c +++ b/main.c @@ -21,7 +21,7 @@ #include "lint.h" int debug = 0; -const char *version = "1.0.10"; +const char *version = "1.0.11"; unsigned long sysopts = 0; unsigned long filterflags = 0; static time_t latest_stamp = 0; @@ -68,6 +68,7 @@ syntax() " dump Show a parsed summary of the EPG.\n" " dumpraw Show raw data from the EPG.\n" " sqldump Produce SQL statements from EPG data.\n" + " json Produce JSON formatted EPG data.\n" #ifdef HAVE_SQLITE3 " sqlitedump Create SQLite database from EPG data.\n" #endif @@ -326,6 +327,103 @@ sqlitedump(struct epg *epg __attribute__((unused)), #endif /* HAVE_SQLITE3 */ +static int json_service_id = 0; + +void +jsonstart() +{ + printf("{\n"); +} + +void +jsonend() +{ + if (json_service_id) + printf("\n }\n"); + printf("}\n"); +} + +void +json(struct epg *epg __attribute__((unused)), + struct section *s, struct data *d, struct descriptor **ds, + void *var __attribute__((unused))) +{ + time_t tm, dur; + + tm = mjd(d->start_date, d->start_hour, d->start_min, d->start_sec); + dur = d->dur_hour * 3600 + d->dur_min * 60 + d->dur_sec; + + if (s->service_id != json_service_id) + { + /* New service. */ + if (json_service_id) + printf("\n },\n"); + printf(" \"%d\": {\n", s->service_id); + json_service_id = s->service_id; + } + else + printf(",\n"); + + printf(" \"%d\": {\n", d->event_id); + printf(" \"start\": %ld,\n", tm); + printf(" \"end\": %ld,\n", tm + dur); + printf(" \"duration\": %ld", dur); + + if (ds[PARSER_SHORT_EVENT]) + { + struct descriptor *d = ds[PARSER_SHORT_EVENT]; + + DECOMPRESS(d->content.d77.name, d->content.d77.namelen); + DECOMPRESS(d->content.d77.text, d->content.d77.textlen); + + printf(",\n \"name\": \"%.*s\"", + d->content.d77.namelen, d->content.d77.name); + printf(",\n \"text\": \"%.*s\"", + d->content.d77.textlen, d->content.d77.text); + } + if (ds[PARSER_USER_DEFINED]) + { + struct descriptor *d = ds[PARSER_USER_DEFINED]; + DECOMPRESS(d->content.d137.warning, d->content.d137.warninglen); + + printf(",\n \"warning\": \"%.*s\"", + d->content.d137.warninglen, d->content.d137.warning); + } + if (ds[PARSER_CONTENT]) + { + printf(",\n \"content_type\": \"%d\"", + ds[PARSER_CONTENT]->content.d84.level1); + printf(",\n \"content\": \"%s\"", + content_type(ds[PARSER_CONTENT])); + } + + if (ds[PARSER_CRID_EVENT]) + { + struct descriptor *d = ds[PARSER_CRID_EVENT]; + printf(",\n \"event_crid\": \"%.*s\"", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + + if (ds[PARSER_CRID_SERIES]) + { + struct descriptor *d = ds[PARSER_CRID_SERIES]; + printf(",\n \"series_crid\": \"%.*s\"", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + + if (ds[PARSER_CRID_REC]) + { + struct descriptor *d = ds[PARSER_CRID_REC]; + printf(",\n \"rec_crid\": \"%.*s\"", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + + printf("\n }"); +} + void sqldumpstart() { @@ -823,6 +921,12 @@ nextopt: parse(epgpath, sqldump, NULL, filter); sqldumpend(); } + else if (!strcmp(argv[0], "json")) + { + jsonstart(); + parse(epgpath, json, NULL, filter); + jsonend(); + } #ifdef HAVE_SQLITE3 else if (!strcmp(argv[0], "sqlitedump") && argc == 2) {