#include "http_curl.h"
#include <curl/curl.h>
#include <json-c/json.h>
#include <json-c/json_util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *url_encode(char *s) { return curl_easy_escape(NULL, s, 0); }

char *web_search_news(char *q) {
  char *news = malloc(4096);
  news[0] = 0;
  char *q_encoded = url_encode(q);
  sprintf(news,
          "https://search.molodetz.nl/search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3Asf0LZ3tOtJ09g4DIFCibGfG_6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4BOI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2GT519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXAgSS3fycyAZQptxP1fNXJqlmyKx4XKCWOHgilU7muIEjNCO2UkRhUF5R8bEcOnPEQVsevALzeycNK4lWKZfmOznSHYej6HArF6FVxPnr-EynH72N7VnHEAlPJXO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pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
          q_encoded);
  free(q_encoded);
  char *ret = curl_get(news);
  free(news);
  json_object *json_ret = json_tokener_parse(ret);

  json_object *json_results = json_object_object_get(json_ret, "results");
  json_object *json_result = json_object_array_get_idx(json_results, 0);
  if (!json_result) {
    json_object_put(json_ret);
    free(ret);
    return web_search_news(q);
  }
  json_object_put(json_ret);
  return ret;
}

char *web_search(char *q) {
  char *news = (char *)malloc(4096);
  news[0] = 0;
  char *q_encoded = url_encode(q);
  sprintf(news, "https://search.molodetz.nl/search?q=%s&format=json&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZ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hWWjNH3DI5LMV7gQd7GehKmbIe_NRBSymdvgVawUnxqVKpce3bREefaCubIU0uyICdEXCXy3qMeS63k5ljTeCz3tbYKb2mHj2qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
          q_encoded);
  free(q_encoded);
  char *ret = curl_get(news);
  free(news);
  json_object *json_ret = json_tokener_parse(ret);

  json_object *json_results = json_object_object_get(json_ret, "results");
  json_object *json_result = json_object_array_get_idx(json_results, 0);
  if (!json_result) {
    json_object_put(json_ret);
    free(ret);
    return web_search(q);
  }
  json_object_put(json_ret);
  return ret;
}

char *web_search_engine(char *q) {
  char *searx = malloc(4096);
  searx[0] = 0;
  sprintf(searx, "https://searx.molodetz.nl/search?q=%s&format=json", q);
  char *ret = curl_get(searx);
  free(searx);
  return ret;
}
