.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
bilibili.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Bilibili is a Chinese video sharing website.
3
4.. _Bilibili: https://www.bilibili.com
5"""
6
7import random
8import string
9from urllib.parse import urlencode
10from datetime import datetime, timedelta
11
12from searx import utils
13
14# Engine metadata
15about = {
16 "website": "https://www.bilibili.com",
17 "wikidata_id": "Q3077586",
18 "official_api_documentation": None,
19 "use_official_api": False,
20 "require_api_key": False,
21 "results": "JSON",
22}
23
24# Engine configuration
25paging = True
26results_per_page = 20
27categories = ["videos"]
28
29# Search URL
30base_url = "https://api.bilibili.com/x/web-interface/search/type"
31
32cookie = {
33 "innersign": "0",
34 "buvid3": "".join(random.choice(string.hexdigits) for _ in range(16)) + "infoc",
35 "i-wanna-go-back": "-1",
36 "b_ut": "7",
37 "FEED_LIVE_VERSION": "V8",
38 "header_theme_version": "undefined",
39 "home_feed_column": "4",
40}
41
42
43def request(query, params):
44 query_params = {
45 "__refresh__": "true",
46 "page": params["pageno"],
47 "page_size": results_per_page,
48 "single_column": "0",
49 "keyword": query,
50 "search_type": "video",
51 }
52
53 params["url"] = f"{base_url}?{urlencode(query_params)}"
54 params["cookies"] = cookie
55
56 return params
57
58
59# Format the video duration
60def format_duration(duration):
61 if not ":" in duration:
62 return None
63 minutes, seconds = map(int, duration.split(":"))
64 total_seconds = minutes * 60 + seconds
65
66 formatted_duration = str(timedelta(seconds=total_seconds))[2:] if 0 <= total_seconds < 3600 else ""
67
68 return formatted_duration
69
70
71def response(resp):
72 search_res = resp.json()
73
74 results = []
75
76 for item in search_res.get("data", {}).get("result", []):
77 title = utils.html_to_text(item["title"])
78 url = item["arcurl"]
79 thumbnail = item["pic"]
80 description = item["description"]
81 author = item["author"]
82 video_id = item["aid"]
83 unix_date = item["pubdate"]
84
85 formatted_date = datetime.utcfromtimestamp(unix_date)
86 formatted_duration = format_duration(item["duration"])
87 iframe_url = f"https://player.bilibili.com/player.html?aid={video_id}&high_quality=1&autoplay=false&danmaku=0"
88
89 results.append(
90 {
91 "title": title,
92 "url": url,
93 "content": description,
94 "author": author,
95 "publishedDate": formatted_date,
96 "length": formatted_duration,
97 "thumbnail": thumbnail,
98 "iframe_src": iframe_url,
99 "template": "videos.html",
100 }
101 )
102
103 return results
format_duration(duration)
Definition bilibili.py:60
request(query, params)
Definition bilibili.py:43