.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
7from urllib.parse import urlencode
8from datetime import datetime
9
10# about
11about = {
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
21categories = ['music', 'lyrics']
22paging = True
23page_size = 5
24
25url = 'https://genius.com/api/'
26search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}'
27music_player = 'https://genius.com{api_path}/apple_music_player'
28
29
30def 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
40def 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 'img_src': 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
66def parse_artist(hit):
67 result = {
68 'url': hit['result']['url'],
69 'title': hit['result']['name'],
70 'content': '',
71 'img_src': hit['result']['image_url'],
72 }
73 return result
74
75
76def 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 'img_src': res['cover_art_url'],
88 'content': content.strip(),
89 }
90
91
92parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'album': parse_album}
93
94
95def 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
request(query, params)
Definition genius.py:30