-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfeedparser.py
More file actions
232 lines (195 loc) · 7.25 KB
/
feedparser.py
File metadata and controls
232 lines (195 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""Parse Atom and RSS feeds in Python.
Time zone handling is not implemented.
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025042001'
import sys
from .globals import STATE_UNKNOWN
try:
from bs4 import BeautifulSoup
except ImportError as e:
print('Python module "BeautifulSoup4" is not installed.')
sys.exit(STATE_UNKNOWN)
from . import time
from . import url
def parse(feed_url, insecure=False, no_proxy=False, timeout=5, encoding='urlencode'):
"""
Parse an Atom or RSS feed from a URL, file, stream, or string.
This function fetches a feed resource, parses it as XML using BeautifulSoup, and attempts to
automatically detect and parse Atom or RSS formats into structured dictionaries.
### Parameters
- **feed_url** (`str`):
The URL or file path of the feed to fetch and parse.
- **insecure** (`bool`, optional):
If `True`, disable SSL verification during download. Default is `False`.
- **no_proxy** (`bool`, optional):
If `True`, ignore any system proxy settings. Default is `False`.
- **timeout** (`int`, optional):
Timeout in seconds for the download request. Default is `5`.
- **encoding** (`str`, optional):
Encoding to use for the URL fetch operation. Default is `'urlencode'`.
### Returns
- **tuple**:
- `(True, dict)`: On success, returns parsed feed data.
- `(False, str or Exception)`: On failure, returns an error message or exception.
### Notes
- Atom feeds must have a `<feed>` root element.
- RSS feeds must have a `<rss>` root element.
- Automatically detects the feed format (Atom or RSS).
### Example
>>> success, result = parse('https://linuxfabrik.ch/feed.xml')
>>> if success:
>>> print(result)
{
'title': 'Linuxfabrik Posts',
'updated': '2025-04-17T11:29:00.000Z',
'updated_parsed': datetime.datetime(2025, 4, 17, 11, 29),
'entries': [
{
'title': 'Lorem ipsum',
'id': 'https://linuxfabrik.ch',
'updated': '2017-04-17T11:29:00.000Z',
...
},
...
]
}
"""
success, xml = url.fetch(
feed_url,
encoding=encoding,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return False, xml
try:
soup = BeautifulSoup(xml, 'xml')
except Exception as e:
return False, e
if soup.feed:
return True, parse_atom(soup)
if soup.rss:
return True, parse_rss(soup)
return False, f'{feed_url} does not seem to be an Atom or RSS feed I understand.'
def parse_atom(soup):
"""
Parse an Atom XML feed into a structured dictionary.
This function processes an Atom feed using BeautifulSoup, extracting metadata such as the feed
title, last updated timestamp, and a list of entries with their respective information.
### Parameters
- **soup** (`BeautifulSoup`):
A BeautifulSoup object parsed from an Atom XML feed.
### Returns
- **dict**:
A dictionary containing feed metadata and a list of parsed entries.
### Notes
- The `updated` fields are also parsed into Python `datetime` objects (`updated_parsed`).
- Summaries are extracted from either `<summary>` or `<content>`, cleaned of HTML tags.
### Example
>>> parse_atom(soup)
{
'title': 'My Feed',
'updated': '2024-04-17T15:04:00+00:00',
'updated_parsed': datetime.datetime(2024, 4, 17, 15, 4, 0),
'entries': [...],
}
"""
result = {
'title': soup.title.string if soup.title else 'n/a',
'updated': soup.updated.string if soup.updated else '1970-01-01T00:00:00',
}
result['updated_parsed'] = time.timestr2datetime(
result['updated'][:19],
pattern='%Y-%m-%dT%H:%M:%S',
)
result['entries'] = []
for entry in soup.find_all('entry'):
tmp = {
'title': entry.title.string if entry.title else 'n/a',
'id': entry.id.string if entry.id else 'n/a',
'updated': entry.updated.string if entry.updated else '1970-01-01T00:00:00',
}
tmp['updated_parsed'] = time.timestr2datetime(
tmp['updated'][:19],
pattern='%Y-%m-%dT%H:%M:%S',
)
# summary
try:
parsed_summary = BeautifulSoup(entry.summary.string, 'lxml')
tmp['summary'] = parsed_summary.get_text()
except Exception:
try:
parsed_summary = BeautifulSoup(entry.content.string, 'lxml')
tmp['summary'] = parsed_summary.get_text()
except Exception:
tmp['summary'] = ''
result['entries'].append(tmp)
return result
def parse_rss(soup):
"""
Parse an RSS XML feed into a structured dictionary.
This function processes an RSS feed using BeautifulSoup, extracting metadata such as the feed
title, last update timestamp, and a list of items with their respective information.
### Parameters
- **soup** (`BeautifulSoup`):
A BeautifulSoup object parsed from an RSS XML feed.
### Returns
- **dict**:
A dictionary containing feed metadata and a list of parsed items.
### Notes
- If `pubDate` is missing, `lastBuildDate` is used instead.
- The `updated` fields are parsed into Python `datetime` objects (`updated_parsed`).
- Summaries are extracted from the `<description>` tag, cleaned of HTML tags.
### Example
>>> parse_rss(soup)
{
'title': 'My RSS Feed',
'updated': 'Wed, 10 Apr 2024 06:12:00 Z',
'updated_parsed': datetime.datetime(2024, 4, 10, 6, 12, 0),
'entries': [...],
}
"""
result = {}
result['title'] = soup.rss.channel.title.string if soup.rss and soup.rss.channel and soup.rss.channel.title else 'n/a'
updated = None
try:
updated = soup.rss.channel.pubDate.string
except Exception:
try:
updated = soup.rss.channel.lastBuildDate.string
except Exception:
pass
if updated:
result['updated'] = updated
result['updated_parsed'] = time.timestr2datetime(
updated[:25],
pattern='%a, %d %b %Y %H:%M:%S',
)
result['entries'] = []
for entry in soup.find_all('item'):
tmp = {
'title': entry.title.string if entry.title else 'n/a',
'id': entry.guid.string if entry.guid else 'n/a',
'updated': entry.pubDate.string if entry.pubDate else 'Wed, 01 Jan 1970 00:00:00',
}
tmp['updated_parsed'] = time.timestr2datetime(
tmp['updated'][:25],
pattern='%a, %d %b %Y %H:%M:%S',
)
try:
description_soup = BeautifulSoup(entry.description.string, 'lxml')
tmp['summary'] = description_soup.get_text()
except Exception:
tmp['summary'] = ''
result['entries'].append(tmp)
return result