Skip to content

Commit 246a1f7

Browse files
author
levywang
committed
feat: Added popular search recommendation function
1 parent 38ed99e commit 246a1f7

File tree

8 files changed

+573
-330
lines changed

8 files changed

+573
-330
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ python main.py
5555
```
5656
The default API address: `http://127.0.0.1:8000/`
5757

58-
You can configure a reverse proxy and domain, replacing `BASE_URL` in line 38 of `web/script.js`.
58+
You can configure a reverse proxy and domain, replacing `BASE_URL` in line 3 of `web/config.js`.
5959

6060
The backend configuration file is located in `data/config.yaml`. Modify it according to your actual needs.
6161

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ python main.py
5656
```
5757
默认运行的API地址:`http://127.0.0.1:8000/`
5858

59-
可以配置反代和域名,替换 `web/script.js` 38行中的 `BASE_URL`
59+
可以配置反代和域名,替换 `web/config.js` 3行中的 `BASE_URL`
6060

6161
后端运行的配置文件在 `data/config.yaml` 中,请根据实际情况修改
6262

main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,15 @@ def read_last_n_lines():
310310
if not search_terms:
311311
return {"status": "succeed", "data": []}
312312

313-
# 统计每个搜索词的出现次数并获取前N名
313+
# 统计每个搜索词的出现次数并获取指定范围的数据
314314
term_counts = Counter(search_terms)
315-
top_terms = [term for term, _ in term_counts.most_common(top_n)]
315+
most_common_all = term_counts.most_common() # 获取全部排序结果
316+
start_index = top_n
317+
end_index = start_index + top_n
318+
selected_terms = most_common_all[start_index:end_index] # 切片获取指定范围
319+
top_terms = [term for term, _ in selected_terms]
316320

317-
logger.info(f"Retrieved top {top_n} popular search terms from last {last_n_lines} lines")
321+
logger.info(f"Retrieved top {top_n*2} popular search terms from last {last_n_lines} lines")
318322
return {"status": "succeed", "data": top_terms}
319323
except asyncio.TimeoutError:
320324
logger.error("Timeout while reading log file")

web/config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// config.js
2+
const API_CONFIG = {
3+
BASE_URL: '/api/v1',
4+
ENDPOINTS: {
5+
SEARCH: '/avcode',
6+
COLLECTIONS: '/hacg',
7+
VIDEO: '/get_video',
8+
HOT_SEARCHES: '/hot_searches'
9+
}
10+
};
11+
12+
// 导出配置
13+
export default API_CONFIG;

web/globals.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// globals.js - 包含所有需要在HTML中直接调用的全局函数
2+
3+
// 在此处存储全局状态
4+
let appState = {
5+
translations: null, // 将在初始化后从脚本中设置
6+
currentLang: 'zh',
7+
SORT_OPTIONS: null, // 将在初始化后从脚本中设置
8+
};
9+
10+
// 注册全局函数
11+
window.switchTab = function(tabName) {
12+
// 调用主脚本中的函数
13+
window.dispatchEvent(new CustomEvent('switchTab', { detail: { tabName } }));
14+
};
15+
16+
window.searchMagnet = function() {
17+
// 触发搜索事件
18+
window.dispatchEvent(new CustomEvent('searchMagnet'));
19+
};
20+
21+
window.copyToClipboard = function(text) {
22+
// 触发复制事件
23+
window.dispatchEvent(new CustomEvent('copyToClipboard', { detail: { text } }));
24+
};
25+
26+
window.showSortMenu = function(button) {
27+
// 触发排序菜单事件
28+
window.dispatchEvent(new CustomEvent('showSortMenu', { detail: { button } }));
29+
};
30+
31+
// 添加热门搜索词点击处理函数
32+
window.searchWithTerm = function(term) {
33+
// 触发热门搜索词点击事件
34+
window.dispatchEvent(new CustomEvent('searchWithTerm', { detail: { term } }));
35+
};
36+
37+
// 添加视频页面复制URL按钮点击事件
38+
window.copyVideoUrl = function() {
39+
const sourceUrlElement = document.getElementById('videoSourceUrl');
40+
const sourceUrl = sourceUrlElement?.textContent;
41+
if (!sourceUrl) return;
42+
43+
// 使用全局copyToClipboard函数
44+
window.copyToClipboard(sourceUrl);
45+
46+
// 更新按钮状态
47+
const copyButton = document.getElementById('copySourceUrl');
48+
if (copyButton) {
49+
copyButton.classList.add('copied');
50+
const textElement = copyButton.querySelector('.tab-text');
51+
if (textElement) {
52+
const originalText = textElement.textContent;
53+
textElement.textContent = appState.translations ?
54+
appState.translations[appState.currentLang].copied :
55+
'已复制';
56+
57+
setTimeout(() => {
58+
copyButton.classList.remove('copied');
59+
textElement.textContent = originalText;
60+
}, 2000);
61+
}
62+
}
63+
};
64+
65+
// 注册全局事件处理函数以便主脚本可以设置全局状态
66+
window.setGlobalState = function(key, value) {
67+
appState[key] = value;
68+
};
69+
70+
// 为主脚本提供初始化方法
71+
window.initializeGlobals = function(data) {
72+
if (data.translations) appState.translations = data.translations;
73+
if (data.currentLang) appState.currentLang = data.currentLang;
74+
if (data.SORT_OPTIONS) appState.SORT_OPTIONS = data.SORT_OPTIONS;
75+
};

web/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<link rel="icon" href="imgs/favicon.ico">
88
<link href="https://testingcf.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
99
<link href="style.css" rel="stylesheet">
10-
<link href="https://testingcf.jsdelivr.net/npm/[email protected]/dist/hls.min.js" rel="stylesheet">
1110
<meta name="theme-color" content="#000000">
1211
<meta name="apple-mobile-web-app-capable" content="yes">
12+
<meta name="mobile-web-app-capable" content="yes">
1313
<meta name="apple-mobile-web-app-status-bar-style" content="black">
1414
<meta name="apple-mobile-web-app-title" content="AvHub">
1515
<link rel="manifest" href="manifest.json">
@@ -175,7 +175,7 @@
175175
</div>
176176
<div class="source-url truncate font-mono" id="videoSourceUrl"></div>
177177
</div>
178-
<button id="copySourceUrl" class="copy-button flex items-center px-3 py-1.5 rounded">
178+
<button id="copySourceUrl" class="copy-button flex items-center px-3 py-1.5 rounded" onclick="copyVideoUrl()">
179179
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
180180
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"></path>
181181
</svg>
@@ -193,8 +193,12 @@
193193
</svg>
194194
</button>
195195

196+
<!-- 正确位置加载HLS.js -->
196197
<script src="https://testingcf.jsdelivr.net/npm/[email protected]/dist/hls.min.js"></script>
197-
<script src="script.js"></script>
198+
<!-- 加载全局函数 -->
199+
<script src="globals.js"></script>
200+
<!-- 加载主脚本模块 -->
201+
<script type="module" src="script.js"></script>
198202

199203
<!-- 在body末尾添加通知元素 -->
200204
<div class="notification" id="notification">

0 commit comments

Comments
 (0)