diff options
-rw-r--r-- | requirements.txt | 5 | ||||
-rw-r--r-- | setup.py | 18 | ||||
-rwxr-xr-x | watchnews-cli | 57 | ||||
-rw-r--r-- | watchnews/__init__.py | 1 | ||||
-rw-r--r-- | watchnews/css.py (renamed from css.py) | 0 | ||||
-rw-r--r-- | watchnews/data.py (renamed from data.py) | 13 | ||||
-rwxr-xr-x | watchnews/fetch.py (renamed from watchnews.py) | 56 | ||||
-rw-r--r-- | watchnews/web.py (renamed from web.py) | 8 | ||||
-rw-r--r-- | watchnews/wsgi.py | 11 |
9 files changed, 103 insertions, 66 deletions
diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 35b9fc2..0000000 --- a/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -peewee==2.7.1 -feedparser>=5.1.0 -newspaper3k==0.1.5 -Flask==0.10.1 -ll-xist==5.13 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f34922b --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +from distutils.core import setup + +setup(name='watchnews', + version='0.0.1', + description='RSS Feed watcher tools', + author='Yves Fischer', + author_email='yvesf+watchnews@xapek.org', + url='https://www.xapek.org/git/yvesf/watchnews', + packages=['watchnews'], + scripts=['watchnews-cli'], + depends=['peewee==2.7.1', + 'feedparser>=5.1.0', + 'newspaper3k==0.1.5', + 'Flask==0.10.1', + 'll-xist==5.13'] + ) diff --git a/watchnews-cli b/watchnews-cli new file mode 100755 index 0000000..53f5104 --- /dev/null +++ b/watchnews-cli @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +from watchnews import data, web, fetch + +import os +import argparse +import logging + + +logging.basicConfig(level=logging.INFO) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Process some integers.') + parser.add_argument('--add', metavar='URL', + help='add feed with URL') + parser.add_argument('--update', action='store_true', + help='Update all known feeds') + parser.add_argument('--update-feed', type=int, metavar='ID', + help='Update feed with ID') + parser.add_argument('--remove', type=int, metavar='ID', + help='Remove feed with ID') + parser.add_argument('--list', action='store_true', + help='List all registered feeds') + parser.add_argument('--web', action='store_true', + help='Run web interface') + parser.add_argument("--db", metavar="DBPATH", + default=os.path.join(os.path.dirname(__file__), "db.sqlite"), + help="Path to sqlite database file") + + args = parser.parse_args() + + data.init_sqlite(args.db) + + if args.update: + for feed in data.Feed.select(): + try: + fetch.update(feed) + except Exception as e: + logging.exception("Failed to update %s", feed.url) + if args.update_feed: + feed = data.Feed.get(data.Feed.id == args.update_feed) + if feed: + fetch.update(feed) + if args.add: + feed = data.Feed.create(url=args.add) + print("Added {}".format(feed)) + if args.remove: + feed = data.Feed.get(data.Feed.id == args.remove) + feed.delete_instance() + print("Removed {}".format(feed)) + if args.list: + for feed in data.Feed.select().order_by(data.Feed.created_date): + print("{0.id} - {1} - {0.title} - {0.url}".format( + feed, feed.created_date.strftime('%x %X'))) + if args.web: + web.get_app().run() + diff --git a/watchnews/__init__.py b/watchnews/__init__.py new file mode 100644 index 0000000..1bb8bf6 --- /dev/null +++ b/watchnews/__init__.py @@ -0,0 +1 @@ +# empty diff --git a/css.py b/watchnews/css.py index b32a274..b32a274 100644 --- a/css.py +++ b/watchnews/css.py diff --git a/data.py b/watchnews/data.py index c1d2e82..e4ecc24 100644 --- a/data.py +++ b/watchnews/data.py @@ -1,15 +1,24 @@ +import peewee as p + import os import datetime -import peewee as p import re +db = p.Proxy() + class SqliteDatabase(p.SqliteDatabase): foreign_keys = True def _add_conn_hooks(self, conn): p.SqliteDatabase._add_conn_hooks(self, conn) conn.execute('PRAGMA foreign_keys = ON;').fetchone() -db = p.Proxy() +def init_sqlite(path): + db.initialize(SqliteDatabase(path)) + + db.connect() + db.create_tables(filter(lambda t: not t.table_exists(), + BaseModel.__subclasses__())) + class BaseModel(p.Model): class Meta: diff --git a/watchnews.py b/watchnews/fetch.py index e22b483..5201c1e 100755 --- a/watchnews.py +++ b/watchnews/fetch.py @@ -1,19 +1,13 @@ #!/usr/bin/env python3 -import data -import web +from . import data import feedparser from newspaper import Article from newspaper.outputformatters import OutputFormatter from newspaper.cleaners import DocumentCleaner -import os -import argparse import logging - -logging.basicConfig(level=logging.INFO) - def update(feed): logging.info("Update %s", feed.url) result = feedparser.parse(feed.url) @@ -87,51 +81,3 @@ def update(feed): except Exception as e: logging.exception("Failed to process %s", entry['link']) - -if __name__ == "__main__": - db = data.SqliteDatabase(os.path.join( - os.path.dirname(__file__), "db.sqlite")) - data.db.initialize(db) - - data.db.connect() - data.db.create_tables(filter(lambda t: not t.table_exists(), - data.BaseModel.__subclasses__())) - - parser = argparse.ArgumentParser(description='Process some integers.') - parser.add_argument('--add', metavar='URL', - help='add feed with URL') - parser.add_argument('--update', action='store_true', - help='Update all known feeds') - parser.add_argument('--update-feed', type=int, metavar='ID', - help='Update feed with ID') - parser.add_argument('--remove', type=int, metavar='ID', - help='Remove feed with ID') - parser.add_argument('--list', action='store_true', - help='List all registered feeds') - parser.add_argument('--web', action='store_true', - help='Run web interface') - - args = parser.parse_args() - if args.update: - for feed in data.Feed.select(): - try: - update(feed) - except Exception as e: - logging.exception("Failed to update %s", feed.url) - if args.update_feed: - feed = data.Feed.get(data.Feed.id == args.update_feed) - if feed: - update(feed) - if args.add: - feed = data.Feed.create(url=args.add) - print("Added {}".format(feed)) - if args.remove: - feed = data.Feed.get(data.Feed.id == args.remove) - feed.delete_instance() - print("Removed {}".format(feed)) - if args.list: - for feed in data.Feed.select().order_by(data.Feed.created_date): - print("{0.id} - {1} - {0.title} - {0.url}".format( - feed, feed.created_date.strftime('%x %X'))) - if args.web: - web.run() diff --git a/web.py b/watchnews/web.py index 26e9233..438e649 100644 --- a/web.py +++ b/watchnews/web.py @@ -1,5 +1,4 @@ -import data -import css +from . import data, css from ll.xist import xsc, parse from ll.xist.ns import html @@ -181,7 +180,7 @@ class Template: versions))) -def run(): +def get_app(): app = Flask(__name__) @app.route('/') @@ -204,4 +203,5 @@ def run(): .order_by(data.Version.created_date) return Template.item(item, list(versions)).string("utf-8") - app.run() + return app + diff --git a/watchnews/wsgi.py b/watchnews/wsgi.py new file mode 100644 index 0000000..bd157cb --- /dev/null +++ b/watchnews/wsgi.py @@ -0,0 +1,11 @@ +from watchnews import web, data +import os + + +dbpath = os.environ.get("WATCHNEWS_DBPATH") +if not dbpath: + raise Exception("WATCHNEWS_DBPATH must be set") + +data.init_sqlite(dbpath) +application = web.get_app() + |