Skip to content

Commit cee6ec9

Browse files
authored
User can able to open multiple urls in browser (#146)
* working on urls webbrowser * This PR inlcudes: - Added two new method to check valid id and tag - User can able to open one url at a time using url id - User can able to open multiple urls using tag name - User can be able to search urls with substring - User can be able to open urls with substring
1 parent cf40ed0 commit cee6ec9

File tree

3 files changed

+78
-130
lines changed

3 files changed

+78
-130
lines changed

README.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Features
2222
********
2323
* Bookmark multiple URLs at a time
2424
* Bookmark URL with respective Tags
25-
* Search and display Bookmarks by TAG
25+
* Search and display Bookmarks by TAG and URL's substring
2626
* Display all Bookmarks in table format
2727
* Remove a Bookmarked URL
2828
* Remove all Bookmarked URLs
@@ -68,9 +68,9 @@ Command line options
6868
-d, --delete TEXT Remove a URL of particular ID
6969
-c, --clear TEXT... Clear bookmarks
7070
-u, --update TEXT... Update a URL for specific ID
71-
-s, --search TEXT Search all bookmarks by Tag
71+
-s, --search TEXT Search all bookmarks by Tag and URL's substring
7272
-v, --view TEXT... Show bookmarks
73-
-o, --openurl TEXT Open URL in Browser
73+
-o, --openurl TEXT Open URL in Browser using id, tag or URL's substring
7474
-V, --version Check latest version
7575
-e, --export TEXT... Export URLs in csv file
7676
-tl, --taglist TEXT... Show all Tags
@@ -129,21 +129,27 @@ Examples
129129
or
130130
$ readit --tag tag_name url
131131
132-
7. **Search** and **Display** all bookmarks using the TAG:
132+
7. **Search** and **Display** all bookmarks using the TAG or URL's substring:
133133

134134
.. code-block:: bash
135135
136136
$ readit -s tag_name
137137
or
138138
$ readit --search tag_name
139+
or
140+
$ readit -s url_substring
139141
140-
8. Open URL in the Browser using specific ID:
142+
8. Open URL in the Browser using specific ID, TAG or URL's substring:
141143

142144
.. code-block:: bash
143145
144146
$ readit -o urlid
145147
or
146148
$ readit --openurl urlid
149+
or
150+
$ readit -o url_substring
151+
or
152+
$ readit -o tag
147153
148154
9. **Export** bookmarks into the CSV file:
149155

readit/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def main(
138138
elif openurl:
139139
database_connection.open_url(openurl)
140140
elif search:
141-
output.print_bookmarks(database_connection.search_by_tag(search))
141+
output.print_bookmarks(database_connection.search_url(search))
142142
elif clear:
143143
is_db_clear = database_connection.delete_all_url()
144144
if is_db_clear:

readit/database.py

Lines changed: 66 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ def init_db(self, cursor, db):
5656
creates or opens file mydatabase with sqlite3 DataBase.
5757
get cursor object.
5858
create table.
59-
60-
Attributes
61-
----------
62-
db : sqlite database connection.
63-
cursor : sqlite database cursor.
6459
"""
6560

6661
try:
@@ -87,17 +82,6 @@ def init_db(self, cursor, db):
8782
def add_url(self, url):
8883
"""
8984
URL will be adding to database.
90-
91-
92-
Parameters
93-
----------
94-
url : str
95-
A URL to add in database.
96-
97-
Returns
98-
-------
99-
str
100-
A URL inserted in database.
10185
"""
10286

10387
try:
@@ -119,19 +103,6 @@ def add_url(self, url):
119103
def tag_url(self, tag_name, tagged_url):
120104
"""
121105
URLs can be added by respective Tags.
122-
123-
Parameters
124-
----------
125-
tag_name : str
126-
A comma separated tag to URL.
127-
128-
tagged_url : str
129-
A URL to be tagged.
130-
131-
Returns
132-
-------
133-
str
134-
A URL inserted with tag.
135106
"""
136107
self.tag = tag_name
137108
self.url = tagged_url
@@ -152,11 +123,6 @@ def tag_url(self, tag_name, tagged_url):
152123
def list_all_tags(self):
153124
"""
154125
Shows list of all available Tags in database.
155-
156-
Returns
157-
-------
158-
list
159-
It returns all available Tags associated with URLs in database.
160126
"""
161127
tag_list = set()
162128
try:
@@ -175,16 +141,6 @@ def list_all_tags(self):
175141
def delete_url(self, url_id):
176142
"""
177143
URLs can deleted as per id number provided.
178-
179-
Parameters
180-
----------
181-
urlid : int
182-
id of url present in database.
183-
184-
Returns
185-
-------
186-
str
187-
A URL deleted from database.
188144
"""
189145
try:
190146
self.url_id = url_id
@@ -206,19 +162,6 @@ def delete_url(self, url_id):
206162
def update_url(self, url_id, url):
207163
"""
208164
URLs can be updated with respect to id.
209-
210-
Parameters
211-
----------
212-
url_id : int
213-
id of url which present in database.
214-
215-
url : str
216-
A URL to update.
217-
218-
Returns
219-
-------
220-
str
221-
A URL updated in database.
222165
"""
223166

224167
try:
@@ -240,11 +183,6 @@ def update_url(self, url_id, url):
240183
def show_url(self):
241184
"""
242185
All URLs from database displayed to user on screen.
243-
244-
Returns
245-
-------
246-
list
247-
A table representing all bookmarks.
248186
"""
249187
try:
250188
self.cursor.execute(""" SELECT id, url, tags, date, time FROM bookmarks """)
@@ -257,29 +195,28 @@ def show_url(self):
257195
except Exception as e4:
258196
return None
259197

260-
def search_by_tag(self, tag):
198+
def search_url(self, search_value):
261199
"""
262-
Group of URLs displayed with respect to Tag.
263-
264-
Parameters
265-
----------
266-
tag : str
267-
tag to search URLs.
268-
269-
Returns
270-
-------
271-
list
272-
A table containing bookmarks.
200+
Group of URLs displayed with respect to search_value.
273201
274202
"""
275203
try:
276-
self.tag = tag
277-
self.cursor.execute(
278-
""" SELECT id, url, tags, date, time
279-
FROM bookmarks WHERE tags=?""",
280-
(self.tag,),
281-
)
282-
all_bookmarks = self.cursor.fetchall()
204+
self.search = search_value
205+
all_bookmarks = []
206+
if self.check_tag(search_value):
207+
self.cursor.execute(
208+
""" SELECT id, url, tags, date, time
209+
FROM bookmarks WHERE tags=?""",
210+
(self.search,),
211+
)
212+
all_bookmarks = self.cursor.fetchall()
213+
214+
else:
215+
self.cursor.execute(""" SELECT * FROM bookmarks""")
216+
bookmarks = self.cursor.fetchall()
217+
for bookmark in bookmarks:
218+
if search_value.lower() in bookmark[1]:
219+
all_bookmarks.append(bookmark)
283220
self.db.commit
284221
if all_bookmarks == []:
285222
return None
@@ -291,11 +228,6 @@ def search_by_tag(self, tag):
291228
def delete_all_url(self):
292229
"""
293230
All URLs from database will be deleted.
294-
295-
Returns
296-
-------
297-
null
298-
All URLs will be removed from database.
299231
"""
300232
try:
301233
if self.check_url_db():
@@ -311,11 +243,6 @@ def delete_all_url(self):
311243
def check_url_db(self):
312244
"""
313245
Checks Whether URL is present in database or not.
314-
315-
Returns
316-
-------
317-
bool
318-
It returns TRUE if URL is present in database else False.
319246
"""
320247
self.cursor.execute(""" SELECT id, url, tags, date, time FROM bookmarks """)
321248
all_bookmarks = self.cursor.fetchall()
@@ -324,41 +251,66 @@ def check_url_db(self):
324251
else:
325252
return False
326253

327-
def open_url(self, urlid):
254+
def open_url(self, url_id_tag):
328255
"""
329-
Opens the URL in default browser.
330-
331-
Parameters
332-
----------
333-
urlid:
334-
id of url present in database.
256+
Opens the URLs in default browser.
257+
"""
258+
try:
259+
if self.check_id(url_id_tag):
260+
all_row = self.check_id(url_id_tag)
261+
elif self.search_url(url_id_tag):
262+
all_rows = self.search_url(url_id_tag)
263+
for bookmark in all_rows:
264+
webbrowser.open(bookmark[1])
265+
self.db.commit()
266+
return True
267+
else:
268+
print(
269+
"Provide either valid url id or url tag name or any valid substring."
270+
)
271+
272+
if all_row:
273+
for i in range(len(all_row)):
274+
for url in all_row[i]:
275+
webbrowser.open_new_tab(url)
276+
self.db.commit()
277+
return True
278+
except Exception as i:
279+
return False
335280

336-
Returns
337-
-------
338-
str
339-
A URL opened in default browser.
281+
def check_tag(self, url_tag):
282+
"""
283+
Checks this tag is available in database.
340284
"""
341285
try:
342-
self.urlid = urlid
343286
self.cursor.execute(
344-
""" SELECT url FROM bookmarks WHERE id=?""", (self.urlid,)
287+
""" SELECT url FROM bookmarks WHERE tags=?""", (url_tag,)
345288
)
346-
all_row = self.cursor.fetchone()
347-
for url in all_row:
348-
webbrowser.open_new(url)
289+
all_row = self.cursor.fetchall()
349290
self.db.commit()
350-
return True
291+
if all_row == []:
292+
return None
293+
return all_row
351294
except Exception as i:
352-
return False
295+
return None
296+
297+
def check_id(self, url_id):
298+
"""
299+
Check this is available in database.
300+
"""
301+
try:
302+
self.cursor.execute(""" SELECT url FROM bookmarks WHERE id=?""", (url_id,))
303+
all_row = self.cursor.fetchall()
304+
if all_row == []:
305+
return None
306+
self.db.commit()
307+
return all_row
308+
except Exception as i:
309+
return None
353310

354311
def export_urls(self):
355312
"""
356313
Exporting urls to csv file from database.
357-
358-
Returns
359-
-------
360-
csv file
361-
A file containing exported bookmarks records.
362314
"""
363315
try:
364316
config_path = os.path.expanduser("~/.config/readit")
@@ -385,16 +337,6 @@ def export_urls(self):
385337
def url_info(self, url):
386338
"""
387339
Display the information regarding already present URL in database.
388-
389-
Parameters
390-
----------
391-
url : str
392-
url to search it's information from database.
393-
394-
Returns
395-
-------
396-
str
397-
All the information about particular URL.
398340
"""
399341
try:
400342
self.url_exist = url

0 commit comments

Comments
 (0)