/
googlev1.c
201 lines (168 loc) · 5.55 KB
/
googlev1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright (C) 2011 Michael D. Täht
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.gnu.org/licenses/
*/
/* This engine implements a:
gnugol -> google web api -> gnugol json translator engine
using the google web api v1, which was deprecated Nov 1, 2010.
According to google's API policy, they will no longer provide
this api 3 years from that date. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <jansson.h>
#include <ctype.h>
#include <curl/curl.h>
#include <iconv.h>
#include "query.h"
#include "utf8.h"
#include "handy.h"
#include "formats.h"
#include "gnugol_engines.h"
#define TEMPLATE "http://ajax.googleapis.com/ajax/services/search/web?v=1.0"
#define LICENSE_URL "I don't know if you can even get one anymore."
#define TOU "http://code.google.com/apis/websearch/terms.html"
const char description[] = "API is nearly feature complete, with support for multiple language input, output, safe search, position, and number of results.";
/* See options at
http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
including:
rsz= large | small (8 vs 4)
start= <0-indexed offset>
hl= <language of searcher>
lr= <language of results>
safe= active | moderate | off
*/
/* Just plain overkill unless we start taking this as a string on
the command line
static const gnugol_intmap_t safe_map[] = {
{ 0, "off" },
{ 1, "moderate" },
{ 2, "active" },
{-1, NULL }
};
*/
static const char *safe_map[] = {
"off", "moderate", "active", NULL
};
int setup(QueryOptions_t *q) {
char string[URL_SIZE];
char key [256];
size_t size;
char hl[8];
char lr[8];
hl[0] = lr[0] = '\0';
if(q->debug > 9) GNUGOL_OUTW(q,"Entering Setup\n");
size = sizeof(key);
if (gnugol_read_key(key,&size,".googlekey") != 0)
{
GNUGOL_OUTE(q,"A license key to search google is recommended. "
"You can get one from: %s",LICENSE_URL);
size = 0;
}
if(q->safe < 0) q->safe = 0;
if(q->safe > 2) q->safe = 2;
if(q->debug > 5) GNUGOL_OUTW(q,"google: safesearch=%s\n",safe_map[q->safe]);
if(q->nresults > 8) q->nresults = 8; // google enforced
if(q->debug > 4) GNUGOL_OUTW(q,"google: KEYWORDS = %s\n", q->keywords);
/*
While we can't be sure the user's two letter code is supported
we CAN try harder to not mess up than this.
*/
if((q->input_language[0] != '\0') &&
(isalpha(q->input_language[0])) &&
(isalpha(q->input_language[1]))) {
hl[0] = '&';
hl[1] = 'h';
hl[2] = 'l';
hl[3] = '=';
hl[4] = q->input_language[0];
hl[5] = q->input_language[1];
hl[6] = '\0';
}
if((q->output_language[0] != '\0') &&
(isalpha(q->output_language[0])) &&
(isalpha(q->output_language[1]))) {
lr[0] = '&';
lr[1] = 'l';
lr[2] = 'r';
lr[3] = '=';
lr[4] = q->output_language[0];
lr[5] = q->output_language[1];
lr[6] = '\0';
}
if (size == 0)
size = snprintf(string,URL_SIZE,
"%s&rsz=%d&start=%d&safe=%s%s%s&q=%s",
TEMPLATE,
q->nresults,q->position,
safe_map[q->safe], hl, lr, q->keywords);
else
size = snprintf(string,URL_SIZE,
"%s&key=%s&rsz=%d&start=%d&safe=%s%s%s&q=%s",
TEMPLATE,key,
q->nresults,q->position,
safe_map[q->safe], hl, lr, q->keywords);
if (size > sizeof(q->querystr))
{
GNUGOL_OUTE(q,"Size of URL exceeds space set aside for it");
return -1;
}
strcpy(q->querystr,string);
if(q->debug > 9) GNUGOL_OUTW(q,"Exiting Setup\n");
return (int)size;
}
// turn quotes back into quotes and other utf-8 stuff
// FIXME: Error outs cause a memory leak from "root"
// use thread local storage? or malloc for the buffer
// FIXME: do fuller error checking
// Fuzz inputs!
// Maybe back off the number of results when we overflow the buffer
// This engine takes advantage (abuses!) the CPP pasting tokens
// with a couple macros to make the interface to json a 1 to 1 relationship
// The code is delightfully short this way.
int search(QueryOptions_t *q) {
unsigned int i;
char *text;
json_t *root,*responseData, *results;
json_error_t error;
if(q->debug) GNUGOL_OUTW(q,"%s: trying url: %s\n", q->engine_name, q->querystr);
text = jsonrequest(q->querystr);
if(!text) {
GNUGOL_OUTE(q,"url failed to work: %s", q->querystr);
return -1;
}
root = json_loads(text, &error);
free(text);
if(!root)
{
GNUGOL_OUTE(q,"error: on line %d: %s\n", error.line, error.text);
return -1;
}
GETOBJ(root,responseData);
GETARRAY(responseData,results);
gnugol_header_out(q);
for(i = 0; i < json_array_size(results); i++)
{
json_t *result, *url, *titleNoFormatting, *content;
GETARRAYIDX(results,result,i);
GETSTRING(result,url);
GETSTRING(result,titleNoFormatting);
GETSTRING(result,content);
gnugol_result_out(q,jsv(url),jsv(titleNoFormatting),jsv(content));
}
gnugol_footer_out(q);
// FIXME: Go recursive if we overflowed the buffer
json_decref(root);
return 0;
}