<s> from __future__ import unicode_literals <EOL> from __future__ import print_function <EOL> import time <EOL> import hashlib <EOL> import random <EOL> import math <EOL> import decimal <EOL> from fractions import Fraction <EOL> import os <EOL> import mimetypes <EOL> from PIL import Image <EOL> from PIL . ExifTags import TAGS <EOL> import datetime <EOL> import logging <EOL> from dateutil . parser import parse <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ExifData : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def takedate ( self ) : <EOL> return self . _takedate <EOL> @ takedate . setter <EOL> def takedate ( self , value ) : <EOL> self . _takedate = value . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) <EOL> iso = "<STR_LIT>" <EOL> make = "<STR_LIT>" <EOL> model = "<STR_LIT>" <EOL> shutter = None <EOL> aperture = None <EOL> exposure = None <EOL> focal = None <EOL> _takedate = None <EOL> taketime = None <EOL> orientation = <NUM_LIT:1> <EOL> def __str__ ( self ) : <EOL> res = "<STR_LIT>" <EOL> res += "<STR_LIT>" + str ( self . iso ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . aperture ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . make ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . model ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . shutter ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . exposure ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . focal ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . takedate ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . taketime ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . orientation ) + "<STR_LIT:\n>" <EOL> return res <EOL> class LycheePhoto : <EOL> """<STR_LIT>""" <EOL> originalname = "<STR_LIT>" <EOL> originalpath = "<STR_LIT>" <EOL> id = "<STR_LIT>" <EOL> albumname = "<STR_LIT>" <EOL> albumid = "<STR_LIT>" <EOL> thumbnailfullpath = "<STR_LIT>" <EOL> thumbnailx2fullpath = "<STR_LIT>" <EOL> title = "<STR_LIT>" <EOL> description = "<STR_LIT>" <EOL> url = "<STR_LIT>" <EOL> public = <NUM_LIT:0> <EOL> type = "<STR_LIT>" <EOL> width = <NUM_LIT:0> <EOL> height = <NUM_LIT:0> <EOL> size = "<STR_LIT>" <EOL> star = <NUM_LIT:0> <EOL> thumbUrl = "<STR_LIT>" <EOL> srcfullpath = "<STR_LIT>" <EOL> destfullpath = "<STR_LIT>" <EOL> exif = None <EOL> _str_datetime = None <EOL> checksum = "<STR_LIT>" <EOL> def convert_strdate_to_timestamp ( self , value ) : <EOL> timestamp = None <EOL> epoch_now = int ( time . time ( ) ) <EOL> timestamp = epoch_now <EOL> if isinstance ( value , int ) : <EOL> timestamp = value <EOL> elif isinstance ( value , datetime . date ) : <EOL> timestamp = ( value - datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) . total_seconds ( ) <EOL> elif value : <EOL> value = str ( value ) <EOL> try : <EOL> the_date = parse ( value ) <EOL> timestamp = time . mktime ( the_date . timetuple ( ) ) <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' + str ( value ) ) <EOL> timestamp = epoch_now <EOL> else : <EOL> timestamp = epoch_now <EOL> return timestamp <EOL> @ property <EOL> def epoch_sysdate ( self ) : <EOL> return self . convert_strdate_to_timestamp ( self . _str_datetime ) <EOL> def __generateHash ( self ) : <EOL> sha1 = hashlib . sha1 ( ) <EOL> with open ( self . srcfullpath , '<STR_LIT:rb>' ) as f : <EOL> sha1 . update ( f . read ( ) ) <EOL> self . checksum = sha1 . hexdigest ( ) <EOL> def __init__ ( self , conf , photoname , album ) : <EOL> self . conf = conf <EOL> self . originalname = photoname <EOL> self . originalpath = album [ '<STR_LIT:path>' ] <EOL> self . albumid = album [ '<STR_LIT:id>' ] <EOL> self . albumname = album [ '<STR_LIT:name>' ] <EOL> if ( '<STR_LIT>' in self . originalname ) or ( '<STR_LIT>' in self . originalname ) : <EOL> self . star = <NUM_LIT:1> <EOL> self . id = str ( int ( time . time ( ) ) ) <EOL> length = len ( self . id ) <EOL> if length < <NUM_LIT> : <EOL> missing_char = <NUM_LIT> - length <EOL> r = random . random ( ) <EOL> r = str ( r ) <EOL> filler = r [ - missing_char : ] <EOL> self . id = self . id + filler <EOL> assert len ( self . id ) == <NUM_LIT> , "<STR_LIT>" . format ( self . id , str ( len ( self . id ) ) ) <EOL> m = hashlib . md5 ( ) <EOL> m . update ( self . id . encode ( '<STR_LIT:utf-8>' ) ) <EOL> crypted = m . hexdigest ( ) <EOL> ext = os . path . splitext ( photoname ) [ <NUM_LIT:1> ] <EOL> self . url = '<STR_LIT>' . join ( [ crypted , ext ] ) . lower ( ) <EOL> self . thumbUrl = self . url <EOL> self . srcfullpath = os . path . join ( self . originalpath , self . originalname ) <EOL> self . destfullpath = os . path . join ( self . conf [ "<STR_LIT>" ] , "<STR_LIT>" , "<STR_LIT>" , self . url ) <EOL> self . __generateHash ( ) <EOL> self . type = mimetypes . guess_type ( self . originalname , False ) [ <NUM_LIT:0> ] <EOL> self . size = os . path . getsize ( self . srcfullpath ) <EOL> self . size = str ( self . size / <NUM_LIT> ) + "<STR_LIT>" <EOL> takedate = datetime . date . today ( ) . isoformat ( ) <EOL> taketime = datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> self . _str_datetime = takedate + "<STR_LIT:U+0020>" + taketime <EOL> self . exif = ExifData ( ) <EOL> try : <EOL> img = Image . open ( self . srcfullpath ) <EOL> w , h = img . size <EOL> self . width = float ( w ) <EOL> self . height = float ( h ) <EOL> if hasattr ( img , '<STR_LIT>' ) : <EOL> try : <EOL> exifinfo = img . _getexif ( ) <EOL> except Exception as e : <EOL> exifinfo = None <EOL> logger . warn ( '<STR_LIT>' , e ) <EOL> if exifinfo is not None : <EOL> for tag , value in exifinfo . items ( ) : <EOL> decode = TAGS . get ( tag , tag ) <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . orientation = value <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . make = value <EOL> if decode == "<STR_LIT>" : <EOL> aperture = math . sqrt ( <NUM_LIT:2> ) ** value [ <NUM_LIT:0> ] <EOL> try : <EOL> aperture = decimal . Decimal ( aperture ) . quantize ( <EOL> decimal . Decimal ( '<STR_LIT>' ) , <EOL> rounding = decimal . ROUND_05UP ) <EOL> except Exception as e : <EOL> logger . debug ( "<STR_LIT>" . format ( aperture ) ) <EOL> logger . debug ( e ) <EOL> self . exif . aperture = aperture <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . focal = value [ <NUM_LIT:0> ] <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . iso = value [ <NUM_LIT:0> ] <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . model = value <EOL> if decode == "<STR_LIT>" : <EOL> self . exif . exposure = value [ <NUM_LIT:0> ] <EOL> if decode == "<STR_LIT>" : <EOL> s = value [ <NUM_LIT:0> ] <EOL> s = <NUM_LIT:2> ** s <EOL> s = decimal . Decimal ( s ) . quantize ( decimal . Decimal ( '<STR_LIT:1>' ) , rounding = decimal . ROUND_05UP ) <EOL> if s <= <NUM_LIT:1> : <EOL> s = decimal . Decimal ( <EOL> <NUM_LIT:1> / <EOL> float ( s ) ) . quantize ( <EOL> decimal . Decimal ( '<STR_LIT>' ) , <EOL> rounding = decimal . ROUND_05UP ) <EOL> else : <EOL> s = "<STR_LIT>" + str ( s ) <EOL> self . exif . shutter = str ( s ) + "<STR_LIT>" <EOL> if decode == "<STR_LIT>" : <EOL> try : <EOL> self . exif . takedate = value [ <NUM_LIT:0> ] . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' + str ( value ) + '<STR_LIT>' + self . srcfullpath ) <EOL> if decode == "<STR_LIT>" : <EOL> try : <EOL> self . exif . taketime = value [ <NUM_LIT:0> ] . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:1> ] <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' + str ( value ) + '<STR_LIT>' + self . srcfullpath ) <EOL> if decode == "<STR_LIT>" and self . exif . takedate is None : <EOL> try : <EOL> self . exif . takedate = value . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' + str ( value ) + '<STR_LIT>' + self . srcfullpath ) <EOL> if decode == "<STR_LIT>" and self . exif . taketime is None : <EOL> try : <EOL> self . exif . taketime = value . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:1> ] <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' + str ( value ) + '<STR_LIT>' + self . srcfullpath ) <EOL> if not ( self . exif . shutter ) and self . exif . exposure : <EOL> if self . exif . exposure < <NUM_LIT:1> : <EOL> e = str ( Fraction ( self . exif . exposure ) . limit_denominator ( ) ) <EOL> else : <EOL> e = decimal . Decimal ( <EOL> self . exif . exposure ) . quantize ( <EOL> decimal . Decimal ( '<STR_LIT>' ) , <EOL> rounding = decimal . ROUND_05UP ) <EOL> self . exif . shutter = e <EOL> if self . exif . shutter : <EOL> self . exif . shutter = str ( self . exif . shutter ) + "<STR_LIT>" <EOL> else : <EOL> self . exif . shutter = "<STR_LIT>" <EOL> if self . exif . exposure : <EOL> self . exif . exposure = str ( self . exif . exposure ) + "<STR_LIT>" <EOL> else : <EOL> self . exif . exposure = "<STR_LIT>" <EOL> if self . exif . focal : <EOL> self . exif . focal = str ( self . exif . focal ) + "<STR_LIT>" <EOL> else : <EOL> self . exif . focal = "<STR_LIT>" <EOL> if self . exif . aperture : <EOL> self . exif . aperture = '<STR_LIT:F>' + str ( self . exif . aperture ) <EOL> else : <EOL> self . exif . aperture = "<STR_LIT>" <EOL> if self . exif . takedate : <EOL> takedate = self . exif . takedate . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) <EOL> taketime = '<STR_LIT>' <EOL> if self . exif . taketime : <EOL> taketime = self . exif . taketime <EOL> self . _str_datetime = takedate + "<STR_LIT:U+0020>" + taketime <EOL> self . description = self . _str_datetime <EOL> except IOError as e : <EOL> logger . debug ( '<STR_LIT>' + self . srcfullpath ) <EOL> raise e <EOL> def __str__ ( self ) : <EOL> res = "<STR_LIT>" <EOL> res += "<STR_LIT>" + str ( self . originalname ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . originalpath ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . id ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . albumname ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . albumid ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . thumbnailfullpath ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . thumbnailx2fullpath ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . title ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . description ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . url ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . public ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . type ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . width ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . height ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . size ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . star ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . thumbUrl ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . srcfullpath ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . destfullpath ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + self . _str_datetime + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . epoch_sysdate ) + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + self . checksum + "<STR_LIT:\n>" <EOL> res += "<STR_LIT>" + str ( self . exif ) + "<STR_LIT:\n>" <EOL> return res </s>
