StringIO -> BytesIO

This commit is contained in:
Samuel Clay 2020-07-01 11:57:51 -04:00
parent 7279171e4c
commit bc7055b971
5 changed files with 15 additions and 14 deletions

View file

@ -2,7 +2,6 @@ import datetime
import mongoengine as mongo
import pickle
import base64
from io import StringIO
from oauth2client.client import Error as OAuthError
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
from lxml import etree

View file

@ -4,17 +4,17 @@ import numpy
import scipy
import scipy.misc
import scipy.cluster
import urllib.parse
import struct
import operator
import gzip
import datetime
import requests
import codecs
import http.client
from PIL import BmpImagePlugin, PngImagePlugin, Image
from socket import error as SocketError
from boto.s3.key import Key
from io import StringIO
from io import BytesIO
from django.conf import settings
from apps.rss_feeds.models import MFeedPage, MFeedIcon
from utils.facebook_fetcher import FacebookFetcher
@ -203,7 +203,7 @@ class IconImporter(object):
elif settings.BACKED_BY_AWS.get('pages_on_s3') and self.feed.s3_page:
key = settings.S3_CONN.get_bucket(settings.S3_PAGES_BUCKET_NAME).get_key(self.feed.s3_pages_key)
compressed_content = key.get_contents_as_string()
stream = StringIO(compressed_content)
stream = BytesIO(compressed_content)
gz = gzip.GzipFile(fileobj=stream)
try:
content = gz.read()
@ -299,7 +299,7 @@ class IconImporter(object):
return None, None
try:
icon_file = StringIO(icon)
icon_file = BytesIO(icon)
image = Image.open(icon_file)
except (IOError, ValueError):
return None, None
@ -381,12 +381,14 @@ class IconImporter(object):
# Find the most frequent color, based on the counts.
index_max = scipy.argmax(counts)
peak = codes.astype(int)[index_max]
color = ''.join(chr(c) for c in peak).encode('hex')
print(f" ---> Color: {peak}")
color = codecs.decode(''.join(chr(c) for c in peak), 'hex')
print(f" ---> Color: {color} {peak}")
return color[:6]
def string_from_image(self, image):
output = StringIO()
output = BytesIO()
image.save(output, 'png', quality=95)
contents = output.getvalue()
output.close()

View file

@ -37,7 +37,7 @@ from utils.story_functions import truncate_chars, strip_tags, linkify, image_siz
from utils.image_functions import ImageOps
from utils.scrubber import SelectiveScriptScrubber
from utils import s3_utils
from io import StringIO
from io import BytesIO
try:
from apps.social.spam import detect_spammers
@ -2328,11 +2328,11 @@ class MSharedStory(mongo.DynamicDocument):
continue
req = requests.get(image_source, headers=headers, stream=True)
try:
datastream = StringIO(req.content)
datastream = BytesIO(req.content)
width, height = ImageOps.image_size(datastream)
except IOError as e:
logging.debug(" ***> Couldn't read image: %s / %s" % (e, image_source))
datastream = StringIO(req.content[:100])
datastream = BytesIO(req.content[:100])
_, width, height = image_size(datastream)
# if width <= 16 or height <= 16:
# continue

View file

@ -919,7 +919,7 @@ class Dispatcher:
@timelimit(10)
def calculate_feed_scores_with_stories(self, user_subs, stories):
for sub in user_subs:
silent = False if self.options['verbose'] >= 2 else True
silent = False if getattr(self.options, 'verbose', 0) >= 2 else True
sub.calculate_feed_scores(silent=silent, stories=stories)
def add_jobs(self, feeds_queue, feeds_count=1):

View file

@ -3,7 +3,7 @@
from PIL import Image
from PIL import ImageOps as PILOps
from PIL.ExifTags import TAGS
from io import StringIO
from io import BytesIO
from vendor import reseekfile
PROFILE_PICTURE_SIZES = {
@ -22,7 +22,7 @@ class ImageOps:
This must happen in this function because PIL is transforming the
original as it works."""
image_file = StringIO(image_body)
image_file = BytesIO(image_body)
try:
image = Image.open(image_file)
except IOError:
@ -42,7 +42,7 @@ class ImageOps:
method=Image.ANTIALIAS,
centering=(0.5, 0.5))
output = StringIO()
output = BytesIO()
if format.lower() == 'jpg':
format = 'jpeg'
image.save(output, format=format, quality=95)