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.sqlite') rarbg_config_file = os.path.join(current_dir, 'rarbg_config.ini') my_style = ''' ''' config = configparser.ConfigParser() config.read(rarbg_config_file) searches_value = config['searches']['value'] def get_style(content): return f'{content}' # 图片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_file) 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'下载搜索结果' return href st.set_page_config(page_title="RARBG Database Search", page_icon="🌞", layout='centered', initial_sidebar_state='auto') st.markdown(f'{my_style}', unsafe_allow_html=True) # 首页 st.markdown(f'# Image :blue[RARBG 磁力搜索]', unsafe_allow_html=True) # 搜索框 keyword = st.text_input("输入你想搜索的关键词:") # 搜索按钮 if st.button("搜索"): 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.toast("搜索完成。", icon="👌") # 显示结果 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.toast("没有找到记录。", icon="🔔") else: st.toast("请输入一些关键词。", icon="❌") st.divider() # st.markdown(f'''##### 广告捐赠 😭 我太穷了。请私聊我 Image @NervosCKB,用户搜索总次数:{get_style(searches_value)}。''', unsafe_allow_html=True) st.markdown('''##### 温馨提示 本站不提供任何包含反动、淫秽、色情、暴力、侮辱、诽谤等不良信息。当你浏览本网站时,请自觉遵守法律法规,对自己的行为承担全部责任。''', unsafe_allow_html=True) st.markdown(f'''##### 使用说明 请在文本框内输入 {get_style("英文")} 关键词,并点击{get_style("搜索")}按钮进行查询。 - 若需输入多个关键词,可用 {get_style("空格")} 进行分隔; - 若希望某个关键词不在搜索结果中出现,可在关键词前加上 {get_style("-")} 符号。''', unsafe_allow_html=True)