2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-08-20 00:05:17 +00:00
|
|
|
"""
|
2021-01-13 10:31:25 +00:00
|
|
|
Genius
|
2017-08-20 00:05:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from json import loads
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2017-08-20 00:05:17 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://genius.com/',
|
|
|
|
"wikidata_id": 'Q3419343',
|
|
|
|
"official_api_documentation": 'https://docs.genius.com/',
|
|
|
|
"use_official_api": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
|
|
|
|
2017-08-20 00:05:17 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['music']
|
|
|
|
paging = True
|
|
|
|
page_size = 5
|
|
|
|
|
|
|
|
url = 'https://genius.com/api/'
|
|
|
|
search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}'
|
|
|
|
|
|
|
|
|
|
|
|
def request(query, params):
|
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
|
|
|
index='multi',
|
|
|
|
page_size=page_size,
|
|
|
|
pageno=params['pageno'])
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def parse_lyric(hit):
|
|
|
|
try:
|
|
|
|
content = hit['highlights'][0]['value']
|
|
|
|
except:
|
2020-12-09 12:01:34 +00:00
|
|
|
content = ''
|
2017-08-20 00:05:17 +00:00
|
|
|
timestamp = hit['result']['lyrics_updated_at']
|
|
|
|
result = {'url': hit['result']['url'],
|
|
|
|
'title': hit['result']['full_title'],
|
|
|
|
'content': content,
|
|
|
|
'thumbnail': hit['result']['song_art_image_thumbnail_url'],
|
|
|
|
'template': 'videos.html'}
|
|
|
|
if timestamp:
|
|
|
|
result.update({'publishedDate': datetime.fromtimestamp(timestamp)})
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def parse_artist(hit):
|
|
|
|
result = {'url': hit['result']['url'],
|
|
|
|
'title': hit['result']['name'],
|
2020-12-09 12:01:34 +00:00
|
|
|
'content': '',
|
2017-08-20 00:05:17 +00:00
|
|
|
'thumbnail': hit['result']['image_url'],
|
|
|
|
'template': 'videos.html'}
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def parse_album(hit):
|
|
|
|
result = {'url': hit['result']['url'],
|
|
|
|
'title': hit['result']['full_title'],
|
|
|
|
'thumbnail': hit['result']['cover_art_url'],
|
2020-12-09 12:01:34 +00:00
|
|
|
'content': '',
|
2017-08-20 00:05:17 +00:00
|
|
|
# 'thumbnail': hit['result']['cover_art_thumbnail_url'],
|
|
|
|
'template': 'videos.html'}
|
|
|
|
try:
|
|
|
|
year = hit['result']['release_date_components']['year']
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if year:
|
|
|
|
result.update({'content': 'Released: {}'.format(year)})
|
|
|
|
return result
|
|
|
|
|
2020-02-01 10:01:17 +00:00
|
|
|
|
2017-08-20 00:05:17 +00:00
|
|
|
parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'album': parse_album}
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
json = loads(resp.text)
|
|
|
|
hits = [hit for section in json['response']['sections'] for hit in section['hits']]
|
|
|
|
for hit in hits:
|
2020-12-09 12:01:34 +00:00
|
|
|
func = parse.get(hit['type'])
|
|
|
|
if func:
|
|
|
|
results.append(func(hit))
|
2017-08-20 00:05:17 +00:00
|
|
|
return results
|