From 2434c29dc535b034d2fb7be2b753e3bfd453609f Mon Sep 17 00:00:00 2001 From: woorst Date: Sat, 19 Aug 2017 19:05:17 -0500 Subject: New engine: Genius (lyrics) --- searx/engines/genius.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 4 +++ 2 files changed, 93 insertions(+) create mode 100644 searx/engines/genius.py (limited to 'searx') diff --git a/searx/engines/genius.py b/searx/engines/genius.py new file mode 100644 index 000000000..c7bce1c68 --- /dev/null +++ b/searx/engines/genius.py @@ -0,0 +1,89 @@ +""" +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 + +indicies = ['top_hit', 'song', 'lyric', 'artist', 'album', 'tag', 'video', 'article', 'user'] +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 + +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 diff --git a/searx/settings.yml b/searx/settings.yml index 4da96b5bf..33d1de829 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -236,6 +236,10 @@ engines: shortcut : frk disabled : True + - name : genius + engine : genius + shortcut : gen + - name : gigablast engine : gigablast shortcut : gb -- cgit v1.2.3-54-g00ecf From 18a4e7035f72a3c31239ae0bd1ee67cc2ad354b8 Mon Sep 17 00:00:00 2001 From: Apply55gx Date: Wed, 25 Oct 2017 10:42:37 +0200 Subject: removed unused indicies array --- searx/engines/genius.py | 1 - 1 file changed, 1 deletion(-) (limited to 'searx') diff --git a/searx/engines/genius.py b/searx/engines/genius.py index c7bce1c68..b265e9d76 100644 --- a/searx/engines/genius.py +++ b/searx/engines/genius.py @@ -20,7 +20,6 @@ paging = True language_support = False page_size = 5 -indicies = ['top_hit', 'song', 'lyric', 'artist', 'album', 'tag', 'video', 'article', 'user'] url = 'https://genius.com/api/' search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}' -- cgit v1.2.3-54-g00ecf