-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery.cpp
313 lines (238 loc) · 6.18 KB
/
Query.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2019 pinc Software. All Rights Reserved.
*/
#include "Query.h"
#include <Application.h>
#include <Bitmap.h>
#include <DataIO.h>
#include <HttpRequest.h>
#include <TranslationUtils.h>
#include <map>
#include <regex.h>
#include <stdio.h>
#include "Utility.h"
#define MAX_GROUPS 10
#define MAX_MATCHES 20
#define COUNTRY_GERMANY 3
#define TYPE_ORIGINAL "SCRM"
#define TYPE_LARGE "SCL"
#define TYPE_THUMB "THUMB"
class Retriever {
public:
Retriever(ResultListener& listener);
virtual ~Retriever();
virtual void Abort() = 0;
virtual void Retrieve(const BString& artist,
const BString& title) = 0;
protected:
ResultListener& fListener;
};
class AmazonRetriever : public Retriever {
public:
AmazonRetriever(int countryCode,
const char* country,
ResultListener& listener);
virtual ~AmazonRetriever();
virtual void Abort();
virtual void Retrieve(const BString& artist,
const BString& title);
private:
BUrl _GetUrl(const BString& id, const char* type);
private:
int fCountryCode;
BString fCountry;
bool fAbort;
};
// #pragma mark - Retriever
Retriever::Retriever(ResultListener& listener)
:
fListener(listener)
{
}
Retriever::~Retriever()
{
}
// #pragma mark - AmazonRetriever
AmazonRetriever::AmazonRetriever(int countryCode, const char* country,
ResultListener& listener)
:
Retriever(listener),
fCountryCode(countryCode),
fCountry(country),
fAbort(false)
{
}
AmazonRetriever::~AmazonRetriever()
{
}
void
AmazonRetriever::Abort()
{
fAbort = true;
}
void
AmazonRetriever::Retrieve(const BString& artist, const BString& title)
{
BString pattern("<a[^>]*title\\s*=\\s*\"([^\"]+)\"[^>]*"
"href\\s*=\\s*\"([^\"]+?/dp/([^/]+)/)[^>]+><h2.*?>([^<]+)(</a>)?</span></div></div>");
//");
regex_t patternCompiled;
regmatch_t groups[MAX_GROUPS];
int patternResult = regcomp(&patternCompiled, pattern.String(),
REG_EXTENDED | REG_ICASE);
if (patternResult != 0) {
printf("bad pattern: %d!\n", patternResult);
char text[256];
regerror(patternResult, &patternCompiled, text, sizeof(text));
printf(" --> %s\n", text);
return;
}
BString urlString("http://www.amazon.%COUNTRY%/gp/search/"
"?search-alias=popular&sort=relevancerank");
if (!artist.IsEmpty())
urlString += "&field-artist=%ARTIST%";
if (!title.IsEmpty())
urlString += "&field-title=%TITLE%";
urlString.ReplaceAll("%COUNTRY%", fCountry);
urlString.ReplaceAll("%ARTIST%", BUrl::UrlEncode(artist));
urlString.ReplaceAll("%TITLE%", BUrl::UrlEncode(title));
printf("URL: %s\n", urlString.String());
BUrl url(urlString);
BHttpRequest request(url);
DataListener listener;
request.SetListener(&listener);
request.Run();
while (request.IsRunning() && !fAbort) {
snooze(50000);
}
if (fAbort)
return;
const BHttpResult& result = dynamic_cast<const BHttpResult&>(
request.Result());
printf("Status: %" B_PRId32 " - %s\n", result.StatusCode(),
result.StatusText().String());
BString data((const char*)listener.IO().Buffer(),
listener.IO().BufferLength());
printf("length: %d\n", (int)listener.IO().BufferLength());
const char* cursor = data.String();
int offset = 0;
for (int index = 0; index < MAX_MATCHES; index++) {
if (regexec(&patternCompiled, cursor, MAX_GROUPS, groups, 0))
break;
BString id;
BString info;
for (int groupIndex = 0; groupIndex < MAX_GROUPS; groupIndex++) {
if (groups[groupIndex].rm_so == -1)
break;
printf("%d. (%d - %d)\n", groupIndex, groups[groupIndex].rm_so, groups[groupIndex].rm_eo);
if (groupIndex == 3)
offset = groups[3].rm_eo;
int length = groups[groupIndex].rm_eo - groups[groupIndex].rm_so;
BString match(cursor + groups[groupIndex].rm_so, length);
if (groupIndex)
printf("%d: match? %s\n", groupIndex, match.String());
switch (groupIndex) {
case 1:
info = match;
break;
case 2:
url = match;
break;
case 3:
id = match;
break;
}
}
if (!id.IsEmpty()) {
UrlMap urls;
urls.insert(std::make_pair(kSource, url));
urls.insert(std::make_pair(kOriginalImage,
_GetUrl(id, TYPE_ORIGINAL)));
urls.insert(std::make_pair(kLargeImage, _GetUrl(id, TYPE_LARGE)));
urls.insert(std::make_pair(kThumbImage, _GetUrl(id, TYPE_THUMB)));
fListener.AddResult(id, info, urls);
}
printf("offset = %d\n", offset);
cursor += offset;
}
regfree(&patternCompiled);
}
BUrl
AmazonRetriever::_GetUrl(const BString& id, const char* type)
{
BString urlString;
urlString.SetToFormat("http://ecx.images-amazon.com/images/P/%s.%02d._%s_",
id.String(), fCountryCode, type);
return BUrl(urlString.String());
}
// #pragma mark - Query
Query::Query(const BString& artist, const BString& title)
:
fRetrievers(5, true),
fListeners(5, true),
fArtist(artist),
fTitle(title),
fAbort(false)
{
printf("THIS NEW %p\n", this);
AddRetriever(new AmazonRetriever(COUNTRY_GERMANY, "de", *this));
}
Query::~Query()
{
puts("DELETE QUERY!");
}
void
Query::AddRetriever(Retriever* retriever)
{
fRetrievers.AddItem(retriever);
printf("new retriever: %ld\n", fRetrievers.CountItems());
}
void
Query::AddListener(ResultListener* listener)
{
fListeners.AddItem(listener);
}
void
Query::Run()
{
printf("retrievers: %ld\n", fRetrievers.CountItems());
fThread = spawn_thread(&_Run, "query artist/title", B_NORMAL_PRIORITY,
this);
if (fThread >= 0)
resume_thread(fThread);
}
void
Query::Abort(bool wait)
{
printf("THIS Abort %p\n", this);
printf("THIS Abort %ld\n", fRetrievers.CountItems());
fAbort = true;
for (int32 index = 0; index < fRetrievers.CountItems(); index++)
fRetrievers.ItemAt(index)->Abort();
if (wait)
wait_for_thread(fThread, NULL);
}
void
Query::AddResult(const BString& id, const char* info, UrlMap urls)
{
if (fAbort)
return;
for (int32 i = 0; i < fListeners.CountItems(); i++) {
fListeners.ItemAt(i)->AddResult(id, info, urls);
}
}
/*static*/ status_t
Query::_Run(void* _self)
{
Query* self = (Query*)_self;
return self->_Run();
}
status_t
Query::_Run()
{
printf("THIS Run %p\n", this);
printf("run retriever: %ld\n", fRetrievers.CountItems());
for (int32 index = 0; index < fRetrievers.CountItems(); index++)
fRetrievers.ItemAt(index)->Retrieve(fArtist, fTitle);
return B_OK;
}