From de404a7e66132b918410c3770c3b802b4052dc02 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Fri, 3 Sep 2021 22:27:03 +0100 Subject: Some more Main changes --- queryt.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/queryt.c b/queryt.c index 546cf64..a91f234 100644 --- a/queryt.c +++ b/queryt.c @@ -2,15 +2,21 @@ #include #include #include +#include #include "include/utils.h" #include "include/video.h" +#include "libs/curl.h" -void errorMessage(int errorCode, char* message); -void processOptions(int *options); +void processOptions(int *options, char **args); int main(int argc, char **argv) { int o = 0, opts[] = { 0, 0, 0, 0, 0 }; + char **args = (char **) calloc(3, sizeof(char *)); + for ( int i = 0; i < 3; i++ ) + { + args[i] = (char *) calloc(1, sizeof(char *)); + } static struct option long_options[] = { {"format", required_argument, 0, 'f'}, @@ -27,35 +33,88 @@ int main(int argc, char **argv) { case 'f': opts[0]++; + args[0] = (char *) realloc(args[0], strlen(optarg) + 1); + strcpy(args[0], optarg); break; case 'h': opts[1]++; break; case 'm': opts[2]++; + args[1] = (char *) realloc(args[1], strlen(optarg) + 1); + strcpy(args[1], optarg); break; case 's': opts[3]++; + args[2] = (char *) realloc(args[2], strlen(optarg) + 1); + strcpy(args[2], optarg); break; case 'v': opts[4]++; - break; - default: - errorMessage(1, ""); } } - processOptions(opts); + processOptions(opts, args); + + for ( int i = 0; i < 3; i++ ) + { + free(args[i]); + } + free(args); + return 0; } -void errorMessage(int errorCode, char* message) +void processOptions(int *options, char **args) { - printf("%s", message); - exit(errorCode); -} + if ( !options[1] && options[3] && !options[4] ) + { + char *query = queryNormalizer(args[2]); + if ( query != NULL ) + { + char *link = (char *) calloc(strlen("https://www.youtube.com/results?search_query=") + strlen(query) + 1, sizeof(char)); + strcpy(link, "https://www.youtube.com/results?search_query="); + strcat(link, query); -void processOptions(int *options) -{ - //ToDo + char *json = extractQueryJSON(link); + free(link); + free(query); + + if ( json != NULL && strlen(json) > 0 ) + { + json_object *queryRoot = json_tokener_parse(json), *handler; + free(json); + if ( queryRoot != NULL && json_object_get_type(queryRoot) != json_type_null ) + { + } + else + { + json_object_put(queryRoot); + } + } + else + { + free(json); + } + + } + } + else + { + if ( options[1] || (options[3] && options[4])) + { + puts("Help Message"); + } + else + { + if ( options[4] ) + { + puts("Version Message"); + } + else + { + puts("-s is required!"); + } + } + } } -- cgit v1.2.3