odysee.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Odysee_ is a decentralised video hosting platform.
  4. .. _Odysee: https://github.com/OdyseeTeam/odysee-frontend
  5. """
  6. import time
  7. from urllib.parse import urlencode
  8. from datetime import datetime
  9. # Engine metadata
  10. about = {
  11. "website": "https://odysee.com/",
  12. "wikidata_id": "Q102046570",
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": "JSON",
  17. }
  18. # Engine configuration
  19. paging = True
  20. time_range_support = True
  21. results_per_page = 20
  22. categories = ['videos']
  23. # Search URL (Note: lighthouse.lbry.com/search works too, and may be faster at times)
  24. base_url = "https://lighthouse.odysee.tv/search"
  25. def request(query, params):
  26. time_range_dict = {
  27. "day": "today",
  28. "week": "thisweek",
  29. "month": "thismonth",
  30. "year": "thisyear",
  31. }
  32. start_index = (params["pageno"] - 1) * results_per_page
  33. query_params = {
  34. "s": query,
  35. "size": results_per_page,
  36. "from": start_index,
  37. "include": "channel,thumbnail_url,title,description,duration,release_time",
  38. "mediaType": "video",
  39. }
  40. if params['time_range'] in time_range_dict:
  41. query_params['time_filter'] = time_range_dict[params['time_range']]
  42. params["url"] = f"{base_url}?{urlencode(query_params)}"
  43. return params
  44. # Format the video duration
  45. def format_duration(duration):
  46. seconds = int(duration)
  47. length = time.gmtime(seconds)
  48. if length.tm_hour:
  49. return time.strftime("%H:%M:%S", length)
  50. return time.strftime("%M:%S", length)
  51. def response(resp):
  52. data = resp.json()
  53. results = []
  54. for item in data:
  55. name = item["name"]
  56. claim_id = item["claimId"]
  57. title = item["title"]
  58. thumbnail_url = item["thumbnail_url"]
  59. description = item["description"] or ""
  60. channel = item["channel"]
  61. release_time = item["release_time"]
  62. duration = item["duration"]
  63. release_date = datetime.strptime(release_time.split("T")[0], "%Y-%m-%d")
  64. formatted_date = datetime.utcfromtimestamp(release_date.timestamp())
  65. url = f"https://odysee.com/{name}:{claim_id}"
  66. iframe_url = f"https://odysee.com/$/embed/{name}:{claim_id}"
  67. odysee_thumbnail = f"https://thumbnails.odycdn.com/optimize/s:390:0/quality:85/plain/{thumbnail_url}"
  68. formatted_duration = format_duration(duration)
  69. results.append(
  70. {
  71. "title": title,
  72. "url": url,
  73. "content": description,
  74. "author": channel,
  75. "publishedDate": formatted_date,
  76. "length": formatted_duration,
  77. "thumbnail": odysee_thumbnail,
  78. "iframe_src": iframe_url,
  79. "template": "videos.html",
  80. }
  81. )
  82. return results