Skip to content

Commit

Permalink
Fix for ordering JSON, new option for episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
farfalleflickan committed Jan 1, 2025
1 parent b890258 commit 6e5a709
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 28 deletions.
38 changes: 32 additions & 6 deletions src/fileList.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions src/fileList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/movies.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions src/tvshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,38 +615,38 @@ 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; j<i; j++) {
pthread_join(threads[j], NULL);
char *showFile=NULL;
char *showPoster=NULL;
if (checkFolder(threadObj[i].list->data[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) {
showPoster=NULL;
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6e5a709

Please sign in to comment.