Spaces:
Paused
Paused
Commit ·
1682796
1
Parent(s): 7e9fe56
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,145 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import sqlite3
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
else:
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
query_string += "".join([f" AND title NOT LIKE ?" for _ in exclude_keywords])
|
| 19 |
-
params = [f"%{keyword}%" for keyword in include_keywords + exclude_keywords]
|
| 20 |
-
cursor.execute(query_string, params)
|
| 21 |
|
| 22 |
-
|
| 23 |
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import configparser
|
| 2 |
+
import pandas as pd
|
| 3 |
import streamlit as st
|
| 4 |
+
import base64
|
| 5 |
import sqlite3
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
|
| 9 |
+
# 获取当前文件目录
|
| 10 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 11 |
+
|
| 12 |
+
# 构造数据库文件路径
|
| 13 |
+
rarbg_db_file = os.path.join(current_dir, 'rarbg_db.db')
|
| 14 |
+
rarbg_config_file = os.path.join(current_dir, 'rarbg_config.ini')
|
| 15 |
+
|
| 16 |
+
my_style = '''
|
| 17 |
+
<style>
|
| 18 |
+
.tag {
|
| 19 |
+
display: inline-block;
|
| 20 |
+
padding: 2px 6px;
|
| 21 |
+
background-color: #f2f2f2; /* 默认的背景颜色 */
|
| 22 |
+
border-radius: 5px; /* 圆角效果 */
|
| 23 |
+
margin: 0 2px;
|
| 24 |
+
transition: background-color 0.3s; /* 平滑的颜色过渡效果 */
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.tag:hover {
|
| 28 |
+
background-color: #cffd51; /* 鼠标经过时的背景颜色 */
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.tag:active {
|
| 32 |
+
background-color: #cffd51; /* 鼠标按下时的背景颜色 */
|
| 33 |
+
}
|
| 34 |
+
</style>
|
| 35 |
+
'''
|
| 36 |
+
|
| 37 |
+
config = configparser.ConfigParser()
|
| 38 |
+
config.read(rarbg_config_file)
|
| 39 |
+
searches_value = config['searches']['value']
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# 图片Base64
|
| 43 |
+
def image_to_base64(img_path):
|
| 44 |
+
with open(img_path, "rb") as image_file:
|
| 45 |
+
return base64.b64encode(image_file.read()).decode()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# 连接数据库
|
| 49 |
+
def get_connection():
|
| 50 |
+
conn = sqlite3.connect("rarbg_db.sqlite")
|
| 51 |
+
return conn
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def search_db(keyword):
|
| 55 |
+
conn = get_connection()
|
| 56 |
+
|
| 57 |
+
# Split keywords by space
|
| 58 |
+
keywords = keyword.split()
|
| 59 |
+
|
| 60 |
+
# Form the LIKE and NOT LIKE patterns
|
| 61 |
+
conditions = []
|
| 62 |
+
params = []
|
| 63 |
+
|
| 64 |
+
for key in keywords:
|
| 65 |
+
if key.startswith("-"):
|
| 66 |
+
conditions.append("title NOT LIKE ?")
|
| 67 |
+
params.append(f"%{key[1:]}%")
|
| 68 |
else:
|
| 69 |
+
conditions.append("title LIKE ?")
|
| 70 |
+
params.append(f"%{key}%")
|
| 71 |
|
| 72 |
+
# Construct the SQL query
|
| 73 |
+
query = f"SELECT id, hash, title, dt, size FROM items WHERE {' AND '.join(conditions)}"
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
df = pd.read_sql_query(query, conn, params=params)
|
| 76 |
conn.close()
|
| 77 |
+
return df
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# 转换文件大小到GB
|
| 81 |
+
def convert_size(size_in_bytes):
|
| 82 |
+
size_in_gb = size_in_bytes / (1024 * 1024 * 1024)
|
| 83 |
+
return round(size_in_gb, 2)
|
| 84 |
|
| 85 |
+
|
| 86 |
+
# 下载搜索结果
|
| 87 |
+
def download_link(df, filename="search_results.csv"):
|
| 88 |
+
csv = df.to_csv(index=False)
|
| 89 |
+
b64 = base64.b64encode(csv.encode()).decode()
|
| 90 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="{filename}">下载搜索结果</a>'
|
| 91 |
+
return href
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
st.set_page_config(page_title="RARBG Database Search", page_icon="🌞", layout='centered', initial_sidebar_state='auto')
|
| 95 |
+
|
| 96 |
+
# 首页
|
| 97 |
+
st.markdown(f'# <a href="https://www.rarbg.quest/" target="_self"><img src="data:image/jpeg;base64,{image_to_base64(os.path.join("rarbg.png"))}" alt="Image" width="50px" height="50px" style="border-radius: 8px; box-shadow: 3px 3px 10px rgba(0,0,0,0.1);"/></a> :rainbow[RARBG 数据库搜索]', unsafe_allow_html=True)
|
| 98 |
+
|
| 99 |
+
# 广告位图片
|
| 100 |
+
st.success(f'''#### 广告位
|
| 101 |
+
😭 接不到广告,我太穷了。请联系我 Telegram: @NervosCKB
|
| 102 |
+
|
| 103 |
+
用户搜索次数:{searches_value}''')
|
| 104 |
+
|
| 105 |
+
# 搜索框
|
| 106 |
+
keyword = st.text_input("输入搜索的关键词:")
|
| 107 |
+
|
| 108 |
+
# 搜索按钮
|
| 109 |
+
if st.button("Search"):
|
| 110 |
+
if keyword:
|
| 111 |
+
# 搜索数据库
|
| 112 |
+
df = search_db(keyword)
|
| 113 |
+
|
| 114 |
+
if not df.empty:
|
| 115 |
+
# 处理结果
|
| 116 |
+
df['hash'] = "magnet:?xt=urn:btih:" + df['hash']
|
| 117 |
+
df['size'] = df['size'].apply(convert_size)
|
| 118 |
+
df = df.rename(columns={'hash': '链接', 'title': '标题', 'size': '文件大小 (GB)', 'dt': '日期'})
|
| 119 |
+
df = df[['标题', '链接', '文件大小 (GB)', '日期']]
|
| 120 |
+
|
| 121 |
+
# 显示结果
|
| 122 |
+
st.dataframe(df, hide_index=False, use_container_width=True)
|
| 123 |
+
|
| 124 |
+
# 下载链接
|
| 125 |
+
st.markdown(download_link(df), unsafe_allow_html=True)
|
| 126 |
+
|
| 127 |
+
# 读取配置文件获取当前搜索次数
|
| 128 |
+
config.read(rarbg_config_file)
|
| 129 |
+
searches_value = int(config['searches']['value'])
|
| 130 |
+
|
| 131 |
+
# 更新搜索次数
|
| 132 |
+
config['searches']['value'] = str(searches_value + 1)
|
| 133 |
+
|
| 134 |
+
# 写回配置文件
|
| 135 |
+
with open(rarbg_config_file, 'w') as configfile:
|
| 136 |
+
config.write(configfile)
|
| 137 |
+
else:
|
| 138 |
+
st.warning("没有找到记录。")
|
| 139 |
else:
|
| 140 |
+
st.error("请输入一些关键词。")
|
| 141 |
+
|
| 142 |
+
st.error('''#### 温馨提示
|
| 143 |
+
本站不提供任何包含反动、淫秽、色情、暴力、侮辱、诽谤等不良信息。当你浏览本网站时,请自觉遵守法律法规,对自己的行为承担全部责任。''')
|
| 144 |
+
st.info('''#### 使用说明
|
| 145 |
+
请在文本框内输入关键词,仅限英文,并点击搜索按钮进行查询。若需输入多个关键词,可用空格进行分隔;若希望某个关键词不在搜索结果中出现,可在关键词前加上 "-" 符号。''')
|