.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
genius.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
# pylint: disable=invalid-name
3
"""Genius
4
5
"""
6
7
from
urllib.parse
import
urlencode
8
from
datetime
import
datetime
9
10
# about
11
about = {
12
"website"
:
'https://genius.com/'
,
13
"wikidata_id"
:
'Q3419343'
,
14
"official_api_documentation"
:
'https://docs.genius.com/'
,
15
"use_official_api"
:
True
,
16
"require_api_key"
:
False
,
17
"results"
:
'JSON'
,
18
}
19
20
# engine dependent config
21
categories = [
'music'
,
'lyrics'
]
22
paging =
True
23
page_size = 5
24
25
url =
'https://genius.com/api/'
26
search_url = url +
'search/{index}?{query}&page={pageno}&per_page={page_size}'
27
music_player =
'https://genius.com{api_path}/apple_music_player'
28
29
30
def
request
(query, params):
31
params[
'url'
] = search_url.format(
32
query=urlencode({
'q'
: query}),
33
index=
'multi'
,
34
page_size=page_size,
35
pageno=params[
'pageno'
],
36
)
37
return
params
38
39
40
def
parse_lyric
(hit):
41
content =
''
42
highlights = hit[
'highlights'
]
43
if
highlights:
44
content = hit[
'highlights'
][0][
'value'
]
45
else
:
46
content = hit[
'result'
].get(
'title_with_featured'
,
''
)
47
48
timestamp = hit[
'result'
][
'lyrics_updated_at'
]
49
result = {
50
'url'
: hit[
'result'
][
'url'
],
51
'title'
: hit[
'result'
][
'full_title'
],
52
'content'
: content,
53
'thumbnail'
: hit[
'result'
][
'song_art_image_thumbnail_url'
],
54
}
55
if
timestamp:
56
result.update({
'publishedDate'
: datetime.fromtimestamp(timestamp)})
57
api_path = hit[
'result'
].get(
'api_path'
)
58
if
api_path:
59
# The players are just playing 30sec from the title. Some of the player
60
# will be blocked because of a cross-origin request and some players will
61
# link to apple when you press the play button.
62
result[
'iframe_src'
] = music_player.format(api_path=api_path)
63
return
result
64
65
66
def
parse_artist
(hit):
67
result = {
68
'url'
: hit[
'result'
][
'url'
],
69
'title'
: hit[
'result'
][
'name'
],
70
'content'
:
''
,
71
'thumbnail'
: hit[
'result'
][
'image_url'
],
72
}
73
return
result
74
75
76
def
parse_album
(hit):
77
res = hit[
'result'
]
78
content = res.get(
'name_with_artist'
, res.get(
'name'
,
''
))
79
x = res.get(
'release_date_components'
)
80
if
x:
81
x = x.get(
'year'
)
82
if
x:
83
content =
"%s / %s"
% (x, content)
84
return
{
85
'url'
: res[
'url'
],
86
'title'
: res[
'full_title'
],
87
'thumbnail'
: res[
'cover_art_url'
],
88
'content'
: content.strip(),
89
}
90
91
92
parse = {
'lyric'
: parse_lyric,
'song'
: parse_lyric,
'artist'
: parse_artist,
'album'
: parse_album}
93
94
95
def
response
(resp):
96
results = []
97
for
section
in
resp.json()[
'response'
][
'sections'
]:
98
for
hit
in
section[
'hits'
]:
99
func = parse.get(hit[
'type'
])
100
if
func:
101
results.append(func(hit))
102
return
results
searx.engines.genius.parse_album
parse_album(hit)
Definition
genius.py:76
searx.engines.genius.parse_artist
parse_artist(hit)
Definition
genius.py:66
searx.engines.genius.response
response(resp)
Definition
genius.py:95
searx.engines.genius.parse_lyric
parse_lyric(hit)
Definition
genius.py:40
searx.engines.genius.request
request(query, params)
Definition
genius.py:30
searxng
searx
engines
genius.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0