Fix for ordering JSON, new option for episodes

This commit is contained in:
farfalleflickan 2025-01-01 12:18:57 +01:00
parent b8902589b2
commit 6e5a709e22
7 changed files with 64 additions and 28 deletions

View File

@ -116,12 +116,21 @@ fileList *sortedJoinLists(fileList *part1, fileList *part2, size_t cmpPos, bool
name2=part2->data[cmpPos]; name2=part2->data[cmpPos];
} }
if (name1!=NULL && name2!=NULL && strcmp(name1, name2)<0 && isAscending==true) { // name2 is later in the alphabet if (name1 != NULL && name2 != NULL) {
list=part1; int cmp = strcasecmp(name1, name2);
list->next=sortedJoinLists(part1->next, part2, cmpPos, isAscending, mode); if ((cmp < 0 && isAscending) || (cmp > 0 && !isAscending)) {
} else { list = part1;
list=part2; list->next = sortedJoinLists(part1->next, part2, cmpPos, isAscending, mode);
list->next=sortedJoinLists(part1, part2->next, 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; return list;
@ -296,3 +305,20 @@ char *fileListToJSONStr(fileList *list) {
strlcat(listStr, "]", sizeStr); strlcat(listStr, "]", sizeStr);
return listStr; 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
}

View File

@ -23,6 +23,7 @@ fileList *JSONtofileList(cJSON *json, ioMode mode);
void splitList(fileList *list, fileList **part1, fileList **part2); void splitList(fileList *list, fileList **part1, fileList **part2);
// mergeSort a list // mergeSort a list
void msortList(fileList **list, size_t cmpPos, bool isAscending, sortMode mode); void msortList(fileList **list, size_t cmpPos, bool isAscending, sortMode mode);
fileList *reverseList(fileList *head);
void freeList(struct fileList *list); void freeList(struct fileList *list);
void printList(fileList *list); void printList(fileList *list);
// save fileList to a file, header is a string to be put at the top of the file // save fileList to a file, header is a string to be put at the top of the file

View File

@ -43,7 +43,8 @@ void printHelp() {
printf(" \t\t--id \t 12345\t\tspecifies new id for fix mode\n"); 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--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--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(" --version\t\t\t\tprint version\n");
printf(" --gen-config\t\t\t\tprint default configuration to shell\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"); 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 (runFlags & HTML_MODE) {
if (tv==NULL && (runFlags & DB_MODE)==0) { if (tv==NULL && (runFlags & DB_MODE)==0) {
tv=JSONtofileList(conf->JSON_tvDB, TV_MODE); tv=JSONtofileList(conf->JSON_tvDB, TV_MODE);
tv=reverseList(tv);
if (tv==NULL) { if (tv==NULL) {
printError("cmyflix warning", false, HYEL, "Running in HTML mode but no database could be found!\nBuilding new database...\n"); 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 tv=createTVShowDB(conf); // find files and create/edit database
@ -156,6 +158,7 @@ void *movieCode(void *args) {
if (runFlags & HTML_MODE) { if (runFlags & HTML_MODE) {
if (movies==NULL && (runFlags & DB_MODE)==0) { if (movies==NULL && (runFlags & DB_MODE)==0) {
movies=JSONtofileList(conf->JSON_moDB, MO_MODE); movies=JSONtofileList(conf->JSON_moDB, MO_MODE);
movies=reverseList(movies);
if (movies==NULL) { if (movies==NULL) {
movies=createMoviesDB(conf); movies=createMoviesDB(conf);
if (movies!=NULL) { if (movies!=NULL) {
@ -343,11 +346,12 @@ int main(int argc, char * argv[]) {
{"id", required_argument, 0, '4'}, {"id", required_argument, 0, '4'},
{"poster", required_argument, 0, '5'}, {"poster", required_argument, 0, '5'},
{"name", required_argument, 0, '6'}, {"name", required_argument, 0, '6'},
{"refresh", no_argument, 0, '7'}, {"titles", no_argument, 0, '7'},
{"version", no_argument, 0, '8'}, {"refresh", no_argument, 0, '8'},
{"gen-config", no_argument, 0, '9'}, {"version", no_argument, 0, '9'},
{"check-update", no_argument, 0, 'u'}, {"check-update", no_argument, 0, 'u'},
{"clean", no_argument, 0, 'c'}, {"clean", no_argument, 0, 'c'},
{"gen-config", no_argument, 0, 'g'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"movies", no_argument, 0, 'm'}, {"movies", no_argument, 0, 'm'},
{"quiet", no_argument, 0, 'q'}, {"quiet", no_argument, 0, 'q'},
@ -407,12 +411,14 @@ int main(int argc, char * argv[]) {
} else if (currOption=='6') { // --name } else if (currOption=='6') { // --name
fixName=optarg; fixName=optarg;
runFlags |= FIX_NAME_MODE; 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; runFlags |= FIX_REFR_MODE;
} else if (currOption=='8') { // --version } else if (currOption=='9') { // --version
printVersion(); printVersion();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (currOption=='9') { // --gen-config } else if (currOption=='g') { // --gen-config
printf("%s\n", DEF_CONF); printf("%s\n", DEF_CONF);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (currOption=='c') { // --clean } else if (currOption=='c') { // --clean
@ -494,7 +500,7 @@ int main(int argc, char * argv[]) {
if (runFlags & FIX_MODE) { // if in fix mode if (runFlags & FIX_MODE) { // if in fix mode
// if in either TV show mode or movie mode *AND* ( --id or --poster or --name) // 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; bool refreshMode=false;
if (runFlags & FIX_REFR_MODE) { if (runFlags & FIX_REFR_MODE) {
refreshMode=true; refreshMode=true;

View File

@ -11,7 +11,8 @@ typedef enum {
FIX_ID_MODE = 1 << 6, // FIX MODE ID FIX_ID_MODE = 1 << 6, // FIX MODE ID
FIX_POSTER_MODE = 1 << 7, // FIX MODE POSTER FIX_POSTER_MODE = 1 << 7, // FIX MODE POSTER
FIX_NAME_MODE = 1 << 8, // FIX MODE NAME 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; } progFlags;
void printHelp(); void printHelp();

View File

@ -61,6 +61,7 @@ struct fileList *createMoviesDB(progConfig *conf) {
temp->dataSize--; temp->dataSize--;
} }
} }
movieJSON=reverseList(movieJSON);
char *jsonStr=fileListToJSONStr(movieJSON); char *jsonStr=fileListToJSONStr(movieJSON);
if (jsonStr!=NULL) { if (jsonStr!=NULL) {
cJSON_Delete(conf->JSON_moDB); cJSON_Delete(conf->JSON_moDB);
@ -128,6 +129,7 @@ struct fileList *createMoviesDB(progConfig *conf) {
temp->dataSize--; temp->dataSize--;
} }
} }
hmovieJSON=reverseList(hmovieJSON);
char *jsonStr=fileListToJSONStr(hmovieJSON); char *jsonStr=fileListToJSONStr(hmovieJSON);
if (jsonStr!=NULL) { if (jsonStr!=NULL) {
cJSON_Delete(conf->JSON_moDB); cJSON_Delete(conf->JSON_moDB);

View File

@ -615,20 +615,20 @@ void createShowsHTML(progConfig *conf, fileList *list) {
addData(htmlList, TV_HTML_TOP); addData(htmlList, TV_HTML_TOP);
char *htmlStr=NULL; char *htmlStr=NULL;
for (i--; i>=0; i--) { for (int j=0; j<i; j++) {
pthread_join(threads[i], NULL); pthread_join(threads[j], NULL);
char *showFile=NULL; char *showFile=NULL;
char *showPoster=NULL; char *showPoster=NULL;
if (checkFolder(threadObj[i].list->data[0], false)==0) { if (checkFolder(threadObj[j].list->data[0], false)==0) {
showFile=getRelativePath(conf->TVhtml, threadObj[i].list->data[0]); showFile=getRelativePath(conf->TVhtml, threadObj[j].list->data[0]);
} else { } 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) { if (checkFolder(threadObj[j].list->data[1], false)==0) {
showPoster=getRelativePath(conf->TVhtml, threadObj[i].list->data[1]); showPoster=getRelativePath(conf->TVhtml, threadObj[j].list->data[1]);
} else { } 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) { if (showPoster==NULL) {
@ -636,17 +636,17 @@ void createShowsHTML(progConfig *conf, fileList *list) {
mallocMacro(showPoster, 1, "createShowsHTML error"); mallocMacro(showPoster, 1, "createShowsHTML error");
showPoster[0]='\0'; 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); htmlStr=realloc(htmlStr, htmlStrSize);
if (htmlStr==NULL) { if (htmlStr==NULL) {
fatalError_abort("createShowsHTML error", "could not realloc;\nError: %s;\n", strerror(errno)); 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); addData(htmlList, htmlStr);
freeList(threadObj[i].list); freeList(threadObj[j].list);
tryFree(showFile); tryFree(showFile);
tryFree(showPoster); tryFree(showPoster);
} }

View File

@ -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); int tempID=parseStrToInt(id);
cJSON *this_episodes=cJSON_GetObjectItem(element, "Episodes"); cJSON *this_episodes=cJSON_GetObjectItem(element, "Episodes");
cJSON *episode=NULL; cJSON *episode=NULL;