summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.c')
-rw-r--r--src/api.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/api.c b/src/api.c
index a366882..1a8c85a 100644
--- a/src/api.c
+++ b/src/api.c
@@ -124,7 +124,10 @@ int sc_fix_url (char ** h) { /* fixes a (result) URL in-place (removes tracking
} /* TODO: be pedantic and remove utm_source and other tracking bullshit */
return 1;
}
-struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct sc_query * q, SC_OPT_TYPE opt) { /* check4cachedB4 */
+enum sc_return sc_query_google (const char * s, /* breaking change: changed return type */
+ struct sc_cache * c,
+ struct sc_query * q,
+ SC_OPT_TYPE opt) { /* check4cachedB4 */
/* query is in most cases NULL. then it will be allocated and put into sc_cache. otherwise response will be put into passed q. */
/* if query is not NULL, it MUST be initialized */
/*
@@ -144,7 +147,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
* I couldn't find anything with ratings, so we won't parse thouse either yet
* captcha: google knows that this nokia phone we're pretending to be doesn't support javascript, but does not care, and loads an obfuscated captcha anyways that would be hard to defeat for now without some kind of chromium emulation we really don't want.
*/
- int rs = 1;
+ enum sc_return rs = 1;
char * xpath = NULL;
char * descclass = NULL;
char * titleclass = NULL;
@@ -152,7 +155,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
htmlDocPtr xmldoc = NULL;
char * txtdoc = NULL;
if (!s || !c) {
- rs = -1;
+ rs = SC_BADCALL;
goto rc;
}
int qwasgiven = 0;
@@ -168,15 +171,18 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
free(us);
if (!txtdoc) {
SC_LOG(SC_LOG_ERROR, c, "!txtdoc");
- rs = -2;
+ rs = SC_EMPTYRESPONSE;
+ goto rc;
+ }
+ if (!strstr(txtdoc, "In the meantime, solving the above CAPTCHA will let you continue")) {
+ rs = SC_CAPTCHA;
goto rc;
}
- /* TODO: check if response is asking for a captcha */
if (opt & SC_OPT_IMAGE) {
imageclass = sc_find_class(txtdoc, "{font-family:Roboto,Helvetica,Arial,sans-serif}");
if (!imageclass) {
SC_LOG(SC_LOG_ERROR, c, "!imageclass, txtdoc = %s", txtdoc);
- rs = -3;
+ rs = SC_NOIMGCLASS;
goto rc;
}
} else {
@@ -184,7 +190,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
descclass = sc_find_class(txtdoc, "{word-break:break-word}");
if (!titleclass || !descclass) {
SC_LOG(SC_LOG_ERROR, c, "!titleclass || !descclass, txtdoc = %s", txtdoc);
- rs = -4;
+ rs = SC_NOCLASS;
goto rc;
}
}
@@ -207,7 +213,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
char * hreflink = (char *) xmlGetProp(node, BAD_CAST "href"); /* xmlGetProp copies and allocates */
if (!hreflink) {
SC_LOG(SC_LOG_ERROR, c, "!hreflink");
- rs = -5;
+ rs = SC_NOHREF;
return;
}
if (opt & SC_OPT_IMAGE) {
@@ -218,7 +224,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s
xmlFree(hreflink);
if (!imgurl && !imgrefurl) {
SC_LOG(SC_LOG_ERROR, c, "!imgurl && !imgrefurl, txtdoc = %s", txtdoc);
- /* rs = -6; */ /* we continue running not fail because of a single picture */
+ /* rs = -7; */ /* we continue running not fail because of a single picture */
free(imgurl);
free(imgrefurl);
return; /* check! */
@@ -313,5 +319,5 @@ rc:
free(descclass);
free(imageclass);
free(xpath);
- return (rs < 0) ? NULL : q;
+ return rs;
}