summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrafa_99 <raroma09@gmail.com>2021-09-01 18:45:53 +0100
committerrafa_99 <raroma09@gmail.com>2021-09-01 18:45:53 +0100
commit99df106665a51925c4f918bf8990c7899c0c0216 (patch)
treea1b51f8837b931d04aa585062453e94d005c2e7d
parente78400340219923af5c4f6ff3c55b5956cb6223a (diff)
Refactored Navigation Algorithm
-rw-r--r--libs/json.c87
-rw-r--r--libs/json.h2
2 files changed, 54 insertions, 35 deletions
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 <json-c/json.h>
#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;
}
diff --git a/libs/json.h b/libs/json.h
index 149176d..7c3cf05 100644
--- a/libs/json.h
+++ b/libs/json.h
@@ -1,8 +1,10 @@
#pragma once
#include <json-c/json.h>
#include "../include/video.h"
+#define MAXLENGTH 256
json_object* jsonParseString(char* stringedJSON);
+json_object* objectPathNavigator(json_object* object, char path[][MAXLENGTH], int depth);
json_object* navigateToVideos(json_object* jsonRoot);
int videoCounter(json_object* contents);
Video* generateVideos(json_object* contents);