From 8166f22b2e828190ee9b5e09f71b45e36b98e05c Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Wed, 13 Jun 2012 15:21:45 +0100 Subject: [PATCH] Add some of the missing stuff to xmltv and EIT. I think the only significant issue now is series numbering in XMLTV. --- src/epg.c | 7 ++- src/epggrab/eit.c | 24 +++++++---- src/epggrab/xmltv.c | 101 ++++++++++++++++---------------------------- src/tvheadend.h | 2 + src/utils.c | 16 +++++++ 5 files changed, 77 insertions(+), 73 deletions(-) diff --git a/src/epg.c b/src/epg.c index d459702e..ca598453 100644 --- a/src/epg.c +++ b/src/epg.c @@ -959,9 +959,14 @@ int epg_episode_fuzzy_match ( epg_episode_t *episode, const char *uri, const char *title, const char *summary, const char *description ) { + // TODO: this is pretty noddy and likely to fail! + // hence the reason I don't recommend mixing external grabbers and EIT if ( !episode ) return 0; if ( uri && episode->_.uri && !strcmp(episode->_.uri, uri) ) return 1; - // TODO: how to match title/summary/description + if ( title && episode->title && (strstr(title, episode->title) || strstr(episode->title, title)) ) return 1; + // TODO: could we do fuzzy string matching on the description/summary + // : there are a few algorithms that might work, but some early testing + // : suggested it wasn't clear cut enough to make sensible decisions. return 0; } diff --git a/src/epggrab/eit.c b/src/epggrab/eit.c index 6bbcb167..2a828792 100644 --- a/src/epggrab/eit.c +++ b/src/epggrab/eit.c @@ -26,11 +26,12 @@ * Module Setup * ***********************************************************************/ -static void _eit_episode_uri - ( char *uri, const char *title, const char *summary ) +static const char * +longest_string ( const char *a, const char *b ) { - // TODO: do something better - snprintf(uri, 1023, "%s::%s", title, summary); + if (!a) return b; + if (!b) return a; + if (strlen(a) - strlen(b) >= 0) return a; } // called from dvb_tables.c @@ -44,10 +45,11 @@ void eit_callback ( channel_t *ch, int id, time_t start, time_t stop, epg_episode_t *ee; const char *summary = NULL; const char *description = NULL; - char uri[1024]; + char *uri; /* Ignore */ if (!ch || !ch->ch_name || !ch->ch_name[0]) return; + if (!title) return; /* Disabled? */ if (!epggrab_eitenabled) return; @@ -56,11 +58,16 @@ void eit_callback ( channel_t *ch, int id, time_t start, time_t stop, ebc = epg_broadcast_find_by_time(ch, start, stop, 1, &save); if (!ebc) return; - /* TODO: Determine summary */ - summary = desc; + /* Determine summary */ + description = summary = desc; + description = longest_string(description, extitem); + description = longest_string(description, extdesc); + description = longest_string(description, exttext); + if (summary == description) description = NULL; + // TODO: is this correct? /* Create episode URI */ - _eit_episode_uri(uri, title, summary); + uri = md5sum(longest_string(title, longest_string(description, summary))); /* Create/Replace episode */ if ( !ebc->episode || @@ -83,6 +90,7 @@ void eit_callback ( channel_t *ch, int id, time_t start, time_t stop, /* Update */ save |= epg_broadcast_set_episode(ebc, ee); } + free(uri); } /* ************************************************************************ diff --git a/src/epggrab/xmltv.c b/src/epggrab/xmltv.c index 4823a2a0..78b5671e 100644 --- a/src/epggrab/xmltv.c +++ b/src/epggrab/xmltv.c @@ -51,25 +51,6 @@ static epggrab_channel_t *_xmltv_channel_find * Parsing * *************************************************************************/ -/** - * Hash the description to get a URI for episode - * - * Note: converts to an ASCII format - * TODO: move somewhere else - */ -static const char *_xmltv_hash ( const char *str ) -{ - size_t i; - static char ret[(MD5_DIGEST_LENGTH*2)+1]; - static unsigned char md5[MD5_DIGEST_LENGTH]; - (void)MD5((const unsigned char*)str, strlen(str), md5); - for ( i = 0; i < MD5_DIGEST_LENGTH; i++ ) { - sprintf(&ret[i*2], "%02X", md5[i]); - } - ret[MD5_DIGEST_LENGTH*2] = '\0'; - return ret; -} - /** * */ @@ -181,8 +162,8 @@ xmltv_ns_get_parse_num(const char *s, int *ap, int *bp) out: - if(ap) *ap = a; - if(bp) *bp = b; + if(ap) *ap = a + 1; + if(bp) *bp = b + 1; return s; } @@ -191,15 +172,11 @@ xmltv_ns_get_parse_num(const char *s, int *ap, int *bp) static void parse_xmltv_ns_episode -(const char *s, int *season, int *episode, int *part) +(const char *s, int *sn, int *sc, int *en, int *ec, int *pn, int *pc) { - s = xmltv_ns_get_parse_num(s, season, NULL); - s = xmltv_ns_get_parse_num(s, episode, NULL); - xmltv_ns_get_parse_num(s, part, NULL); - - *season += 1; - *episode += 1; - *part += 1; + s = xmltv_ns_get_parse_num(s, sn, sc); + s = xmltv_ns_get_parse_num(s, en, ec); + xmltv_ns_get_parse_num(s, pn, pc); } /** @@ -207,7 +184,7 @@ parse_xmltv_ns_episode */ static void get_episode_info -(htsmsg_t *tags, const char **screen, int *season, int *episode, int *part) +(htsmsg_t *tags, const char **screen, int *sn, int *sc, int *en, int *ec, int *pn, int *pc) { htsmsg_field_t *f; htsmsg_t *c, *a; @@ -223,7 +200,7 @@ get_episode_info if(!strcmp(sys, "onscreen")) *screen = cdata; else if(!strcmp(sys, "xmltv_ns")) - parse_xmltv_ns_episode(cdata, season, episode, part); + parse_xmltv_ns_episode(cdata, sn, sc, en, ec, pn, pc); } } @@ -237,54 +214,50 @@ _xmltv_parse_programme_tags(channel_t *ch, htsmsg_t *tags, int save = 0, save2 = 0; epg_episode_t *ee; epg_broadcast_t *ebc; - int sn = 0, en = 0, pn = 0; + int sn = 0, sc = 0, en = 0, ec = 0, pn = 0, pc = 0; const char *onscreen = NULL; - const char *uri = NULL; + char *uri; const char *title = htsmsg_xml_get_cdata_str(tags, "title"); const char *desc = htsmsg_xml_get_cdata_str(tags, "desc"); - //const char *category = htsmsg_xml_get_cdata_str(tags, "category"); - get_episode_info(tags, &onscreen, &sn, &en, &pn); + const char *category[2]; + get_episode_info(tags, &onscreen, &sn, &sc, &en, &ec, &pn, &pc); + + /* Ignore */ + if (!title) return 0; + + /* Build episode */ + uri = md5sum(desc ?: title); + ee = epg_episode_find_by_uri(uri, 1, &save); + free(uri); + if (!ee) return 0; + stats->episodes.total++; + if (save) stats->episodes.created++; + + category[0] = htsmsg_xml_get_cdata_str(tags, "category"); + category[1] = NULL; + if (title) save |= epg_episode_set_title(ee, title); + if (desc) save |= epg_episode_set_description(ee, desc); + if (*category) save |= epg_episode_set_genre_str(ee, category); + if (pn) save |= epg_episode_set_part(ee, pn, pc); + if (en) save |= epg_episode_set_number(ee, en); + if (save) stats->episodes.modified++; + + // TODO: need to handle season numbering! + // TODO: need to handle onscreen numbering + //if (onscreen) save |= epg_episode_set_onscreen(ee, onscreen); /* Create/Find broadcast */ ebc = epg_broadcast_find_by_time(ch, start, stop, 1, &save2); if ( ebc != NULL ) { stats->broadcasts.total++; if (save2) stats->broadcasts.created++; - //save2 |= epg_broadcast_set_episode(ebc, ee); + save2 |= epg_broadcast_set_episode(ebc, ee); if (save2) stats->broadcasts.modified++; } - // TODO: this needs reworking - - // TODO: - // do we attempt to make an episode URI from info? - // OR - // do we first try to find the broadcast and do a fuzzy check on params? - // OR - // do we do something else? - - /* Generate a URI */ - if ( desc ) - uri = _xmltv_hash(desc); - if ( !uri ) return 0; - - /* Find episode */ - ee = epg_episode_find_by_uri(uri, 1, &save); - stats->episodes.total++; - if (save) stats->episodes.created++; - if (title) save |= epg_episode_set_title(ee, title); - if (desc) save |= epg_episode_set_description(ee, desc); - //if (category) save |= epg_episode_set_genre_str(ee, category); - //if (onscreen) save |= epg_episode_set_onscreen(ee, onscreen); - if (pn) save |= epg_episode_set_part(ee, pn, 0); - if (en) save |= epg_episode_set_number(ee, en); - // TODO: how can we handle season number? - if (save) stats->episodes.modified++; - return save | save2; } - /** * Parse a tag from xmltv */ diff --git a/src/tvheadend.h b/src/tvheadend.h index 91211deb..ae10afd1 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -449,4 +449,6 @@ void sbuf_put_be16(sbuf_t *sb, uint16_t u16); void sbuf_put_byte(sbuf_t *sb, uint8_t u8); +char *md5sum ( const char *str ); + #endif /* TV_HEAD_H */ diff --git a/src/utils.c b/src/utils.c index 5b792d66..391597b1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,8 +20,10 @@ #include #include #include +#include #include "tvheadend.h" + /** * CRC32 */ @@ -306,3 +308,17 @@ sbuf_cut(sbuf_t *sb, int off) sb->sb_ptr = sb->sb_ptr - off; memmove(sb->sb_data, sb->sb_data + off, sb->sb_ptr); } + +char * +md5sum ( const char *str ) +{ + int i; + static unsigned char md5[MD5_DIGEST_LENGTH]; + char *ret = malloc((MD5_DIGEST_LENGTH * 2) + 1); + MD5((const unsigned char*)str, strlen(str), md5); + for ( i = 0; i < MD5_DIGEST_LENGTH; i++ ) { + sprintf(&ret[i*2], "%02X", md5[i]); + } + ret[MD5_DIGEST_LENGTH*2] = '\0'; + return ret; +}