Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0770649 authored by Nicolas Gelot's avatar Nicolas Gelot
Browse files

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
Loading
Loading
Loading
Loading
+1 −11
Original line number Original line Diff line number Diff line
@@ -60,22 +60,12 @@
                     "params": {"message": "Rate limit exceeded 10004"}}
                     "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",
                "name": "rss/json limit",
                "interval": 3600,
                "interval": 3600,
                "limit": 4,
                "limit": 4,
                "stop": true,
                "stop": true,
                "filters": ["Param:format=(csv|json|rss)"],
                "filters": ["Param:format=(csv|rss)"],
                "aggregations": ["Header:X-Forwarded-For"],
                "aggregations": ["Header:X-Forwarded-For"],
                "actions": [
                "actions": [
                    {"name": "block",
                    {"name": "block",
+3 −1
Original line number Original line Diff line number Diff line
@@ -29,7 +29,8 @@ from searx.plugins import (oa_doi_rewrite,
                           self_info,
                           self_info,
                           search_on_category_select,
                           search_on_category_select,
                           tracker_url_remover,
                           tracker_url_remover,
                           vim_hotkeys)
                           vim_hotkeys,
                           rest_api)


required_attrs = (('name', (str, unicode)),
required_attrs = (('name', (str, unicode)),
                  ('description', (str, unicode)),
                  ('description', (str, unicode)),
@@ -86,3 +87,4 @@ plugins.register(self_info)
plugins.register(search_on_category_select)
plugins.register(search_on_category_select)
plugins.register(tracker_url_remover)
plugins.register(tracker_url_remover)
plugins.register(vim_hotkeys)
plugins.register(vim_hotkeys)
plugins.register(rest_api)
+44 −0
Original line number Original line Diff line number Diff line
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
+69 −2
Original line number Original line Diff line number Diff line
@@ -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) {
  function configure_image_view(target, view_url) {
    document.getElementById("image_view_image").src = view_url;
    document.getElementById("image_view_image").src = view_url;
    document.getElementById("image_view_file_link").href = target.href;
    document.getElementById("image_view_file_link").href = target.href;
    document.getElementById("image_view_url_link").href = target.dataset.url;
    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();
    event.preventDefault();
    var target = event.target;
    var target = event.target;
    var view_url = target.src;
    var view_url = target.src;
@@ -117,6 +158,32 @@ $(document).ready(function(){
    document.getElementById("image_view_url_link").href = "#";
    document.getElementById("image_view_url_link").href = "#";
    document.body.classList.remove("lock");
    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
 * searx is free software: you can redistribute it and/or modify
+0 −0

File changed.

Preview suppressed by a .gitattributes entry or the file's encoding is unsupported.

Loading