Updates for new file format

This commit is contained in:
hummypkg 2019-11-27 15:21:41 +00:00
parent 9fd6117cd5
commit 5e697dc42c
1 changed files with 109 additions and 41 deletions

150
tvdb.c
View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@ -54,6 +55,8 @@ unescape(char *txt)
{ {
HANDLE("&quot;", '"', 5); HANDLE("&quot;", '"', 5);
HANDLE("&amp;", '&', 4); HANDLE("&amp;", '&', 4);
HANDLE("&#xD;", '\n', 4);
HANDLE("&#xA;", '\r', 4);
p++; p++;
} }
@ -74,27 +77,55 @@ unescape(char *txt)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char buf[0x1000], *p, *q; char *buf, *p, *q;
struct series s; struct series s;
struct episode e; struct episode e;
struct stat st;
char *error; char *error;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
sqlite3 *db; sqlite3 *db;
FILE *fp; int fd, epcnt = 0;
int epcnt = 0; int debug = 0;
if (argc != 2) if (argc < 2)
{ {
printf("Syntax: %s <xml file>\n", argv[0]); printf("Syntax: %s [-d] <xml file>\n", argv[0]);
return 0; return 0;
} }
if (!(fp = fopen(argv[1], "r"))) if (!strcmp(argv[1], "-d"))
{
debug = 1;
argc--, argv++;
}
if (stat(argv[1], &st) == -1)
{
perror("stat");
return 0;
}
if (!(buf = malloc(st.st_size)))
{
perror("malloc");
return 0;
}
if ((fd = open(argv[1], O_RDONLY)) == -1)
{ {
perror("open"); perror("open");
return 0; return 0;
} }
if (read(fd, buf, st.st_size) != st.st_size)
{
perror("read");
return 0;
}
if (debug)
printf("Read - %lu bytes\n", st.st_size);
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
if (sqlite3_open_v2(SERIESDB, &db, if (sqlite3_open_v2(SERIESDB, &db,
@ -131,53 +162,66 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
while (fgets(buf, sizeof(buf), fp)) p = strchr(buf, '<');
for (;;)
{ {
p = buf; if (!p)
while ((*p++) != '<') break;
; p++;
if (!strncmp(p, "Series>", 7)) if (!strncmp(p, "Series>", 7))
{ {
memset(&s, '\0', sizeof(s)); memset(&s, '\0', sizeof(s));
continue;
} }
if (!strncmp(p, "id>", 3)) if (!strncmp(p, "id>", 3))
{ {
s.id = strtoul(p + 3, (char **)NULL, 10); p += 3;
continue; s.id = strtoul(p, (char **)NULL, 10);
} }
if (!strncmp(p, "IMDB_ID>", 8)) if (!strncmp(p, "IMDB_ID>", 8))
{ {
s.imdb = strdup(p + 8); p += 8;
if ((q = strstr(s.imdb, "</IMDB_ID>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
s.imdb = strdup(p);
p = q;
continue; continue;
} }
if (!strncmp(p, "SeriesName>", 11)) if (!strncmp(p, "SeriesName>", 11))
{ {
s.name = strdup(p + 11); p += 11;
if ((q = strstr(s.name, "</SeriesName>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
s.name = strdup(p);
unescape(s.name); unescape(s.name);
printf("Series: %s\n", s.name);
p = q;
continue; continue;
} }
if (!strncmp(p, "Overview>", 9)) if (!strncmp(p, "Overview>", 9))
{ {
s.overview = strdup(p + 9); p += 9;
if ((q = strstr(s.overview, "</Overview>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
s.overview = strdup(p);
unescape(s.overview); unescape(s.overview);
if (debug)
printf(" %s\n", s.overview);
p = q;
continue;
} }
if (!strncmp(p, "banner>", 7)) if (!strncmp(p, "banner>", 7))
{ {
s.banner = strdup(p + 7); p += 7;
if ((q = strstr(s.banner, "</banner>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
s.banner = strdup(p);
unescape(s.banner); unescape(s.banner);
p = q;
continue;
} }
if (!strncmp(p, "/Series>", 7)) if (!strncmp(p, "/Series>", 8))
{ {
if (s.id && s.name && s.overview) if (s.id && s.name && s.overview)
{ {
@ -192,6 +236,7 @@ main(int argc, char **argv)
} }
break; break;
} }
p = strchr(p, '<');
} }
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
@ -227,11 +272,12 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
while (fgets(buf, sizeof(buf), fp)) for (;;)
{ {
p = buf; if (!p)
while ((*p++) != '<') break;
; p++;
if (!strncmp(p, "Episode>", 8)) if (!strncmp(p, "Episode>", 8))
{ {
/* New episode */ /* New episode */
@ -239,34 +285,53 @@ main(int argc, char **argv)
} }
if (!strncmp(p, "id>", 3)) if (!strncmp(p, "id>", 3))
e.id = strtoul(p + 3, (char **)NULL, 10); {
if (!strncmp(p, "Combined_episodenumber>", 23)) p += 3;
e.episode = strtoul(p + 23, (char **)NULL, 10); e.id = strtoul(p, (char **)NULL, 10);
if (!strncmp(p, "Combined_season>", 16)) }
e.series = strtoul(p + 16, (char **)NULL, 10); if (!strncmp(p, "Combined_episodenumber>", 23)) {
p += 23;
e.episode = strtoul(p, (char **)NULL, 10);
}
if (!strncmp(p, "Combined_season>", 16)) {
p += 16;
e.series = strtoul(p, (char **)NULL, 10);
}
if (!strncmp(p, "EpisodeName>", 12)) if (!strncmp(p, "EpisodeName>", 12))
{ {
e.name = strdup(p + 12); p += 12;
if ((q = strstr(e.name, "</EpisodeName>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
e.name = strdup(p);
unescape(e.name); unescape(e.name);
printf("Episode: %s\n", e.name);
p = q;
continue;
} }
if (!strncmp(p, "Overview>", 9)) if (!strncmp(p, "Overview>", 9))
{ {
e.overview = strdup(p + 9); p += 9;
if ((q = strstr(e.overview, "</Overview>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
e.overview = strdup(p);
unescape(e.overview); unescape(e.overview);
if (debug)
printf(" %s\n", e.overview);
p = q;
continue;
} }
if (!strncmp(p, "filename>", 9)) if (!strncmp(p, "filename>", 9))
{ {
e.thumb = strdup(p + 9); p += 9;
if ((q = strstr(e.thumb, "</filename>"))) if ((q = strchr(p, '<')))
*q = '\0'; *q = '\0';
e.thumb = strdup(p);
unescape(e.thumb); unescape(e.thumb);
p = q;
continue;
} }
if (!strncmp(p, "/Episode>", 8)) if (!strncmp(p, "/Episode>", 9))
{ {
/* End of episode */ /* End of episode */
if (e.id && e.name && e.overview) if (e.id && e.name && e.overview)
@ -288,13 +353,16 @@ main(int argc, char **argv)
if (e.thumb) free(e.thumb); if (e.thumb) free(e.thumb);
memset(&e, '\0', sizeof(e)); memset(&e, '\0', sizeof(e));
} }
p = strchr(p, '<');
} }
fclose(fp); close(fd);
free(buf);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
sqlite3_close(db); sqlite3_close(db);
/*printf("Episodes: %d\n", epcnt);*/ printf("Episodes: %d\n", epcnt);
return 0; return 0;
} }