some work on wtv-search
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Web Search Proxy</title>
|
||||
</head>
|
||||
<body bgcolor="#191919" text="#44cc55" link="36d5ff" vlink="36d5ff">
|
||||
<form method="POST" action="wtv-search:/search">
|
||||
<label for="q">Query:</label>
|
||||
<input type="text" id="q" name="q" value="{{ request_headers.query.q }}" size=30>
|
||||
<select name="categories">
|
||||
{% if request_headers.query.categories == 'general' %}
|
||||
<option value="general" selected>General</option>
|
||||
{% else %}
|
||||
<option value="general">General</option>
|
||||
{% endif %}
|
||||
|
||||
{% if request_headers.query.categories == 'images' %}
|
||||
<option value="images" selected>Images</option>
|
||||
{% else %}
|
||||
<option value="images">Images</option>
|
||||
{% endif %}
|
||||
</select>
|
||||
<input type="submit" value="Search">
|
||||
</form>
|
||||
<h1>Search Results</h1>
|
||||
{% if request_headers.query.categories == 'general' %}
|
||||
{% for result in content %}
|
||||
<p><font color="gold">{{ result.title }}</font><br><a href="{{ result.url }}" target="_blank">Direct</a> - <a href="wtv-proxy:/proxy?url={{ result.encodedurl }}">Proxy</a><br>{{ result.content }}</p><br>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% for result in content %}
|
||||
<a href="{{ result.img_src }}"><img src="{{ result.thumbnail_src }}" width="50"></a>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,54 @@
|
||||
minisrv_service_file = true;
|
||||
request_is_async = true;
|
||||
|
||||
|
||||
|
||||
async function handleRequest(request_headers) {
|
||||
const imageUrl = request_headers.query.url;
|
||||
if (!imageUrl) {
|
||||
var errpage = wtvshared.doErrorPage(400, "Missing url parameter");
|
||||
sendToClient(socket, errpage[0], errpage[1]);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const urlObj = new URL(imageUrl);
|
||||
const lib = urlObj.protocol === 'https:' ? https : http;
|
||||
const fetch = (lib, options) => new Promise((resolve, reject) => {
|
||||
const req = lib.request(imageUrl, options, (res) => {
|
||||
let data = [];
|
||||
res.on('data', chunk => data.push(chunk));
|
||||
res.on('end', () => {
|
||||
res.buffer = async () => Buffer.concat(data);
|
||||
res.ok = res.statusCode >= 200 && res.statusCode < 300;
|
||||
resolve(res);
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
const imgRes = await fetch(lib, { method: 'GET', headers: { 'User-Agent': 'Mozilla/4.0 WebTV/2.6 (compatible; MSIE 4.0)' } });
|
||||
if (!imgRes.ok) {
|
||||
var errpage = wtvshared.doErrorPage(502, "Failed to fetch image");
|
||||
sendToClient(socket, errpage[0], errpage[1]);
|
||||
return;
|
||||
}
|
||||
const imgBuffer = await imgRes.buffer();
|
||||
|
||||
const resized = await sharp(imgBuffer)
|
||||
.resize(50, 50, { fit: 'inside' })
|
||||
.toBuffer();
|
||||
|
||||
headers = `200 OK
|
||||
Content-Type: image/png`;
|
||||
data = resized;
|
||||
sendToClient(socket, headers, data);
|
||||
} catch (err) {
|
||||
var errpage = wtvshared.doErrorPage(500, "Error processing image: " + err.message);
|
||||
sendToClient(socket, errpage[0], errpage[1]);
|
||||
}
|
||||
}
|
||||
|
||||
handleRequest(request_headers);
|
||||
|
||||
// Example usage: handleRequest(request_headers, response);
|
||||
@@ -9,8 +9,7 @@ searchUrl += 'search';
|
||||
|
||||
if (!request_headers.query.q) {
|
||||
const headers = `200 OK\nContent-Type: text/html`;
|
||||
const data = `
|
||||
<html>
|
||||
const data = `<html>
|
||||
<head>
|
||||
<title>Web Search Proxy</title>
|
||||
</head>
|
||||
@@ -40,6 +39,7 @@ if (!request_headers.query.q) {
|
||||
}
|
||||
}
|
||||
params.append('format', 'json');
|
||||
params.append('limit', '10');
|
||||
const urlObj = new URL(searchUrl);
|
||||
const lib = urlObj.protocol === 'https:' ? https : http;
|
||||
var post_data = params.toString();
|
||||
@@ -63,7 +63,7 @@ if (!request_headers.query.q) {
|
||||
fetch(lib, options, post_data)
|
||||
.then(response => response.text())
|
||||
.then(text => { process(text); })
|
||||
.catch(err => { finishPage(`Error fetching page: ${err.message}`); });
|
||||
.catch(err => { finishPage([`Error fetching page: ${err.message}`]); });
|
||||
}
|
||||
|
||||
function fetch(lib, options, post_data) {
|
||||
@@ -86,12 +86,20 @@ function process(data) {
|
||||
const results = JSON.parse(data).results || [];
|
||||
let content = '';
|
||||
if (results.length === 0) {
|
||||
content = '<h1>No results found</h1>';
|
||||
content = ['<h1>No results found</h1>'];
|
||||
} else {
|
||||
content = '<h1>Search Results</h1>';
|
||||
content = [];
|
||||
results.forEach(result => {
|
||||
result.title = result.title.replace(/\</g, '<').replace(/\>/g, '>');
|
||||
content += `<p><font color="gold">${result.title}</font><br><a href="${result.url}" target="_blank">Direct</a> - <a href="wtv-proxy:/proxy?url=${encodeURIComponent(result.url)}">Proxy</a><br>${result.content || `<img src="${result.thumbnail_src}">` || ''}</p><br>`;
|
||||
if (result.description) {
|
||||
result.description = result.description.replace(/\</g, '<').replace(/\>/g, '>');
|
||||
}
|
||||
result.encodedurl = encodeURIComponent(result.url);
|
||||
if (result.thumbnail_src) {
|
||||
result.thumbnail_src = "wtv-search:/imgproxy?url=" + encodeURIComponent(result.thumbnail_src);
|
||||
}
|
||||
|
||||
content.push(result);
|
||||
});
|
||||
}
|
||||
finishPage(content);
|
||||
@@ -101,23 +109,7 @@ function process(data) {
|
||||
|
||||
function finishPage(content) {
|
||||
const headers = `200 OK\nContent-Type: text/html`;
|
||||
const data = `
|
||||
<html>
|
||||
<head>
|
||||
<title>Web Search Proxy</title>
|
||||
</head>
|
||||
<body bgcolor="#191919" text="#44cc55" link="36d5ff" vlink="36d5ff">
|
||||
<form method="POST" action="wtv-search:/search">
|
||||
<label for="q">Query:</label>
|
||||
<input type="text" id="q" name="q" value="${request_headers.query.q}" size=30>
|
||||
<select name="categories">
|
||||
<option value="general" ${request_headers.query.categories === 'general' ? 'selected' : ''}>General</option>
|
||||
<option value="images" ${request_headers.query.categories === 'images' ? 'selected' : ''}>Images</option>
|
||||
</select>
|
||||
<input type="submit" value="Search">
|
||||
</form>
|
||||
${content}
|
||||
</body>
|
||||
</html>`;
|
||||
nunjucks.configure({ autoescape: true });
|
||||
const data = nunjucks.render(wtvshared.getServiceDep('wtv-search/results.njk', true), { content, request_headers });
|
||||
sendToClient(socket, headers, data);
|
||||
}
|
||||
Reference in New Issue
Block a user