Permalink
Browse files

restructured using more objects, very hacky but works

  • Loading branch information...
baracoder committed Dec 2, 2011
1 parent 516ed90 commit 3abb9951d17feaba66966cad026b8336e3592d3f
Showing with 148 additions and 237 deletions.
  1. +8 −7 config-sample.py
  2. +3 −230 feedhub.py
  3. +6 −0 feeds/__init__.py
  4. +131 −0 feeds/default.py
View
@@ -1,11 +1,12 @@
-DELICIOUS, TWITTER, BLOG, GPLUS = range(4)
-LIMIT = 10
-TEASER_LEN = 280
+from feeds.default import Feed
+LIMIT = 50
FEEDS = [
- # type (BLOG|TWITTER|DELICIOUS), site, feed url and flavor (atom|rss)
-# (BLOG, 'http://someblog.com/', 'http://someblog.com/feed/', 'atom'),
-# (DELICIOUS, 'http://delicious.com/username', 'http://feeds.delicious.com/v2/rss/username?count=15', 'rss'),
- (TWITTER, 'http://twitter.com/passiomatc', 'http://twitter.com/statuses/user_timeline/14973295.atom', 'atom'),
+ # add your feeds here
+ # Feed(URL,TITLE,CSS_CLASS),
+ # examples:
+ #Feed('http://twitter.com/statuses/user_timeline/55714996.rss','twitter','twitter'),
+ #Feed('http://plusfeed.appspot.com/105123658415843460405','google+','gplus'),
+ #Feed('http://api.flickr.com/services/feeds/photos_public.gne?id=15622444@N04&lang=en-us&format=rss_200','flickr','flickr'),
]
View
@@ -1,261 +1,34 @@
-#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
-from xml.dom import minidom
-import urllib2
-import time
-
-from cgi import escape
-
-from datetime import datetime
-
-import sanitizer
-USER_AGENT = 'Passiomatic Feedhub/0.5.1 <http://lab.passiomatic.com/feedhub/>'
-
-
try:
import config
FEEDS = config.FEEDS
except ImportError:
print "Unable to find file config.py. Please rename config-sample.py first."
- FEEDS = []
import sys; sys.exit(1)
-
-def get_feed(url):
- opener = urllib2.build_opener()
- opener.addheaders = [('User-agent', USER_AGENT)]
- request = opener.open(url)
- data = request.read()
- request.close()
- return data
-
-
-class Item(object):
- def __repr__(self): return getattr(self, 'title', 'Untitled').encode('utf-8', 'replace')
- __str__ = __repr__
-
-
-def _get_atom_entries(the_feed, document):
- return _get_items(the_feed, document, 'entry')
-
-
-def _get_rss_entries(the_feed, document):
- return _get_items(the_feed, document, 'item')
-
-
-def _get_items(the_feed, document, tag_name):
- items = []
- for entry in document.getElementsByTagName(tag_name):
- item = _get_item_details(entry)
- item.the_feed = the_feed
- items.append(item)
-
- return items
-
-def _get_item_details(entry):
- item = Item()
-
- try:
- item.url = entry.getElementsByTagName('link')[0].getAttribute('href')
- except IndexError:
- item.url = entry.getElementsByTagName('link')[0].firstChild.nodeValue
-
- try:
- item.id = entry.getElementsByTagName('id')[0].firstChild.nodeValue
- except IndexError:
- item.id = entry.getElementsByTagName('guid')[0].firstChild.nodeValue
-
- try:
- item.title = entry.getElementsByTagName('title')[0].firstChild.nodeValue
- except AttributeError:
- item.title = ''
-
- try:
- item.content = entry.getElementsByTagName('content')[0].firstChild.nodeValue
- except IndexError:
- try:
- item.content = entry.getElementsByTagName('summary')[0].firstChild.nodeValue
- except IndexError:
- try:
- item.content = entry.getElementsByTagName('description')[0].firstChild.nodeValue
- except IndexError:
- item.content = ''
-
- try:
- date_published = entry.getElementsByTagName('published')[0].firstChild.nodeValue
- except IndexError:
- try:
- date_published = entry.getElementsByTagName('pubDate')[0].firstChild.nodeValue
- except IndexError:
- date_published = entry.getElementsByTagName('updated')[0].firstChild.nodeValue
-
- # get date
- try:
- # 2008-10-27T11:06:52 (ignore time zone)
- date_published = datetime(*(time.strptime(date_published[:19], '%Y-%m-%dT%H:%M:%S')[0:6]))
- except ValueError:
- try:
- # Sun, 08 Feb 2009 22:48:25 (ignore time zone)
- date_published = datetime(*(time.strptime(date_published[:25], '%a, %d %b %Y %H:%M:%S')[0:6]))
- except ValueError:
- date_published = datetime.now()
-
- item.date_published = date_published
-
- return item
-
-
-def parse_feed(the_feed):
- try:
- s = get_feed(the_feed[2])
- except urllib2.URLError:
- return []
-
- document = minidom.parseString(s)
-
- # flavor?
- if the_feed[3] == 'atom':
- items = _get_atom_entries(the_feed, document)
- else:
- items = _get_rss_entries(the_feed, document)
-
- document.unlink()
-
- return items
-
-# -----------------------------------------------------------
-
-def format_delicious(item):
- d = {
- 'url':escape(item.url),
- 'permalink':escape(item.id),
- 'title': escape(item.title),
- #'content':item.content
- }
- return d
-
-
-import re
-
-def linkify(text):
- r1 = r"(\b(http|https)://([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))"
- #r2 = r"((^|\b)www\.([-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]))"
-
- return re.sub(r1,r'<a href="\1">\1</a>',text)
-
-def format_twitter(item):
- d = {
- 'permalink':escape(item.url),
- 'title': linkify(item.title.lstrip('passiomatic:')),
- #'content':item.content
- }
- return d
-
-
-def format_blog(item):
- if item.content:
- content = sanitizer.strip(item.content)[:config.content_LEN] + '(&hellip;)'
- else:
- content = ''
-
- d = {
- 'permalink': escape(item.url),
- 'title': item.title,
- 'content': content
- }
- return d
-
-def format_gplus(item):
-
- d = {
- 'permalink': escape(item.url),
- 'title': item.title,
- 'content': item.content
- }
- return d
-
-
-TEMPLATES = {
-
- config.DELICIOUS : ("""
- <li class="clear">
- <h4>Bookmark</h4>
- <h3><a href="$url">$title</a></h3>
- <div class="meta">
- $date from <a href="$permalink">$site_name</a>
- </div>
- </li>
- """, format_delicious),
-
- config.TWITTER : ("""
- <li class="clear">
- <h4>Tweet</h4>
- <h3>$title</h3>
- <div class="meta">$date from <a href="$permalink">$site_name</a></div>
- </li>
- """, format_twitter),
-
- config.BLOG : ("""
- <li class="clear">
- <h4>Post</h4>
- <h3><a href="$permalink">$title &mdash; </a></h3>
- <p>$content</p>
- <div class="meta">$date from <a href="$permalink">$site_name</a></div>
- </li>
- """, format_blog),
-
- config.GPLUS : ("""
- <li class="clear">
- <h4>google+ Post</h4>
- <h3><a href="$permalink">$title</a></h3>
- <p>$content</p>
- <div class="meta">$date from <a href="$permalink">$site_name</a></div>
- </li>
- """, format_gplus),
-}
-
# -----------------------------------------------------------
-from string import Template
-
#def _u(s): return s.encode('utf-8', 'replace')
#from itertools import cycle
#last_loop = cycle(['', '', '', 'last'])
-import urlparse
-
-
-def get_friendly_name(url):
- return urlparse.urlsplit(url)[1]
- #return urlparse.urlsplit(url).hostname
-
def generate_html(items):
s = ''
for item in items:
- t, fn = TEMPLATES[item.the_feed[0]][0], TEMPLATES[item.the_feed[0]][1]
+ s = s + item.the_feed.render_item_html(item)
- s = s + Template(t).safe_substitute(
- site_name=escape(get_friendly_name(item.the_feed[1])),
- date=item.date_published.strftime('%b %d, %Y'),
-
- **fn(item)
- )
return s
if __name__ == "__main__":
items = []
- html = ''
for feed in FEEDS:
- items.extend(parse_feed(feed))
-
+ items.extend(feed.parse_feed())
items.sort(key=lambda i : i.date_published, reverse=True)
+
html = generate_html(items[:config.LIMIT]).encode('utf-8', 'replace')
- #~ f = open('./_feed_items.html', 'w')
- #~ f.write(html)
- #~ f.close()
print html
View
@@ -0,0 +1,6 @@
+__all__ = ['default', ]
+
+class Item(object):
+ def __repr__(self): return getattr(self, 'title', 'Untitled').encode('utf-8', 'replace')
+ __str__ = __repr__
+
Oops, something went wrong.

0 comments on commit 3abb995

Please sign in to comment.