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