Commit f0770649 authored by Nicolas Gelot's avatar Nicolas Gelot

Restore images and videos results on main page

Previously, to display images and videos we had performed
a lot of changes in Searx project which leads to hard rebase
to upstream, then it adds a latency on each request to load
the main page.

Now, images and videos requests are asynchronous, these ones are
called from JS code with the Searx REST API.

Close: #45
parent 0a2f8255
......@@ -60,22 +60,12 @@
"params": {"message": "Rate limit exceeded 10004"}}
]
},
{
"name": "block accept everything",
"filters": ["!Header:Accept=text/html"],
"limit": 0,
"stop": true,
"actions": [
{"name": "block",
"params": {"message": "Rate limit exceeded 10005"}}
]
},
{
"name": "rss/json limit",
"interval": 3600,
"limit": 4,
"stop": true,
"filters": ["Param:format=(csv|json|rss)"],
"filters": ["Param:format=(csv|rss)"],
"aggregations": ["Header:X-Forwarded-For"],
"actions": [
{"name": "block",
......
......@@ -29,7 +29,8 @@ from searx.plugins import (oa_doi_rewrite,
self_info,
search_on_category_select,
tracker_url_remover,
vim_hotkeys)
vim_hotkeys,
rest_api)
required_attrs = (('name', (str, unicode)),
('description', (str, unicode)),
......@@ -86,3 +87,4 @@ plugins.register(self_info)
plugins.register(search_on_category_select)
plugins.register(tracker_url_remover)
plugins.register(vim_hotkeys)
plugins.register(rest_api)
import hmac
import hashlib
from flask_babel import gettext
from searx import settings
from searx.url_utils import urlencode
name = gettext('Rest API')
description = gettext('Update REST API')
default_on = True
preference_section = 'general'
def proxify(url):
""" helper copied from webapp module
"""
if url.startswith('//'):
url = 'https:' + url
if not settings.get('result_proxy'):
return url
url_params = dict(mortyurl=url.encode('utf-8'))
if settings['result_proxy'].get('key'):
url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'],
url.encode('utf-8'),
hashlib.sha256).hexdigest()
return '{0}?{1}'.format(settings['result_proxy']['url'],
urlencode(url_params))
def on_result(request, search, result):
if request.form.get("format") != "json":
return True
for attr in ["thumbnail", "thumbnail_src"]:
if attr in result:
result[attr] = proxify(result[attr])
return True
......@@ -88,14 +88,55 @@ $(document).ready(function(){
});
}
});
;$(document).ready(function(){
;/*jshint multistr: true */
function build_video(videos) {
var i;
var res = "";
for (i = 0; i < videos.length; i++) {
res += "\
<div class='result result-videos'>\
<div class='result-content'>\
<a class='thumbnail' href='" + videos[i].url + "' rel='noreferrer'>\
<img src='" + videos[i].thumbnail + "' alt=\"" + videos[i].title + "\">\
</a>\
<div>\
<h4 class='result_header'>\
<a href='" + videos[i].url +"' rel='noreferrer'>" + videos[i].title + "</a>\
</h4>\
</div>\
</div>\
</div>";
}
return res;
}
function build_image(images) {
var i;
var res = "";
for (i = 0; i < images.length; i++) {
res += "\
<div class='result result-images'>\
<a href=" + images[i].img_src + "' data-url='" + images[i].url + "' class='img-thumb-link'>\
<img src='" + images[i].thumbnail_src + "' alt=\"" + images[i].title + "\" title=\"" + images[i].title + "\" class='img-thumbnail'>\
</a>\
</div>";
}
return res;
}
$(document).ready(function(){
function configure_image_view(target, view_url) {
document.getElementById("image_view_image").src = view_url;
document.getElementById("image_view_file_link").href = target.href;
document.getElementById("image_view_url_link").href = target.dataset.url;
}
$(".result.result-images").click(function (event) {
$("#results").on("click", ".result.result-images", function (event) {
event.preventDefault();
var target = event.target;
var view_url = target.src;
......@@ -117,6 +158,32 @@ $(document).ready(function(){
document.getElementById("image_view_url_link").href = "#";
document.body.classList.remove("lock");
});
if ($("#results").has(".first-page-media-results").length) {
var headers = {"headers": { "Content-Type": "application/json"}};
var query_params = $.param({
"format": "json",
"q": $("#q").attr("value"),
"language": $("select[name='language']").find('option:selected').attr("value"),
"time_range": $("select[name='time_range']").find('option:selected').attr("value")
});
fetch("/?categories=images&" + query_params, {"headers": headers})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.results) {
$("#default_images_container").append(build_image(data.results.slice(0, 5)));
}
});
fetch("/?categories=videos&" + query_params, {"headers": headers})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.results) {
$(".videos-gallery").append(build_video(data.results.slice(0, 2)));
}
});
}
});
;/**
* searx is free software: you can redistribute it and/or modify
......
This diff is collapsed.
/*jshint multistr: true */
function build_video(videos) {
var i;
var res = "";
for (i = 0; i < videos.length; i++) {
res += "\
<div class='result result-videos'>\
<div class='result-content'>\
<a class='thumbnail' href='" + videos[i].url + "' rel='noreferrer'>\
<img src='" + videos[i].thumbnail + "' alt=\"" + videos[i].title + "\">\
</a>\
<div>\
<h4 class='result_header'>\
<a href='" + videos[i].url +"' rel='noreferrer'>" + videos[i].title + "</a>\
</h4>\
</div>\
</div>\
</div>";
}
return res;
}
function build_image(images) {
var i;
var res = "";
for (i = 0; i < images.length; i++) {
res += "\
<div class='result result-images'>\
<a href=" + images[i].img_src + "' data-url='" + images[i].url + "' class='img-thumb-link'>\
<img src='" + images[i].thumbnail_src + "' alt=\"" + images[i].title + "\" title=\"" + images[i].title + "\" class='img-thumbnail'>\
</a>\
</div>";
}
return res;
}
$(document).ready(function(){
function configure_image_view(target, view_url) {
document.getElementById("image_view_image").src = view_url;
......@@ -5,7 +46,7 @@ $(document).ready(function(){
document.getElementById("image_view_url_link").href = target.dataset.url;
}
$(".result.result-images").click(function (event) {
$("#results").on("click", ".result.result-images", function (event) {
event.preventDefault();
var target = event.target;
var view_url = target.src;
......@@ -27,4 +68,30 @@ $(document).ready(function(){
document.getElementById("image_view_url_link").href = "#";
document.body.classList.remove("lock");
});
if ($("#results").has(".first-page-media-results").length) {
var headers = {"headers": { "Content-Type": "application/json"}};
var query_params = $.param({
"format": "json",
"q": $("#q").attr("value"),
"language": $("select[name='language']").find('option:selected').attr("value"),
"time_range": $("select[name='time_range']").find('option:selected').attr("value")
});
fetch("/?categories=images&" + query_params, {"headers": headers})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.results) {
$("#default_images_container").append(build_image(data.results.slice(0, 5)));
}
});
fetch("/?categories=videos&" + query_params, {"headers": headers})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.results) {
$(".videos-gallery").append(build_video(data.results.slice(0, 2)));
}
});
}
});
......@@ -66,7 +66,24 @@
<div id="results" {% if use_gallery_layout %} class="{{selected_categories[0]}}-gallery" {% endif %}>
{{ put_results(results, True) }}
{{ put_results(results[:3], True) }}
{% if pageno == 1 and 'general' in selected_categories %}
<div class='first-page-media-results'>
<div class='header'>
<b class="title">{{_("Images")}}</b>
{{ search_link(_("More"), q|e, method=method, time_range=time_range, lang=current_language, category="images") }}
</div>
<div id='default_images_container'></div>
</div>
<div class='first-page-media-results'>
<div class='header'>
<b class="title">{{_("Videos")}}</b>
{{ search_link(_("More"), q|e, method=method, time_range=time_range, lang=current_language, category="videos") }}
</div>
<div class='videos-gallery'></div>
</div>
{% endif %}
{{ put_results(results[4:], True) }}
</div>
{% if paging %}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment