Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions termfeed/dbinit.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#!/usr/bin/env python

# This should be exectued once to initialize the db from urls.py

import os
import shelve
from os import path

from termfeed.urls import rss

homedir = path.expanduser('~')

# initiate database datafile
d = shelve.open(path.join(homedir, '.termfeed'))


# dump urls.py into rss_shelf.db
for topic in rss:
links = rss[topic]
d[topic] = [link for link in links]

d.close()
def initialize_database(database_path, rss_data):
try:
with shelve.open(database_path) as db:
for topic, links in rss_data.items():
db[topic] = links
except Exception as e:
print(f"Error while initializing the database: {e}")

def main():

# Determine home directory and database file path
home_dir = os.path.expanduser('~')
database_file_path = os.path.join(home_dir, '.termfeed')

# Initialize database with rss data from 'urls.py'
initialize_database(database_file_path, rss)

if __name__ == "__main__":
main()
123 changes: 60 additions & 63 deletions termfeed/dbop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,93 @@
#-*- coding: utf-8 -*-

"""
database operations.

dbop.py manipulate database add, update, delete
Database operations module (dbop.py) to manipulate a database:
add, update, delete.
"""

import os
import shelve
from os import path
import termfeed.dbinit

homedir = path.expanduser('~')
HOME_DIR = os.path.expanduser('~')
DB_PATH = os.path.join(HOME_DIR, '.termfeed.db')

def rebuild_library():
import termfeed.dbinit
print('created ".termfeed.db" in {}'.format(homedir))
"""Rebuild the library by calling the dbinit module."""
termfeed.dbinit.main()
print(f'Created ".termfeed.db" in {HOME_DIR}')

# instantiate db if it's not created yet
if not path.exists(homedir + '/.termfeed.db'):
# Instantiate db if it's not created yet
if not os.path.exists(DB_PATH):
rebuild_library()


# connect to db
d = shelve.open(path.join(homedir, '.termfeed'), 'w')

def open_db():
"""Open the database and return the connection."""
return shelve.open(DB_PATH, 'w')

def topics():
return d.keys()

"""Get the list of topics in the database."""
with open_db() as db:
return list(db.keys())

def read(topic):
if topic in d.keys():
return d[topic]
else:
return None

"""Read a topic from the database."""
with open_db() as db:
return db.get(topic, None)

def browse_links(topic):
if topic in d.keys():
links = d[topic]
print('{} resources:'.format(topic))
for link in links:
print('\t{}'.format(link))
else:
print('no category named {}'.format(topic))
print_topics()

"""Print the links for a given topic."""
with open_db() as db:
links = db.get(topic)
if links:
print(f'{topic} resources:')
for link in links:
print(f'\t{link}')
else:
print(f'No category named {topic}')
print_topics()

def print_topics():
print('available topics: ')
"""Print available topics."""
print('Available topics: ')
for t in topics():
print('\t{}'.format(t))

print(f'\t{t}')

def add_link(link, topic='General'):

if topic in d.keys():
if link not in d[topic]:
# to add a new url: copy, mutates, store back
temp = d[topic]
temp.append(link)
d[topic] = temp
print('Updated .. {}'.format(topic))
"""Add a link to a topic. If the topic does not exist, create it."""
with open_db() as db:
links = db.get(topic, [])
if link not in links:
links.append(link)
db[topic] = links
print(f'Updated .. {topic}')
else:
print('{} already exists in {}!!'.format(link, topic))
else:
print('Created new category .. {}'.format(topic))
d[topic] = [link]

print(f'{link} already exists in {topic}!!')

def remove_link(link):

done = False
for topic in topics():
if link in d[topic]:
d[topic] = [l for l in d[topic] if l != link]
print('removed: {}\nfrom: {}'.format(link, topic))
done = True

if not done:
print('URL not found: {}'.format(link))

"""Remove a link from all topics."""
with open_db() as db:
for topic in topics():
links = db.get(topic, [])
if link in links:
links.remove(link)
db[topic] = links
print(f'Removed: {link}\nFrom: {topic}')
return
print(f'URL not found: {link}')

def delete_topic(topic):
"""Delete a topic."""
if topic == 'General':
print('Default topic "General" cannot be removed.')
exit()
try:
del d[topic]
print('Removed "{}" from your library.'.format(topic))
except KeyError:
print('"{}" is not in your library!'.format(topic))
exit()
return

with open_db() as db:
if topic in db:
del db[topic]
print(f'Removed "{topic}" from your library.')
else:
print(f'"{topic}" is not in your library!')


# if __name__ == '__main__':
Expand Down
39 changes: 14 additions & 25 deletions termfeed/feed.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""TermFeed 0.0.11

Usage:
feed
feed <rss-url>
feed -b
feed -a <rss-url> [<category>]
feed -d <rss-url>
feed -t [<category>]
feed -D <category>
feed -R
feed (-h | --help)
feed --version

Options:
List feeds from the default category 'General' of your library.
<URL> List feeds from the provided url source.
-b Browse feed by category avaialble in the database file.
-a URL Add new url <rss-url> to database under [<category>] (or 'General' otherwise).
-d URL Delete <rss-url> from the database file.
-t See the stored categories in your library, or list the URLs stored under <category> in your library.
-D TOPIC Remove entire cateogry (and its urls) from your library.
-R Rebuild the library from the url.py
-h --help Show this screen.

"""
TermFeed 0.0.11: Command Line RSS Feed Manager

Commands:
feed - Lists feeds from 'General' category.
feed <rss-url> - Lists feeds from provided RSS URL.
feed -b - Browses feeds by category.
feed -a <url> [cat] - Adds new RSS URL to [cat] (default: 'General').
feed -d <rss-url> - Deletes specified RSS URL from database.
feed -t [cat] - Shows categories or URLs under a category.
feed -D <cat> - Deletes a category and its URLs.
feed -R - Rebuilds library from url.py.
feed (-h | --help) - Displays help screen.
feed --version - Shows TermFeed version.
"""


Expand Down