forked from STARTeurope/rssreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrssreader.py
More file actions
116 lines (92 loc) · 3.28 KB
/
Copy pathrssreader.py
File metadata and controls
116 lines (92 loc) · 3.28 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import webapp2
import jinja2
import os
import urllib
from xml.dom import minidom, Node
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class IndexPage(webapp2.RequestHandler):
def get(self):
self.response.out.write(self.init())
def post(self):
self.response.out.write(self.init())
def init(self):
rss_reader = RSSReader('http://feeds.feedburner.com/[FeedBurner_Projekt_Name]?format=xml') #add feed url to RSS Reader - tested with feedburner
items = rss_reader.GetItems()
template_values = {
"items": items
}
template = jinja_environment.get_template('templates/index.html')
return template.render(template_values)
class RSSItem:
"""This is an RSS item, it contain all the RSS info like Tile and Description"""
def __init__(self,item_node):
self.title = self.GetChildText(item_node,"title")
self.link = self.GetChildText(item_node, "link")
self.pubDate = self.GetChildText(item_node, "pubDate")[0:16]
self.description = self.GetChildText(item_node,"description")
self.content = self.GetChildText(item_node,"content:encoded")
self.img = self.FindIMG(self.content)
def GetItemText(self,xml_node):
"""Get the text from an xml item"""
text = ""
for text_node in xml_node.childNodes:
if (text_node.nodeType == Node.TEXT_NODE):
text += text_node.nodeValue
elif (text_node.nodeType == 4): #CDATA Node Type
text += text_node.nodeValue
return text
def GetChildText(self, xml_node, child_name):
"""Get a child node from the xml node"""
if (not xml_node):
print "Error GetChildNode: No xml_node"
return ""
for item_node in xml_node.childNodes:
if (item_node.nodeName==child_name):
return self.GetItemText(item_node)
"""Return Nothing"""
return ""
def FindIMG(self, content):
"""find first image in content and extract src"""
if (content):
pos = string.find(content, "<img")
tmp = content[pos:len(content)]
posend = string.find(tmp, ">")
tmp = tmp[0:posend]
pos = string.find(tmp, "src=")
tmp = tmp[pos:len(tmp)]
posend = string.find(tmp, " ")
tmp = tmp[0:posend]
return tmp[5:len(tmp)-1]
class RSSReader:
"""This class is an RSS reader, it should have a better docstring"""
def __init__(self,RSSUrl):
"""Initialize the class"""
self.items = []
self.RSSUrl = RSSUrl
self.xmldoc = self.GetXMLDocument(RSSUrl)
if (not self.xmldoc):
print "Error Getting XML Document!"
def GetXMLDocument(self,RSSUrl):
"""This function reads in a RSS URL and then"""
"""returns the XML document on success"""
url_info = urllib.urlopen(RSSUrl)
xmldoc = None
if (url_info):
xmldoc = minidom.parse(url_info)
else :
print "Error Getting URL"
return xmldoc
def GetItems(self):
"""Generator to get items"""
for item_node in self.xmldoc.documentElement.childNodes:
if (item_node.nodeName == "channel"):
for child_node in item_node.childNodes:
if (child_node.nodeName == "item"):
"""Allright we have an item"""
rss_item = RSSItem(child_node)
self.items.append(rss_item)
return self.items
app = webapp2.WSGIApplication([('/.*', IndexPage)], debug=True)