From fde21c8f99a578f60332bebc336b6f2e9fbe3874 Mon Sep 17 00:00:00 2001 From: micsthepick <11528421+micsthepick@users.noreply.github.com> Date: Sun, 18 Feb 2024 10:01:13 +1100 Subject: [PATCH] [feat] engine: Add LiveSpace livestreams --- AUTHORS.rst | 3 +- searx/engines/livespace.py | 101 +++++++++++++++++++++++++++++++++++++ searx/settings.yml | 7 +++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100755 searx/engines/livespace.py diff --git a/AUTHORS.rst b/AUTHORS.rst index 47af54f26..a4478846a 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -170,4 +170,5 @@ features or generally made searx better: - @blob42 ``_ - Paolo Basso `` - Bernie Huang `` -- Austin Olacsi `` \ No newline at end of file +- Austin Olacsi `` +- @micsthepick diff --git a/searx/engines/livespace.py b/searx/engines/livespace.py new file mode 100755 index 000000000..3f466bbf0 --- /dev/null +++ b/searx/engines/livespace.py @@ -0,0 +1,101 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""LiveSpace (Videos) + +.. hint:: + + This engine only search for **live streams**! + +""" + +from urllib.parse import urlencode +from datetime import datetime +from babel import dates + +about = { + "website": 'https://live.space', + "wikidata_id": None, + "official_api_documentation": None, + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +categories = ['videos'] + +base_url = 'https://backend.live.space' + +# engine dependent config +paging = True +results_per_page = 10 + + +def request(query, params): + + args = {'page': params['pageno'] - 1, 'searchKey': query, 'size': results_per_page} + params['url'] = f"{base_url}/search/public/stream?{urlencode(args)}" + params['headers'] = {'Accept': 'application/json', 'Content-Type': 'application/json'} + + return params + + +def response(resp): + + results = [] + json = resp.json() + now = datetime.now() + + # for live videos + + for result in json.get('result', []): + + title = result.get("title") + thumbnailUrl = result.get("thumbnailUrl") + category = result.get("category/name") + username = result.get("user", {}).get("userName", "") + url = f'https://live.space/watch/{username}' + + # stream tags + # currently the api seems to always return null before the first tag, + # so strip that unless it's not already there + tags = '' + if result.get("tags"): + tags = [x for x in result.get("tags").split(';') if x and x != 'null'] + tags = ', '.join(tags) + + content = [] + if category: + content.append(f'category - {category}') + + if tags and len(tags) > 0: + content.append(f'tags - {tags}') + + # time & duration + start_time = None + if result.get("startTimeStamp"): + start_time = datetime.fromtimestamp(result.get("startTimeStamp") / 1000) + + # for VODs (videos on demand) + end_time = None + if result.get("endTimeStamp"): + end_time = datetime.fromtimestamp(result.get("endTimeStamp") / 1000) + + timestring = "" + if start_time: + delta = (now if end_time is None else end_time) - start_time + timestring = dates.format_timedelta(delta, granularity='second') + + results.append( + { + 'url': url, + 'title': title, + 'content': "No category or tags." if len(content) == 0 else ' '.join(content), + 'author': username, + 'length': (">= " if end_time is None else "") + timestring, + 'publishedDate': start_time, + 'thumbnail': thumbnailUrl, + 'template': 'videos.html', + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index efdab6fe5..6847db53d 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -2042,6 +2042,13 @@ engines: categories: videos disabled: true + - name: livespace + engine: livespace + shortcut: ls + categories: videos + disabled: true + timeout: 5.0 + - name: wordnik engine: wordnik shortcut: def