-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrssstream.py
More file actions
executable file
·300 lines (264 loc) · 7.91 KB
/
Copy pathrssstream.py
File metadata and controls
executable file
·300 lines (264 loc) · 7.91 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import xml.sax
import requests
from datetime import datetime
def datesort(rss,reverse):
if isinstance(rss,RssStreamParser):
rss = list(rss.parseItems())
rss.sort(key=lambda x: x['pubDate']['content'], reverse=reverse)
return rss
def newest2oldest(rss):
return datesort(rss, True)
def oldest2newest(rss):
return datesort(rss, False)
# TODO: if a channel has an atom:link tag, it might have a type attribute
def ct2type(ct):
typ = None
if "atom+xml" in ct:
typ = "atom"
elif "xml" in ct:
typ = "rss"
elif "rss+xml" in ct:
typ = "rss"
elif "application/octet-stream" in ct:
typ = "octets"
else:
typ = "unknown"
return typ
# class from https://stackoverflow.com/questions/7693535/what-is-a-good-xml-stream-parser-for-python
class RssStreamSaxHandler(xml.sax.handler.ContentHandler):
def __init__(self,contentcb=None):
self.contentcb = contentcb
self.tagstack = []
self.attribdict = {}
self.contentdict = {}
def path(self):
return '/' + '/'.join(self.tagstack)
def startElement(self, name, attrs):
self.tagstack.append(name)
self.attribdict[self.path()] = attrs
def endElement(self, name):
path = self.path()
if path in self.attribdict:
a = self.attribdict[path].items()
del self.attribdict[path]
else:
a = None
if path in self.contentdict:
c = self.contentdict[path]
del self.contentdict[path]
else:
c = None
#print("\n{}\n attribs\t{}\n content\t{}".format(path,str(a),c))
self.tagstack.pop()
if self.contentcb is not None:
#print("cb")
self.contentcb(path,c,a)
def characters(self, content):
if content is None or content == "" or content == " ":
return
content = content.encode('ascii','ignore')
if self.path() in self.contentdict:
self.contentdict[self.path()] += content
else:
self.contentdict[self.path()] = content
#print("{} content={}".format(self.tagstack,content))
class RssStreamParser(object):
def __init__(self,url, text=None, resp=None, type=None, verb=False, ua=None, headers=None, timeout=None, raize=False, verify=True):
self.verify = verify
self.bail = False
self.raize = raize
self.verb = verb
self.url = url
self.parser = xml.sax.make_parser()
self.parser.setContentHandler(RssStreamSaxHandler(contentcb=self.contentcb))
self.elems = []
if headers is None:
headers = {
'Accept': "*/*",
}
if ua is not None:
headers['User-Agent'] = ua
self.type = None
self.ct = None
self.charset = 'utf-8'
if text is not None:
self.resp = None
self.text = text
self.type = type
elif resp is not None:
self.resp = resp
self.text = ''
self.check_resp()
else:
self.resp = requests.get(self.url,stream=True,headers=headers,timeout=timeout,verify=self.verify)
self.text = ''
self.check_resp()
def check_resp(self):
try:
if self.verb:
print("Content-type of {url} is {ct}".format(ct=self.resp.headers['content-type'],url=self.url))
self.ct = self.resp.headers['content-type']
except KeyError as e:
if self.verb:
print(e)
print(self.resp.status_code)
for h in self.resp.headers:
print(h,self.resp.headers[h])
print(self.resp.content)
if self.resp.status_code == 404:
self.ct = None
else:
raise e
self.type = ct2type(self.ct)
if self.type is None:
self.bail = True
if "charset=" in self.ct:
self.charset = self.ct[self.ct.find("charset=") + len("charset="):]
if self.verb: print("Found charset {}".format(self.charset))
if self.type == "unknown" and self.verb:
print("Got unknown content-type {}\n{}".format(self.ct, self.resp.text))
if self.type == "octets":
if self.charset.lower() == 'utf-8':
self.type = 'rss'
if self.verb: print("Blindly assuming octetstream type is RSS")
if self.verb:
print("character set is {}".format(self.charset))
def contentcb(self,path,content,attributes):
if isinstance(content,bytes):
content = content.decode(self.charset)
a = {}
for t in attributes:
k,v=t
a[k]=v
if path.endswith('pubDate'):
fs = [
'%a, %d %b %Y %H:%M:%S %Z', #Mon, 26 Apr 2021 21:26:03 GMT
'%a, %d %b %Y %H:%M:%S %z', #Sun, 24 Nov 2019 17:21:51 -0500
]
for f in fs:
try:
content = datetime.strptime(content, f)
except ValueError as e:
pass
else:
break
self.elems.append((path,content,a))
def parse(self):
if len(self.text) > 0:
for i in self.feed(self.text):
yield i
return
oldchunk = None
for chunk in self.resp.iter_content(chunk_size=1024):
#print("chunk")
if chunk:
if oldchunk is not None:
chunk = oldchunk + chunk
oldchunk = None
try:
chunk = chunk.decode(self.charset)
except UnicodeDecodeError as e:
if "unexpected end of data" in str(e):
oldchunk = chunk
continue
if self.verb: print("bad chunk in hex\n{}".format(chunk.hex()))
raise e
self.text += chunk
# NOTE: chunk was left as bytes for years, but recently the sax parser in feed() started choking on it if it had certain UTF characters (0xe28098 aka U+2018).
# so now we are converting chunk to string before feeding it to the parser
for i in self.feed(chunk):
yield i
def parseItems(self):
if self.isRss():
return self.parseRssItems()
elif self.isAtom():
return self.parseAtomEntries()
def isAtom(self):
return self.type == "atom"
def isRss(self):
return self.type == "rss"
def parseAtomEntries(self):
obj = {}
for elem,content,attributes in self.parse():
if elem == "/feed/entry":
yield obj
obj = {}
elif elem.startswith("/feed/entry"):
tag = elem[len("/feed/entry")+1:]
if tag in obj:
if obj[tag] is not None and content is not None:
obj[tag] += content
else:
obj[tag] = content
def parseRssItems(self):
obj = {}
for elem,content,attributes in self.parse():
if elem == "/rss/channel/item":
yield obj
obj = {}
elif elem.startswith("/rss/channel/item"):
tag = elem[len("/rss/channel/item")+1:]
if tag in obj:
if obj[tag] is not None and content is not None:
if not isinstance(obj[tag], list):
tmp = obj[tag]
obj[tag] = []
obj[tag].append(tmp)
obj[tag].append({'content':content, 'attributes':attributes})
else:
obj[tag] = {'content':content, 'attributes':attributes}
def feed(self,chunk):
try:
self.parser.feed(chunk)
except xml.sax._exceptions.SAXParseException as e:
if self.raize:
raise RssParseException(e)
#raise RuntimeError("SAX parsing failed on {}".format(chunk))
if self.verb: print("error {} in feed for {}, skipping".format(e, self.url))
self.bail = True
if self.bail:
return
for i in self.elems:
yield i
#print("fed")
self.elems = []
class RssParseException (Exception):
pass
if __name__ == "__main__":
from sys import argv
#import pprint
bbc = 'http://feeds.bbci.co.uk/news/world/rss.xml'
verb=True
feed = None
if len(argv) > 1:
feed = argv[1]
else:
feed = bbc
#fmt = "{pubDate[content]}\t{title[content]}\t{description[content]}"
fmt = "{guid[content]}\t{pubDate[content]}\t{title[content]}\t{description[content]}\t{enclosure[attributes][url]}"
if verb:
print(feed)
print("Builtin requests")
#for i in RssStreamParser(feed,verb=verb).parseItems():
for i in oldest2newest(RssStreamParser(feed,verb=verb,ua="Me!",raize=True)):
#print(type(i['pubDate']['content']))
if not "description" in i:
if "itunes:summary" in i:
i["description"] = i["itunes:summary"]
else:
i["description"] = {"content": "", "attributes": {}}
#pprint.pp(i)
print(fmt.format(**i))
#print("External requests")
#resp = requests.get(feed)
#if resp.status_code != 200:
# print("error downloading {f}: {s}".format(f=feed,s=resp.status))
#typ = ct2type(resp.headers['content-type'])
#for i in RssStreamParser(None,text=resp.text,type=typ,verb=verb).parseItems():
# #print(i)
# if not "description" in i:
# if "itunes:summary" in i:
# i["description"] = i["itunes:summary"]
# print(fmt.format(**i))