Merge pull request #912 from return42/fix-genius

[fix] genius: add player and avoid exceptional programming
This commit is contained in:
Markus Heiser 2022-02-21 22:23:17 +01:00 committed by GitHub
commit 97181080b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 33 deletions

View File

@ -82,7 +82,7 @@ def response(resp):
thumbnail = result.xpath('.//div[@class="art"]/img/@src') thumbnail = result.xpath('.//div[@class="art"]/img/@src')
if thumbnail: if thumbnail:
new_result['thumbnail'] = thumbnail[0] new_result['img_src'] = thumbnail[0]
result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0] result_id = parse_qs(urlparse(link.get('href')).query)["search_item_id"][0]
itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower() itemtype = extract_text(result.xpath('.//div[@class="itemtype"]')).lower()

View File

@ -5,7 +5,6 @@
""" """
from json import loads
from urllib.parse import urlencode from urllib.parse import urlencode
from datetime import datetime from datetime import datetime
@ -26,6 +25,7 @@ page_size = 5
url = 'https://genius.com/api/' url = 'https://genius.com/api/'
search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}' search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}'
music_player = 'https://genius.com{api_path}/apple_music_player'
def request(query, params): def request(query, params):
@ -39,20 +39,28 @@ def request(query, params):
def parse_lyric(hit): def parse_lyric(hit):
try:
content = hit['highlights'][0]['value']
except Exception as e: # pylint: disable=broad-except
logger.error(e, exc_info=True)
content = '' content = ''
highlights = hit['highlights']
if highlights:
content = hit['highlights'][0]['value']
else:
content = hit['result'].get('title_with_featured', '')
timestamp = hit['result']['lyrics_updated_at'] timestamp = hit['result']['lyrics_updated_at']
result = { result = {
'url': hit['result']['url'], 'url': hit['result']['url'],
'title': hit['result']['full_title'], 'title': hit['result']['full_title'],
'content': content, 'content': content,
'thumbnail': hit['result']['song_art_image_thumbnail_url'], 'img_src': hit['result']['song_art_image_thumbnail_url'],
} }
if timestamp: if timestamp:
result.update({'publishedDate': datetime.fromtimestamp(timestamp)}) result.update({'publishedDate': datetime.fromtimestamp(timestamp)})
api_path = hit['result'].get('api_path')
if api_path:
# The players are just playing 30sec from the title. Some of the player
# will be blocked because of a cross-origin request and some players will
# link to apple when you press the play button.
result['iframe_src'] = music_player.format(api_path=api_path)
return result return result
@ -61,26 +69,25 @@ def parse_artist(hit):
'url': hit['result']['url'], 'url': hit['result']['url'],
'title': hit['result']['name'], 'title': hit['result']['name'],
'content': '', 'content': '',
'thumbnail': hit['result']['image_url'], 'img_src': hit['result']['image_url'],
} }
return result return result
def parse_album(hit): def parse_album(hit):
result = { res = hit['result']
'url': hit['result']['url'], content = res.get('name_with_artist', res.get('name', ''))
'title': hit['result']['full_title'], x = res.get('release_date_components')
'thumbnail': hit['result']['cover_art_url'], if x:
'content': '', x = x.get('year')
if x:
content = "%s / %s" % (x, content)
return {
'url': res['url'],
'title': res['full_title'],
'img_src': res['cover_art_url'],
'content': content.strip(),
} }
try:
year = hit['result']['release_date_components']['year']
except Exception as e: # pylint: disable=broad-except
logger.error(e, exc_info=True)
else:
if year:
result.update({'content': 'Released: {}'.format(year)})
return result
parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'album': parse_album} parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'album': parse_album}
@ -88,9 +95,8 @@ parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'alb
def response(resp): def response(resp):
results = [] results = []
json = loads(resp.text) for section in resp.json()['response']['sections']:
hits = [hit for section in json['response']['sections'] for hit in section['hits']] for hit in section['hits']:
for hit in hits:
func = parse.get(hit['type']) func = parse.get(hit['type'])
if func: if func:
results.append(func(hit)) results.append(func(hit))

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -27,3 +27,7 @@ iframe[src^="https://bandcamp.com/EmbeddedPlayer/track"] {
// hide playlist // hide playlist
height: 120px; height: 120px;
} }
iframe[src^="https://genius.com/songs"] {
height: 65px;
}