mirror of
https://github.com/wcbing/wcbing-apt-repo.git
synced 2025-12-28 18:18:31 +08:00
refactor: list page
This commit is contained in:
parent
e23a8d7b5b
commit
d87ed92313
166
gen-list-html.py
166
gen-list-html.py
@ -1,166 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
|
||||
# 获取数据库中所有表的数据
|
||||
def fetch_table_data(db: str) -> dict:
|
||||
conn = sqlite3.connect(db)
|
||||
conn.row_factory = sqlite3.Row # 使得查询结果可以通过列名访问
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 获取数据库中的所有表
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
||||
tables = cursor.fetchall()
|
||||
|
||||
# 获取每个表的内容
|
||||
table_data = {}
|
||||
for table in tables:
|
||||
table_name = table["name"]
|
||||
cursor.execute(f"SELECT * FROM '{table_name}'")
|
||||
table_data[table_name] = cursor.fetchall()
|
||||
|
||||
conn.close()
|
||||
return table_data
|
||||
|
||||
|
||||
# 生成静态 HTML 页面
|
||||
def generate_html(table_data, filename):
|
||||
html = """<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>仓库内容</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
}
|
||||
.tabs {
|
||||
overflow: hidden;
|
||||
background-color: #f1f1f1;
|
||||
}
|
||||
.tabs button {
|
||||
background-color: #ddd;
|
||||
border: none;
|
||||
display: inline-block;
|
||||
padding: 10px 20px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
.tabs button:hover {
|
||||
background-color: #ccc;
|
||||
}
|
||||
.tabs button.active {
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
}
|
||||
.tab-content {
|
||||
display: none;
|
||||
padding: 20px;
|
||||
border-top: 2px solid #ddd;
|
||||
}
|
||||
.tab-content.active {
|
||||
display: block;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 20px;
|
||||
}
|
||||
table, th, td {
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
th, td {
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
}
|
||||
/* 增加水平滚动条的样式 */
|
||||
.table-wrapper {
|
||||
overflow-x: auto; /* 启用水平滚动 */
|
||||
-webkit-overflow-scrolling: touch; /* 提升在触控设备上的滚动体验 */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="tabs">"""
|
||||
|
||||
# 创建标签栏
|
||||
for table_name in table_data.keys():
|
||||
html += f'<button class="tab-button" id="tab-{table_name}" onclick="showTab(\'{table_name}\')">{table_name}</button>'
|
||||
|
||||
html += "</div>\n "
|
||||
|
||||
# 渲染每个表格的内容
|
||||
for table_name, rows in table_data.items():
|
||||
html += f'<div class="tab-content" id="content-{table_name}">'
|
||||
html += f"<h2>{table_name}</h2>"
|
||||
|
||||
# 表格外层包裹 div,启用水平滚动
|
||||
html += '<div class="table-wrapper">'
|
||||
html += "<table>"
|
||||
|
||||
# 表头
|
||||
if rows:
|
||||
html += "<tr>"
|
||||
for column in rows[0].keys():
|
||||
html += f"<th>{column}</th>"
|
||||
html += "</tr>"
|
||||
|
||||
# 表格数据
|
||||
for row in rows:
|
||||
html += "<tr>"
|
||||
for column in row.keys():
|
||||
html += f"<td>{row[column]}</td>"
|
||||
html += "</tr>"
|
||||
|
||||
html += "</table>"
|
||||
html += "</div>" # 结束 .table-wrapper
|
||||
html += "</div>"
|
||||
|
||||
# 添加 JavaScript 以实现标签切换功能
|
||||
html += """
|
||||
<script>
|
||||
function showTab(tableName) {
|
||||
// 隐藏所有 tab 内容
|
||||
var contents = document.querySelectorAll('.tab-content');
|
||||
contents.forEach(function(content) {
|
||||
content.classList.remove('active');
|
||||
});
|
||||
// 移除所有按钮的 active 类
|
||||
var buttons = document.querySelectorAll('.tab-button');
|
||||
buttons.forEach(function(button) {
|
||||
button.classList.remove('active');
|
||||
});
|
||||
// 显示当前选中的 tab 内容
|
||||
document.getElementById('content-' + tableName).classList.add('active');
|
||||
// 给当前按钮添加 active 类
|
||||
document.getElementById('tab-' + tableName).classList.add('active');
|
||||
}
|
||||
// 默认显示第一个表
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var firstTable = document.querySelector('.tab-button');
|
||||
if (firstTable) {
|
||||
firstTable.click();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
dir = os.path.dirname(filename)
|
||||
if not os.path.exists(dir):
|
||||
os.makedirs(dir)
|
||||
# 保存为静态 HTML 文件
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.write(html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
db = "data/deb.db"
|
||||
filename="deb/list/index.html"
|
||||
table_data = fetch_table_data(db)
|
||||
generate_html(table_data, filename)
|
||||
@ -18,7 +18,7 @@ from apt_pkg import version_compare
|
||||
apt_pkg.init() # 初始化 apt_pkg
|
||||
|
||||
USER_AGENT = "Debian APT-HTTP/1.3 (3.0.3)" # from Debian 13
|
||||
arch_List = ["all", "amd64", "arm64", "i386"]
|
||||
arch_List = ["amd64", "arm64", "all", "i386"]
|
||||
lock = {arch: Lock() for arch in arch_List}
|
||||
packages = {arch: {} for arch in arch_List} # 存放用于生成 Packages 的内容
|
||||
""" packages format:
|
||||
@ -26,6 +26,7 @@ packages = {arch: {} for arch in arch_List} # 存放用于生成 Packages 的内
|
||||
"arch": {
|
||||
"package1": {
|
||||
"version": "1.0.0",
|
||||
"url": "https://example.com/package1.deb",
|
||||
"package": ""
|
||||
}
|
||||
}
|
||||
@ -99,6 +100,7 @@ def split_latest(packages_file_content: bytes):
|
||||
|
||||
find_name = re.compile(rb"Package:[ ]*(.+)")
|
||||
find_arch = re.compile(rb"Architecture:[ ]*(.+)")
|
||||
find_url = re.compile(rb"Filename:[ ]*(.+)")
|
||||
find_version = re.compile(rb"Version:[ ]*(.+)")
|
||||
|
||||
for package in package_list:
|
||||
@ -106,6 +108,7 @@ def split_latest(packages_file_content: bytes):
|
||||
try:
|
||||
name = find_name.search(package).group(1).decode()
|
||||
arch = find_arch.search(package).group(1).decode()
|
||||
url = find_url.search(package).group(1).decode()
|
||||
tmp_version = find_version.search(package).group(1).decode()
|
||||
with lock[arch]:
|
||||
# 使用 apt_pkg 进行版本比较
|
||||
@ -113,7 +116,7 @@ def split_latest(packages_file_content: bytes):
|
||||
name not in packages[arch]
|
||||
or version_compare(tmp_version, packages[arch][name]["version"]) > 0
|
||||
):
|
||||
packages[arch][name] = {"package": package, "version": tmp_version}
|
||||
packages[arch][name] = {"version": tmp_version, "url": url, "package": package}
|
||||
except Exception as e:
|
||||
logging.error(f"Error processing package {name}: {e}")
|
||||
return
|
||||
@ -170,3 +173,9 @@ if __name__ == "__main__":
|
||||
f.write(i["package"])
|
||||
for i in packages["all"].values():
|
||||
f.write(i["package"])
|
||||
|
||||
# 输出 packages.json,用于展示仓库内容
|
||||
for arch in arch_List:
|
||||
for i in packages[arch].values():
|
||||
i.pop("package")
|
||||
json.dump(packages, open("deb/list/packages.json", "w"), indent=4)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user