Skip to content

Commit 14b893e

Browse files
authored
Merge pull request #164 from pythonpune/check-9-oct
Feature: User can select the path for exported bookmarked urls file
2 parents 34e567b + d2ac02e commit 14b893e

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

readit/database.py

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import os
1919
import sqlite3
2020
import sys
21+
import tkinter as tk
2122
import webbrowser
2223
from glob import glob
24+
from tkinter import filedialog
2325

2426
date = datetime.date.today()
2527

@@ -329,31 +331,75 @@ def check_id(self, url_id):
329331
except Exception:
330332
raise sqlite3.OperationalError
331333

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+
332350
def export_urls(self):
333351
"""
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.
335354
"""
336355
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
340362
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)}"
343366
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")
345369
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}")
357403

358404
def url_info(self, url):
359405
"""

0 commit comments

Comments
 (0)