summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorrafa_99 <raroma09@gmail.com>2021-09-01 17:45:10 +0100
committerrafa_99 <raroma09@gmail.com>2021-09-01 17:45:10 +0100
commite78400340219923af5c4f6ff3c55b5956cb6223a (patch)
tree8149715be3624b7ec587d9bf2ed118fa2b58f1c8 /libs
parente2ffc88b409b54f7a23d963095ea3a2439a361c1 (diff)
Grabbed video info from JSON and created Video Structures with it
Diffstat (limited to 'libs')
-rw-r--r--libs/json.c77
-rw-r--r--libs/json.h3
2 files changed, 74 insertions, 6 deletions
diff --git a/libs/json.c b/libs/json.c
index 3631c19..1c8bf55 100644
--- a/libs/json.c
+++ b/libs/json.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <json-c/json.h>
#include "json.h"
+#include "../include/video.h"
#define MAXLENGTH 256
json_object* jsonParseString(char* stringedJSON)
@@ -23,19 +24,83 @@ json_object* navigateToVideos(json_object* jsonRoot)
"contents"
};
- json_object *videos = json_object_object_get(jsonRoot, path[0]);
+ json_object *navigator = json_object_object_get(jsonRoot, path[0]);
for( int i = 1; i < sizeof(path)/sizeof(path[0]); i++ )
{
- if ( json_object_get_type(videos) == json_type_array )
+ 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;
+}
+
+int videoCounter(json_object* contents)
+{
+ int counter = 0;
+ json_object *iterator;
+
+ for ( int i = 0; i < json_object_array_length(contents) ; i++)
+ {
+ iterator = json_object_array_get_idx(contents, i);
+ iterator = json_object_object_get(iterator, "videoRenderer");
+
+ if ( json_object_get_type(iterator) != json_type_null )
{
- videos = json_object_array_get_idx(videos, atoi(path[i]));
+ counter++;
}
- else
+ }
+
+ return counter;
+}
+
+Video* generateVideos(json_object* contents)
+{
+ Video *videos = (Video *) calloc(json_object_array_length(contents), sizeof(Video));
+
+ for ( int i = 0, k = 0; i < json_object_array_length(contents); i++ )
+ {
+ // Initializing Vars For Each Video Structure
+ 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
+ iterator = json_object_object_get(iterator, "videoRenderer");
+ if ( json_object_get_type(iterator) != json_type_null )
{
- videos = json_object_object_get(videos, path[i]);
+ dataHolder = json_object_object_get(iterator, "videoId");
+ 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");
+ 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");
+ strcpy(author, json_object_get_string(dataHolder));
+
+ // Creating Video Objects
+ videos[k++] = createVideo(title, author, id, duration);
}
}
-
return videos;
}
diff --git a/libs/json.h b/libs/json.h
index f769d9c..149176d 100644
--- a/libs/json.h
+++ b/libs/json.h
@@ -1,5 +1,8 @@
#pragma once
#include <json-c/json.h>
+#include "../include/video.h"
json_object* jsonParseString(char* stringedJSON);
json_object* navigateToVideos(json_object* jsonRoot);
+int videoCounter(json_object* contents);
+Video* generateVideos(json_object* contents);