Ethscriptions commited on
Commit
1682796
·
1 Parent(s): 7e9fe56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -49
app.py CHANGED
@@ -1,59 +1,145 @@
 
 
1
  import streamlit as st
 
2
  import sqlite3
 
3
 
4
 
5
- def search_db(query):
6
- conn = sqlite3.connect('rarbg_db.sqlite')
7
- cursor = conn.cursor()
8
- include_keywords = []
9
- exclude_keywords = []
10
- for keyword in query.split():
11
- if keyword.startswith('-'):
12
- exclude_keywords.append(keyword[1:])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  else:
14
- include_keywords.append(keyword)
 
15
 
16
- query_string = "SELECT title, hash, size, dt as time FROM items WHERE "
17
- query_string += " AND ".join([f"title LIKE ?" for _ in include_keywords])
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
- results = cursor.fetchall()
23
  conn.close()
 
 
 
 
 
 
 
24
 
25
- return results
26
-
27
- st.title("RARBG Database Search")
28
- st.markdown("> 警告!本站不提供任何包含反动、淫秽、色情、暴力、侮辱、诽谤等不良信息。当你浏览本网站时,请自觉遵守法律法规,对自己的行为承担全部责任。")
29
- st.markdown('> 使用说明:请在文本框内输入关键词,并点击搜索按钮进行查询。若需输入多个关键词,可用空格进行分隔;若希望某个关键词不在搜索结果中出现,可在关键词前加上 "-" 符号。')
30
- search_query = st.text_input("输入搜索的关键词:")
31
-
32
- if st.button("搜索"):
33
- results = search_db(search_query)
34
- if results:
35
- st.write("结果:")
36
- html_table = '<table style="width:100%; table-layout: fixed;"><tr><th>No.</th><th>标题</th><th>文件大小(GB)</th><th>时间</th></tr>'
37
- all_links = []
38
-
39
- for i, (title, hash_value, size, time) in enumerate(results, start=1):
40
- magnet_link = f'magnet:?xt=urn:btih:{hash_value}'
41
- all_links.append(magnet_link)
42
- if size is not None:
43
- size_gb = size / (1024 ** 3)
44
- size_gb_str = f"{size_gb:.2f} GB"
45
- else:
46
- size_gb_str = "Unknown"
47
-
48
- html_table += f'<tr><td style="overflow-wrap: break-word;">{i}</td><td style="overflow-wrap: break-word;"><a href="{magnet_link}" target="_blank">{title}</a></td><td style="overflow-wrap: break-word;">{size_gb_str}</td><td style="overflow-wrap: break-word;">{time}</td></tr>'
49
- html_table += '</table>'
50
- st.markdown(html_table, unsafe_allow_html=True)
51
- # links_str = "\n".join(all_links)
52
- # copy_html = f'{links_str}'
53
- # st.markdown("")
54
- # st.markdown("Telegram: @s_h_a_o_n_v")
55
- # st.button("显示全部内容")
56
- # st.markdown(copy_html, unsafe_allow_html=True)
57
- # st.code(copy_html, language="textile", line_numbers=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  else:
59
- st.write("没有找到任何记录。")
 
 
 
 
 
 
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
+ 请在文本框内输入关键词,仅限英文,并点击搜索按钮进行查询。若需输入多个关键词,可用空格进行分隔;若希望某个关键词不在搜索结果中出现,可在关键词前加上 "-" 符号。''')