aboutsummaryrefslogtreecommitdiff
path: root/src/Scraping
diff options
context:
space:
mode:
Diffstat (limited to 'src/Scraping')
-rw-r--r--src/Scraping/ImageScraping.c46
1 files changed, 13 insertions, 33 deletions
diff --git a/src/Scraping/ImageScraping.c b/src/Scraping/ImageScraping.c
index 2341244..a3d5773 100644
--- a/src/Scraping/ImageScraping.c
+++ b/src/Scraping/ImageScraping.c
@@ -1,5 +1,6 @@
#include "ImageScraping.h"
#include "../Utility/HttpClient.h"
+#include "../Utility/Unescape.h"
#include "Config.h"
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
@@ -29,51 +30,30 @@ static char *build_proxy_url(const char *image_url) {
}
static char *extract_json_string(const char *json, const char *key) {
- if (!json || !key)
- return NULL;
+ if (!json || !key) return NULL;
char search_key[64];
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
const char *key_pos = strstr(json, search_key);
- if (!key_pos)
- return NULL;
+ if (!key_pos) return NULL;
const char *colon = strchr(key_pos + strlen(search_key), ':');
- if (!colon)
- return NULL;
+ if (!colon) return NULL;
colon++;
- while (*colon == ' ' || *colon == '\t' || *colon == '\n' || *colon == '\r')
- colon++;
-
- if (*colon != '"')
- return NULL;
+ while (*colon == ' ' || *colon == '\t' || *colon == '\n' || *colon == '\r') colon++;
+ if (*colon != '"') return NULL;
colon++;
- size_t len = 0;
- const char *start = colon;
- while (*colon && *colon != '"') {
- if (*colon == '\\' && *(colon + 1))
- colon++;
- colon++;
- len++;
- }
-
- char *result = malloc(len + 1);
- if (!result)
- return NULL;
-
- colon = start;
- size_t i = 0;
- while (*colon && *colon != '"') {
- if (*colon == '\\' && *(colon + 1))
- colon++;
- result[i++] = *colon++;
+ const char *end = colon;
+ while (*end && *end != '"') {
+ if (*end == '\\' && end[1]) end += 2;
+ else end += 1;
}
- result[i] = '\0';
+ if (*end != '"') return NULL;
- return result;
+ return json_string_unescape(colon, end);
}
static int parse_iusc_node(xmlNodePtr node, ImageResult *result) {
@@ -140,7 +120,7 @@ int scrape_images(const char *query, int page, ImageResult **out_results,
return -1;
}
- htmlDocPtr doc = htmlReadMemory(resp.memory, resp.size, NULL, NULL,
+ htmlDocPtr doc = htmlReadMemory(resp.memory, resp.size, NULL, "UTF-8",
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR);
if (!doc) {
http_response_free(&resp);