mirror of
https://github.com/searxng/searxng.git
synced 2026-09-23 06:36:12 +00:00
[fix] rewrite vimeo engine
This commit is contained in:
@@ -1,67 +1,98 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
"""
|
"""Vimeo (videos)"""
|
||||||
Wikipedia (Web
|
|
||||||
"""
|
|
||||||
|
|
||||||
from urllib.parse import urlencode
|
import typing as t
|
||||||
from json import loads
|
from datetime import datetime, timedelta
|
||||||
from dateutil import parser
|
from urllib.parse import urlencode, urlparse
|
||||||
|
from searx.result_types import EngineResults
|
||||||
|
from searx.network import get
|
||||||
|
from searx.enginelib import EngineCache
|
||||||
|
|
||||||
from searx.utils import extr
|
# Engine metadata
|
||||||
|
|
||||||
# about
|
|
||||||
about = {
|
about = {
|
||||||
"website": 'https://vimeo.com/',
|
"website": 'https://vimeo.com/',
|
||||||
"wikidata_id": 'Q156376',
|
"wikidata_id": 'Q156376',
|
||||||
"official_api_documentation": 'http://developer.vimeo.com/api',
|
"official_api_documentation": 'http://developer.vimeo.com/api',
|
||||||
"use_official_api": False,
|
"use_official_api": False,
|
||||||
"require_api_key": False,
|
"require_api_key": False,
|
||||||
"results": 'HTML',
|
"results": 'JSON',
|
||||||
}
|
}
|
||||||
|
|
||||||
# engine dependent config
|
|
||||||
categories = ['videos']
|
# Engine configuration
|
||||||
paging = True
|
paging = True
|
||||||
|
categories = ['videos']
|
||||||
|
results_per_page = 20
|
||||||
|
|
||||||
# search-url
|
# Search URL
|
||||||
base_url = 'https://vimeo.com/'
|
base_url = "https://api.vimeo.com"
|
||||||
search_url = base_url + 'search/page:{pageno}?{query}'
|
|
||||||
|
# Cache keys & expiration
|
||||||
|
JWT_CACHE_KEY = "jwt"
|
||||||
|
JWT_CACHE_EXPIRATION_SECONDS = 300
|
||||||
|
|
||||||
|
CACHE: EngineCache
|
||||||
|
|
||||||
|
|
||||||
# do search-request
|
def setup(engine_settings: dict[str, t.Any]) -> bool:
|
||||||
def request(query, params):
|
global CACHE # pylint: disable=global-statement
|
||||||
params['url'] = search_url.format(pageno=params['pageno'], query=urlencode({'q': query}))
|
CACHE = EngineCache(engine_settings["name"])
|
||||||
|
|
||||||
return params
|
|
||||||
|
|
||||||
|
|
||||||
# get response from search-request
|
def fetch_json_web_token():
|
||||||
def response(resp):
|
|
||||||
results = []
|
|
||||||
|
|
||||||
data = loads(extr(resp.text, 'var data = ', ';\n'))
|
if json_web_token := CACHE.get(JWT_CACHE_KEY):
|
||||||
|
return json_web_token
|
||||||
|
|
||||||
# parse results
|
jwt_url = "https://vimeo.com/_next/jwt"
|
||||||
for result in data['filtered']['data']:
|
|
||||||
result = result[result['type']]
|
|
||||||
videoid = result['uri'].split('/')[-1]
|
|
||||||
url = base_url + videoid
|
|
||||||
title = result['name']
|
|
||||||
thumbnail = result['pictures']['sizes'][-1]['link']
|
|
||||||
publishedDate = parser.parse(result['created_time'])
|
|
||||||
|
|
||||||
# append result
|
jwt_headers = {
|
||||||
results.append(
|
'x-requested-with': "XMLHttpRequest",
|
||||||
{
|
}
|
||||||
'url': url,
|
|
||||||
'title': title,
|
resp = get(url=jwt_url, headers=jwt_headers, timeout=5)
|
||||||
'content': '',
|
json_web_token = resp.json()["token"]
|
||||||
'template': 'videos.html',
|
|
||||||
'publishedDate': publishedDate,
|
CACHE.set(key=JWT_CACHE_KEY, value=json_web_token, expire=JWT_CACHE_EXPIRATION_SECONDS)
|
||||||
'iframe_src': "https://player.vimeo.com/video/" + videoid,
|
|
||||||
'thumbnail': thumbnail,
|
return json_web_token
|
||||||
}
|
|
||||||
|
|
||||||
|
def request(query: str, params: "OnlineParams"):
|
||||||
|
|
||||||
|
json_web_token = fetch_json_web_token()
|
||||||
|
|
||||||
|
query_params = {
|
||||||
|
"filter_type": "clip",
|
||||||
|
"query": query,
|
||||||
|
"page": params["pageno"],
|
||||||
|
"per_page": results_per_page,
|
||||||
|
}
|
||||||
|
|
||||||
|
params["url"] = f"{base_url}/search?{urlencode(query_params)}"
|
||||||
|
params["headers"]["content-type"] = "application/json"
|
||||||
|
params["headers"]["authorization"] = "jwt " + json_web_token
|
||||||
|
params["headers"]["Accept"] = "application/vnd.vimeo.*+json;version=3.3"
|
||||||
|
|
||||||
|
|
||||||
|
def response(resp: "SXNG_Response") -> EngineResults:
|
||||||
|
results = EngineResults()
|
||||||
|
search_res = resp.json()
|
||||||
|
|
||||||
|
for item in search_res["data"]:
|
||||||
|
video_id = urlparse(item["clip"]["link"]).path.strip('/')
|
||||||
|
|
||||||
|
results.add(
|
||||||
|
results.types.LegacyResult(
|
||||||
|
template="videos.html",
|
||||||
|
title=item["clip"]["name"],
|
||||||
|
url=item["clip"]["link"],
|
||||||
|
author=item["clip"]["user"]["name"],
|
||||||
|
thumbnail=item["clip"]["pictures"]["base_link"],
|
||||||
|
length=timedelta(seconds=item["clip"]["duration"]),
|
||||||
|
publishedDate=datetime.fromisoformat(item["clip"]["created_time"]),
|
||||||
|
iframe_src="https://player.vimeo.com/video/" + video_id,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# return results
|
|
||||||
return results
|
return results
|
||||||
|
|||||||
Reference in New Issue
Block a user