Fixing image size calculator for new Twitter Cards implementation.

This commit is contained in:
Samuel Clay 2015-04-07 10:47:08 -07:00
parent e6e3b9b41c
commit 487bc4afb0
3 changed files with 23 additions and 8 deletions

View file

@ -33,6 +33,7 @@ from utils import log as logging
from utils import json_functions as json
from utils.feed_functions import relative_timesince, chunks
from utils.story_functions import truncate_chars, strip_tags, linkify, image_size
from utils.image_functions import ImageOps
from utils.scrubber import SelectiveScriptScrubber
from utils import s3_utils
from StringIO import StringIO
@ -1478,7 +1479,7 @@ class MSharedStory(mongo.Document):
self.shared_date = self.shared_date or datetime.datetime.utcnow()
self.has_replies = bool(len(self.replies))
super(MSharedStory, self).save(*args, **kwargs)
author = MSocialProfile.get_user(self.user_id)
@ -2191,8 +2192,13 @@ class MSharedStory(mongo.Document):
if any(ignore in image_source for ignore in IGNORE_IMAGE_SOURCES):
continue
req = requests.get(image_source, headers=headers, stream=True)
datastream = StringIO(req.content[:30])
_, width, height = image_size(datastream)
try:
datastream = StringIO(req.content)
width, height = ImageOps.image_size(datastream)
except IOError, e:
logging.debug(" ***> Couldn't read image: %s / %s" % (e, image_source))
datastream = StringIO(req.content[:100])
_, width, height = image_size(datastream)
if width <= 16 or height <= 16:
continue
image_sizes.append({'src': image_source, 'size': (width, height)})
@ -2204,7 +2210,8 @@ class MSharedStory(mongo.Document):
self.image_count = len(image_sizes)
self.save()
logging.debug(" ---> ~SN~FGFetched image sizes on shared story: ~SB%s images" % self.image_count)
logging.debug(" ---> ~SN~FGFetched image sizes on shared story: ~SB%s/%s images" %
(self.image_count, len(image_sources)))
return image_sizes
@ -2652,6 +2659,7 @@ class MSocialServices(mongo.Document):
def post_to_twitter(self, shared_story):
message = shared_story.generate_post_to_service_message(truncate=140)
shared_story.calculate_image_sizes()
try:
api = self.twitter_api()

View file

@ -28,7 +28,7 @@
<meta property="og:image" content="{{ active_story.image_url }}">
{% endif %}
<meta property="og:title" content="{{ active_story.story_title }}">
<meta property="og:description" content="{{ active_story.story_content|striptags|addslashes|safe }}">
<meta property="og:description" content="{{ active_story.story_content|slice:":300"|striptags|addslashes|safe }}">
<meta property="article:published_time" content="{{ active_story.iso8601 }}">
<meta property="article:author" content="{{ active_story.story_authors }}">
<meta property="article:tag" content="{{ active_story.tags }}">
@ -42,8 +42,8 @@
{% endif %}
<meta name="twitter:site" content="@newsblur">
<meta name="twitter:creator" content="@{{ user_social_services.twitter_username }}">
<meta name="twitter:title" content="{{ active_story.story_image }}">
<meta name="twitter:description" content="{{ active_story.story_content|striptags|addslashes|safe }}">
<meta name="twitter:title" content="{{ active_story.story_title }}">
<meta name="twitter:description" content="{{ active_story.story_content|slice:":300"|striptags|addslashes|safe }}">
{% endif %}
{% include_stylesheets "blurblog" %}

View file

@ -4,6 +4,7 @@ from PIL import Image
from PIL import ImageOps as PILOps
from PIL.ExifTags import TAGS
from StringIO import StringIO
from vendor import reseekfile
PROFILE_PICTURE_SIZES = {
'fullsize': (256, 256),
@ -67,4 +68,10 @@ class ImageOps:
if value == 3:
image = image.rotate(180)
break
return image
return image
@classmethod
def image_size(cls, datastream):
datastream = reseekfile.ReseekFile(datastream)
image = Image.open(datastream)
return image.size