openfree commited on
Commit
0057f4f
ยท
verified ยท
1 Parent(s): 0c3b5c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -26
app.py CHANGED
@@ -82,20 +82,16 @@ CATEGORIES = {
82
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 3. DATABASE FUNCTIONS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
83
  def init_db():
84
  """Initialize both JSON and SQLite databases"""
85
- # Log database paths for debugging
86
- logger.info(f"JSON DB path: {DB_FILE}")
87
- logger.info(f"SQLite DB path: {SQLITE_DB}")
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
- except Exception as e:
96
- logger.error(f"Error creating JSON file: {e}")
97
 
98
- # Initialize SQLite database
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
- # If we have data in JSON but not in SQLite (first run with new SQLite DB),
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
- add_url_to_sqlite(url)
122
- new_urls += 1
123
- logger.info(f"Migrated {new_urls} new URLs from JSON to SQLite")
 
 
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%%</div>
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
- # Instead of writing to a file, we'll serve the HTML directly
 
 
 
1105
  categories_json = json.dumps(list(CATEGORIES.keys()))
1106
- return HTML_TEMPLATE % categories_json
 
 
 
 
 
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
- app.run(host='0.0.0.0', port=7860)
 
 
 
 
 
 
 
 
 
 
 
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 ์ถ”๊ฐ€๋กœ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ™•์ธ