-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_yandex.cpp
281 lines (225 loc) · 6.86 KB
/
engine_yandex.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Web-shooter is a shoot them up game with random graphics.
Copyright (C) 2013-2018 [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "SDL.h"
#include "SDL_image.h"
#include <SDL_opengl.h>
#include "errno.h"
#include "debug.h"
#include "parameter.h"
#include "common.h"
#include "network.h"
#include "loader.h"
#include <pthread.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string>
#define TIME_BETWEEN_REQUEST 5
static std::string size_string[SIZE_NUM] = {
"small"
,"medium"
,"large"
};
static std::string filter_string[FILTER_NUM] = {
"&ncrnd=5290"
,"&ncrnd=9763"
};
typedef struct internal {
network_page_t * page;
int page_num;
int read_index;
const char * image_size;
char * keyword;
const char * filter;
pthread_mutex_t page_mutex;
} internal_t;
time_t time_last_request;
/*******************************
create_url
You MUST free the return string
******************************/
static char * create_url(internal_t * internal)
{
char buf[1024];
char * url = NULL;
char word[1024];
url_percent(internal->keyword,word);
sprintf(buf,"https://www.yandex.com/images/search?p=%d&text=%s&isize=%s%s",internal->page_num*5,word,internal->image_size,internal->filter);
printd(DEBUG_HTTP,"Creating URL : %s\n",buf);
url = strdup(buf);
return url;
}
/*******************************
get_response_page
return -1 on error
******************************/
static int get_response_page(internal_t * internal)
{
char * url;
time_t time_current;
url = create_url(internal);
if(url == NULL) {
printd(DEBUG_ERROR,"Can't get URL from Yandex engine\n");
return -1;
}
time_current = time(NULL);
while( time_current < time_last_request + TIME_BETWEEN_REQUEST ) {
printd(DEBUG_ERROR,"Waiting a bit before next request\n");
usleep(1000000);
time_current = time(NULL);
}
time_last_request = time(NULL);
if ( web_to_memory(url,internal->page) == -1 ) {
printd(DEBUG_ERROR,"web_to_memory error\n");
free(url);
return -1;
}
free(url);
internal->page_num++;
return 0;
}
/*******************************
parse_response_page
return string MUST be freed
******************************/
static char * parse_response_page(internal_t * internal)
{
char * substring = NULL;
char * substring_start = NULL;
char * substring_end = NULL;
char * url = NULL;
if( internal == NULL || internal->page == NULL || internal->page->data == NULL) {
printd(DEBUG_ERROR,"Invalid memory block\n");
return 0;
}
substring=strstr(internal->page->data + internal->read_index,"fullscreen"");
if( substring == NULL ) {
printd(DEBUG_ERROR,"No more URL on page %d\n",internal->page_num);
return NULL;
}
/* get the url */
substring_start=strstr(substring,"http");
substring_end = strstr(substring_start+strlen("http"),""");
url = strndup(substring_start,substring_end-substring_start);
printd(DEBUG_URL,"URL: %s\n",url);
internal->read_index = substring_end - internal->page->data;
return url;
}
/******************************
engine_destroy
return 0 if no error
******************************/
static int engine_destroy(engine_t * engine)
{
internal_t * internal = (internal_t*)engine->internal;
if(internal) {
pthread_mutex_destroy(&internal->page_mutex);
if(internal->page) {
if(internal->page->data) {
free(internal->page->data);
}
free(internal->page);
}
if(internal->keyword) {
free(internal->keyword);
}
free(internal);
}
return 0;
}
/*******************************
engine_get_url
Return string MUST be freed
******************************/
static char * engine_get_url(engine_t * engine)
{
int first_page = FALSE;
char * url = NULL;
int res;
internal_t * internal = static_cast<internal_t*>(engine->internal);
pthread_mutex_lock(&internal->page_mutex);
while( url == NULL ) {
while( internal->page->data == NULL ) {
printd(DEBUG_PAGE | DEBUG_HTTP,"Reading result page %d for keyword \"%s\"\n",internal->page_num,internal->keyword);
res = get_response_page(internal);
if( res == -1 || internal->page->data == NULL) {
printd(DEBUG_PAGE | DEBUG_HTTP,"Can not get result page %d for keyword \"%s\", starting back\n",internal->page_num,internal->keyword);
internal->read_index = 0;
internal->page_num = 0;
if (first_page == TRUE) {
printd(DEBUG_PAGE | DEBUG_HTTP,"No URL for keyword \"%s\"\n",internal->keyword);
pthread_mutex_unlock(&internal->page_mutex);
return NULL;
}
first_page = TRUE;
} else {
printd(DEBUG_PAGE | DEBUG_HTTP,"Got result page %d for keyword \"%s\"\n",internal->page_num-1,internal->keyword);
}
}
url = parse_response_page(internal);
if( url == NULL ) {
printd(DEBUG_PAGE | DEBUG_HTTP,"No more URL for keyword \"%s\" on page %d\n",internal->keyword,internal->page_num);
free(internal->page->data);
internal->page->data=NULL;
internal->page->size=0;
internal->read_index=0;
}
}
pthread_mutex_unlock(&internal->page_mutex);
return url;
}
/******************************
yandex_engine_init
return 0 if no error
******************************/
int yandex_engine_init(engine_t * engine,const char * keyword,int size,int filter)
{
internal_t * internal;
printf("Yandex engine\n");
internal = static_cast<internal_t*>(malloc(sizeof(internal_t)));
memset(internal,0,sizeof(internal_t));
internal->page = static_cast<network_page_t*>(malloc(sizeof(network_page_t)));
memset(internal->page,0,sizeof(network_page_t));
engine->internal = internal;
internal->page_num=0;
internal->read_index=0;
if(keyword == NULL) {
internal->keyword = readline("Enter key word: ");
if(internal->keyword[0] == 0 ) {
free(internal->keyword);
internal->keyword = getenv("USER");
}
} else {
internal->keyword = strdup(keyword);
}
internal->image_size=size_string[size].c_str();
internal->filter=filter_string[filter].c_str();
pthread_mutex_init(&internal->page_mutex,NULL);
engine->engine_destroy=engine_destroy;
engine->engine_get_url=engine_get_url;
time_last_request = time(NULL);
time_last_request -= TIME_BETWEEN_REQUEST;
return 0;
}