<s> """<STR_LIT>""" <EOL> import hashlib <EOL> import os <EOL> import urllib2 <EOL> from PIL import Image <EOL> from goose . utils . encoding import smart_str <EOL> from goose . images . image import ImageDetails <EOL> from goose . images . image import LocallyStoredImage <EOL> class ImageUtils ( object ) : <EOL> @ classmethod <EOL> def get_image_dimensions ( self , identify_program , path ) : <EOL> image = Image . open ( path ) <EOL> image_details = ImageDetails ( ) <EOL> image_details . set_mime_type ( image . format ) <EOL> width , height = image . size <EOL> image_details . set_width ( width ) <EOL> image_details . set_height ( height ) <EOL> return image_details <EOL> @ classmethod <EOL> def store_image ( self , http_client , link_hash , src , config ) : <EOL> """<STR_LIT>""" <EOL> image = self . read_localfile ( link_hash , src , config ) <EOL> if image : <EOL> return image <EOL> data = self . fetch ( http_client , src ) <EOL> if data : <EOL> image = self . write_localfile ( data , link_hash , src , config ) <EOL> if image : <EOL> return image <EOL> return None <EOL> @ classmethod <EOL> def get_mime_type ( self , image_details ) : <EOL> mime_type = image_details . get_mime_type ( ) . lower ( ) <EOL> mimes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return mimes . get ( mime_type , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def read_localfile ( self , link_hash , src , config ) : <EOL> local_image_name = self . get_localfile_name ( link_hash , src , config ) <EOL> if os . path . isfile ( local_image_name ) : <EOL> identify = config . imagemagick_identify_path <EOL> image_details = self . get_image_dimensions ( identify , local_image_name ) <EOL> file_extension = self . get_mime_type ( image_details ) <EOL> bytes = os . path . getsize ( local_image_name ) <EOL> return LocallyStoredImage ( <EOL> src = src , <EOL> local_filename = local_image_name , <EOL> link_hash = link_hash , <EOL> bytes = bytes , <EOL> file_extension = file_extension , <EOL> height = image_details . get_height ( ) , <EOL> width = image_details . get_width ( ) <EOL> ) <EOL> return None <EOL> @ classmethod <EOL> def write_localfile ( self , entity , link_hash , src , config ) : <EOL> local_path = self . get_localfile_name ( link_hash , src , config ) <EOL> f = open ( local_path , '<STR_LIT:wb>' ) <EOL> f . write ( entity ) <EOL> f . close ( ) <EOL> return self . read_localfile ( link_hash , src , config ) <EOL> @ classmethod <EOL> def get_localfile_name ( self , link_hash , src , config ) : <EOL> image_hash = hashlib . md5 ( smart_str ( src ) ) . hexdigest ( ) <EOL> return os . path . join ( config . local_storage_path , '<STR_LIT>' % ( link_hash , image_hash ) ) <EOL> @ classmethod <EOL> def clean_src_string ( self , src ) : <EOL> return src . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def fetch ( self , http_client , src ) : <EOL> try : <EOL> req = urllib2 . Request ( src ) <EOL> f = urllib2 . urlopen ( req ) <EOL> data = f . read ( ) <EOL> return data <EOL> except : <EOL> return None </s>
