.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
pixabay.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""Pixabay provides royalty-free media (images, videos)"""
3
4
from
datetime
import
timedelta
5
from
urllib.parse
import
quote_plus, urlencode
6
from
dateutil
import
parser
7
from
searx.utils
import
gen_useragent
8
9
# about
10
about = {
11
"website"
:
'https://pixabay.com'
,
12
"wikidata_id"
:
'Q1746538'
,
13
"official_api_documentation"
:
'https://pixabay.com/api/docs/'
,
14
"use_official_api"
:
False
,
15
"require_api_key"
:
False
,
16
"results"
:
'JSON'
,
17
}
18
19
base_url =
'https://pixabay.com'
20
categories = [
'images'
]
21
pixabay_type =
"images"
# alternative: 'videos'
22
23
paging =
True
24
safesearch =
True
25
time_range_support =
True
26
27
safesearch_map = {0:
'off'
, 1:
'1'
, 2:
'1'
}
28
time_range_map = {
'day'
:
'1d'
,
'week'
:
'1w'
,
'month'
:
'1m'
,
'year'
:
'1y'
}
29
30
# using http2 returns forbidden errors
31
enable_http2 =
False
32
33
34
def
request
(query, params):
35
args = {
36
'pagi'
: params[
'pageno'
],
37
}
38
if
params[
'time_range'
]:
39
args[
'date'
] = time_range_map[params[
'time_range'
]]
40
41
params[
'url'
] = f
"{base_url}/{pixabay_type}/search/{quote_plus(query)}/?{urlencode(args)}"
42
params[
'headers'
] = {
43
'User-Agent'
: gen_useragent() +
" Pixabay"
,
44
'Accept'
:
'application/json'
,
45
'x-bootstrap-cache-miss'
:
'1'
,
46
'x-fetch-bootstrap'
:
'1'
,
47
}
48
params[
'cookies'
][
'g_rated'
] = safesearch_map[params[
'safesearch'
]]
49
50
# prevent automatic redirects to first page on pagination
51
params[
'allow_redirects'
] =
False
52
53
return
params
54
55
56
def
_image_result
(result):
57
return
{
58
'template'
:
'images.html'
,
59
'url'
: base_url + result[
"href"
],
60
# images are sorted in ascending quality
61
'thumbnail_src'
: list(result[
'sources'
].values())[0],
62
'img_src'
: list(result[
'sources'
].values())[-1],
63
'title'
: result.get(
'name'
),
64
'content'
: result.get(
'description'
,
''
),
65
}
66
67
68
def
_video_result
(result):
69
return
{
70
'template'
:
'videos.html'
,
71
'url'
: base_url + result[
"href"
],
72
# images are sorted in ascending quality
73
'thumbnail'
: result[
'sources'
].get(
'thumbnail'
),
74
'iframe_src'
: result[
'sources'
].get(
'embed'
),
75
'title'
: result.get(
'name'
),
76
'content'
: result.get(
'description'
,
''
),
77
'length'
: timedelta(seconds=result[
'duration'
]),
78
'publishedDate'
: parser.parse(result[
'uploadDate'
]),
79
}
80
81
82
def
response
(resp):
83
results = []
84
85
# if there are no results on this page, we get a redirect
86
# to the first page
87
if
resp.status_code == 302:
88
return
results
89
90
json_data = resp.json()
91
92
for
result
in
json_data.get(
'page'
, {}).get(
'results'
, []):
93
if
result[
'mediaType'
]
in
(
'photo'
,
'illustration'
,
'vector'
):
94
results.append(
_image_result
(result))
95
elif
result[
'mediaType'
] ==
'video'
:
96
results.append(
_video_result
(result))
97
98
return
results
searx.engines.pixabay._image_result
_image_result(result)
Definition
pixabay.py:56
searx.engines.pixabay.response
response(resp)
Definition
pixabay.py:82
searx.engines.pixabay._video_result
_video_result(result)
Definition
pixabay.py:68
searx.engines.pixabay.request
request(query, params)
Definition
pixabay.py:34
searx.utils
Definition
utils.py:1
searxng
searx
engines
pixabay.py
Generated on
for .oO SearXNG Developer Documentation Oo. by
1.14.0