7 Commits

Author SHA1 Message Date
Markus Heiser
073d9549a0 [build] /static 2025-01-20 13:52:43 +01:00
Markus Heiser
601ffcb8a3 [mod] add swipe events to the image gallery (gesture control)
Adds [1] to the searxng.min.js and horizontal swipe events to the image gallery.

[1] https://www.npmjs.com/package/swiped-events

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2025-01-20 13:52:43 +01:00
Markus Heiser
d9115b8d48 [build] /static 2025-01-20 13:29:26 +01:00
Bnyro
c760ad0808 [feat] image results: dismiss image viewer on back button press on mobile devices 2025-01-20 13:29:26 +01:00
Bnyro
2f087a3a22 [feat] public domain image archive: automatically obtain algolia api key 2025-01-20 12:56:15 +01:00
Denperidge
3333d9f385 [feat] engines: public domain image archive 2025-01-20 12:56:15 +01:00
Popolon
1a885b70ce [feat] wikidata: add mastodon, peertube and Lemmy accounts to infobox
Co-authored-by: Popolon <popolon@popolon.org>
Co-authored-by: Bnyro <bnyro@tutanota.com>
2025-01-20 11:19:56 +01:00
10 changed files with 245 additions and 16 deletions

View File

@@ -0,0 +1,149 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Public domain image archive"""
from urllib.parse import urlencode, urlparse, urlunparse, parse_qsl
from json import dumps
from searx.network import get
from searx.utils import extr
from searx.exceptions import SearxEngineAccessDeniedException, SearxEngineException
THUMBNAIL_SUFFIX = "?fit=max&h=360&w=360"
"""
Example thumbnail urls (from requests & html):
- https://the-public-domain-review.imgix.net
/shop/nov-2023-prints-00043.jpg
?fit=max&h=360&w=360
- https://the-public-domain-review.imgix.net
/collections/the-history-of-four-footed-beasts-and-serpents-1658/
8616383182_5740fa7851_o.jpg
?fit=max&h=360&w=360
Example full image urls (from html)
- https://the-public-domain-review.imgix.net/shop/
nov-2023-prints-00043.jpg
?fit=clip&w=970&h=800&auto=format,compress
- https://the-public-domain-review.imgix.net/collections/
the-history-of-four-footed-beasts-and-serpents-1658/8616383182_5740fa7851_o.jpg
?fit=clip&w=310&h=800&auto=format,compress
The thumbnail url from the request will be cleaned for the full image link
The cleaned thumbnail url will have THUMBNAIL_SUFFIX added to them, based on the original thumbnail parameters
"""
# about
about = {
"website": 'https://pdimagearchive.org',
"use_official_api": False,
"require_api_key": False,
"results": 'JSON',
}
base_url = 'https://oqi2j6v4iz-dsn.algolia.net'
pdia_config_url = 'https://pdimagearchive.org/_astro/config.BiNvrvzG.js'
categories = ['images']
page_size = 20
paging = True
__CACHED_API_KEY = None
def _clean_url(url):
parsed = urlparse(url)
query = [(k, v) for (k, v) in parse_qsl(parsed.query) if k not in ['ixid', 's']]
return urlunparse((parsed.scheme, parsed.netloc, parsed.path, parsed.params, urlencode(query), parsed.fragment))
def _get_algolia_api_key():
global __CACHED_API_KEY # pylint:disable=global-statement
if __CACHED_API_KEY:
return __CACHED_API_KEY
resp = get(pdia_config_url)
if resp.status_code != 200:
raise LookupError("Failed to obtain Algolia API key for PDImageArchive")
api_key = extr(resp.text, 'r="', '"', default=None)
if api_key is None:
raise LookupError("Couldn't obtain Algolia API key for PDImageArchive")
__CACHED_API_KEY = api_key
return api_key
def _clear_cached_api_key():
global __CACHED_API_KEY # pylint:disable=global-statement
__CACHED_API_KEY = None
def request(query, params):
api_key = _get_algolia_api_key()
args = {
'x-algolia-api-key': api_key,
'x-algolia-application-id': 'OQI2J6V4IZ',
}
params['url'] = f"{base_url}/1/indexes/*/queries?{urlencode(args)}"
params["method"] = "POST"
request_params = {
"page": params["pageno"] - 1,
"query": query,
"highlightPostTag": "__ais-highlight__",
"highlightPreTag": "__ais-highlight__",
}
data = {
"requests": [
{"indexName": "prod_all-images", "params": urlencode(request_params)},
]
}
params["data"] = dumps(data)
# http errors are handled manually to be able to reset the api key
params['raise_for_httperror'] = False
return params
def response(resp):
results = []
json_data = resp.json()
if resp.status_code == 403:
_clear_cached_api_key()
raise SearxEngineAccessDeniedException()
if resp.status_code != 200:
raise SearxEngineException()
if 'results' not in json_data:
return []
for result in json_data['results'][0]['hits']:
content = []
if "themes" in result:
content.append("Themes: " + result['themes'])
if "encompassingWork" in result:
content.append("Encompassing work: " + result['encompassingWork'])
content = "\n".join(content)
base_image_url = result['thumbnail'].split("?")[0]
results.append(
{
'template': 'images.html',
'url': _clean_url(f"{about['website']}/images/{result['objectID']}"),
'img_src': _clean_url(base_image_url),
'thumbnail_src': _clean_url(base_image_url + THUMBNAIL_SUFFIX),
'title': f"{result['title'].strip()} by {result['artist']} {result.get('displayYear', '')}",
'content': content,
}
)
return results

View File

@@ -60,6 +60,9 @@ WIKIDATA_PROPERTIES = {
'P2002': 'Twitter',
'P2013': 'Facebook',
'P2003': 'Instagram',
'P4033': 'Mastodon',
'P11947': 'Lemmy',
'P12622': 'PeerTube',
}
# SERVICE wikibase:mwapi : https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI
@@ -363,8 +366,8 @@ def get_attributes(language):
def add_label(name):
attributes.append(WDLabelAttribute(name))
def add_url(name, url_id=None, **kwargs):
attributes.append(WDURLAttribute(name, url_id, kwargs))
def add_url(name, url_id=None, url_path_prefix=None, **kwargs):
attributes.append(WDURLAttribute(name, url_id, url_path_prefix, kwargs))
def add_image(name, url_id=None, priority=1):
attributes.append(WDImageAttribute(name, url_id, priority))
@@ -476,6 +479,11 @@ def get_attributes(language):
add_url('P2013', url_id='facebook_profile')
add_url('P2003', url_id='instagram_profile')
# Fediverse
add_url('P4033', url_path_prefix='/@') # Mastodon user
add_url('P11947', url_path_prefix='/c/') # Lemmy community
add_url('P12622', url_path_prefix='/c/') # PeerTube channel
# Map
attributes.append(WDGeoAttribute('P625'))
@@ -592,22 +600,50 @@ class WDURLAttribute(WDAttribute):
HTTP_WIKIMEDIA_IMAGE = 'http://commons.wikimedia.org/wiki/Special:FilePath/'
__slots__ = 'url_id', 'kwargs'
__slots__ = 'url_id', 'url_path_prefix', 'kwargs'
def __init__(self, name, url_id=None, url_path_prefix=None, kwargs=None):
"""
:param url_id: ID matching one key in ``external_urls.json`` for
converting IDs to full URLs.
:param url_path_prefix: Path prefix if the values are of format
``account@domain``. If provided, value are rewritten to
``https://<domain><url_path_prefix><account>``. For example::
WDURLAttribute('P4033', url_path_prefix='/@')
Adds Property `P4033 <https://www.wikidata.org/wiki/Property:P4033>`_
to the wikidata query. This field might return for example
``libreoffice@fosstodon.org`` and the URL built from this is then:
- account: ``libreoffice``
- domain: ``fosstodon.org``
- result url: https://fosstodon.org/@libreoffice
"""
def __init__(self, name, url_id=None, kwargs=None):
super().__init__(name)
self.url_id = url_id
self.url_path_prefix = url_path_prefix
self.kwargs = kwargs
def get_str(self, result, language):
value = result.get(self.name + 's')
if self.url_id and value is not None and value != '':
value = value.split(',')[0]
if not value:
return None
value = value.split(',')[0]
if self.url_id:
url_id = self.url_id
if value.startswith(WDURLAttribute.HTTP_WIKIMEDIA_IMAGE):
value = value[len(WDURLAttribute.HTTP_WIKIMEDIA_IMAGE) :]
url_id = 'wikimedia_image'
return get_external_url(url_id, value)
if self.url_path_prefix:
[account, domain] = value.split('@')
return f"https://{domain}{self.url_path_prefix}{account}"
return value

View File

@@ -1542,6 +1542,10 @@ engines:
require_api_key: false
results: HTML
- name: Public Domain Image Archive
engine: public_domain_image_archive
shortcut: pdia
- name: pubmed
engine: pubmed
shortcut: pub

View File

@@ -24,7 +24,7 @@ module.exports = function (grunt) {
files: ['gruntfile.js', 'eslint.config.mjs', '.stylelintrc.json', 'src/**'],
tasks: [
'eslint',
'stylelint',
'stylelint',
'copy',
'uglify',
'less',
@@ -109,7 +109,8 @@ module.exports = function (grunt) {
'js/searxng.head.min.js': ['src/js/head/*.js'],
'js/searxng.min.js': [
'src/js/main/*.js',
'./node_modules/autocomplete-js/dist/autocomplete.js'
'./node_modules/autocomplete-js/dist/autocomplete.js',
'./node_modules/swiped-events/src/swiped-events.js'
]
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -7,7 +7,8 @@
"dependencies": {
"autocomplete-js": "^2.7.1",
"leaflet": "^1.9.4",
"normalize.css": "^8.0.1"
"normalize.css": "^8.0.1",
"swiped-events": "^1.2.0"
},
"devDependencies": {
"ejs": "^3.1.10",
@@ -8989,6 +8990,12 @@
"dev": true,
"license": "CC0-1.0"
},
"node_modules/swiped-events": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/swiped-events/-/swiped-events-1.2.0.tgz",
"integrity": "sha512-KRu67z1hb4sPxMdFIF2kaufYHTcWOb8NVLbIl2g5dPWZkEQ6D3wfSIVJ7iXbicTt9cO3e0vARqgx9fITtTZxQw==",
"license": "MIT"
},
"node_modules/table": {
"version": "6.9.0",
"resolved": "https://registry.npmjs.org/table/-/table-6.9.0.tgz",

View File

@@ -24,7 +24,8 @@
"dependencies": {
"autocomplete-js": "^2.7.1",
"leaflet": "^1.9.4",
"normalize.css": "^8.0.1"
"normalize.css": "^8.0.1",
"swiped-events": "^1.2.0"
},
"scripts": {
"all": "npm install && grunt",

View File

@@ -52,6 +52,7 @@
this.innerText = this.dataset.copiedText;
});
const isMobile = screen.orientation.type.startsWith('portrait');
searxng.selectImage = function (resultElement) {
/* eslint no-unused-vars: 0 */
if (resultElement) {
@@ -82,26 +83,47 @@
}
}
d.getElementById('results').classList.add('image-detail-open');
// add a hash to the browser history so that pressing back doesn't return to the previous page
// this allows us to dismiss the image details on pressing the back button on mobile devices
window.location.hash = '#image-viewer';
searxng.scrollPageToSelected();
}
};
searxng.closeDetail = function (e) {
d.getElementById('results').classList.remove('image-detail-open');
searxng.scrollPageToSelected();
}
};
searxng.on('.result-detail-close', 'click', e => {
e.preventDefault();
searxng.closeDetail();
});
searxng.on('.result-detail-previous', 'click', e => {
e.preventDefault();
searxng.selectPrevious(false)
searxng.selectPrevious(false);
});
searxng.on('.result-detail-next', 'click', e => {
e.preventDefault();
searxng.selectNext(false);
});
// listen for the back button to be pressed and dismiss the image details when called
window.addEventListener('hashchange', () => {
if (!window.location.hash) searxng.closeDetail();
});
d.querySelectorAll('.swipe-horizontal').forEach(
obj => {
obj.addEventListener('swiped-left', function (e) {
searxng.selectNext(false);
});
obj.addEventListener('swiped-right', function (e) {
searxng.selectPrevious(false);
});
}
);
w.addEventListener('scroll', function () {
var e = d.getElementById('backToTop'),
scrollTop = document.documentElement.scrollTop || document.body.scrollTop,

View File

@@ -5,7 +5,7 @@
<span class="title">{{ result.title|striptags }}</span>{{- "" -}}
<span class="source">{{- result.parsed_url.netloc -}}</span>{{- "" -}}
</a>{{- "" -}}
<div class="detail">{{- "" -}}
<div class="detail swipe-horizontal">{{- "" -}}
<a class="result-detail-close" href="#">{{ icon('close') }}</a>{{- "" -}}
<a class="result-detail-previous" href="#">{{ icon('chevron-left') }}</a>{{- "" -}}
<a class="result-detail-next" href="#">{{ icon('chevron-right') }}</a>{{- "" -}}