odysee.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Odysee_ is a decentralized video hosting platform.
  3. .. _Odysee: https://github.com/OdyseeTeam/odysee-frontend
  4. """
  5. import time
  6. from urllib.parse import urlencode
  7. from datetime import datetime
  8. import babel
  9. from searx.network import get
  10. from searx.locales import language_tag
  11. from searx.enginelib.traits import EngineTraits
  12. traits: EngineTraits
  13. # Engine metadata
  14. about = {
  15. "website": "https://odysee.com/",
  16. "wikidata_id": "Q102046570",
  17. "official_api_documentation": None,
  18. "use_official_api": False,
  19. "require_api_key": False,
  20. "results": "JSON",
  21. }
  22. # Engine configuration
  23. paging = True
  24. time_range_support = True
  25. results_per_page = 20
  26. categories = ['videos']
  27. # Search URL (Note: lighthouse.lbry.com/search works too, and may be faster at times)
  28. base_url = "https://lighthouse.odysee.tv/search"
  29. def request(query, params):
  30. time_range_dict = {
  31. "day": "today",
  32. "week": "thisweek",
  33. "month": "thismonth",
  34. "year": "thisyear",
  35. }
  36. start_index = (params["pageno"] - 1) * results_per_page
  37. query_params = {
  38. "s": query,
  39. "size": results_per_page,
  40. "from": start_index,
  41. "include": "channel,thumbnail_url,title,description,duration,release_time",
  42. "mediaType": "video",
  43. }
  44. lang = traits.get_language(params['searxng_locale'], None)
  45. if lang is not None:
  46. query_params['language'] = lang
  47. if params['time_range'] in time_range_dict:
  48. query_params['time_filter'] = time_range_dict[params['time_range']]
  49. params["url"] = f"{base_url}?{urlencode(query_params)}"
  50. return params
  51. # Format the video duration
  52. def format_duration(duration):
  53. seconds = int(duration)
  54. length = time.gmtime(seconds)
  55. if length.tm_hour:
  56. return time.strftime("%H:%M:%S", length)
  57. return time.strftime("%M:%S", length)
  58. def response(resp):
  59. data = resp.json()
  60. results = []
  61. for item in data:
  62. name = item["name"]
  63. claim_id = item["claimId"]
  64. title = item["title"]
  65. thumbnail_url = item["thumbnail_url"]
  66. description = item["description"] or ""
  67. channel = item["channel"]
  68. release_time = item["release_time"]
  69. duration = item["duration"]
  70. release_date = datetime.strptime(release_time.split("T")[0], "%Y-%m-%d")
  71. formatted_date = datetime.utcfromtimestamp(release_date.timestamp())
  72. url = f"https://odysee.com/{name}:{claim_id}"
  73. iframe_url = f"https://odysee.com/$/embed/{name}:{claim_id}"
  74. odysee_thumbnail = f"https://thumbnails.odycdn.com/optimize/s:390:0/quality:85/plain/{thumbnail_url}"
  75. formatted_duration = format_duration(duration)
  76. results.append(
  77. {
  78. "title": title,
  79. "url": url,
  80. "content": description,
  81. "author": channel,
  82. "publishedDate": formatted_date,
  83. "length": formatted_duration,
  84. "thumbnail": odysee_thumbnail,
  85. "iframe_src": iframe_url,
  86. "template": "videos.html",
  87. }
  88. )
  89. return results
  90. def fetch_traits(engine_traits: EngineTraits):
  91. """
  92. Fetch languages from Odysee's source code.
  93. """
  94. resp = get(
  95. 'https://raw.githubusercontent.com/OdyseeTeam/odysee-frontend/master/ui/constants/supported_browser_languages.js', # pylint: disable=line-too-long
  96. timeout=60,
  97. )
  98. if not resp.ok:
  99. print("ERROR: can't determine languages from Odysee")
  100. return
  101. for line in resp.text.split("\n")[1:-4]:
  102. lang_tag = line.strip().split(": ")[0].replace("'", "")
  103. try:
  104. sxng_tag = language_tag(babel.Locale.parse(lang_tag, sep="-"))
  105. except babel.UnknownLocaleError:
  106. print("ERROR: %s is unknown by babel" % lang_tag)
  107. continue
  108. conflict = engine_traits.languages.get(sxng_tag)
  109. if conflict:
  110. if conflict != lang_tag:
  111. print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, lang_tag))
  112. continue
  113. engine_traits.languages[sxng_tag] = lang_tag