Skip to content

Commit 9771c7d

Browse files
committed
Feature: add url and tag both at a time
Signed-off-by: Ganesh Hubale <[email protected]>
1 parent 627b9ad commit 9771c7d

File tree

2 files changed

+43
-63
lines changed

2 files changed

+43
-63
lines changed

readit/cli.py

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626

2727

2828
@click.command()
29-
@click.option("--add", "-a", nargs=0, help="Add URLs with space-separated")
30-
@click.option("--tag", "-t", nargs=2, help="Add Tag with space-separated URL")
31-
@click.option("--delete", "-d", nargs=1, help="Remove a URL of particular ID")
32-
@click.option("--clear", "-c", multiple=True, nargs=0, help="Clear bookmarks")
33-
@click.option("--update", "-u", nargs=2, help="Update a URL for specific ID")
34-
@click.option("--search", "-s", nargs=1, help="Search for bookmarks using either a tag or a substring of the URL")
35-
@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks")
36-
@click.option("--openurl", "-o", nargs=1, help="Open a URL in your browser by entering a part of the URL.")
37-
@click.option("--version", "-V", is_flag=True, help="Check latest version")
38-
@click.option("--export", "-e", multiple=True, nargs=0, help="Export URLs in csv file")
39-
@click.option("--taglist", "-tl", multiple=True, nargs=0, help="Show all Tags")
29+
@click.option("--add", "-a", help="Add urls --> readit -a <url1> <url2>")
30+
@click.option("--tag", "-t", help="Use to tag url --> readit -a <url1> -t <tag1>")
31+
@click.option("--delete", "-d", help="Remove a URL of particular ID --> readit -d <url_id>")
32+
@click.option("--clear", "-c", help="Clear bookmarks --> readit -c")
33+
@click.option("--update", "-u", help="Update a URL for specific ID --> readit -u <existing_id> <new_url>")
34+
@click.option("--search", "-s", help="Search for bookmarks using either a tag or a substring of the URL --> readit -s <tag> or <substring>")
35+
@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks --> readit -v")
36+
@click.option("--openurl", "-o", help="Open a URL in your browser by entering a part of the URL. --> readit -o <url_substring>")
37+
@click.option("--version", "-V", is_flag=True, help="Check latest version --> readit readit -V")
38+
@click.option("--export", "-e", multiple=True, nargs=0, help="Export URLs in csv file --> readit -e")
39+
@click.option("--taglist", "-tl", multiple=True, nargs=0, help="Show all Tags --> readit -tl")
4040
@click.argument("insert", nargs=-1, required=False)
4141
def main(
4242
insert,
@@ -55,27 +55,33 @@ def main(
5555
"""
5656
Readit - Command-line bookmark manager tool.
5757
"""
58-
if add:
59-
for url in add:
60-
try:
61-
validate_code = check_url_validation(url)
62-
63-
if validate_code == 200:
64-
is_url_added = database_connection.add_url(url)
65-
if is_url_added:
66-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
67-
else:
68-
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
69-
if option_yes_no():
70-
is_url_added = database_connection.add_url(url)
71-
if is_url_added:
72-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
73-
except Exception:
58+
if add or tag:
59+
# readit -a https://github.com/pythonpune/readit -t project
60+
url = add
61+
if not tag:
62+
tag = "general" # Default tag if none provided
63+
if not url:
64+
print("\033[91m\nError: URL not provided. Please use the following format: readit -a <url> -t <tag>\033[0m")
65+
sys.exit(0)
66+
try:
67+
validate_code = check_url_validation(url)
68+
69+
if validate_code == 200:
70+
is_url_added = database_connection.tag_url(url, tag)
71+
if is_url_added:
72+
print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m")
73+
else:
7474
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
7575
if option_yes_no():
76-
is_url_added = database_connection.add_url(url)
76+
is_url_added = database_connection.tag_url(url, tag)
7777
if is_url_added:
78-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
78+
print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m")
79+
except Exception:
80+
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
81+
if option_yes_no():
82+
is_url_added = database_connection.tag_url(url, tag)
83+
if is_url_added:
84+
print(f"\033[92m\nSuccess! Bookmarked URL `{url}` with tag `{tag}`. 🎉\033[0m")
7985
elif delete:
8086
database_connection.delete_url(delete)
8187
elif update:
@@ -98,7 +104,7 @@ def main(
98104
if option_yes_no():
99105
is_url_updated = database_connection.update_url(url_id, url)
100106
if is_url_updated:
101-
print(f"\033[92m\nSuccess: URL of ID {url_id} updated.\033[0m")
107+
print(f"\033[92m\nSuccess! URL of ID {url_id} updated.\033[0m")
102108
elif view:
103109
output.print_bookmarks(database_connection.show_urls())
104110
elif openurl:
@@ -107,38 +113,14 @@ def main(
107113
output.print_bookmarks(database_connection.search_url(search))
108114
elif clear:
109115
database_connection.delete_all_url()
110-
elif tag:
111-
tag_list = []
112-
for tag_to_url in tag:
113-
tag_list.append(tag_to_url)
114-
tag_name = tag_list[0]
115-
tagged_url = tag_list[1]
116-
try:
117-
validate_code = check_url_validation(tagged_url)
118-
if validate_code == 200:
119-
is_url_tagged = database_connection.tag_url(tag_name, tagged_url)
120-
if is_url_tagged:
121-
print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m")
122-
else:
123-
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
124-
if option_yes_no():
125-
is_url_tagged = database_connection.tag_url(tag_name, tagged_url)
126-
if is_url_tagged:
127-
print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m")
128-
except Exception:
129-
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
130-
if option_yes_no():
131-
is_url_tagged = database_connection.tag_url(tag_name, tagged_url)
132-
if is_url_tagged:
133-
print(f"\033[92m\nSuccess: Bookmarked URL `{tagged_url}` with tag `{tag_name}`.\033[0m")
134116
elif taglist:
135117
output.print_all_tags(database_connection.list_all_tags())
136118
elif version:
137119
print("\033[92m\nreadit v0.3 \033[0m")
138120
elif export:
139121
path = database_connection.export_urls()
140122
if path:
141-
print(f"\033[92m\nSuccess: Exported bookmarks available at `{path}`.\033[0m")
123+
print(f"\033[92m\nSuccess! Exported bookmarks available at `{path}`.\033[0m")
142124
else:
143125
print("\033[91m\nError: Bookmarks are not exported in csv file.\033[0m")
144126
else:
@@ -149,20 +131,19 @@ def main(
149131
if validate_code == 200:
150132
is_url_added = database_connection.add_url(url)
151133
if is_url_added:
152-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
134+
print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m")
153135
else:
154136
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
155137
if option_yes_no():
156138
is_url_added = database_connection.add_url(url)
157139
if is_url_added:
158-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
159-
140+
print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m")
160141
except Exception:
161142
print("\033[93m\nWarning: The URL seems to be inaccessible at the moment.\033[0m") # Warning in yellow
162143
if option_yes_no():
163144
is_url_added = database_connection.add_url(url)
164145
if is_url_added:
165-
print(f"\033[92m\nSuccess: The URL '{url}' has been added to the database.\033[0m")
146+
print(f"\033[92mSuccess! The URL '{url}' has been successfully bookmarked. 🎉\033[0m")
166147

167148
def option_yes_no():
168149
"""

readit/database.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def add_url(self, url):
9898
If the URL already exists, provide a user-friendly error message.
9999
"""
100100
try:
101-
self.url = url
102101
global date
103102
start = datetime.datetime.now()
104103
time = start.strftime("%H:%M:%S")
@@ -107,14 +106,14 @@ def add_url(self, url):
107106
"""
108107
INSERT INTO bookmarks(url, date, time) VALUES (?, ?, ?)
109108
""",
110-
(self.url, date, time),
109+
(url, date, time),
111110
)
112111
self.db.commit()
113112
return True
114113

115114
except sqlite3.IntegrityError as e:
116115
if 'UNIQUE constraint failed' in str(e):
117-
print(f"\033[91m\nError: The URL '{self.url}' already bookmarked.\033[0m")
116+
print(f"\033[91m\nError: The URL '{url}' already bookmarked.\033[0m")
118117
return False
119118
else:
120119
print(f"\nDatabase error: {str(e)}")
@@ -124,7 +123,7 @@ def add_url(self, url):
124123
print(f"\nAn unexpected error occurred: {str(e)}")
125124
return False
126125

127-
def tag_url(self, tag_name, tagged_url):
126+
def tag_url(self, tagged_url, tag_name):
128127
"""
129128
URLs can be tagged with multiple tags. If the URL already exists, associate it with the new tag.
130129
"""

0 commit comments

Comments
 (0)