.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
livespace.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""LiveSpace (Videos)
3
4.. hint::
5
6 This engine only search for **live streams**!
7
8"""
9
10from urllib.parse import urlencode
11from datetime import datetime
12from babel import dates
13
14about = {
15 "website": 'https://live.space',
16 "wikidata_id": None,
17 "official_api_documentation": None,
18 "use_official_api": True,
19 "require_api_key": False,
20 "results": 'JSON',
21}
22
23categories = ['videos']
24
25base_url = 'https://backend.live.space'
26
27# engine dependent config
28paging = True
29results_per_page = 10
30
31
32def request(query, params):
33
34 args = {'page': params['pageno'] - 1, 'searchKey': query, 'size': results_per_page}
35 params['url'] = f"{base_url}/search/public/stream?{urlencode(args)}"
36 params['headers'] = {'Accept': 'application/json', 'Content-Type': 'application/json'}
37
38 return params
39
40
41def response(resp):
42
43 results = []
44 json = resp.json()
45 now = datetime.now()
46
47 # for live videos
48
49 for result in json.get('result', []):
50
51 title = result.get("title")
52 thumbnailUrl = result.get("thumbnailUrl")
53 category = result.get("category/name")
54 username = result.get("user", {}).get("userName", "")
55 url = f'https://live.space/watch/{username}'
56
57 # stream tags
58 # currently the api seems to always return null before the first tag,
59 # so strip that unless it's not already there
60 tags = ''
61 if result.get("tags"):
62 tags = [x for x in result.get("tags").split(';') if x and x != 'null']
63 tags = ', '.join(tags)
64
65 content = []
66 if category:
67 content.append(f'category - {category}')
68
69 if tags and len(tags) > 0:
70 content.append(f'tags - {tags}')
71
72 # time & duration
73 start_time = None
74 if result.get("startTimeStamp"):
75 start_time = datetime.fromtimestamp(result.get("startTimeStamp") / 1000)
76
77 # for VODs (videos on demand)
78 end_time = None
79 if result.get("endTimeStamp"):
80 end_time = datetime.fromtimestamp(result.get("endTimeStamp") / 1000)
81
82 timestring = ""
83 if start_time:
84 delta = (now if end_time is None else end_time) - start_time
85 timestring = dates.format_timedelta(delta, granularity='second')
86
87 results.append(
88 {
89 'url': url,
90 'title': title,
91 'content': "No category or tags." if len(content) == 0 else ' '.join(content),
92 'author': username,
93 'length': (">= " if end_time is None else "") + timestring,
94 'publishedDate': start_time,
95 'thumbnail': thumbnailUrl,
96 'template': 'videos.html',
97 }
98 )
99
100 return results
request(query, params)
Definition livespace.py:32