Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,20 +82,16 @@ CATEGORIES = {
|
|
| 82 |
# ββββββββββββββββββββββββββ 3. DATABASE FUNCTIONS ββββββββββββββββββββββββββ
|
| 83 |
def init_db():
|
| 84 |
"""Initialize both JSON and SQLite databases"""
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# Initialize JSON file if it doesn't exist
|
| 90 |
-
if not os.path.exists(DB_FILE):
|
| 91 |
-
try:
|
| 92 |
with open(DB_FILE, "w", encoding="utf-8") as f:
|
| 93 |
json.dump([], f, ensure_ascii=False)
|
| 94 |
logger.info("Created new JSON database file")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
#
|
| 99 |
try:
|
| 100 |
conn = sqlite3.connect(SQLITE_DB)
|
| 101 |
cursor = conn.cursor()
|
|
@@ -107,25 +103,26 @@ def init_db():
|
|
| 107 |
)
|
| 108 |
''')
|
| 109 |
conn.commit()
|
| 110 |
-
logger.info("SQLite database initialized successfully")
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
# migrate the data from JSON to SQLite
|
| 114 |
json_urls = load_json()
|
| 115 |
if json_urls:
|
| 116 |
-
logger.info(f"Found {len(json_urls)} URLs in JSON file")
|
| 117 |
db_urls = load_db_sqlite()
|
| 118 |
-
new_urls = 0
|
| 119 |
for url in json_urls:
|
| 120 |
if url not in db_urls:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
conn.close()
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Error initializing SQLite database: {e}")
|
| 128 |
|
|
|
|
|
|
|
| 129 |
# Create default URLs file if none exists
|
| 130 |
if not load_db():
|
| 131 |
default_urls = [
|
|
@@ -735,7 +732,7 @@ function loadManage() {
|
|
| 735 |
<div style="height:20px; background-color:#f0f0f0; border-radius:4px; overflow:hidden;">
|
| 736 |
<div id="progress-fill" style="height:100%; width:0%; background-color:#4a6dd8; transition:width 0.3s;"></div>
|
| 737 |
</div>
|
| 738 |
-
<div id="progress-text" style="text-align:center; margin-top:5px; font-size:14px;">0
|
| 739 |
</div>
|
| 740 |
|
| 741 |
<h2>Manage Saved URLs</h2>
|
|
@@ -883,8 +880,8 @@ function addBatchUrls() {
|
|
| 883 |
const progressFill = document.getElementById('progress-fill');
|
| 884 |
const progressText = document.getElementById('progress-text');
|
| 885 |
progressBar.style.display = 'block';
|
| 886 |
-
progressFill.style.width = '0
|
| 887 |
-
progressText.textContent = '0
|
| 888 |
|
| 889 |
// Add URLs one by one
|
| 890 |
let processed = 0;
|
|
@@ -892,8 +889,8 @@ function addBatchUrls() {
|
|
| 892 |
|
| 893 |
function updateProgress() {
|
| 894 |
const percentage = Math.round((processed / urls.length) * 100);
|
| 895 |
-
progressFill.style.width = percentage + '
|
| 896 |
-
progressText.textContent = `${processed}/${urls.length} (${percentage}
|
| 897 |
}
|
| 898 |
|
| 899 |
function addNextUrl(index) {
|
|
@@ -1101,9 +1098,17 @@ window.addEventListener('load', function() {
|
|
| 1101 |
# ββββββββββββββββββββββββββ 8. MAIN ROUTES ββββββββββββββββββββββββββ
|
| 1102 |
@app.route('/')
|
| 1103 |
def home():
|
| 1104 |
-
#
|
|
|
|
|
|
|
|
|
|
| 1105 |
categories_json = json.dumps(list(CATEGORIES.keys()))
|
| 1106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1107 |
|
| 1108 |
# Initialize database on startup
|
| 1109 |
init_db()
|
|
@@ -1148,4 +1153,14 @@ def before_request_func():
|
|
| 1148 |
logger.info(f"Database status - SQLite: {len(load_db_sqlite())} URLs, JSON: {len(load_json())} URLs")
|
| 1149 |
|
| 1150 |
if __name__ == '__main__':
|
| 1151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# ββββββββββββββββββββββββββ 3. DATABASE FUNCTIONS ββββββββββββββββββββββββββ
|
| 83 |
def init_db():
|
| 84 |
"""Initialize both JSON and SQLite databases"""
|
| 85 |
+
# JSON νμΌ μ΄κΈ°ν
|
| 86 |
+
try:
|
| 87 |
+
if not os.path.exists(DB_FILE):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with open(DB_FILE, "w", encoding="utf-8") as f:
|
| 89 |
json.dump([], f, ensure_ascii=False)
|
| 90 |
logger.info("Created new JSON database file")
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"Error creating JSON file: {e}")
|
| 93 |
|
| 94 |
+
# SQLite λ°μ΄ν°λ² μ΄μ€ μ΄κΈ°ν
|
| 95 |
try:
|
| 96 |
conn = sqlite3.connect(SQLITE_DB)
|
| 97 |
cursor = conn.cursor()
|
|
|
|
| 103 |
)
|
| 104 |
''')
|
| 105 |
conn.commit()
|
|
|
|
| 106 |
|
| 107 |
+
# JSONμμ λ°μ΄ν° λ§μ΄κ·Έλ μ΄μ
|
|
|
|
| 108 |
json_urls = load_json()
|
| 109 |
if json_urls:
|
|
|
|
| 110 |
db_urls = load_db_sqlite()
|
|
|
|
| 111 |
for url in json_urls:
|
| 112 |
if url not in db_urls:
|
| 113 |
+
try:
|
| 114 |
+
cursor.execute("INSERT INTO urls (url) VALUES (?)", (url,))
|
| 115 |
+
except sqlite3.IntegrityError:
|
| 116 |
+
pass # URLμ΄ μ΄λ―Έ μ‘΄μ¬νλ κ²½μ°
|
| 117 |
+
conn.commit()
|
| 118 |
|
| 119 |
conn.close()
|
| 120 |
+
logger.info("SQLite database initialized successfully")
|
| 121 |
except Exception as e:
|
| 122 |
logger.error(f"Error initializing SQLite database: {e}")
|
| 123 |
|
| 124 |
+
|
| 125 |
+
|
| 126 |
# Create default URLs file if none exists
|
| 127 |
if not load_db():
|
| 128 |
default_urls = [
|
|
|
|
| 732 |
<div style="height:20px; background-color:#f0f0f0; border-radius:4px; overflow:hidden;">
|
| 733 |
<div id="progress-fill" style="height:100%; width:0%; background-color:#4a6dd8; transition:width 0.3s;"></div>
|
| 734 |
</div>
|
| 735 |
+
<div id="progress-text" style="text-align:center; margin-top:5px; font-size:14px;">0%</div>
|
| 736 |
</div>
|
| 737 |
|
| 738 |
<h2>Manage Saved URLs</h2>
|
|
|
|
| 880 |
const progressFill = document.getElementById('progress-fill');
|
| 881 |
const progressText = document.getElementById('progress-text');
|
| 882 |
progressBar.style.display = 'block';
|
| 883 |
+
progressFill.style.width = '0%';
|
| 884 |
+
progressText.textContent = '0%';
|
| 885 |
|
| 886 |
// Add URLs one by one
|
| 887 |
let processed = 0;
|
|
|
|
| 889 |
|
| 890 |
function updateProgress() {
|
| 891 |
const percentage = Math.round((processed / urls.length) * 100);
|
| 892 |
+
progressFill.style.width = percentage + '%';
|
| 893 |
+
progressText.textContent = `${processed}/${urls.length} (${percentage}%)`;
|
| 894 |
}
|
| 895 |
|
| 896 |
function addNextUrl(index) {
|
|
|
|
| 1098 |
# ββββββββββββββββββββββββββ 8. MAIN ROUTES ββββββββββββββββββββββββββ
|
| 1099 |
@app.route('/')
|
| 1100 |
def home():
|
| 1101 |
+
# HTMLμ μ§μ λ°ν (ν
νλ¦Ώ νμΌ μμ± λμ )
|
| 1102 |
+
from string import Template
|
| 1103 |
+
|
| 1104 |
+
# μΉ΄ν
κ³ λ¦¬ JSON λ°°μ΄ μμ±
|
| 1105 |
categories_json = json.dumps(list(CATEGORIES.keys()))
|
| 1106 |
+
|
| 1107 |
+
# Template κ°μ²΄ μμ± - HTML_TEMPLATE λ΄ ${categories} λ¬Έμμ΄μ λ체
|
| 1108 |
+
template = Template(HTML_TEMPLATE)
|
| 1109 |
+
return template.substitute(categories=categories_json)
|
| 1110 |
+
|
| 1111 |
+
|
| 1112 |
|
| 1113 |
# Initialize database on startup
|
| 1114 |
init_db()
|
|
|
|
| 1153 |
logger.info(f"Database status - SQLite: {len(load_db_sqlite())} URLs, JSON: {len(load_json())} URLs")
|
| 1154 |
|
| 1155 |
if __name__ == '__main__':
|
| 1156 |
+
# λͺ
μμ μΌλ‘ DB μ΄κΈ°ν
|
| 1157 |
+
logger.info("Initializing database...")
|
| 1158 |
+
init_db()
|
| 1159 |
+
|
| 1160 |
+
# λ°μ΄ν°λ² μ΄μ€ νμΌ μν νμΈ
|
| 1161 |
+
if os.path.exists(SQLITE_DB):
|
| 1162 |
+
logger.info(f"Database file exists, size: {os.path.getsize(SQLITE_DB)} bytes")
|
| 1163 |
+
else:
|
| 1164 |
+
logger.error("Database file does not exist after initialization!")
|
| 1165 |
+
|
| 1166 |
+
app.run(host='0.0.0.0', port=7860, debug=True) # debug=True μΆκ°λ‘ μ€λ₯ λ©μμ§ νμΈ
|