.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
youtube_api.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""
3
Youtube (Videos)
4
"""
5
6
from
json
import
loads
7
from
urllib.parse
import
urlencode
8
9
from
dateutil
import
parser
10
from
searx.exceptions
import
SearxEngineAPIException
11
12
# about
13
about = {
14
"website"
:
'https://www.youtube.com/'
,
15
"wikidata_id"
:
'Q866'
,
16
"official_api_documentation"
:
'https://developers.google.com/youtube/v3/docs/search/list?apix=true'
,
17
"use_official_api"
:
True
,
18
"require_api_key"
:
False
,
19
"results"
:
'JSON'
,
20
}
21
22
# engine dependent config
23
categories = [
'videos'
,
'music'
]
24
paging =
False
25
api_key =
None
26
27
# search-url
28
base_url =
'https://www.googleapis.com/youtube/v3/search'
29
search_url = base_url +
'?part=snippet&{query}&maxResults=20&key={api_key}'
30
base_youtube_url =
'https://www.youtube.com/watch?v='
31
32
33
# do search-request
34
def
request
(query, params):
35
params[
'url'
] = search_url.format(query=urlencode({
'q'
: query}), api_key=api_key)
36
37
# add language tag if specified
38
if
params[
'language'
] !=
'all'
:
39
params[
'url'
] +=
'&relevanceLanguage='
+ params[
'language'
].split(
'-'
)[0]
40
41
return
params
42
43
44
# get response from search-request
45
def
response
(resp):
46
results = []
47
48
search_results = loads(resp.text)
49
50
if
'error'
in
search_results
and
'message'
in
search_results[
'error'
]:
51
raise
SearxEngineAPIException
(search_results[
'error'
][
'message'
])
52
53
# return empty array if there are no results
54
if
'items'
not
in
search_results:
55
return
[]
56
57
# parse results
58
for
result
in
search_results[
'items'
]:
59
if
"videoId"
not
in
result[
"id"
]:
60
# ignore channels
61
continue
62
63
videoid = result[
'id'
][
'videoId'
]
64
65
title = result[
'snippet'
][
'title'
]
66
content =
''
67
thumbnail =
''
68
69
pubdate = result[
'snippet'
][
'publishedAt'
]
70
publishedDate = parser.parse(pubdate)
71
72
thumbnail = result[
'snippet'
][
'thumbnails'
][
'high'
][
'url'
]
73
74
content = result[
'snippet'
][
'description'
]
75
76
url = base_youtube_url + videoid
77
78
# append result
79
results.append(
80
{
81
'url'
: url,
82
'title'
: title,
83
'content'
: content,
84
'template'
:
'videos.html'
,
85
'publishedDate'
: publishedDate,
86
'iframe_src'
:
"https://www.youtube-nocookie.com/embed/"
+ videoid,
87
'thumbnail'
: thumbnail,
88
}
89
)
90
91
# return results
92
return
results
searx.exceptions.SearxEngineAPIException
Definition
exceptions.py:54
searx.engines.youtube_api.request
request(query, params)
Definition
youtube_api.py:34
searx.engines.youtube_api.response
response(resp)
Definition
youtube_api.py:45
searx.exceptions
Definition
exceptions.py:1
searxng
searx
engines
youtube_api.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0