|
18 | 18 | import os |
19 | 19 | import sqlite3 |
20 | 20 | import sys |
| 21 | +import tkinter as tk |
21 | 22 | import webbrowser |
22 | 23 | from glob import glob |
| 24 | +from tkinter import filedialog |
23 | 25 |
|
24 | 26 | date = datetime.date.today() |
25 | 27 |
|
@@ -329,31 +331,75 @@ def check_id(self, url_id): |
329 | 331 | except Exception: |
330 | 332 | raise sqlite3.OperationalError |
331 | 333 |
|
| 334 | + @staticmethod |
| 335 | + def select_directory(): |
| 336 | + # Function to open a directory dialog and select a folder |
| 337 | + # Create the root window |
| 338 | + root = tk.Tk() |
| 339 | + root.withdraw() # Hide the root window |
| 340 | + |
| 341 | + # Open directory dialog |
| 342 | + folder_path = filedialog.askdirectory(title="Select a folder") |
| 343 | + |
| 344 | + if folder_path: |
| 345 | + print(f"\nSelected folder: {folder_path}") |
| 346 | + else: |
| 347 | + print("\nNo folder selected") |
| 348 | + return folder_path |
| 349 | + |
332 | 350 | def export_urls(self): |
333 | 351 | """ |
334 | | - Exporting urls to csv file from database. |
| 352 | + Export URLs from the database to a CSV file. |
| 353 | + Returns the path of the exported CSV file or a tuple with a failure message. |
335 | 354 | """ |
336 | 355 | try: |
337 | | - config_path = os.path.expanduser("~/.config/readit") |
338 | | - if not os.path.exists(config_path): |
339 | | - msg = "File path does not exist: " + config_path |
| 356 | + folder_path = self.select_directory() |
| 357 | + if not folder_path: |
| 358 | + folder_path = os.path.expanduser("~/.config/readit") |
| 359 | + |
| 360 | + if not os.path.exists(folder_path): |
| 361 | + msg = "File path does not exist: " + folder_path |
340 | 362 | return False, msg |
341 | | - except OSError: |
342 | | - msg = "\nError: Finding directory: " + config_path |
| 363 | + |
| 364 | + except OSError as e: |
| 365 | + msg = f"Error: Finding directory: {str(e)}" |
343 | 366 | return False, msg |
344 | | - databasefile = os.path.join(config_path, "bookmarks.db") |
| 367 | + |
| 368 | + database_file = os.path.join(os.path.expanduser("~/.config/readit"), "bookmarks.db") |
345 | 369 | try: |
346 | | - self.conn = sqlite3.connect(glob(os.path.expanduser(databasefile))[0]) |
347 | | - self.cursor = self.conn.cursor() |
348 | | - self.cursor.execute("select * from bookmarks") |
349 | | - with open("exported_bookmarks.csv", "w", newline="") as csv_file: |
350 | | - csv_writer = csv.writer(csv_file, delimiter="\t") |
351 | | - csv_writer.writerow([i[0] for i in self.cursor.description]) |
352 | | - csv_writer.writerows(self.cursor) |
353 | | - dirpath = os.getcwd() + "/exported_bookmarks.csv" |
354 | | - return dirpath |
355 | | - except Exception: |
356 | | - raise sqlite3.OperationalError |
| 370 | + # Ensure the database file exists |
| 371 | + db_file_paths = glob(os.path.expanduser(database_file)) |
| 372 | + if not db_file_paths: |
| 373 | + return False, f"Error: Database file not found at: {database_file}" |
| 374 | + |
| 375 | + # Open the database connection and export to CSV |
| 376 | + with sqlite3.connect(db_file_paths[0]) as conn: |
| 377 | + cursor = conn.cursor() |
| 378 | + cursor.execute("SELECT * FROM bookmarks") |
| 379 | + |
| 380 | + # Export to CSV |
| 381 | + csv_file_path = os.path.join(folder_path, "exported_bookmarks.csv") |
| 382 | + with open(csv_file_path, "w", newline="") as csv_file: |
| 383 | + csv_writer = csv.writer(csv_file, delimiter="\t") |
| 384 | + csv_writer.writerow([i[0] for i in cursor.description]) # Headers |
| 385 | + csv_writer.writerows(cursor) # Data rows |
| 386 | + |
| 387 | + return csv_file_path |
| 388 | + |
| 389 | + except sqlite3.OperationalError as e: |
| 390 | + msg = f"Database operation failed: {str(e)}" |
| 391 | + return False, msg |
| 392 | + |
| 393 | + except Exception as e: |
| 394 | + msg = f"Unexpected error: {str(e)}" |
| 395 | + return False, msg |
| 396 | + |
| 397 | + exporter = BookmarkExporter() |
| 398 | + result = exporter.export_urls() |
| 399 | + if isinstance(result, tuple) and result[0] is False: |
| 400 | + print(result[1]) # Print error message |
| 401 | + else: |
| 402 | + print(f"Bookmarks exported successfully to: {result}") |
357 | 403 |
|
358 | 404 | def url_info(self, url): |
359 | 405 | """ |
|
0 commit comments