From 8e1c6ab14169bc507a5b71f2e9646351b41f15a4 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Fri, 15 Jun 2012 11:38:16 +0100 Subject: [PATCH] Added automatic configuration migration for epggrab (inc xmltv) and epgdb. --- src/epg.c | 58 ++++++++++++++++++++++++++++++++ src/epggrab.c | 80 ++++++++++++++++++++++++++++++++++++--------- src/epggrab/eit.c | 4 +++ src/epggrab/eit.h | 1 + src/epggrab/pyepg.c | 7 ++-- src/epggrab/pyepg.h | 1 + src/epggrab/xmltv.c | 8 +++-- src/epggrab/xmltv.h | 1 + 8 files changed, 138 insertions(+), 22 deletions(-) diff --git a/src/epg.c b/src/epg.c index d46013bf..8e795730 100644 --- a/src/epg.c +++ b/src/epg.c @@ -91,6 +91,55 @@ static int _episode_order ( const void *_a, const void *_b ) * Setup / Update * *************************************************************************/ +static void _epg_event_deserialize ( htsmsg_t *c, epggrab_stats_t *stats ) +{ + channel_t *ch; + epg_episode_t *ee; + epg_broadcast_t *ebc; + uint32_t ch_id = 0; + uint32_t e_start = 0; + uint32_t e_stop = 0; + uint32_t u32; + const char *title, *desc; + char *uri; + int save = 0; + + /* Check key info */ + if(htsmsg_get_u32(c, "ch_id", &ch_id)) return; + if((ch = channel_find_by_identifier(ch_id)) == NULL) return; + if(htsmsg_get_u32(c, "start", &e_start)) return; + if(htsmsg_get_u32(c, "stop", &e_stop)) return; + if(!(title = htsmsg_get_str(c, "title"))) return; + + /* Create broadcast */ + save = 0; + ebc = epg_broadcast_find_by_time(ch, e_start, e_stop, 1, &save); + if (!ebc) return; + if (save) stats->broadcasts.total++; + + /* Create episode */ + save = 0; + desc = htsmsg_get_str(c, "desc"); + uri = md5sum(desc ?: title); + ee = epg_episode_find_by_uri(uri, 1, &save); + free(uri); + if (!ee) return; + if (save) stats->episodes.total++; + if (title) + save |= epg_episode_set_title(ee, title); + if (desc) + save |= epg_episode_set_summary(ee, desc); + if (!htsmsg_get_u32(c, "episode", &u32)) + save |= epg_episode_set_number(ee, u32); + if (!htsmsg_get_u32(c, "part", &u32)) + save |= epg_episode_set_part(ee, u32, 0); + // TODO: season number! + // TODO: onscreen + + /* Set episode */ + save |= epg_broadcast_set_episode(ebc, ee); +} + static int _epg_write ( int fd, htsmsg_t *m ) { int ret = 1; @@ -173,6 +222,7 @@ void epg_init ( void ) char *sect = NULL; const char *s; epggrab_stats_t stats; + int old = 0; /* Map file to memory */ fd = hts_settings_open_file(0, "epgdb"); @@ -217,6 +267,14 @@ void epg_init ( void ) if (s) { if (sect) free(sect); sect = strdup(s); + + /* Assume OLD data */ + } else if ( !sect ) { + if (!old) { + old = 1; + tvhlog(LOG_INFO, "epg", "migrating old database"); + } + _epg_event_deserialize(m, &stats); /* Brand */ } else if ( !strcmp(sect, "brands") ) { diff --git a/src/epggrab.c b/src/epggrab.c index 3aad8eed..d1a6a705 100644 --- a/src/epggrab.c +++ b/src/epggrab.c @@ -650,25 +650,70 @@ static void _epggrab_load ( void ) { epggrab_module_t *mod; htsmsg_t *m, *a; + uint32_t enabled = 1; const char *str; - - /* No config */ - if ((m = hts_settings_load("epggrab/config")) == NULL) - return; + int old = 0; /* Load settings */ - htsmsg_get_u32(m, "eit", &epggrab_eitenabled); - htsmsg_get_u32(m, "interval", &epggrab_interval); - if ( (str = htsmsg_get_str(m, "module")) ) - epggrab_module = epggrab_module_find_by_id(str); - if ( (a = htsmsg_get_map(m, "mod_enabled")) ) { - LIST_FOREACH(mod, &epggrab_modules, link) { - if (htsmsg_get_u32_or_default(a, mod->id, 0)) { - if (mod->enable) mod->enable(mod, 1); + if (!(m = hts_settings_load("epggrab/config"))) { + printf("failed to load config\n"); + assert(0); + if ((m = hts_settings_load("xmltv/config"))) + old = 1; + } + if (old) tvhlog(LOG_INFO, "epggrab", "migrating old configuration"); + + /* Process */ + if (m) { + htsmsg_get_u32(m, "eit", &epggrab_eitenabled); + if (!htsmsg_get_u32(m, old ? "grab-interval" : "interval", &epggrab_interval)) + if (old) epggrab_interval *= 3600; + if ( !htsmsg_get_u32(m, "grab-enabled", &enabled) ) + if (enabled) { + if ( (str = htsmsg_get_str(m, old ? "current-grabber" : "module")) ) + epggrab_module = epggrab_module_find_by_id(str); + if ( (a = htsmsg_get_map(m, "mod_enabled")) ) { + LIST_FOREACH(mod, &epggrab_modules, link) { + if (htsmsg_get_u32_or_default(a, mod->id, 0)) { + if (mod->enable) mod->enable(mod, 1); + } + } } } + htsmsg_destroy(m); } - htsmsg_destroy(m); + + /* Finish up migration */ + if (old) { + htsmsg_field_t *f; + htsmsg_t *xc, *ch; + htsmsg_t *xchs = hts_settings_load("xmltv/channels"); + htsmsg_t *chs = hts_settings_load("channels"); + if (xchs) { + HTSMSG_FOREACH(f, chs) { + if ((ch = htsmsg_get_map_by_field(f))) { + if ((str = htsmsg_get_str(ch, "xmltv-channel"))) { + if ((xc = htsmsg_get_map(xchs, str))) { + htsmsg_add_u32(xc, "channel", atoi(f->hmf_name)); + } + } + } + } + HTSMSG_FOREACH(f, xchs) { + if ((xc = htsmsg_get_map_by_field(f))) { + hts_settings_save(xc, "epggrab/xmltv/channels/%s", f->hmf_name); + } + } + } + + /* Save epggrab config */ + epggrab_save(); + } + + /* Load module config (channels) */ + eit_load(); + xmltv_load(); + pyepg_load(); } void epggrab_save ( void ) @@ -686,11 +731,14 @@ void epggrab_save ( void ) htsmsg_add_u32(m, "interval", epggrab_interval); if ( epggrab_module ) htsmsg_add_str(m, "module", epggrab_module->id); - a = htsmsg_create_map(); + a = NULL; LIST_FOREACH(mod, &epggrab_modules, link) { - if (mod->enabled) htsmsg_add_u32(a, mod->id, 1); + if (mod->enabled) { + if (!a) a = htsmsg_create_map(); + htsmsg_add_u32(a, mod->id, 1); + } } - htsmsg_add_msg(m, "mod_enabled", a); + if (a) htsmsg_add_msg(m, "mod_enabled", a); hts_settings_save(m, "epggrab/config"); htsmsg_destroy(m); } diff --git a/src/epggrab/eit.c b/src/epggrab/eit.c index 2a828792..3670eacd 100644 --- a/src/epggrab/eit.c +++ b/src/epggrab/eit.c @@ -107,3 +107,7 @@ void eit_init ( epggrab_module_list_t *list ) LIST_INSERT_HEAD(list, &_eit_mod, link); // Note: this is mostly ignored anyway as EIT is treated as a special case! } + +void eit_load ( void ) +{ +} diff --git a/src/epggrab/eit.h b/src/epggrab/eit.h index bc8b17c8..be58edeb 100644 --- a/src/epggrab/eit.h +++ b/src/epggrab/eit.h @@ -22,6 +22,7 @@ #include "epggrab.h" void eit_init ( epggrab_module_list_t *list ); +void eit_load ( void ); void eit_callback ( struct channel *ch, int id, time_t start, time_t stop, const char *title, const char *desc, diff --git a/src/epggrab/pyepg.c b/src/epggrab/pyepg.c index 40e70989..827ac798 100644 --- a/src/epggrab/pyepg.c +++ b/src/epggrab/pyepg.c @@ -442,8 +442,9 @@ void pyepg_init ( epggrab_module_list_t *list ) mod->parse = _pyepg_parse; *((uint8_t*)&mod->flags) = EPGGRAB_MODULE_EXTERNAL; LIST_INSERT_HEAD(list, mod, link); - - /* Load channel config */ - epggrab_module_channels_load(_pyepg_module); } +void pyepg_load ( void ) +{ + epggrab_module_channels_load(_pyepg_module); +} diff --git a/src/epggrab/pyepg.h b/src/epggrab/pyepg.h index a005c165..9686c784 100644 --- a/src/epggrab/pyepg.h +++ b/src/epggrab/pyepg.h @@ -22,5 +22,6 @@ #include "epggrab.h" void pyepg_init ( epggrab_module_list_t *list ); +void pyepg_load ( void ); #endif diff --git a/src/epggrab/xmltv.c b/src/epggrab/xmltv.c index 78b5671e..0b959298 100644 --- a/src/epggrab/xmltv.c +++ b/src/epggrab/xmltv.c @@ -426,7 +426,9 @@ void xmltv_init ( epggrab_module_list_t *list ) /* Standard modules */ _xmltv_load_grabbers(list); - - /* Load channel config */ - epggrab_module_channels_load(mod); +} + +void xmltv_load ( void ) +{ + epggrab_module_channels_load(_xmltv_module); } diff --git a/src/epggrab/xmltv.h b/src/epggrab/xmltv.h index b4637a1a..7a2d1bee 100644 --- a/src/epggrab/xmltv.h +++ b/src/epggrab/xmltv.h @@ -23,5 +23,6 @@ #include "epggrab.h" void xmltv_init ( epggrab_module_list_t *list ); +void xmltv_load ( void ); #endif