<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> __author__ = '<STR_LIT>' <EOL> import logging <EOL> import os <EOL> import urllib <EOL> import time <EOL> import gdata . photos <EOL> from gdata . photos . service import PhotosService , GooglePhotosException <EOL> import googlecl <EOL> import googlecl . base <EOL> import googlecl . service <EOL> import googlecl . picasa <EOL> import googlecl . calendar . date <EOL> safe_encode = googlecl . safe_encode <EOL> safe_decode = googlecl . safe_decode <EOL> LOG = logging . getLogger ( googlecl . picasa . LOGGER_NAME ) <EOL> SUPPORTED_VIDEO_TYPES = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , } <EOL> gdata . photos . service . SUPPORTED_UPLOAD_TYPES += tuple ( set ( [ vtype . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> for vtype in SUPPORTED_VIDEO_TYPES . values ( ) ] ) ) <EOL> DOWNLOAD_VIDEO_TYPES = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , } <EOL> class PhotosServiceCL ( PhotosService , googlecl . service . BaseServiceCL ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> """<STR_LIT>""" <EOL> PhotosService . __init__ ( self ) <EOL> googlecl . service . BaseServiceCL . __init__ ( self , <EOL> googlecl . picasa . SECTION_HEADER , <EOL> config ) <EOL> def build_entry_list ( self , user = '<STR_LIT:default>' , titles = None , query = None , <EOL> force_photos = False , photo_title = None ) : <EOL> """<STR_LIT>""" <EOL> album_entry = [ ] <EOL> if titles [ <NUM_LIT:0> ] or not ( titles [ <NUM_LIT:0> ] or query ) : <EOL> album_entry = self . GetAlbum ( user = user , titles = titles ) <EOL> if photo_title or query or force_photos : <EOL> uri = '<STR_LIT>' + user <EOL> if query and not album_entry : <EOL> entries = self . GetEntries ( <EOL> uri + '<STR_LIT>' + query , photo_title ) <EOL> else : <EOL> entries = [ ] <EOL> uri += '<STR_LIT>' <EOL> if query : <EOL> uri += '<STR_LIT>' + query <EOL> for album in album_entry : <EOL> photo_entries = self . GetEntries ( uri % album . gphoto_id . text , <EOL> photo_title ) <EOL> entries . extend ( photo_entries ) <EOL> else : <EOL> entries = album_entry <EOL> return entries <EOL> def create_album ( self , title , summary , access , date ) : <EOL> """<STR_LIT>""" <EOL> if date : <EOL> parser = googlecl . calendar . date . DateParser ( ) <EOL> date = parser . determine_day ( date , shift_dates = False ) <EOL> if date : <EOL> timestamp = time . mktime ( date . timetuple ( ) ) <EOL> timestamp_ms = '<STR_LIT>' % int ( ( timestamp * <NUM_LIT:1000> ) ) <EOL> else : <EOL> LOG . error ( '<STR_LIT>' % <EOL> date ) <EOL> timestamp_ms = '<STR_LIT>' <EOL> else : <EOL> timestamp_ms = '<STR_LIT>' <EOL> access = googlecl . picasa . _map_access_string ( access ) <EOL> return self . InsertAlbum ( title = title , summary = summary , <EOL> access = access , <EOL> timestamp = timestamp_ms ) <EOL> CreateAlbum = create_album <EOL> def download_album ( self , base_path , user , video_format = '<STR_LIT>' , titles = None , <EOL> photo_title = None ) : <EOL> """<STR_LIT>""" <EOL> def _get_download_info ( photo_or_video , video_format ) : <EOL> """<STR_LIT>""" <EOL> wanted_content = None <EOL> for content in photo_or_video . media . content : <EOL> if content . medium == '<STR_LIT:image>' and not wanted_content : <EOL> wanted_content = content <EOL> elif content . type == DOWNLOAD_VIDEO_TYPES [ video_format ] : <EOL> wanted_content = content <EOL> if not wanted_content : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> LOG . debug ( '<STR_LIT>' + photo_or_video . media ) <EOL> return None <EOL> elif wanted_content . medium == '<STR_LIT:image>' : <EOL> url = googlecl . picasa . make_download_url ( <EOL> photo_or_video . content . src ) <EOL> mimetype = photo_or_video . content . type <EOL> extension = mimetype . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> else : <EOL> url = wanted_content . url <EOL> extension = video_format <EOL> return ( url , extension ) <EOL> if not user : <EOL> user = '<STR_LIT:default>' <EOL> entries = self . GetAlbum ( user = user , titles = titles ) <EOL> if video_format not in DOWNLOAD_VIDEO_TYPES . keys ( ) : <EOL> LOG . error ( '<STR_LIT>' + video_format ) <EOL> LOG . info ( '<STR_LIT>' + <EOL> str ( DOWNLOAD_VIDEO_TYPES . keys ( ) ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> video_format = '<STR_LIT>' <EOL> LOG . info ( '<STR_LIT>' + video_format ) <EOL> for album in entries : <EOL> album_path = os . path . join ( base_path , safe_decode ( album . title . text ) ) <EOL> album_concat = <NUM_LIT:1> <EOL> if os . path . exists ( album_path ) : <EOL> base_album_path = album_path <EOL> while os . path . exists ( album_path ) : <EOL> album_path = base_album_path + '<STR_LIT>' % album_concat <EOL> album_concat += <NUM_LIT:1> <EOL> os . makedirs ( album_path ) <EOL> uri = ( '<STR_LIT>' % <EOL> ( user , album . gphoto_id . text ) ) <EOL> photo_entries = self . GetEntries ( uri , photo_title ) <EOL> for photo_or_video in photo_entries : <EOL> photo_or_video_name = safe_decode ( photo_or_video . title . text ) <EOL> photo_or_video_name = photo_or_video_name . split ( os . extsep ) [ <NUM_LIT:0> ] <EOL> url , extension = _get_download_info ( <EOL> photo_or_video , video_format ) <EOL> path = os . path . join ( album_path , <EOL> photo_or_video_name + os . extsep + extension ) <EOL> if os . path . exists ( path ) : <EOL> base_path = path <EOL> photo_concat = <NUM_LIT:1> <EOL> while os . path . exists ( path ) : <EOL> path = base_path + '<STR_LIT>' % photo_concat <EOL> photo_concat += <NUM_LIT:1> <EOL> LOG . info ( safe_encode ( '<STR_LIT>' % <EOL> ( safe_decode ( photo_or_video . title . text ) , path ) ) ) <EOL> urllib . urlretrieve ( url , path ) <EOL> DownloadAlbum = download_album <EOL> def get_album ( self , user = '<STR_LIT:default>' , titles = None ) : <EOL> """<STR_LIT>""" <EOL> uri = '<STR_LIT>' + user + '<STR_LIT>' <EOL> return self . GetEntries ( uri , titles ) <EOL> GetAlbum = get_album <EOL> def get_single_album ( self , user = '<STR_LIT:default>' , title = None ) : <EOL> """<STR_LIT>""" <EOL> uri = '<STR_LIT>' + user + '<STR_LIT>' <EOL> return self . GetSingleEntry ( uri , title = title ) <EOL> GetSingleAlbum = get_single_album <EOL> def insert_media_list ( self , album , media_list , tags = '<STR_LIT>' , user = '<STR_LIT:default>' , <EOL> photo_name = None , caption = None ) : <EOL> """<STR_LIT>""" <EOL> album_url = ( '<STR_LIT>' % <EOL> ( user , album . gphoto_id . text ) ) <EOL> keywords = tags <EOL> if caption is None : <EOL> caption = '<STR_LIT>' <EOL> failures = [ ] <EOL> for path in media_list : <EOL> LOG . info ( safe_encode ( '<STR_LIT>' + path + '<STR_LIT>' + <EOL> safe_decode ( album . title . text ) ) ) <EOL> ext = googlecl . get_extension_from_path ( path ) <EOL> if not ext : <EOL> LOG . debug ( '<STR_LIT>' + path ) <EOL> content_type = '<STR_LIT>' <EOL> else : <EOL> ext = ext . lower ( ) <EOL> try : <EOL> content_type = SUPPORTED_VIDEO_TYPES [ ext ] <EOL> except KeyError : <EOL> content_type = '<STR_LIT>' + ext . lower ( ) <EOL> title = photo_name <EOL> if not title : <EOL> title = os . path . split ( path ) [ <NUM_LIT:1> ] <EOL> try : <EOL> self . InsertPhotoSimple ( album_url , <EOL> title = title , <EOL> summary = caption , <EOL> filename_or_handle = path , <EOL> keywords = keywords , <EOL> content_type = content_type ) <EOL> except GooglePhotosException , err : <EOL> LOG . error ( '<STR_LIT>' , path , <EOL> err . args [ <NUM_LIT:0> ] , <EOL> err . args [ <NUM_LIT:1> ] ) <EOL> failures . append ( file ) <EOL> except Exception , err : <EOL> LOG . error ( safe_encode ( '<STR_LIT>' + unicode ( err ) ) ) <EOL> failures . append ( file ) <EOL> if failures : <EOL> LOG . info ( str ( len ( failures ) ) + '<STR_LIT>' ) <EOL> LOG . debug ( safe_encode ( '<STR_LIT>' + unicode ( failures ) ) ) <EOL> return failures <EOL> InsertMediaList = insert_media_list <EOL> def is_token_valid ( self , test_uri = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return googlecl . base . BaseCL . IsTokenValid ( self , test_uri ) <EOL> IsTokenValid = is_token_valid <EOL> def tag_photos ( self , photo_entries , tags , caption ) : <EOL> """<STR_LIT>""" <EOL> from gdata . media import Group , Keywords <EOL> from atom import Summary <EOL> if tags is not None : <EOL> remove_set , add_set , replace_tags = googlecl . base . generate_tag_sets ( <EOL> tags ) <EOL> for photo in photo_entries : <EOL> if tags is not None : <EOL> if not photo . media : <EOL> photo . media = Group ( ) <EOL> if not photo . media . keywords : <EOL> photo . media . keywords = Keywords ( ) <EOL> if photo . media . keywords . text and remove_set and not replace_tags : <EOL> current_tags = photo . media . keywords . text . replace ( '<STR_LIT:U+002CU+0020>' , '<STR_LIT:U+002C>' ) <EOL> current_set = set ( current_tags . split ( '<STR_LIT:U+002C>' ) ) <EOL> photo . media . keywords . text = '<STR_LIT:U+002C>' . join ( <EOL> current_set - remove_set ) <EOL> if replace_tags or not photo . media . keywords . text : <EOL> photo . media . keywords . text = '<STR_LIT:U+002C>' . join ( add_set ) <EOL> elif add_set : <EOL> photo . media . keywords . text += '<STR_LIT:U+002C>' + '<STR_LIT:U+002C>' . join ( add_set ) <EOL> if caption is not None : <EOL> if not photo . summary : <EOL> photo . summary = Summary ( text = caption , summary_type = '<STR_LIT:text>' ) <EOL> else : <EOL> photo . summary . text = caption <EOL> self . UpdatePhotoMetadata ( photo ) <EOL> TagPhotos = tag_photos <EOL> SERVICE_CLASS = PhotosServiceCL </s>
