Spaces:
Paused
Paused
Upload streamlit_app.py
Browse files- streamlit_app.py +74 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sqlite3
|
| 3 |
+
|
| 4 |
+
def search_db(query):
|
| 5 |
+
# 连接SQLite数据库
|
| 6 |
+
conn = sqlite3.connect('rarbg_db.sqlite')
|
| 7 |
+
cursor = conn.cursor()
|
| 8 |
+
|
| 9 |
+
# 拆分查询以便在标题中使用多个关键字
|
| 10 |
+
include_keywords = []
|
| 11 |
+
exclude_keywords = []
|
| 12 |
+
for keyword in query.split():
|
| 13 |
+
if keyword.startswith('-'):
|
| 14 |
+
exclude_keywords.append(keyword[1:])
|
| 15 |
+
else:
|
| 16 |
+
include_keywords.append(keyword)
|
| 17 |
+
|
| 18 |
+
# 构建查询字符串
|
| 19 |
+
query_string = "SELECT title, hash, size, dt as time FROM items WHERE "
|
| 20 |
+
query_string += " AND ".join([f"title LIKE ?" for _ in include_keywords])
|
| 21 |
+
query_string += "".join([f" AND title NOT LIKE ?" for _ in exclude_keywords])
|
| 22 |
+
|
| 23 |
+
# 将关键字添加为通配符参数
|
| 24 |
+
params = [f"%{keyword}%" for keyword in include_keywords + exclude_keywords]
|
| 25 |
+
cursor.execute(query_string, params)
|
| 26 |
+
|
| 27 |
+
results = cursor.fetchall()
|
| 28 |
+
conn.close()
|
| 29 |
+
|
| 30 |
+
return results
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# 创建Streamlit app
|
| 35 |
+
st.title("RARBG Database Search")
|
| 36 |
+
search_query = st.text_input("Enter your search keywords:")
|
| 37 |
+
|
| 38 |
+
if st.button("Search"):
|
| 39 |
+
results = search_db(search_query)
|
| 40 |
+
if results:
|
| 41 |
+
# 显示结果
|
| 42 |
+
st.write("Results:")
|
| 43 |
+
|
| 44 |
+
# 创建HTML表格
|
| 45 |
+
html_table = '<table><tr><th>No.</th><th>Title with Magnet Link</th><th>Size (GB)</th><th>Time</th></tr>'
|
| 46 |
+
all_links = []
|
| 47 |
+
|
| 48 |
+
for i, (title, hash_value, size, time) in enumerate(results, start=1):
|
| 49 |
+
magnet_link = f'magnet:?xt=urn:btih:{hash_value}'
|
| 50 |
+
all_links.append(magnet_link)
|
| 51 |
+
|
| 52 |
+
# 检查 size 是否为 None
|
| 53 |
+
if size is not None:
|
| 54 |
+
size_gb = size / (1024 ** 3)
|
| 55 |
+
size_gb_str = f"{size_gb:.2f} GB"
|
| 56 |
+
else:
|
| 57 |
+
size_gb_str = "Unknown"
|
| 58 |
+
|
| 59 |
+
html_table += f'<tr><td>{i}</td><td><a href="{magnet_link}" target="_blank">{title}</a></td><td>{size_gb_str}</td><td>{time}</td></tr>'
|
| 60 |
+
|
| 61 |
+
html_table += '</table>'
|
| 62 |
+
|
| 63 |
+
# 使用markdown来渲染HTML表格
|
| 64 |
+
st.markdown(html_table, unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
# 创建一个包含所有链接的文本区域
|
| 67 |
+
links_str = "\n".join(all_links)
|
| 68 |
+
copy_html = f'<textarea id="copy_area" readonly style="width: 100%; height: 100px">{links_str}</textarea>'
|
| 69 |
+
st.markdown(copy_html, unsafe_allow_html=True)
|
| 70 |
+
|
| 71 |
+
else:
|
| 72 |
+
st.write("No results found.")
|
| 73 |
+
|
| 74 |
+
|