From 99df106665a51925c4f918bf8990c7899c0c0216 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Wed, 1 Sep 2021 18:45:53 +0100 Subject: Refactored Navigation Algorithm --- libs/json.c | 87 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 35 deletions(-) (limited to 'libs/json.c') diff --git a/libs/json.c b/libs/json.c index 1c8bf55..280c26e 100644 --- a/libs/json.c +++ b/libs/json.c @@ -4,13 +4,28 @@ #include #include "json.h" #include "../include/video.h" -#define MAXLENGTH 256 json_object* jsonParseString(char* stringedJSON) { return ( stringedJSON != NULL && strlen(stringedJSON) > 0 ) ? json_tokener_parse(stringedJSON) : NULL; } +json_object* objectPathNavigator(json_object* object, char path[][MAXLENGTH], int depth) +{ + json_object *navigator = ( json_object_get_type(object) == json_type_array ) ? + json_object_array_get_idx(object, atoi(path[0])) : + json_object_object_get(object, path[0]); + + for( int i = 1; i < depth; i++ ) + { + navigator = ( json_object_get_type(navigator) == json_type_array ) ? + json_object_array_get_idx(navigator, atoi(path[i])) : + json_object_object_get(navigator, path[i]); + } + + return navigator; +} + json_object* navigateToVideos(json_object* jsonRoot) { char path[][MAXLENGTH] = { @@ -24,16 +39,7 @@ json_object* navigateToVideos(json_object* jsonRoot) "contents" }; - json_object *navigator = json_object_object_get(jsonRoot, path[0]); - - for( int i = 1; i < sizeof(path)/sizeof(path[0]); i++ ) - { - navigator = ( json_object_get_type(navigator) == json_type_array ) ? - json_object_array_get_idx(navigator, atoi(path[i])) : - json_object_object_get(navigator, path[i]); - } - - return navigator; + return objectPathNavigator(jsonRoot, path, sizeof(path)/sizeof(path[0])); } int videoCounter(json_object* contents) @@ -57,7 +63,30 @@ int videoCounter(json_object* contents) Video* generateVideos(json_object* contents) { - Video *videos = (Video *) calloc(json_object_array_length(contents), sizeof(Video)); + char idPath[][MAXLENGTH] = { + "videoId" + }; + + char titlePath[][MAXLENGTH] = { + "title", + "runs", + "0", + "text" + }; + + char authorPath[][MAXLENGTH] = { + "ownerText", + "runs", + "0", + "text" + }; + + char lengthPath[][MAXLENGTH] = { + "lengthText", + "simpleText" + }; + + Video *videos = (Video *) calloc(videoCounter(contents), sizeof(Video)); for ( int i = 0, k = 0; i < json_object_array_length(contents); i++ ) { @@ -65,42 +94,30 @@ Video* generateVideos(json_object* contents) char title[TITLE + 1], author[AUTHOR + 1], id[VIDEOID + 1], duration[DURATION + 1]; json_object *iterator = json_object_array_get_idx(contents, i), *dataHolder; - // Grabbing the information + // Grabbing the information from each path of the JSON iterator = json_object_object_get(iterator, "videoRenderer"); if ( json_object_get_type(iterator) != json_type_null ) { - dataHolder = json_object_object_get(iterator, "videoId"); + // ID Path + dataHolder = objectPathNavigator(iterator, idPath, sizeof(idPath)/sizeof(idPath[0])); strcpy(id, json_object_get_string(dataHolder)); // Title Path - dataHolder = json_object_object_get(iterator, "title"); - dataHolder = json_object_object_get(dataHolder, "runs"); - dataHolder = json_object_array_get_idx(dataHolder, 0); - dataHolder = json_object_object_get(dataHolder, "text"); + dataHolder = objectPathNavigator(iterator, titlePath, sizeof(titlePath)/sizeof(titlePath[0])); strcpy(title, json_object_get_string(dataHolder)); - // Video Length Path - dataHolder = json_object_object_get(iterator, "lengthText"); - if ( json_object_get_type(dataHolder) != json_type_null ) - { - dataHolder = json_object_object_get(dataHolder, "simpleText"); - strcpy(duration, json_object_get_string(dataHolder)); - } - else - { - strcpy(duration, "LIVE NOW"); - } - // Author Path - dataHolder = json_object_object_get(iterator, "ownerText"); - dataHolder = json_object_object_get(dataHolder, "runs"); - dataHolder = json_object_array_get_idx(dataHolder, 0); - dataHolder = json_object_object_get(dataHolder, "text"); + dataHolder = objectPathNavigator(iterator, authorPath, sizeof(authorPath)/sizeof(authorPath[0])); strcpy(author, json_object_get_string(dataHolder)); - // Creating Video Objects + // Video Length Path + dataHolder = objectPathNavigator(iterator, lengthPath, sizeof(lengthPath)/sizeof(lengthPath[0])); + ( json_object_get_type(dataHolder) != json_type_null ) ? strcpy(duration, json_object_get_string(dataHolder)) : strcpy(duration, "LIVE NOW") ; + + // Creating Videos Data videos[k++] = createVideo(title, author, id, duration); } } + return videos; } -- cgit v1.2.3