Skip to content

Commit 675378d

Browse files
aalavenderiantrich
authored andcommitted
Add "show_topn" and try find image from summary filed (#20)
* Add a Configuration variables "show_topn", it's disabled when show_topn > number of rss entries * Update README.md * when inclusions contains image option, it will try to find image within summary filed if image not found * blank lines modified
1 parent b86fcf7 commit 675378d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ key | description
4545
**name (Required)** | Name your feed
4646
**feed_url (Required)** | The RSS feed URL
4747
**date_format (Optional)** | strftime date format for date strings **Default** `%a, %b %d %I:%M %p`
48+
**show_topn (Optional)** | fetch how many entres from rss source,if not set then fetch all
4849
**inclusions (Optional)** | List of fields to include from populating the list
4950
**exclusions (Optional)** | List of fields to exclude from populating the list
5051

custom_components/feedparser/sensor.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Following spec from https://validator.w3.org/feed/docs/rss2.html
88
"""
9-
9+
import re
1010
import feedparser
1111
import logging
1212
import voluptuous as vol
@@ -28,6 +28,7 @@
2828
CONF_DATE_FORMAT = 'date_format'
2929
CONF_INCLUSIONS = 'inclusions'
3030
CONF_EXCLUSIONS = 'exclusions'
31+
CONF_SHOW_TOPN = 'show_topn'
3132

3233
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
3334

@@ -39,6 +40,7 @@
3940
vol.Required(CONF_NAME): cv.string,
4041
vol.Required(CONF_FEED_URL): cv.string,
4142
vol.Required(CONF_DATE_FORMAT, default='%a, %b %d %I:%M %p'): cv.string,
43+
vol.Optional(CONF_SHOW_TOPN, default=9999): cv.positive_int,
4244
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
4345
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
4446
})
@@ -52,6 +54,7 @@ def __init__(self, hass, config):
5254
self._feed = config[CONF_FEED_URL]
5355
self._name = config[CONF_NAME]
5456
self._date_format = config[CONF_DATE_FORMAT]
57+
self._show_topn = config[CONF_SHOW_TOPN]
5558
self._inclusions = config[CONF_INCLUSIONS]
5659
self._exclusions = config[CONF_EXCLUSIONS]
5760
self._state = None
@@ -64,21 +67,29 @@ def update(self):
6467
if not parsedFeed:
6568
return False
6669
else:
67-
self._state = len(parsedFeed.entries)
70+
self._state = self._show_topn if len(parsedFeed.entries) > self._show_topn else len(parsedFeed.entries)
6871
self._entries = []
69-
70-
for entry in parsedFeed.entries:
72+
73+
for entry in parsedFeed.entries[:self._state]:
7174
entryValue = {}
7275

7376
for key, value in entry.items():
7477
if (self._inclusions and key not in self._inclusions) or ('parsed' in key) or (key in self._exclusions):
7578
continue
76-
7779
if key in ['published', 'updated', 'created', 'expired']:
7880
value = parser.parse(value).strftime(self._date_format)
7981

8082
entryValue[key] = value
8183

84+
if 'image' in self._inclusions and 'image' not in entryValue.keys():
85+
images = []
86+
if 'summary' in entry.keys():
87+
images = re.findall(r"<img.+?src=\"(.+?)\".+?>", entry['summary'])
88+
if images:
89+
entryValue['image'] = images[0]
90+
else:
91+
entryValue['image'] = "https://www.home-assistant.io/images/favicon-192x192-full.png"
92+
8293
self._entries.append(entryValue)
8394

8495
@property

0 commit comments

Comments
 (0)