2009-06-16 03:08:55 +00:00
|
|
|
import datetime
|
2012-09-10 17:41:01 -07:00
|
|
|
import struct
|
2012-04-17 11:48:28 -07:00
|
|
|
from HTMLParser import HTMLParser
|
2012-10-01 19:04:09 -07:00
|
|
|
from lxml.html.diff import tokenize, fixup_ins_del_tags, htmldiff_tokens
|
2012-10-01 19:31:33 -07:00
|
|
|
from lxml.etree import ParserError
|
2011-12-14 23:26:07 -08:00
|
|
|
from itertools import chain
|
2012-04-17 11:48:28 -07:00
|
|
|
from django.utils.dateformat import DateFormat
|
2012-07-21 16:38:37 -07:00
|
|
|
from django.utils.html import strip_tags as strip_tags_django
|
2012-04-17 11:48:28 -07:00
|
|
|
from django.conf import settings
|
2012-07-21 16:38:37 -07:00
|
|
|
from utils.tornado_escape import linkify as linkify_tornado
|
2012-07-23 23:23:34 -07:00
|
|
|
from utils.tornado_escape import xhtml_unescape as xhtml_unescape_tornado
|
2012-09-10 17:41:01 -07:00
|
|
|
from vendor import reseekfile
|
2011-01-15 18:41:41 -05:00
|
|
|
|
|
|
|
def story_score(story, bottom_delta=None):
|
|
|
|
# A) Date - Assumes story is unread and within unread range
|
|
|
|
if not bottom_delta:
|
|
|
|
bottom_delta = datetime.timedelta(days=settings.DAYS_OF_UNREAD)
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
date_delta = now - story['story_date']
|
|
|
|
seconds = lambda td: td.seconds + (td.days * 86400)
|
2011-01-16 15:48:27 -05:00
|
|
|
date_score = max(0, 1 - (seconds(date_delta) / float(seconds(bottom_delta))))
|
2011-01-15 18:41:41 -05:00
|
|
|
|
|
|
|
# B) Statistics
|
|
|
|
statistics_score = 0
|
|
|
|
|
|
|
|
# C) Intelligence
|
2011-01-30 10:46:53 -05:00
|
|
|
intelligence_score = 1
|
|
|
|
# intelligence_score = feed_counts[int(story['story_feed_id'])] / float(max_feed_count)
|
2011-01-15 18:41:41 -05:00
|
|
|
|
|
|
|
# print "%s - %s" % (story['story_date'], date_score)
|
2011-01-30 10:46:53 -05:00
|
|
|
return (30/100. * date_score) + (55/100. * statistics_score) + (15/100. * intelligence_score)
|
2009-06-16 03:08:55 +00:00
|
|
|
|
2011-01-12 23:30:38 -05:00
|
|
|
def format_story_link_date__short(date, now=None):
|
|
|
|
if not now: now = datetime.datetime.now()
|
|
|
|
diff = date.date() - now.date()
|
|
|
|
if diff.days == 0:
|
|
|
|
return date.strftime('%I:%M%p').lstrip('0').lower()
|
|
|
|
elif diff.days == 1:
|
|
|
|
return 'Yesterday, ' + date.strftime('%I:%M%p').lstrip('0').lower()
|
2009-06-16 03:08:55 +00:00
|
|
|
else:
|
2011-01-12 23:30:38 -05:00
|
|
|
return date.strftime('%d %b %Y, ') + date.strftime('%I:%M%p').lstrip('0').lower()
|
2009-06-16 03:08:55 +00:00
|
|
|
|
2011-01-12 23:30:38 -05:00
|
|
|
def format_story_link_date__long(date, now=None):
|
|
|
|
if not now: now = datetime.datetime.utcnow()
|
|
|
|
diff = now.date() - date.date()
|
|
|
|
parsed_date = DateFormat(date)
|
|
|
|
if diff.days == 0:
|
|
|
|
return 'Today, ' + parsed_date.format('F jS ') + date.strftime('%I:%M%p').lstrip('0').lower()
|
|
|
|
elif diff.days == 1:
|
2010-07-15 23:32:37 -04:00
|
|
|
return 'Yesterday, ' + parsed_date.format('F jS g:ia').replace('.','')
|
2011-01-12 23:30:38 -05:00
|
|
|
elif date.date().timetuple()[7] == now.date().timetuple()[7]:
|
2009-06-16 03:08:55 +00:00
|
|
|
return parsed_date.format('l, F jS g:ia').replace('.','')
|
|
|
|
else:
|
|
|
|
return parsed_date.format('l, F jS, Y g:ia').replace('.','')
|
|
|
|
|
|
|
|
def _extract_date_tuples(date):
|
|
|
|
parsed_date = DateFormat(date)
|
|
|
|
date_tuple = datetime.datetime.timetuple(date)[:3]
|
2010-10-10 23:55:00 -04:00
|
|
|
today_tuple = datetime.datetime.timetuple(datetime.datetime.utcnow())[:3]
|
2009-06-16 03:08:55 +00:00
|
|
|
today = datetime.datetime.today()
|
|
|
|
yesterday_tuple = datetime.datetime.timetuple(today - datetime.timedelta(1))[:3]
|
|
|
|
|
2009-12-18 20:47:44 +00:00
|
|
|
return parsed_date, date_tuple, today_tuple, yesterday_tuple
|
|
|
|
|
|
|
|
def pre_process_story(entry):
|
2010-09-17 13:33:11 -04:00
|
|
|
publish_date = entry.get('published_parsed', entry.get('updated_parsed'))
|
|
|
|
entry['published'] = datetime.datetime(*publish_date[:6]) if publish_date else datetime.datetime.utcnow()
|
2009-12-18 20:47:44 +00:00
|
|
|
|
2012-03-27 18:37:04 -07:00
|
|
|
# entry_link = entry.get('link') or ''
|
|
|
|
# protocol_index = entry_link.find("://")
|
|
|
|
# if protocol_index != -1:
|
|
|
|
# entry['link'] = (entry_link[:protocol_index+3]
|
|
|
|
# + urlquote(entry_link[protocol_index+3:]))
|
|
|
|
# else:
|
|
|
|
# entry['link'] = urlquote(entry_link)
|
2010-12-10 15:58:21 -05:00
|
|
|
if isinstance(entry.get('guid'), dict):
|
|
|
|
entry['guid'] = unicode(entry['guid'])
|
2011-11-25 10:38:39 -05:00
|
|
|
|
|
|
|
# Normalize story content/summary
|
2011-11-25 00:08:17 -05:00
|
|
|
if entry.get('content'):
|
2012-03-27 18:37:04 -07:00
|
|
|
entry['story_content'] = entry['content'][0].get('value', '').strip()
|
2011-11-25 10:38:39 -05:00
|
|
|
else:
|
2012-08-02 11:04:11 -07:00
|
|
|
summary = entry.get('summary') or ''
|
|
|
|
entry['story_content'] = summary.strip()
|
2011-11-25 10:38:39 -05:00
|
|
|
|
|
|
|
# Add each media enclosure as a Download link
|
2012-03-26 17:04:35 -07:00
|
|
|
for media_content in chain(entry.get('media_content', [])[:5], entry.get('links', [])[:5]):
|
2011-11-25 10:38:39 -05:00
|
|
|
media_url = media_content.get('url', '')
|
|
|
|
media_type = media_content.get('type', '')
|
2012-02-24 14:39:23 -08:00
|
|
|
if media_url and media_type and entry['story_content'] and media_url not in entry['story_content']:
|
2012-01-14 17:13:05 -08:00
|
|
|
media_type_name = media_type.split('/')[0]
|
2011-11-25 10:38:39 -05:00
|
|
|
if 'audio' in media_type and media_url:
|
|
|
|
entry['story_content'] += """<br><br>
|
2012-03-05 12:57:47 -08:00
|
|
|
<audio controls="controls" preload="none">
|
2011-11-25 10:38:39 -05:00
|
|
|
<source src="%(media_url)s" type="%(media_type)s" />
|
|
|
|
</audio>""" % {
|
|
|
|
'media_url': media_url,
|
|
|
|
'media_type': media_type
|
|
|
|
}
|
|
|
|
elif 'image' in media_type and media_url:
|
|
|
|
entry['story_content'] += """<br><br><img src="%s" />""" % media_url
|
2012-01-15 19:17:17 -08:00
|
|
|
continue
|
2011-12-14 23:26:07 -08:00
|
|
|
elif media_content.get('rel') == 'alternative' or 'text' in media_content.get('type'):
|
|
|
|
continue
|
2012-01-14 17:13:05 -08:00
|
|
|
elif media_type_name in ['application']:
|
|
|
|
continue
|
2011-11-25 10:38:39 -05:00
|
|
|
entry['story_content'] += """<br><br>
|
|
|
|
Download %(media_type)s: <a href="%(media_url)s">%(media_url)s</a>""" % {
|
2012-01-14 17:13:05 -08:00
|
|
|
'media_type': media_type_name,
|
2011-11-25 00:26:30 -05:00
|
|
|
'media_url': media_url,
|
|
|
|
}
|
2011-11-24 15:58:04 -05:00
|
|
|
|
|
|
|
entry['guid'] = entry.get('guid') or entry.get('id') or entry.get('link') or str(entry.get('published'))
|
2012-04-17 11:48:28 -07:00
|
|
|
|
|
|
|
if not entry.get('title') and entry.get('story_content'):
|
|
|
|
story_title = strip_tags(entry['story_content'])
|
|
|
|
if len(story_title) > 80:
|
|
|
|
story_title = story_title[:80] + '...'
|
|
|
|
entry['title'] = story_title
|
2011-11-24 15:58:04 -05:00
|
|
|
|
2012-07-21 16:38:37 -07:00
|
|
|
entry['title'] = strip_tags(entry.get('title'))
|
|
|
|
entry['author'] = strip_tags(entry.get('author'))
|
|
|
|
|
2011-01-07 16:26:17 -05:00
|
|
|
return entry
|
|
|
|
|
|
|
|
class bunch(dict):
|
|
|
|
"""Example of overloading __getatr__ and __setattr__
|
|
|
|
This example creates a dictionary where members can be accessed as attributes
|
|
|
|
"""
|
|
|
|
def __init__(self, indict=None, attribute=None):
|
|
|
|
if indict is None:
|
|
|
|
indict = {}
|
|
|
|
# set any attributes here - before initialisation
|
|
|
|
# these remain as normal attributes
|
|
|
|
self.attribute = attribute
|
|
|
|
dict.__init__(self, indict)
|
|
|
|
self.__initialised = True
|
|
|
|
# after initialisation, setting attributes is the same as setting an item
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
"""Maps values to attributes.
|
|
|
|
Only called if there *isn't* an attribute with this name
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.__getitem__(item)
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __setattr__(self, item, value):
|
|
|
|
"""Maps attributes to values.
|
|
|
|
Only if we are initialised
|
|
|
|
"""
|
|
|
|
if not self.__dict__.has_key('_bunch__initialised'): # this test allows attributes to be set in the __init__ method
|
|
|
|
return dict.__setattr__(self, item, value)
|
|
|
|
elif self.__dict__.has_key(item): # any normal attributes are handled normally
|
|
|
|
dict.__setattr__(self, item, value)
|
|
|
|
else:
|
2012-04-17 11:48:28 -07:00
|
|
|
self.__setitem__(item, value)
|
|
|
|
|
|
|
|
class MLStripper(HTMLParser):
|
|
|
|
def __init__(self):
|
|
|
|
self.reset()
|
|
|
|
self.fed = []
|
|
|
|
def handle_data(self, d):
|
|
|
|
self.fed.append(d)
|
|
|
|
def get_data(self):
|
|
|
|
return ' '.join(self.fed)
|
|
|
|
|
|
|
|
def strip_tags(html):
|
2012-07-21 16:38:37 -07:00
|
|
|
if not html:
|
|
|
|
return ''
|
|
|
|
return strip_tags_django(html)
|
|
|
|
|
2012-04-17 11:48:28 -07:00
|
|
|
s = MLStripper()
|
|
|
|
s.feed(html)
|
2012-06-27 23:57:57 -07:00
|
|
|
return s.get_data()
|
2012-07-21 16:38:37 -07:00
|
|
|
|
|
|
|
def linkify(*args, **kwargs):
|
2012-07-23 23:23:34 -07:00
|
|
|
return xhtml_unescape_tornado(linkify_tornado(*args, **kwargs))
|
2012-06-27 23:57:57 -07:00
|
|
|
|
|
|
|
def truncate_chars(value, max_length):
|
|
|
|
if len(value) <= max_length:
|
|
|
|
return value
|
|
|
|
|
|
|
|
truncd_val = value[:max_length]
|
|
|
|
if value[max_length] != " ":
|
|
|
|
rightmost_space = truncd_val.rfind(" ")
|
|
|
|
if rightmost_space != -1:
|
|
|
|
truncd_val = truncd_val[:rightmost_space]
|
|
|
|
|
2012-09-10 17:41:01 -07:00
|
|
|
return truncd_val + "..."
|
|
|
|
|
|
|
|
def image_size(datastream):
|
|
|
|
datastream = reseekfile.ReseekFile(datastream)
|
|
|
|
data = str(datastream.read(30))
|
|
|
|
size = len(data)
|
|
|
|
height = -1
|
|
|
|
width = -1
|
|
|
|
content_type = ''
|
|
|
|
|
|
|
|
# handle GIFs
|
|
|
|
if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):
|
|
|
|
# Check to see if content_type is correct
|
|
|
|
content_type = 'image/gif'
|
|
|
|
w, h = struct.unpack("<HH", data[6:10])
|
|
|
|
width = int(w)
|
|
|
|
height = int(h)
|
|
|
|
|
|
|
|
# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
|
|
|
|
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
|
|
|
|
# and finally the 4-byte width, height
|
|
|
|
elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')
|
|
|
|
and (data[12:16] == 'IHDR')):
|
|
|
|
content_type = 'image/png'
|
|
|
|
w, h = struct.unpack(">LL", data[16:24])
|
|
|
|
width = int(w)
|
|
|
|
height = int(h)
|
|
|
|
|
|
|
|
# Maybe this is for an older PNG version.
|
|
|
|
elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):
|
|
|
|
# Check to see if we have the right content type
|
|
|
|
content_type = 'image/png'
|
|
|
|
w, h = struct.unpack(">LL", data[8:16])
|
|
|
|
width = int(w)
|
|
|
|
height = int(h)
|
|
|
|
|
|
|
|
# handle JPEGs
|
|
|
|
elif (size >= 2) and data.startswith('\377\330'):
|
|
|
|
content_type = 'image/jpeg'
|
|
|
|
datastream.seek(0)
|
|
|
|
datastream.read(2)
|
|
|
|
b = datastream.read(1)
|
|
|
|
try:
|
|
|
|
while (b and ord(b) != 0xDA):
|
|
|
|
while (ord(b) != 0xFF): b = datastream.read(1)
|
|
|
|
while (ord(b) == 0xFF): b = datastream.read(1)
|
|
|
|
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
|
|
|
|
datastream.read(3)
|
|
|
|
h, w = struct.unpack(">HH", datastream.read(4))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
datastream.read(int(struct.unpack(">H", datastream.read(2))[0])-2)
|
|
|
|
b = datastream.read(1)
|
|
|
|
width = int(w)
|
|
|
|
height = int(h)
|
|
|
|
except struct.error:
|
|
|
|
pass
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return content_type, width, height
|
|
|
|
|
2012-10-01 19:04:09 -07:00
|
|
|
def htmldiff(old_html, new_html):
|
2012-10-01 19:31:33 -07:00
|
|
|
try:
|
|
|
|
old_html_tokens = tokenize(old_html, include_hrefs=False)
|
|
|
|
new_html_tokens = tokenize(new_html, include_hrefs=False)
|
|
|
|
except (KeyError, ParserError):
|
|
|
|
return new_html
|
|
|
|
|
2012-10-01 19:04:09 -07:00
|
|
|
result = htmldiff_tokens(old_html_tokens, new_html_tokens)
|
|
|
|
result = ''.join(result).strip()
|
|
|
|
|
|
|
|
return fixup_ins_del_tags(result)
|