diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/utils.h (renamed from utils.h) | 0 | ||||
-rw-r--r-- | include/video.h (renamed from video.h) | 10 | ||||
-rw-r--r-- | libs/json.c | 77 | ||||
-rw-r--r-- | libs/json.h | 3 | ||||
-rw-r--r-- | src/utils.c (renamed from utils.c) | 4 | ||||
-rw-r--r-- | src/video.c (renamed from video.c) | 16 |
7 files changed, 88 insertions, 24 deletions
@@ -2,7 +2,7 @@ # See LICENSE file for copyright and license details. # Source Code -SRC = queryt.c utils.c video.c libs/curl.c libs/string.c libs/json.c +SRC = queryt.c src/utils.c src/video.c libs/curl.c libs/string.c libs/json.c # Libraries LIBS = -lcurl -ljson-c diff --git a/utils.h b/include/utils.h index 987b7fe..987b7fe 100644 --- a/utils.h +++ b/include/utils.h diff --git a/video.h b/include/video.h index 59ee2ae..e51721d 100644 --- a/video.h +++ b/include/video.h @@ -1,24 +1,22 @@ #pragma once -#include <time.h> #define TITLE 100 #define AUTHOR 120 #define VIDEOID 11 -#define DESCRIPTION 5000 +#define DURATION 8 /* * title -> max 100 characters * author -> max 120 characters combined * video id -> max 11 characters - * video description -> max 5000 characters + * duration -> max 8 characters * thumbnail -> https://i.ytimg.com/vi/[id]/hq720.jpg */ typedef struct video { - char *title, *author, *id, *description; - time_t duration; + char *title, *author, *id, *duration; } Video; -Video createVideo(char *title, char *author, char *id, char *description, time_t duration); +Video createVideo(char *title, char *author, char *id, char *duration); void freeVideo(Video *v); 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); @@ -2,8 +2,8 @@ #include <stdlib.h> #include <string.h> #include "utils.h" -#include "libs/curl.h" -#include "libs/string.h" +#include "../libs/curl.h" +#include "../libs/string.h" int tokenCount(char *string, char *delimiter) { @@ -1,9 +1,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "video.h" +#include "../include/video.h" -Video createVideo(char *title, char *author, char *id, char *description, time_t duration) +Video createVideo(char *title, char *author, char *id, char *duration) { Video video; @@ -25,18 +25,16 @@ Video createVideo(char *title, char *author, char *id, char *description, time_t strcpy(video.id, id); } - if (description != NULL && strlen(description) > 0) + if (duration != NULL && strlen(duration) > 0) { - video.description = (char *) calloc(DESCRIPTION + 1, sizeof(char)); - strcpy(video.description, description); + video.duration = (char *) calloc(DURATION + 1, sizeof(char)); + strcpy(video.duration, duration); } else { - video.description = (char *) calloc(1, sizeof(char)); + video.duration = (char *) calloc(1, sizeof(char)); } - video.duration = duration; - return video; } @@ -45,5 +43,5 @@ void freeVideo(Video *v) free(v->title); free(v->author); free(v->id); - free(v->description); + free(v->duration); } |