2013-03-26 20:20:51 +01:00
|
|
|
from sqlalchemy import Column, Integer, ForeignKey, String, Text, DateTime
|
|
|
|
from datetime import datetime
|
|
|
|
from time import mktime
|
2013-04-08 21:02:48 +02:00
|
|
|
import urllib2
|
|
|
|
#import urllib
|
|
|
|
#import hn
|
|
|
|
import html2text
|
|
|
|
import HTMLParser
|
2013-03-26 20:20:51 +01:00
|
|
|
|
|
|
|
from models import Base
|
|
|
|
|
2013-04-08 21:02:48 +02:00
|
|
|
def fetch_readability(link):
|
|
|
|
text = hn.upgradeLink(link)
|
|
|
|
text = text.decode('utf8')
|
|
|
|
return text
|
|
|
|
|
|
|
|
def fetch_full_page(link):
|
|
|
|
opener = urllib.FancyURLopener({})
|
|
|
|
response = opener.open(link)
|
|
|
|
html = response.read()
|
|
|
|
html = html.decode('utf8')
|
|
|
|
text = html2text.html2text(html)
|
|
|
|
return text.encode('latin-1', 'replace')
|
|
|
|
|
|
|
|
def size_human_readable(bytesize):
|
|
|
|
for x in ['bytes','KB','MB','GB']:
|
|
|
|
if bytesize < 1024.0:
|
|
|
|
return "%3.1f%s" % (bytesize, x)
|
|
|
|
bytesize /= 1024.0
|
|
|
|
return "%3.1f%s" % (bytesize, 'TB')
|
|
|
|
|
2013-03-26 20:20:51 +01:00
|
|
|
class Entry(Base):
|
|
|
|
__tablename__ = 'entry'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
feed_id = Column(Integer, ForeignKey('feed.id'))
|
|
|
|
title = Column(String(255))
|
|
|
|
link = Column(String(255))
|
|
|
|
summary = Column(Text)
|
|
|
|
content = Column(Text)
|
|
|
|
author = Column(String(255))
|
|
|
|
enclosures = Column(Text)
|
|
|
|
|
|
|
|
resolvedlink = Column(String(255))
|
|
|
|
fullpage = Column(Text)
|
|
|
|
readability = Column(Text)
|
|
|
|
updated = Column(DateTime)
|
|
|
|
firstfetched = Column(DateTime)
|
|
|
|
lastfetched = Column(DateTime)
|
|
|
|
sent = Column(DateTime)
|
|
|
|
|
2013-04-08 21:02:48 +02:00
|
|
|
def __init__(self, entry, feed):
|
2013-03-26 20:20:51 +01:00
|
|
|
if entry.has_key('title'):
|
|
|
|
self.title = entry.get('title')
|
|
|
|
if entry.has_key('link'):
|
|
|
|
self.link = entry.get('link')
|
|
|
|
if entry.has_key('summary'):
|
|
|
|
self.summary = entry.get('summary')
|
|
|
|
if entry.has_key('content'):
|
|
|
|
self.content = entry.get('content')[0].value
|
|
|
|
if entry.has_key('author'):
|
|
|
|
self.author = entry.get('author')
|
|
|
|
if entry.has_key('updated_parsed'):
|
|
|
|
updated_parsed = entry.get('updated_parsed')
|
|
|
|
self.updated = datetime.fromtimestamp(mktime(updated_parsed))
|
|
|
|
if entry.has_key('enclosures') and len(entry.get('enclosures')) > 0:
|
2013-04-08 21:02:48 +02:00
|
|
|
enclosures = ''
|
|
|
|
for enclosure in entry.get('enclosures'):
|
|
|
|
if not enclosures == '':
|
|
|
|
enclosures += '\n'
|
2013-04-08 21:19:29 +02:00
|
|
|
enclosures += enclosure['href']
|
|
|
|
if enclosure.has_key('length'):
|
|
|
|
enclosures += ' (%s)' % size_human_readable(int(enclosure['length']))
|
2013-04-08 21:02:48 +02:00
|
|
|
self.enclosures = enclosures
|
|
|
|
if feed.resolveredirects:
|
|
|
|
print ' fetching final link <%s>' % entry.link
|
|
|
|
request = urllib2.Request(entry.link)
|
|
|
|
opener = urllib2.build_opener()
|
2013-04-09 12:39:12 +02:00
|
|
|
try:
|
|
|
|
result = opener.open(request)
|
|
|
|
self.resolvedlink = result.url
|
|
|
|
print ' final link: <%s>' % result.url
|
|
|
|
except:
|
|
|
|
print ' FAILED opening URL'
|
2013-04-08 21:02:48 +02:00
|
|
|
if feed.fullpage:
|
|
|
|
print ' fetching full page <%s>' % entry.link
|
|
|
|
self.fullpage = fetch_full_page(entry.link)
|
|
|
|
if feed.readability:
|
|
|
|
print ' fetching readability <%s>' % entry.link
|
|
|
|
self.readability = fetch_readability(entry.link)
|
|
|
|
if feed.html2textcontent:
|
|
|
|
print ' converting summary'
|
|
|
|
h2t = html2text.HTML2Text()
|
|
|
|
h2t.body_width = 0
|
|
|
|
h2t.inline_links = False
|
2013-04-09 12:23:05 +02:00
|
|
|
if feed.html2textignorelinks:
|
|
|
|
h2t.ignore_links = True
|
2013-04-08 21:02:48 +02:00
|
|
|
if feed.html2textignoreimages:
|
|
|
|
h2t.ignore_images = True
|
2013-04-08 22:47:22 +02:00
|
|
|
if feed.contentcolumn == 'summary' and self.summary:
|
2013-04-08 21:02:48 +02:00
|
|
|
self.summary = h2t.handle(self.summary)
|
2013-04-08 22:47:22 +02:00
|
|
|
elif feed.contentcolumn == 'content' and self.content:
|
2013-04-08 21:02:48 +02:00
|
|
|
self.content = h2t.handle(self.content)
|
2013-04-08 22:47:22 +02:00
|
|
|
elif feed.contentcolumn == 'fullpage' and self.fullpage:
|
2013-04-08 21:02:48 +02:00
|
|
|
self.fullpage = h2t.handle(self.fullpage)
|
2013-04-08 22:47:22 +02:00
|
|
|
elif feed.contentcolumn == 'readability' and self.readability:
|
2013-04-08 21:02:48 +02:00
|
|
|
self.readability = h2t.handle(self.readability)
|
|
|
|
hp = HTMLParser.HTMLParser()
|
|
|
|
if self.summary:
|
|
|
|
self.summary = hp.unescape(self.summary)
|
|
|
|
if self.content:
|
|
|
|
self.content = hp.unescape(self.content)
|
|
|
|
if self.fullpage:
|
|
|
|
self.fullpage = hp.unescape(self.fullpage)
|
|
|
|
if self.readability:
|
|
|
|
self.readability = hp.unescape(self.readability)
|
|
|
|
self.firstfetched = datetime.now()
|
2013-03-26 20:20:51 +01:00
|
|
|
self.lastfetched = datetime.now()
|
2013-04-03 00:06:06 +02:00
|
|
|
|
2013-04-08 21:02:48 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return u'%d -> %s' % (self.id, self.title)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return unicode(self).encode('utf-8')
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Entry('%d','%s')>" % (self.id, self.title)
|
|
|
|
|
2013-04-03 00:06:06 +02:00
|
|
|
def get_text(self):
|
2013-04-08 22:47:22 +02:00
|
|
|
text = ''
|
|
|
|
if self.feed.contentcolumn == 'summary' and self.summary:
|
2013-04-03 00:06:06 +02:00
|
|
|
text = self.summary
|
2013-04-08 22:47:22 +02:00
|
|
|
elif self.feed.contentcolumn == 'content' and self.content:
|
2013-04-03 00:06:06 +02:00
|
|
|
text = self.content
|
2013-04-08 22:47:22 +02:00
|
|
|
elif self.feed.contentcolumn == 'fullpage' and self.fullpage:
|
2013-04-03 00:06:06 +02:00
|
|
|
text = self.fullpage
|
2013-04-08 22:47:22 +02:00
|
|
|
elif self.feed.contentcolumn == 'readability' and self.readability:
|
2013-04-03 00:06:06 +02:00
|
|
|
text = self.readability
|
2013-04-08 21:02:48 +02:00
|
|
|
if self.enclosures:
|
|
|
|
text += '\n\nEnclosures:\n%s' % self.enclosures
|
2013-04-03 00:06:06 +02:00
|
|
|
return text
|
|
|
|
|
2013-04-09 23:39:02 +02:00
|
|
|
# -*- coding: utf-8 -*-
|