-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
170 lines (134 loc) · 4.63 KB
/
proxy.js
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
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
const router = new Router();
const request = require('koa2-request');
const parser = require('xml2json');
const formatDate = require('./libs/format-date');
app.use(router.routes());
router
.get('/:user/:key/:query/', async (ctx, next) => {
let params = ctx.params;
let now = new Date();
now = formatDate(now);
now = now.split(',')[0];
let response = await request({
url: `https://yandex.ru/search/xml?user=${params.user}&key=${params.key}&query=host:${params.query}`,
method: 'get',
headers: {
'User-Agent': 'request',
'content-type': 'application/xml',
'charset': 'UTF-8'
},
});
let jsonBody = parser.toJson(response.body);
jsonBody = JSON.parse(jsonBody);
if (jsonBody.yandexsearch.response.error) {
let response = await request({
url: `https://yandex.ru/search/xml?user=${params.user}&key=${params.key}&query=site:${params.query}`,
method: 'get',
headers: {
'User-Agent': 'request',
'content-type': 'application/xml',
'charset': 'UTF-8'
},
});
jsonBody = parser.toJson(response.body);
jsonBody = JSON.parse(jsonBody);
}
if (jsonBody.yandexsearch.response.error) {
let response = await request({
url: `https://yandex.ru/search/xml?user=${params.user}&key=${params.key}&query=host:www.${params.query}`,
method: 'get',
headers: {
'User-Agent': 'request',
'content-type': 'application/xml',
'charset': 'UTF-8'
},
});
jsonBody = parser.toJson(response.body);
jsonBody = JSON.parse(jsonBody);
}
if (!jsonBody.yandexsearch.response.error) {
let found = jsonBody.yandexsearch.response.found[2].$t;
ctx.body = [now, params.query, found, 'ok'].join();
} else {
ctx.body = [now, params.query, 'error', jsonBody.yandexsearch.response.error.$t].join();
}
})
.get('/position/:user/:key/:query/:region/:site/', async (ctx, next) => {
let params = ctx.params;
let response = await request({
url: 'https://yandex.ru/search/xml?' +
'user=' + params.user +
'&key=' + params.key +
'&query=' + encodeURIComponent(params.query) +
'&lr=' + params.region +
'&l10n=ru' +
'&sortby=rlv' +
'&filter=none' +
'&groupby=attr=\"\".mode=flat.groups-on-page=50.docs-in-group=1',
method: 'get',
headers: {
'User-Agent': 'request',
'content-type': 'application/xml',
'charset': 'UTF-8'
},
});
let resultSite = [];
let position = 0;
let url = '-';
let jsonBody = parser.toJson(response.body);
jsonBody = JSON.parse(jsonBody);
let resultGroup = jsonBody.yandexsearch.response.results.grouping.group;
resultGroup.forEach(result => {
resultSite.push(result.doc);
});
for (let i = 0; i < resultSite.length; i++) {
if ((resultSite[i]['domain'] == params.site) || (resultSite[i]['domain'] == 'www.' + params.site)) {
position = i + 1;
url = resultSite[i]['url'];
break;
}
}
let now = new Date();
now = formatDate(now);
now = now.split(',')[0];
ctx.body = now + ',' + params.site + ',' + params.query + ',' + url + ',' + position;
})
.get('/top/:user/:key/:query/:region/', async (ctx, next) => {
let params = ctx.params;
let response = await request({
url: 'https://yandex.ru/search/xml?' +
'user=' + params.user +
'&key=' + params.key +
'&query=' + encodeURIComponent(params.query) +
'&lr=' + params.region +
'&l10n=ru' +
'&sortby=rlv' +
'&filter=none' +
'&groupby=attr=\"\".mode=flat.groups-on-page=10.docs-in-group=1',
method: 'get',
headers: {
'User-Agent': 'request',
'content-type': 'application/xml',
'charset': 'UTF-8'
},
});
let resultSite = [];
let sites = [];
let urls = [];
let jsonBody = parser.toJson(response.body);
jsonBody = JSON.parse(jsonBody);
let resultGroup = jsonBody.yandexsearch.response.results.grouping.group;
resultGroup.forEach(result => {
resultSite.push(result.doc);
});
for (let i = 0; i < resultSite.length; i++) {
sites.push([params.query, resultSite[i]['domain'], resultSite[i]['url']]);
}
ctx.body = sites;
});
const server = app.listen({port: 3001}, () => {
console.log('Proxy start on port: 3001...');
});