@@ -56,50 +56,49 @@ def main(
5656 Readit - Command-line bookmark manager tool.
5757 """
5858 if add :
59- for url_to_add in add :
60- url = url_to_add .strip () # Strip any leading/trailing whitespace
59+ for url in add :
6160 try :
62- validate_url = requests . get (url )
63- validate_code = validate_url . status_code
61+ validate_code = check_url_validation (url )
62+
6463 if validate_code == 200 :
6564 is_url_added = database_connection .add_url (url )
6665 if is_url_added :
67- print (f"\n Success: The URL '{ url } ' has been added to the database." )
66+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
6867 else :
69- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
68+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
7069 if option_yes_no ():
7170 is_url_added = database_connection .add_url (url )
7271 if is_url_added :
73- print (f"\n Success: The URL '{ url } ' has been added to the database." )
72+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
7473 except Exception :
75- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
74+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
7675 if option_yes_no ():
7776 is_url_added = database_connection .add_url (url )
7877 if is_url_added :
79- print (f"\n Success: The URL '{ url } ' has been added to the database." )
78+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
8079 elif delete :
8180 database_connection .delete_url (delete )
8281 elif update :
8382 url_list = []
8483 for update_to_url in update :
8584 url_list .append (update_to_url )
8685 url_id = url_list [0 ]
87- url = url_list [1 ]. strip () # Strip any leading/trailing whitespace
86+ url = url_list [1 ]
8887 try :
89- validate_url = requests . get (url )
90- validate_code = validate_url . status_code
88+ validate_code = check_url_validation (url )
89+
9190 if validate_code == 200 :
9291 database_connection .update_url (url_id , url )
9392 else :
94- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
93+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
9594 if option_yes_no ():
9695 database_connection .update_url (url_id , url )
9796 except Exception :
98- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
97+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
9998 if option_yes_no ():
10099 is_url_updated = database_connection .update_url (url_id , url )
101100 if is_url_updated :
102- print (f"\n Success: URL of ID { url_id } updated." )
101+ print (f"\033 [92m \ n Success: URL of ID { url_id } updated.\033 [0m " )
103102 elif view :
104103 output .print_bookmarks (database_connection .show_urls ())
105104 elif openurl :
@@ -113,66 +112,90 @@ def main(
113112 for tag_to_url in tag :
114113 tag_list .append (tag_to_url )
115114 tag_name = tag_list [0 ]
116- tagged_url = tag_list [1 ]. strip () # Strip any leading/trailing whitespace
115+ tagged_url = tag_list [1 ]
117116 try :
118- validate_url = requests .get (tagged_url )
119- validate_code = validate_url .status_code
117+ validate_code = check_url_validation (tagged_url )
120118 if validate_code == 200 :
121119 is_url_tagged = database_connection .tag_url (tag_name , tagged_url )
122120 if is_url_tagged :
123- print (f"\n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `." )
121+ print (f"\033 [92m \ n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `.\033 [0m " )
124122 else :
125- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
123+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
126124 if option_yes_no ():
127125 is_url_tagged = database_connection .tag_url (tag_name , tagged_url )
128126 if is_url_tagged :
129- print (f"\n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `." )
127+ print (f"\033 [92m \ n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `.\033 [0m " )
130128 except Exception :
131- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
129+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
132130 if option_yes_no ():
133131 is_url_tagged = database_connection .tag_url (tag_name , tagged_url )
134132 if is_url_tagged :
135- print (f"\n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `." )
133+ print (f"\033 [92m \ n Success: Bookmarked URL `{ tagged_url } ` with tag `{ tag_name } `.\033 [0m " )
136134 elif taglist :
137135 output .print_all_tags (database_connection .list_all_tags ())
138136 elif version :
139- print ("readit v0.3" )
137+ print ("\033 [92m \n readit v0.3 \033 [0m " )
140138 elif export :
141139 path = database_connection .export_urls ()
142140 if path :
143- print (f"\n Exported bookmarks available at `{ path } `" )
141+ print (f"\033 [92m \n Success: Exported bookmarks available at `{ path } `. \033 [0m " )
144142 else :
145- print ("\n Error: Bookmarks are not exported in csv file." )
143+ print ("\033 [91m \ n Error: Bookmarks are not exported in csv file.\033 [0m " )
146144 else :
147- for url_to_add in insert :
148- url = url_to_add .strip () # Strip any leading/trailing whitespace
145+ for url in insert :
149146 try :
150- validate_url = requests . get (url )
151- validate_code = validate_url . status_code
147+ validate_code = check_url_validation (url )
148+
152149 if validate_code == 200 :
153150 is_url_added = database_connection .add_url (url )
154151 if is_url_added :
155- print (f"\n Success: The URL '{ url } ' has been added to the database." )
152+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
156153 else :
157- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
154+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
158155 if option_yes_no ():
159156 is_url_added = database_connection .add_url (url )
160157 if is_url_added :
161- print (f"\n Success: The URL '{ url } ' has been added to the database." )
158+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
162159
163160 except Exception :
164- print ("*" * 12 , " \n Invalid URL \n " , "*" * 11 )
161+ print ("\033 [93m \n Warning: The URL seems to be inaccessible at the moment. \033 [0m" ) # Warning in yellow
165162 if option_yes_no ():
166163 is_url_added = database_connection .add_url (url )
167164 if is_url_added :
168- print (f"\n Success: The URL '{ url } ' has been added to the database." )
165+ print (f"\033 [92m \ n Success: The URL '{ url } ' has been added to the database.\033 [0m " )
169166
170167def option_yes_no ():
171168 """
172169 Asks whether to bookmark invalid URLs or Offline URLs to database.
173170 """
174- option = input ("Still you want to bookmark: Yes/No ? " )
171+ option = input ("\033 [96m \n Are you sure you want to bookmark this URL? ( Yes/No) \033 [0m " )
175172 if option .lower () in ["yes" , "y" ]:
176173 return True
177174 else :
178175 sys .exit (0 )
176+
177+ def check_url_validation (url_given ):
178+ url = url_given .strip () # Strip any leading/trailing whitespace
179+ if not url :
180+ print ("\033 [91m\n Error: Cannot add an empty URL.\033 [0m" )
181+ sys .exit (0 )
182+
183+ # Initialize the validation code
184+ validate_code = 0
185+
186+ if not url .startswith (("http://" , "https://" )):
187+ # First try with http, if it fails, try https
188+ for prefix in ["http://" , "https://" ]:
189+ full_url = prefix + url
190+ response = requests .get (full_url )
191+ if response .status_code == 200 :
192+ validate_code = 200
193+ url = full_url # Update URL with valid prefix
194+ break
195+ else :
196+ validate_code = 0
197+ else :
198+ # If URL starts with http or https, validate directly
199+ response = requests .get (url )
200+ validate_code = response .status_code
201+ return validate_code
0 commit comments