2016-04-22 13:48:40 +00:00
|
|
|
"""
|
|
|
|
Support for RSS/Atom feed.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/feedreader/
|
|
|
|
"""
|
2016-04-19 15:14:36 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from logging import getLogger
|
2016-05-29 21:33:53 +00:00
|
|
|
from os.path import exists
|
|
|
|
from threading import Lock
|
|
|
|
import pickle
|
2016-04-19 15:14:36 +00:00
|
|
|
import voluptuous as vol
|
2016-05-29 21:33:53 +00:00
|
|
|
|
2016-05-04 01:40:30 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
2016-04-19 15:14:36 +00:00
|
|
|
from homeassistant.helpers.event import track_utc_time_change
|
|
|
|
|
|
|
|
REQUIREMENTS = ['feedparser==5.2.1']
|
|
|
|
_LOGGER = getLogger(__name__)
|
|
|
|
DOMAIN = "feedreader"
|
|
|
|
EVENT_FEEDREADER = "feedreader"
|
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: {
|
|
|
|
'urls': [vol.Url()],
|
|
|
|
}
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2016-05-04 01:40:30 +00:00
|
|
|
MAX_ENTRIES = 20
|
2016-04-19 15:14:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class FeedManager(object):
|
|
|
|
"""Abstraction over feedparser module."""
|
|
|
|
|
2016-05-29 21:33:53 +00:00
|
|
|
def __init__(self, url, hass, storage):
|
2016-04-19 15:14:36 +00:00
|
|
|
"""Initialize the FeedManager object, poll every hour."""
|
|
|
|
self._url = url
|
|
|
|
self._feed = None
|
|
|
|
self._hass = hass
|
2016-04-28 13:01:44 +00:00
|
|
|
self._firstrun = True
|
2016-05-29 21:33:53 +00:00
|
|
|
self._storage = storage
|
|
|
|
self._last_entry_timestamp = None
|
|
|
|
self._has_published_parsed = False
|
2016-05-04 01:40:30 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START,
|
|
|
|
lambda _: self._update())
|
2016-04-19 15:14:36 +00:00
|
|
|
track_utc_time_change(hass, lambda now: self._update(),
|
|
|
|
minute=0, second=0)
|
|
|
|
|
|
|
|
def _log_no_entries(self):
|
|
|
|
"""Send no entries log at debug level."""
|
2016-05-29 21:33:53 +00:00
|
|
|
_LOGGER.debug('No new entries to be published in feed "%s"', self._url)
|
2016-04-19 15:14:36 +00:00
|
|
|
|
|
|
|
def _update(self):
|
2016-05-04 01:40:30 +00:00
|
|
|
"""Update the feed and publish new entries to the event bus."""
|
2016-04-19 15:14:36 +00:00
|
|
|
import feedparser
|
2016-04-28 13:01:44 +00:00
|
|
|
_LOGGER.info('Fetching new data from feed "%s"', self._url)
|
2016-04-19 15:14:36 +00:00
|
|
|
self._feed = feedparser.parse(self._url,
|
|
|
|
etag=None if not self._feed
|
|
|
|
else self._feed.get('etag'),
|
|
|
|
modified=None if not self._feed
|
|
|
|
else self._feed.get('modified'))
|
|
|
|
if not self._feed:
|
2016-05-04 01:40:30 +00:00
|
|
|
_LOGGER.error('Error fetching feed data from "%s"', self._url)
|
2016-04-19 15:14:36 +00:00
|
|
|
else:
|
|
|
|
if self._feed.bozo != 0:
|
2016-05-04 01:40:30 +00:00
|
|
|
_LOGGER.error('Error parsing feed "%s"', self._url)
|
2016-04-19 15:14:36 +00:00
|
|
|
# Using etag and modified, if there's no new data available,
|
|
|
|
# the entries list will be empty
|
|
|
|
elif len(self._feed.entries) > 0:
|
2016-05-04 01:40:30 +00:00
|
|
|
_LOGGER.debug('%s entri(es) available in feed "%s"',
|
2016-04-28 13:01:44 +00:00
|
|
|
len(self._feed.entries),
|
|
|
|
self._url)
|
2016-05-04 01:40:30 +00:00
|
|
|
if len(self._feed.entries) > MAX_ENTRIES:
|
2016-05-29 21:33:53 +00:00
|
|
|
_LOGGER.debug('Processing only the first %s entries '
|
2016-05-04 01:40:30 +00:00
|
|
|
'in feed "%s"', MAX_ENTRIES, self._url)
|
|
|
|
self._feed.entries = self._feed.entries[0:MAX_ENTRIES]
|
2016-04-19 15:14:36 +00:00
|
|
|
self._publish_new_entries()
|
2016-05-29 21:33:53 +00:00
|
|
|
if self._has_published_parsed:
|
|
|
|
self._storage.put_timestamp(self._url,
|
|
|
|
self._last_entry_timestamp)
|
2016-04-19 15:14:36 +00:00
|
|
|
else:
|
|
|
|
self._log_no_entries()
|
2016-04-28 13:01:44 +00:00
|
|
|
_LOGGER.info('Fetch from feed "%s" completed', self._url)
|
|
|
|
|
|
|
|
def _update_and_fire_entry(self, entry):
|
|
|
|
"""Update last_entry_timestamp and fire entry."""
|
|
|
|
# We are lucky, `published_parsed` data available,
|
|
|
|
# let's make use of it to publish only new available
|
|
|
|
# entries since the last run
|
|
|
|
if 'published_parsed' in entry.keys():
|
2016-05-29 21:33:53 +00:00
|
|
|
self._has_published_parsed = True
|
2016-04-28 13:01:44 +00:00
|
|
|
self._last_entry_timestamp = max(entry.published_parsed,
|
|
|
|
self._last_entry_timestamp)
|
|
|
|
else:
|
2016-05-29 21:33:53 +00:00
|
|
|
self._has_published_parsed = False
|
2016-04-28 13:01:44 +00:00
|
|
|
_LOGGER.debug('No `published_parsed` info available '
|
|
|
|
'for entry "%s"', entry.title)
|
|
|
|
entry.update({'feed_url': self._url})
|
|
|
|
self._hass.bus.fire(EVENT_FEEDREADER, entry)
|
2016-04-19 15:14:36 +00:00
|
|
|
|
|
|
|
def _publish_new_entries(self):
|
|
|
|
"""Publish new entries to the event bus."""
|
|
|
|
new_entries = False
|
2016-05-29 21:33:53 +00:00
|
|
|
self._last_entry_timestamp = self._storage.get_timestamp(self._url)
|
|
|
|
if self._last_entry_timestamp:
|
|
|
|
self._firstrun = False
|
|
|
|
else:
|
|
|
|
# Set last entry timestamp as epoch time if not available
|
|
|
|
self._last_entry_timestamp = \
|
|
|
|
datetime.utcfromtimestamp(0).timetuple()
|
2016-04-19 15:14:36 +00:00
|
|
|
for entry in self._feed.entries:
|
2016-04-28 13:01:44 +00:00
|
|
|
if self._firstrun or (
|
|
|
|
'published_parsed' in entry.keys() and
|
|
|
|
entry.published_parsed > self._last_entry_timestamp):
|
|
|
|
self._update_and_fire_entry(entry)
|
2016-04-19 15:14:36 +00:00
|
|
|
new_entries = True
|
2016-04-28 13:01:44 +00:00
|
|
|
else:
|
2016-05-04 01:40:30 +00:00
|
|
|
_LOGGER.debug('Entry "%s" already processed', entry.title)
|
2016-04-19 15:14:36 +00:00
|
|
|
if not new_entries:
|
|
|
|
self._log_no_entries()
|
2016-04-28 13:01:44 +00:00
|
|
|
self._firstrun = False
|
2016-04-19 15:14:36 +00:00
|
|
|
|
|
|
|
|
2016-05-29 21:33:53 +00:00
|
|
|
class StoredData(object):
|
|
|
|
"""Abstraction over pickle data storage."""
|
|
|
|
|
|
|
|
def __init__(self, data_file):
|
|
|
|
"""Initialize pickle data storage."""
|
|
|
|
self._data_file = data_file
|
|
|
|
self._lock = Lock()
|
|
|
|
self._cache_outdated = True
|
|
|
|
self._data = {}
|
|
|
|
self._fetch_data()
|
|
|
|
|
|
|
|
def _fetch_data(self):
|
|
|
|
"""Fetch data stored into pickle file."""
|
|
|
|
if self._cache_outdated and exists(self._data_file):
|
|
|
|
try:
|
|
|
|
_LOGGER.debug('Fetching data from file %s', self._data_file)
|
|
|
|
with self._lock, open(self._data_file, 'rb') as myfile:
|
|
|
|
self._data = pickle.load(myfile) or {}
|
|
|
|
self._cache_outdated = False
|
|
|
|
# pylint: disable=bare-except
|
|
|
|
except:
|
|
|
|
_LOGGER.error('Error loading data from pickled file %s',
|
|
|
|
self._data_file)
|
|
|
|
|
|
|
|
def get_timestamp(self, url):
|
|
|
|
"""Return stored timestamp for given url."""
|
|
|
|
self._fetch_data()
|
|
|
|
return self._data.get(url)
|
|
|
|
|
|
|
|
def put_timestamp(self, url, timestamp):
|
|
|
|
"""Update timestamp for given url."""
|
|
|
|
self._fetch_data()
|
|
|
|
with self._lock, open(self._data_file, 'wb') as myfile:
|
|
|
|
self._data.update({url: timestamp})
|
|
|
|
_LOGGER.debug('Overwriting feed "%s" timestamp in storage file %s',
|
|
|
|
url, self._data_file)
|
|
|
|
try:
|
|
|
|
pickle.dump(self._data, myfile)
|
|
|
|
# pylint: disable=bare-except
|
|
|
|
except:
|
|
|
|
_LOGGER.error('Error saving pickled data to %s',
|
|
|
|
self._data_file)
|
|
|
|
self._cache_outdated = True
|
|
|
|
|
|
|
|
|
2016-04-19 15:14:36 +00:00
|
|
|
def setup(hass, config):
|
|
|
|
"""Setup the feedreader component."""
|
|
|
|
urls = config.get(DOMAIN)['urls']
|
2016-05-29 21:33:53 +00:00
|
|
|
data_file = hass.config.path("{}.pickle".format(DOMAIN))
|
|
|
|
storage = StoredData(data_file)
|
|
|
|
feeds = [FeedManager(url, hass, storage) for url in urls]
|
2016-04-19 15:14:36 +00:00
|
|
|
return len(feeds) > 0
|