From ae7a4d4229d640b97c501ed35dadf43713282ba1 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Sat, 18 Sep 2021 00:30:59 +0100 Subject: Found New Faster Algorithm --- include/utils.h | 3 -- src/utils.c | 98 +++------------------------------------------------------ 2 files changed, 5 insertions(+), 96 deletions(-) diff --git a/include/utils.h b/include/utils.h index 48ada10..3dd902a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,9 +1,6 @@ #pragma once #include "video.h" -int tokenCount (char *string, char *delimiter); -char **tokenizer (char *string, char *delimiter); -void freeTokens (char **tokens, int size); char *queryNormalizer (char *query); char *extractQueryJSON (char *youtubeurl); int checkNumber (char *num); diff --git a/src/utils.c b/src/utils.c index 5391e6d..f004aa4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,73 +7,7 @@ #include #include -int -tokenCount (char *string, char *delimiter) -{ - int totalCount = 0; - - if (string != NULL && delimiter != NULL) - { - char *auxString = (char *)calloc (strlen (string) + 1, sizeof (char)); - strcpy (auxString, string); - - char *token = strtok (auxString, delimiter); - - while (token != NULL) - { - totalCount++; - token = strtok (NULL, delimiter); - } - - free (auxString); - } - - return totalCount; -} - -char ** -tokenizer (char *string, char *delimiter) -{ - char **tokens = NULL; - - if (string != NULL && delimiter != NULL) - { - char *auxString = (char *)calloc (strlen (string) + 1, sizeof (char)); - strcpy (auxString, string); - - tokens = (char **)calloc (tokenCount (auxString, delimiter), - sizeof (char *)); - char *tempToken = strtok (auxString, delimiter); - - for (int i = 0; tempToken != NULL; i++) - { - tokens[i] = (char *)calloc (strlen (tempToken) + 1, sizeof (char)); - strcpy (tokens[i], tempToken); - tempToken = strtok (NULL, delimiter); - } - - free (auxString); - } - else - { - tokens = (char **)calloc (1, sizeof (char)); - } - - return tokens; -} - -void -freeTokens (char **tokens, int size) -{ - if (size > 0) - { - for (int i = 0; i < size; i++) - { - free (tokens[i]); - } - } - free (tokens); -} +#define JSONVAR "ytInitialData = " char * queryNormalizer (char *query) @@ -153,34 +87,12 @@ extractQueryJSON (char *youtubeurl) char *htmlPage = downloadPage (youtubeurl); if (htmlPage != NULL && strlen (htmlPage) > 0) { - // Setting Up Vars - char jsonVar[] = "ytInitialData", - needlessHTML[] = "; 0 ) { - if (strcmp (tokens[i], jsonVar) == 0) - { - for (j = i + 2; strstr (tokens[j - 1], needlessHTML) == NULL; - j++) - { - json = realloc ( - json, (strlen (json) + strlen (tokens[j]) + 2)); - strcat (json, tokens[j]); - strcat (json, " "); - } - } + json = (char *) calloc(strlen(jsonVar) + 1, sizeof(char)); + strcpy(json, jsonVar); } - - json[strlen (json) - strlen (needlessHTML)] = '\0'; - freeTokens (tokens, numberOfTokens); } free (htmlPage); } -- cgit v1.2.3