Skip to content

Instantly share code, notes, and snippets.

@lqc
Created April 21, 2010 23:39
Show Gist options
  • Select an option

  • Save lqc/374595 to your computer and use it in GitHub Desktop.

Select an option

Save lqc/374595 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8
from xml.dom import minidom
from urllib import urlopen
from os import getcwd
import codecs
class HackingRssReader:
def __init__(self, feed_url = 'http://hacking.pl/rss.xml' ):
self.feed_url = feed_url# nie ma powodu tego ukrywać
def _get_raw(self):
response = urlopen(self.feed_url)
return response.read()
def _parse_raw(self, source):
rss = minidom.parseString(source)
all_news = []
for node in rss.childNodes[0].getElementsByTagName('item'):
news = []
for field in ['title', 'description', 'pubDate']:
news.append(node.getElementsByTagName(field)[0].childNodes[0].toxml())
all_news.append(news)
return all_news
def get_news(self):
return self._parse_raw(self._get_raw())
def save_news_txt(self, news, path, encoding='utf-8'):
with codecs.open(path, 'wb', encoding) as out:
out.write(u'#Tytuł\n#Treść\n#Data\n*****\n')
for item in news:
out.write(u'\n'.join(item))
out.write('\n#####\n')
def print_news(self, news):
for n, item in enumerate(news):
print "%d. # %s # %s" %(n, item[0], item[2])
print item[1]
print
rss = HackingRssReader()
news = rss.get_news()
rss.print_news(news)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment