#!/usr/bin/env python3 from watchnews import data, web, fetch, rss 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') parser.add_argument('--rss', action='store_true', help='Dump a RSS feeds') 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() if args.rss: print(rss.rss())