From 9771c7deb6e8df76784b6f239e27eb6656e360a3 Mon Sep 17 00:00:00 2001 From: Ganesh Hubale Date: Fri, 18 Oct 2024 13:20:17 +0530 Subject: [PATCH] Feature: add url and tag both at a time Signed-off-by: Ganesh Hubale --- readit/cli.py | 99 +++++++++++++++++++--------------------------- readit/database.py | 7 ++-- 2 files changed, 43 insertions(+), 63 deletions(-) diff --git a/readit/cli.py b/readit/cli.py index e3764d7..15f58a7 100644 --- a/readit/cli.py +++ b/readit/cli.py @@ -26,17 +26,17 @@ @click.command() -@click.option("--add", "-a", nargs=0, help="Add URLs with space-separated") -@click.option("--tag", "-t", nargs=2, help="Add Tag with space-separated URL") -@click.option("--delete", "-d", nargs=1, help="Remove a URL of particular ID") -@click.option("--clear", "-c", multiple=True, nargs=0, help="Clear bookmarks") -@click.option("--update", "-u", nargs=2, help="Update a URL for specific ID") -@click.option("--search", "-s", nargs=1, help="Search for bookmarks using either a tag or a substring of the URL") -@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks") -@click.option("--openurl", "-o", nargs=1, help="Open a URL in your browser by entering a part of the URL.") -@click.option("--version", "-V", is_flag=True, help="Check latest version") -@click.option("--export", "-e", multiple=True, nargs=0, help="Export URLs in csv file") -@click.option("--taglist", "-tl", multiple=True, nargs=0, help="Show all Tags") +@click.option("--add", "-a", help="Add urls --> readit -a ") +@click.option("--tag", "-t", help="Use to tag url --> readit -a -t ") +@click.option("--delete", "-d", help="Remove a URL of particular ID --> readit -d ") +@click.option("--clear", "-c", help="Clear bookmarks --> readit -c") +@click.option("--update", "-u", help="Update a URL for specific ID --> readit -u ") +@click.option("--search", "-s", help="Search for bookmarks using either a tag or a substring of the URL --> readit -s or ") +@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks --> readit -v") +@click.option("--openurl", "-o", help="Open a URL in your browser by entering a part of the URL. --> readit -o ") +@click.option("--version", "-V", is_flag=True, help="Check latest version --> readit readit -V") +@click.option("--export", "-e", multiple=True, nargs=0, help="Export URLs in csv file --> readit -e") +@click.option("--taglist", "-tl", multiple=True, nargs=0, help="Show all Tags --> readit -tl") @click.argument("insert", nargs=-1, required=False) def main( insert, @@ -55,27 +55,33 @@ def main( """ Readit - Command-line bookmark manager tool. """ - if add: - for url in add: - try: - validate_code = check_url_validation(url) - - if validate_code == 200: - is_url_added = database_connection.add_url(url) - if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") - else: - print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow - if option_yes_no(): - is_url_added = database_connection.add_url(url) - if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") - except Exception: + if add or tag: + # readit -a https://github.com/pythonpune/readit -t project + url = add + if not tag: + tag = "general" # Default tag if none provided + if not url: + print("\033[91m\nError: URL not provided. Please use the following format: readit -a -t \033[0m") + sys.exit(0) + try: + validate_code = check_url_validation(url) + + if validate_code == 200: + is_url_added = database_connection.tag_url(url, tag) + if is_url_added: + print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m") + else: print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow if option_yes_no(): - is_url_added = database_connection.add_url(url) + is_url_added = database_connection.tag_url(url, tag) if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") + print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m") + except Exception: + print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow + if option_yes_no(): + is_url_added = database_connection.tag_url(url, tag) + if is_url_added: + print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m") elif delete: database_connection.delete_url(delete) elif update: @@ -98,7 +104,7 @@ def main( if option_yes_no(): is_url_updated = database_connection.update_url(url_id, url) if is_url_updated: - print(f"\033[92m\nSuccess: URL of ID {url_id} updated.\033[0m") + print(f"\033[92m\nSuccess! URL of ID {url_id} updated.\033[0m") elif view: output.print_bookmarks(database_connection.show_urls()) elif openurl: @@ -107,30 +113,6 @@ def main( output.print_bookmarks(database_connection.search_url(search)) elif clear: database_connection.delete_all_url() - elif tag: - tag_list = [] - for tag_to_url in tag: - tag_list.append(tag_to_url) - tag_name = tag_list[0] - tagged_url = tag_list[1] - try: - validate_code = check_url_validation(tagged_url) - if validate_code == 200: - is_url_tagged = database_connection.tag_url(tag_name, tagged_url) - if is_url_tagged: - print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m") - else: - print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow - if option_yes_no(): - is_url_tagged = database_connection.tag_url(tag_name, tagged_url) - if is_url_tagged: - print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m") - except Exception: - print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow - if option_yes_no(): - is_url_tagged = database_connection.tag_url(tag_name, tagged_url) - if is_url_tagged: - print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m") elif taglist: output.print_all_tags(database_connection.list_all_tags()) elif version: @@ -138,7 +120,7 @@ def main( elif export: path = database_connection.export_urls() if path: - print(f"\033[92m\nSuccess: Exported bookmarks available at `{path}`.\033[0m") + print(f"\033[92m\nSuccess! Exported bookmarks available at `{path}`.\033[0m") else: print("\033[91m\nError: Bookmarks are not exported in csv file.\033[0m") else: @@ -149,20 +131,19 @@ def main( if validate_code == 200: is_url_added = database_connection.add_url(url) if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") + print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m") else: print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow if option_yes_no(): is_url_added = database_connection.add_url(url) if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") - + print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m") except Exception: print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow if option_yes_no(): is_url_added = database_connection.add_url(url) if is_url_added: - print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m") + print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m") def option_yes_no(): """ diff --git a/readit/database.py b/readit/database.py index 219ee4c..752e8a9 100644 --- a/readit/database.py +++ b/readit/database.py @@ -98,7 +98,6 @@ def add_url(self, url): If the URL already exists, provide a user-friendly error message. """ try: - self.url = url global date start = datetime.datetime.now() time = start.strftime("%H:%M:%S") @@ -107,14 +106,14 @@ def add_url(self, url): """ INSERT INTO bookmarks(url, date, time) VALUES (?, ?, ?) """, - (self.url, date, time), + (url, date, time), ) self.db.commit() return True except sqlite3.IntegrityError as e: if 'UNIQUE constraint failed' in str(e): - print(f"\033[91m\nError: The URL '{self.url}' already bookmarked.\033[0m") + print(f"\033[91m\nError: The URL '{url}' already bookmarked.\033[0m") return False else: print(f"\nDatabase error: {str(e)}") @@ -124,7 +123,7 @@ def add_url(self, url): print(f"\nAn unexpected error occurred: {str(e)}") return False - def tag_url(self, tag_name, tagged_url): + def tag_url(self, tagged_url, tag_name): """ URLs can be tagged with multiple tags. If the URL already exists, associate it with the new tag. """