1 # Copyright (C) 2012-2013 W. Trevor King <wking@tremily.us>
3 # This file is part of rss2email.
5 # rss2email is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 2 of the License, or (at your option) version 3 of
10 # rss2email is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # rss2email. If not, see <http://www.gnu.org/licenses/>.
21 import xml.dom.minidom as _minidom
22 import xml.sax.saxutils as _saxutils
24 from . import LOG as _LOG
25 from . import error as _error
29 "Create a new feed database."
31 _LOG.info('set the default target email to {}'.format(args.email))
32 feeds.config['DEFAULT']['to'] = args.email
35 def email(feeds, args):
36 "Update the default target email address"
38 _LOG.info('unset the default target email')
40 _LOG.info('set the default target email to {}'.format(args.email))
41 feeds.config['DEFAULT']['to'] = args.email
45 "Add a new feed to the database"
46 feed = feeds.new_feed(name=args.name, url=args.url, to=args.email)
47 _LOG.info('add new feed {}'.format(feed))
49 raise _error.NoToEmailAddress(feeds=feeds)
53 "Fetch feeds and send entry emails."
55 args.index = range(len(feeds))
57 for index in args.index:
58 feed = feeds.index(index)
61 feed.run(send=args.send)
62 except _error.RSS2EmailError as e:
67 def list(feeds, args):
68 "List all the feeds in the database"
69 for i,feed in enumerate(feeds):
74 print('{}: [{}] {}'.format(i, active_char, feed))
76 def _set_active(feeds, args, active=True):
77 "Shared by `pause` and `unpause`."
83 args.index = range(len(feeds))
84 for index in args.index:
85 feed = feeds.index(index)
86 _LOG.info('{} feed {}'.format(action, feed))
90 def pause(feeds, args):
91 "Pause a feed (disable fetching)"
92 _set_active(feeds=feeds, args=args, active=False)
94 def unpause(feeds, args):
95 "Unpause a feed (enable fetching)"
96 _set_active(feeds=feeds, args=args, active=True)
98 def delete(feeds, args):
99 "Remove a feed from the database"
101 for index in args.index:
102 feed = feeds.index(index)
103 to_remove.append(feed)
104 for feed in to_remove:
105 _LOG.info('deleting feed {}'.format(feed))
109 def reset(feeds, args):
110 "Forget dynamic feed data (e.g. to re-send old entries)"
112 args.index = range(len(feeds))
113 for index in args.index:
114 feed = feeds.index(index)
115 _LOG.info('resetting feed {}'.format(feed))
119 def opmlimport(feeds, args):
120 "Import configuration from OPML."
122 _LOG.info('importing feeds from {}'.format(args.file))
123 f = open(args.file, 'rb')
125 _LOG.info('importing feeds from stdin')
128 dom = _minidom.parse(f)
129 new_feeds = dom.getElementsByTagName('outline')
130 except Exception as e:
131 raise _error.OPMLReadError() from e
134 for feed in new_feeds:
135 if feed.hasAttribute('xmlUrl'):
136 url = _saxutils.unescape(feed.getAttribute('xmlUrl'))
137 feed = feeds.new_feed(url=url)
138 _LOG.info('add new feed {}'.format(feed))
141 def opmlexport(feeds, args):
142 "Export configuration to OPML."
144 _LOG.info('exporting feeds to {}'.format(args.file))
145 f = open(args.file, 'rb')
147 _LOG.info('exporting feeds to stdout')
150 '<?xml version="1.0" encoding="UTF-8"?>\n'
151 '<opml version="1.0">\n'
153 '<title>rss2email OPML export</title>\n'
157 url = _saxutils.escape(feed.url)
158 f.write('<outline type="rss" text="{0}" xmlUrl="{0}"/>'.format(url))