genius.py 2.46 KB
Newer Older
woorst's avatar
woorst committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Genius

 @website     https://www.genius.com/
 @provide-api yes (https://docs.genius.com/)

 @using-api   yes
 @results     JSON
 @stable      yes
 @parse       url, title, content, thumbnail, publishedDate
"""

from json import loads
from searx.url_utils import urlencode
from datetime import datetime

# engine dependent config
categories = ['music']
paging = True
language_support = False
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:
        content = None
    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'],
              'content': None,
              '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'],
              # '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

75

woorst's avatar
woorst committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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:
        try:
            func = parse[hit['type']]
        except KeyError:
            continue
        results.append(func(hit))
    return results