Spaces:
Running
Running
import configparser | |
import pandas as pd | |
import streamlit as st | |
import base64 | |
import sqlite3 | |
import os | |
# 获取当前文件目录 | |
current_dir = os.path.dirname(os.path.abspath(__file__)) | |
# 构造数据库文件路径 | |
rarbg_db_file = os.path.join(current_dir, 'rarbg_db.db') | |
rarbg_config_file = os.path.join(current_dir, 'rarbg_config.ini') | |
my_style = ''' | |
<style> | |
.tag { | |
display: inline-block; | |
padding: 2px 6px; | |
background-color: #f2f2f2; /* 默认的背景颜色 */ | |
border-radius: 5px; /* 圆角效果 */ | |
margin: 0 2px; | |
transition: background-color 0.3s; /* 平滑的颜色过渡效果 */ | |
} | |
.tag:hover { | |
background-color: #cffd51; /* 鼠标经过时的背景颜色 */ | |
} | |
.tag:active { | |
background-color: #cffd51; /* 鼠标按下时的背景颜色 */ | |
} | |
</style> | |
''' | |
config = configparser.ConfigParser() | |
config.read(rarbg_config_file) | |
searches_value = config['searches']['value'] | |
# 图片Base64 | |
def image_to_base64(img_path): | |
with open(img_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode() | |
# 连接数据库 | |
def get_connection(): | |
conn = sqlite3.connect("rarbg_db.sqlite") | |
return conn | |
def search_db(keyword): | |
conn = get_connection() | |
# Split keywords by space | |
keywords = keyword.split() | |
# Form the LIKE and NOT LIKE patterns | |
conditions = [] | |
params = [] | |
for key in keywords: | |
if key.startswith("-"): | |
conditions.append("title NOT LIKE ?") | |
params.append(f"%{key[1:]}%") | |
else: | |
conditions.append("title LIKE ?") | |
params.append(f"%{key}%") | |
# Construct the SQL query | |
query = f"SELECT id, hash, title, dt, size FROM items WHERE {' AND '.join(conditions)}" | |
df = pd.read_sql_query(query, conn, params=params) | |
conn.close() | |
return df | |
# 转换文件大小到GB | |
def convert_size(size_in_bytes): | |
size_in_gb = size_in_bytes / (1024 * 1024 * 1024) | |
return round(size_in_gb, 2) | |
# 下载搜索结果 | |
def download_link(df, filename="search_results.csv"): | |
csv = df.to_csv(index=False) | |
b64 = base64.b64encode(csv.encode()).decode() | |
href = f'<a href="data:file/csv;base64,{b64}" download="{filename}">下载搜索结果</a>' | |
return href | |
st.set_page_config(page_title="RARBG Database Search", page_icon="🌞", layout='centered', initial_sidebar_state='auto') | |
# 首页 | |
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) | |
# 广告位图片 | |
st.success(f'''#### 广告位 | |
😭 接不到广告,我太穷了。请联系我 Telegram: @NervosCKB | |
用户搜索次数:{searches_value}''') | |
# 搜索框 | |
keyword = st.text_input("输入搜索的关键词:") | |
# 搜索按钮 | |
if st.button("Search"): | |
if keyword: | |
# 搜索数据库 | |
df = search_db(keyword) | |
if not df.empty: | |
# 处理结果 | |
df['hash'] = "magnet:?xt=urn:btih:" + df['hash'] | |
df['size'] = df['size'].apply(convert_size) | |
df = df.rename(columns={'hash': '链接', 'title': '标题', 'size': '文件大小 (GB)', 'dt': '日期'}) | |
df = df[['标题', '链接', '文件大小 (GB)', '日期']] | |
# 显示结果 | |
st.dataframe(df, hide_index=False, use_container_width=True) | |
# 下载链接 | |
st.markdown(download_link(df), unsafe_allow_html=True) | |
# 读取配置文件获取当前搜索次数 | |
config.read(rarbg_config_file) | |
searches_value = int(config['searches']['value']) | |
# 更新搜索次数 | |
config['searches']['value'] = str(searches_value + 1) | |
# 写回配置文件 | |
with open(rarbg_config_file, 'w') as configfile: | |
config.write(configfile) | |
else: | |
st.warning("没有找到记录。") | |
else: | |
st.error("请输入一些关键词。") | |
st.error('''#### 温馨提示 | |
本站不提供任何包含反动、淫秽、色情、暴力、侮辱、诽谤等不良信息。当你浏览本网站时,请自觉遵守法律法规,对自己的行为承担全部责任。''') | |
st.info('''#### 使用说明 | |
请在文本框内输入关键词,仅限英文,并点击搜索按钮进行查询。若需输入多个关键词,可用空格进行分隔;若希望某个关键词不在搜索结果中出现,可在关键词前加上 "-" 符号。''') | |