livespace.py 2.7 KB

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