googlefs: fix parsing

This commit is contained in:
François Revol
2015-11-08 04:48:37 +01:00
parent 26b35e649f
commit 02487f3c4b
3 changed files with 17 additions and 28 deletions
@@ -35,7 +35,7 @@
#define FMT_Q "&q=%s"
/* parse_google_html.c */
extern int google_parse_results(const char *html, size_t htmlsize, struct google_result **results);
extern int google_parse_results(const char *html, size_t htmlsize, long *nextid, struct google_result **results);
// move that to ksocket inlined
static int kinet_aton(const char *in, struct in_addr *addr)
@@ -150,7 +150,7 @@ status_t google_request_process(struct google_request *req)
close(fd);
}
#endif /* FAKE_INPUT */
err = count = google_parse_results(req->cnx->data, req->cnx->datalen, &req->results);
err = count = google_parse_results(req->cnx->data, req->cnx->datalen, &req->nextid, &req->results);
if (err < 0)
goto err_get;
#ifdef DO_PUBLISH
@@ -12,6 +12,7 @@ struct google_request {
char *query_string;
struct http_cnx *cnx;
struct google_result *results;
long nextid;
};
/* those are quite arbitrary values */
@@ -34,9 +34,9 @@ int dbgstep = 0;
//#define G_BEGIN_URL "<p class=g><a class=l href=\""
//#define G_BEGIN_URL "<div class=g><a class=l href=\""
//#define G_BEGIN_URL "<div class=g><a href=\""
#define G_BEGIN_URL "<li class=g><h3 class=r><a href=\""
#define G_BEGIN_URL "<h3 class=\"r\"><a href=\""
//#define G_END_URL "\">"
#define G_END_URL "\" class=l>"
#define G_END_URL "\">"
//#define G_BEGIN_NAME
#define G_END_NAME "</a>"
#define G_BEGIN_SNIPSET /*"<td class=j>"*/"<font size=-1>"
@@ -44,15 +44,14 @@ int dbgstep = 0;
#define G_BEGIN_CACHESIM " <a class=fl href=\""
#define G_END_CACHESIM "\">"
int google_parse_results(const char *html, size_t htmlsize, struct google_result **results)
int google_parse_results(const char *html, size_t htmlsize, long *nextid, struct google_result **results)
{
struct google_result *res = NULL, *nres = NULL, *prev = NULL;
char *p, *q;
char *nextresult = NULL;
long numres = 0;
long maxres = 0;
long startid = 0;
long lastid = 0;
long maxres = 1000;
//long startid = 0;
int done = 0;
int err = ENOMEM;
@@ -63,8 +62,10 @@ int google_parse_results(const char *html, size_t htmlsize, struct google_result
PRST;
/* google now sends <!doctype html><head> sometimes... */
if (strstr(html, "<!doctype html><head>") != html) {
if (strstr(html, "<html><head>") != html)
return EINVAL;
if (strstr(html, "<html><head>") != html) {
if (strstr(html, "<!doctype html><html ") != html)
return EINVAL;
}
}
PRST;
// p = strstr(html, "<title>Google Search:");
@@ -74,26 +75,12 @@ int google_parse_results(const char *html, size_t htmlsize, struct google_result
p = strstr(html, "<body");
if (!p) return EINVAL;
PRST;
p = strstr(html, ">&nbsp;Results <b>");
if (!p) return EINVAL;
PRST;
p+= strlen(">&nbsp;Results <b>");
startid = strtol(p, &p, 10);
if (!p) return EINVAL;
PRST;
p = strstr(html, "</b> - <b>");
p+= strlen("</b> - <b>");
if (!p) return EINVAL;
PRST;
lastid = strtol(p, &p, 10);
if (!p) return EINVAL;
PRST;
maxres = lastid - startid + 1;
printf(DBG"getting %ld results (%ld to %ld)\n", maxres, startid, lastid);
/*
p = strstr(html, "Search Results<");
if (!p) return EINVAL;
PRST;
*/
printf(DBG"parsing...\n");
@@ -111,7 +98,7 @@ int google_parse_results(const char *html, size_t htmlsize, struct google_result
goto err0;
}
memset(nres, 0, sizeof(struct google_result));
nres->id = startid + numres; //- 1;
nres->id = (*nextid)++; //- 1;
PRST;
/* find url */
@@ -273,13 +260,14 @@ int main(int argc, char **argv)
size_t len;
char *p;
int err;
long nextid = 0;
p = malloc(BUFSZ+8);
len = read(0, p+4, BUFSZ);
p[BUFSZ+4-1] = '\0';
*(uint32 *)p = 0xa5a5a5a5;
*(uint32 *)(&p[BUFSZ+4]) = 0x5a5a5a5a;
err = google_parse_results(p+4, len, &results);
err = google_parse_results(p+4, len, &nextid, &results);
printf("error 0x%08lx\n", err);
if (err < 0)
return 1;