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