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 import json_functions as json
from utils.feed_functions import relative_timesince, chunks from utils.feed_functions import relative_timesince, chunks
from utils.story_functions import truncate_chars, strip_tags, linkify, image_size 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.scrubber import SelectiveScriptScrubber
from utils import s3_utils from utils import s3_utils
from StringIO import StringIO from StringIO import StringIO
@ -2191,7 +2192,12 @@ class MSharedStory(mongo.Document):
if any(ignore in image_source for ignore in IGNORE_IMAGE_SOURCES): if any(ignore in image_source for ignore in IGNORE_IMAGE_SOURCES):
continue continue
req = requests.get(image_source, headers=headers, stream=True) req = requests.get(image_source, headers=headers, stream=True)
datastream = StringIO(req.content[:30]) 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) _, width, height = image_size(datastream)
if width <= 16 or height <= 16: if width <= 16 or height <= 16:
continue continue
@ -2204,7 +2210,8 @@ class MSharedStory(mongo.Document):
self.image_count = len(image_sizes) self.image_count = len(image_sizes)
self.save() 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 return image_sizes
@ -2652,6 +2659,7 @@ class MSocialServices(mongo.Document):
def post_to_twitter(self, shared_story): def post_to_twitter(self, shared_story):
message = shared_story.generate_post_to_service_message(truncate=140) message = shared_story.generate_post_to_service_message(truncate=140)
shared_story.calculate_image_sizes()
try: try:
api = self.twitter_api() api = self.twitter_api()

View file

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

View file

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