From 6e5a709e22b882a43929c514435746b3b1debdf3 Mon Sep 17 00:00:00 2001 From: farfalleflickan <6597735+farfalleflickan@users.noreply.github.com> Date: Wed, 1 Jan 2025 12:18:57 +0100 Subject: [PATCH] Fix for ordering JSON, new option for episodes --- src/fileList.c | 38 ++++++++++++++++++++++++++++++++------ src/fileList.h | 1 + src/main.c | 22 ++++++++++++++-------- src/main.h | 3 ++- src/movies.c | 2 ++ src/tvshow.c | 24 ++++++++++++------------ src/utils.c | 2 +- 7 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/fileList.c b/src/fileList.c index 870b145..c79b15a 100644 --- a/src/fileList.c +++ b/src/fileList.c @@ -116,12 +116,21 @@ fileList *sortedJoinLists(fileList *part1, fileList *part2, size_t cmpPos, bool name2=part2->data[cmpPos]; } - if (name1!=NULL && name2!=NULL && strcmp(name1, name2)<0 && isAscending==true) { // name2 is later in the alphabet - list=part1; - list->next=sortedJoinLists(part1->next, part2, cmpPos, isAscending, mode); - } else { - list=part2; - list->next=sortedJoinLists(part1, part2->next, cmpPos, isAscending, mode); + if (name1 != NULL && name2 != NULL) { + int cmp = strcasecmp(name1, name2); + if ((cmp < 0 && isAscending) || (cmp > 0 && !isAscending)) { + list = part1; + list->next = sortedJoinLists(part1->next, part2, cmpPos, isAscending, mode); + } else { + list = part2; + list->next = sortedJoinLists(part1, part2->next, cmpPos, isAscending, mode); + } + } else if (name1 == NULL) { + list = part2; + list->next = sortedJoinLists(part1, part2->next, cmpPos, isAscending, mode); + } else if (name2 == NULL) { + list = part1; + list->next = sortedJoinLists(part1->next, part2, cmpPos, isAscending, mode); } } return list; @@ -296,3 +305,20 @@ char *fileListToJSONStr(fileList *list) { strlcat(listStr, "]", sizeStr); return listStr; } + +fileList *reverseList(fileList *head) { + fileList *prev = NULL; + fileList *current = head; + fileList *next = NULL; + size_t listSize = 0; + + while (current != NULL) { + next = current->next; + current->next = prev; + prev = current; + current->listSize = ++listSize; + current = next; + } + + return prev; // The new head of the reversed list +} \ No newline at end of file diff --git a/src/fileList.h b/src/fileList.h index fa1729d..72dcf68 100644 --- a/src/fileList.h +++ b/src/fileList.h @@ -23,6 +23,7 @@ fileList *JSONtofileList(cJSON *json, ioMode mode); void splitList(fileList *list, fileList **part1, fileList **part2); // mergeSort a list void msortList(fileList **list, size_t cmpPos, bool isAscending, sortMode mode); +fileList *reverseList(fileList *head); void freeList(struct fileList *list); void printList(fileList *list); // save fileList to a file, header is a string to be put at the top of the file diff --git a/src/main.c b/src/main.c index 7f2100c..38bd12f 100644 --- a/src/main.c +++ b/src/main.c @@ -43,7 +43,8 @@ void printHelp() { printf(" \t\t--id \t 12345\t\tspecifies new id for fix mode\n"); printf(" \t\t--name \t \"new name\"\tspecifies new name for fix mode\n"); printf(" \t\t--poster path/to/file\tspecifies new poster for fix mode\n"); - printf(" \t\t--refresh\t\trefresh info of \"name\", so refreshes episode names in TV shows (also re-downloads poster if --poster \"\" is passed)\n"); + printf(" \t\t--titles\t\trefreshes the episode titles\n"); + printf(" \t\t--refresh\t\trefresh info of \"name\", re-downloads poster if --poster \"\" is passed & refreshes episode names in TV shows if --titles is also passed\n"); printf(" --version\t\t\t\tprint version\n"); printf(" --gen-config\t\t\t\tprint default configuration to shell\n"); printf(" --check-update\t\t\tcheck if program update is available\n"); @@ -116,6 +117,7 @@ void *tvCode(void *args) { if (runFlags & HTML_MODE) { if (tv==NULL && (runFlags & DB_MODE)==0) { tv=JSONtofileList(conf->JSON_tvDB, TV_MODE); + tv=reverseList(tv); if (tv==NULL) { printError("cmyflix warning", false, HYEL, "Running in HTML mode but no database could be found!\nBuilding new database...\n"); tv=createTVShowDB(conf); // find files and create/edit database @@ -156,6 +158,7 @@ void *movieCode(void *args) { if (runFlags & HTML_MODE) { if (movies==NULL && (runFlags & DB_MODE)==0) { movies=JSONtofileList(conf->JSON_moDB, MO_MODE); + movies=reverseList(movies); if (movies==NULL) { movies=createMoviesDB(conf); if (movies!=NULL) { @@ -343,11 +346,12 @@ int main(int argc, char * argv[]) { {"id", required_argument, 0, '4'}, {"poster", required_argument, 0, '5'}, {"name", required_argument, 0, '6'}, - {"refresh", no_argument, 0, '7'}, - {"version", no_argument, 0, '8'}, - {"gen-config", no_argument, 0, '9'}, + {"titles", no_argument, 0, '7'}, + {"refresh", no_argument, 0, '8'}, + {"version", no_argument, 0, '9'}, {"check-update", no_argument, 0, 'u'}, {"clean", no_argument, 0, 'c'}, + {"gen-config", no_argument, 0, 'g'}, {"help", no_argument, 0, 'h'}, {"movies", no_argument, 0, 'm'}, {"quiet", no_argument, 0, 'q'}, @@ -407,12 +411,14 @@ int main(int argc, char * argv[]) { } else if (currOption=='6') { // --name fixName=optarg; runFlags |= FIX_NAME_MODE; - } else if (currOption=='7') { // --refresh + } else if (currOption=='7') { // --titles + runFlags |= FIX_TITLES_MODE; + } else if (currOption=='8') { // --refresh runFlags |= FIX_REFR_MODE; - } else if (currOption=='8') { // --version + } else if (currOption=='9') { // --version printVersion(); exit(EXIT_SUCCESS); - } else if (currOption=='9') { // --gen-config + } else if (currOption=='g') { // --gen-config printf("%s\n", DEF_CONF); exit(EXIT_SUCCESS); } else if (currOption=='c') { // --clean @@ -494,7 +500,7 @@ int main(int argc, char * argv[]) { if (runFlags & FIX_MODE) { // if in fix mode // if in either TV show mode or movie mode *AND* ( --id or --poster or --name) - if (((runFlags & SHOWS_MODE) || (runFlags & MOVIES_MODE)) && ((runFlags & FIX_ID_MODE) || (runFlags & FIX_POSTER_MODE) || (runFlags & FIX_NAME_MODE) || (runFlags & FIX_REFR_MODE))) { + if (((runFlags & SHOWS_MODE) || (runFlags & MOVIES_MODE)) && ((runFlags & FIX_ID_MODE) || (runFlags & FIX_POSTER_MODE) || (runFlags & FIX_NAME_MODE) || (runFlags & FIX_TITLES_MODE) || (runFlags & FIX_REFR_MODE))) { bool refreshMode=false; if (runFlags & FIX_REFR_MODE) { refreshMode=true; diff --git a/src/main.h b/src/main.h index d8b39c4..40c61b2 100644 --- a/src/main.h +++ b/src/main.h @@ -11,7 +11,8 @@ typedef enum { FIX_ID_MODE = 1 << 6, // FIX MODE ID FIX_POSTER_MODE = 1 << 7, // FIX MODE POSTER FIX_NAME_MODE = 1 << 8, // FIX MODE NAME - FIX_REFR_MODE = 1 << 9 // FIX MODE REFRESH + FIX_TITLES_MODE = 1 << 9, // FIX MODE NAME + FIX_REFR_MODE = 1 << 10 // FIX MODE REFRESH } progFlags; void printHelp(); diff --git a/src/movies.c b/src/movies.c index e50e306..bc95415 100644 --- a/src/movies.c +++ b/src/movies.c @@ -61,6 +61,7 @@ struct fileList *createMoviesDB(progConfig *conf) { temp->dataSize--; } } + movieJSON=reverseList(movieJSON); char *jsonStr=fileListToJSONStr(movieJSON); if (jsonStr!=NULL) { cJSON_Delete(conf->JSON_moDB); @@ -128,6 +129,7 @@ struct fileList *createMoviesDB(progConfig *conf) { temp->dataSize--; } } + hmovieJSON=reverseList(hmovieJSON); char *jsonStr=fileListToJSONStr(hmovieJSON); if (jsonStr!=NULL) { cJSON_Delete(conf->JSON_moDB); diff --git a/src/tvshow.c b/src/tvshow.c index e47c8bc..0c4a858 100755 --- a/src/tvshow.c +++ b/src/tvshow.c @@ -615,20 +615,20 @@ void createShowsHTML(progConfig *conf, fileList *list) { addData(htmlList, TV_HTML_TOP); char *htmlStr=NULL; - for (i--; i>=0; i--) { - pthread_join(threads[i], NULL); + for (int j=0; jdata[0], false)==0) { - showFile=getRelativePath(conf->TVhtml, threadObj[i].list->data[0]); + if (checkFolder(threadObj[j].list->data[0], false)==0) { + showFile=getRelativePath(conf->TVhtml, threadObj[j].list->data[0]); } else { - fatalError_exit("createShowsHTML", "could not find \"%s\";\nExiting...\n", threadObj[i].list->data[0]); + fatalError_exit("createShowsHTML", "could not find \"%s\";\nExiting...\n", threadObj[j].list->data[0]); } - if (checkFolder(threadObj[i].list->data[1], false)==0) { - showPoster=getRelativePath(conf->TVhtml, threadObj[i].list->data[1]); + if (checkFolder(threadObj[j].list->data[1], false)==0) { + showPoster=getRelativePath(conf->TVhtml, threadObj[j].list->data[1]); } else { - fatalError_exit("createShowsHTML", "could not find \"%s\";\nExiting...\n", threadObj[i].list->data[1]); + fatalError_exit("createShowsHTML", "could not find \"%s\";\nExiting...\n", threadObj[j].list->data[1]); } if (showPoster==NULL) { @@ -636,17 +636,17 @@ void createShowsHTML(progConfig *conf, fileList *list) { mallocMacro(showPoster, 1, "createShowsHTML error"); showPoster[0]='\0'; } - char *showName=threadObj[i].list->data[2]; + char *showName=threadObj[j].list->data[2]; - size_t htmlStrSize=strlen(TV_HTML_FRAME)+strlen(showFile)+strlen(showPoster)+strlen(showName)*2+intSize(i)+1; + size_t htmlStrSize=strlen(TV_HTML_FRAME)+strlen(showFile)+strlen(showPoster)+strlen(showName)*2+intSize(j)+1; htmlStr=realloc(htmlStr, htmlStrSize); if (htmlStr==NULL) { fatalError_abort("createShowsHTML error", "could not realloc;\nError: %s;\n", strerror(errno)); } - snprintf(htmlStr, htmlStrSize, TV_HTML_FRAME, i, showName, showFile, showPoster, showName, i, i, i); + snprintf(htmlStr, htmlStrSize, TV_HTML_FRAME, j, showName, showFile, showPoster, showName, j, j, j); addData(htmlList, htmlStr); - freeList(threadObj[i].list); + freeList(threadObj[j].list); tryFree(showFile); tryFree(showPoster); } diff --git a/src/utils.c b/src/utils.c index 42609df..e4fa1b7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -936,7 +936,7 @@ int fixMode(progConfig *conf, progFlags flags, const char *toFix, const char *id } } - if ((flags & SHOWS_MODE) && conf->getEpisodeName && refreshMode) { + if ((flags & SHOWS_MODE) && (flags & FIX_TITLES_MODE) && refreshMode) { int tempID=parseStrToInt(id); cJSON *this_episodes=cJSON_GetObjectItem(element, "Episodes"); cJSON *episode=NULL;