Exemple #1
0
    def _upload_or_replace_flickr(self,directory,fn,_tags=None,\
            _megapixels=None,resize_request=None):
        """Does the actual upload to flickr.
        if resize_request, will resize picture only if
        it already exists and the geometry on flickr doesn't match
        what we want,
        returns (status,replaced)"""
        # We should check here if
        db = self._loadDB(directory)

        status = False
        replaced = False

        if not _megapixels:
            mpstring = "original"
        else:
            mpstring = ("%0.1f MP" % (_megapixels))

        # If resize request, make tempfile and
        # resize.
        if _megapixels:
            fp = tempfile.NamedTemporaryFile()
            fullfile_resized = fp.name
            logger.debug("tempfile for resized is %s" % (fp.name))

        fullfile = os.path.join(directory, fn)

        ext = os.path.splitext(fullfile)[1].lower()
        # If JPEG, then resize
        if ext == '.jpg':
            isJPG = True
        else:
            isJPG = False

        # Possibly scale before uploading
        if fn not in db:
            # Do we have to resize?
            if _megapixels and isJPG:
                if pusher_utils.resize_image(fullfile, fullfile_resized,
                                             _megapixels):
                    logger.debug("%s resized to %s successfully"\
                            %(fullfile,fullfile_resized))
                    fullfile = fullfile_resized
                else:
                    logger.warning("%s couldn't resize, uploading original"\
                            %(fullfile))

            logger.debug("Upload %s to flickr, tags=%s", fn, _tags)

            print("%s - Uploading to flickr, tags[%s] size=%s"\
                    %(fn,_tags,mpstring))

            # Do the actual upload
            uplxml=self.flickr.upload(filename=fullfile,\
                    #title=_title,\
                    tags=_tags,\
                    format='etree')

            if uplxml.attrib['stat'] != 'ok':
                print("%s - flickr: upload failed with status: %s",\
                        fn,uplxml.attrib['stat'])
                status = False
                replaced = False
                return status, replaced

            pid = uplxml.find('photoid').text
            db[fn] = {}
            db[fn]['photoid'] = pid
            logger.debug("%s - flickr: uploaded with photoid %s", fn, pid)
            status = True
            replaced = False
        else:
            # File already exists, let's replace it.
            pid = db[fn]['photoid']
            # If this is an actual resize request,
            # go check geometry on flickr and resize
            # if it hasn't been already
            if resize_request and isJPG:
                if self._already_resized_on_flickr(fullfile, pid, _megapixels):
                    status = True
                    replaced = True
                    logger.info(\
                            '%s - flickr: already correct size - skipping'\
                            %(fn))
                    # File already resized, skip it.
                    return status, replaced

            # If megapixels given, will resize image
            if _megapixels and isJPG:
                if pusher_utils.resize_image(fullfile, fullfile_resized,
                                             _megapixels):
                    logger.debug("%s resized to %s successfully"\
                            %(fullfile,fullfile_resized))
                    fullfile = fullfile_resized
                else:
                    logger.warning("%s couldn't resize, uploading original"\
                            %(fullfile))

            logger.info("%s - Replace on flickr pid=%s", fn, pid)
            uplxml = self.flickr.replace(filename=fullfile, photo_id=pid)
            if uplxml.attrib['stat'] != 'ok':
                print("%s - flickr: replace failed with status: %s",\
                        uplxml.attrib['stat'])
                status = False
                replaced = False
                return status, replaced
            else:
                replaced = True
                status = True

        # We should check here if
        self._saveDB(directory, db)
        return status, replaced
Exemple #2
0
    def _upload_or_replace_flickr(self,directory,fn,_tags=None,\
            _megapixels=None,resize_request=None):
        """Does the actual upload to flickr.
        if resize_request, will resize picture only if
        it already exists and the geometry on flickr doesn't match
        what we want,
        returns (status,replaced)"""
        # We should check here if 
        db=self._loadDB(directory)

        status=False
        replaced=False

        if not _megapixels:
            mpstring="original"
        else:
            mpstring=("%0.1f MP"%(_megapixels))

        # If resize request, make tempfile and
        # resize.
        if _megapixels:
            fp = tempfile.NamedTemporaryFile()
            fullfile_resized=fp.name
            logger.debug("tempfile for resized is %s"%(fp.name))

        fullfile=os.path.join(directory,fn)

        ext=os.path.splitext(fullfile)[1].lower()
        # If JPEG, then resize
        if ext=='.jpg':
            isJPG=True
        else:
            isJPG=False
            

        # Possibly scale before uploading
        if fn not in db:
            # Do we have to resize?
            if _megapixels and isJPG:
                if pusher_utils.resize_image(fullfile,fullfile_resized,_megapixels):
                    logger.debug("%s resized to %s successfully"\
                            %(fullfile,fullfile_resized))
                    fullfile=fullfile_resized
                else:
                    logger.warning("%s couldn't resize, uploading original"\
                            %(fullfile))

            logger.debug("Upload %s to flickr, tags=%s",fn,_tags)

            print("%s - Uploading to flickr, tags[%s] size=%s"\
                    %(fn,_tags,mpstring))

            # Do the actual upload
            uplxml=self.flickr.upload(filename=fullfile,\
                    #title=_title,\
                    tags=_tags,\
                    format='etree')

            if uplxml.attrib['stat']!='ok':
                print("%s - flickr: upload failed with status: %s",\
                        fn,uplxml.attrib['stat']);
                status=False
                replaced=False
                return status,replaced

            pid=uplxml.find('photoid').text
            db[fn]={}
            db[fn]['photoid']=pid
            logger.debug("%s - flickr: uploaded with photoid %s",fn,pid);
            status=True
            replaced=False
        else:
            # File already exists, let's replace it.
            pid=db[fn]['photoid']
            # If this is an actual resize request,
            # go check geometry on flickr and resize
            # if it hasn't been already
            if resize_request and isJPG:
                if self._already_resized_on_flickr(fullfile,pid,_megapixels):
                    status=True
                    replaced=True
                    logger.info(\
                            '%s - flickr: already correct size - skipping'\
                            %(fn))
                    # File already resized, skip it.
                    return status,replaced

            # If megapixels given, will resize image
            if _megapixels and isJPG:
                if pusher_utils.resize_image(fullfile,fullfile_resized,_megapixels):
                    logger.debug("%s resized to %s successfully"\
                            %(fullfile,fullfile_resized))
                    fullfile=fullfile_resized
                else:
                    logger.warning("%s couldn't resize, uploading original"\
                            %(fullfile))
                    
            logger.info("%s - Replace on flickr pid=%s",fn,pid)
            uplxml=self.flickr.replace(filename=fullfile,photo_id=pid)
            if uplxml.attrib['stat']!='ok':
                print("%s - flickr: replace failed with status: %s",\
                        uplxml.attrib['stat']);
                status=False
                replaced=False
                return status,replaced
            else:
                replaced=True
                status=True

        # We should check here if 
        self._saveDB(directory,db)
        return status,replaced
    def _upload_or_replace_fb(self,directory,fn,_album_id,\
            _megapixels=None,resize_request=None,movealbum_request=None,\
            changetitle_request=None,_title=None):
        """Does the actual upload to fb.
        if resize_request, will resize picture only if
        it already exists and the geometry on fb doesn't match
        what we want,
        returns (status)"""
        # We should check here if 
        db=self._loadDB(directory)

        # If resize request, make tempfile and
        # resize.
        if _megapixels:
            fp = tempfile.NamedTemporaryFile()
            fullfile_resized=fp.name
            logger.debug("tempfile for resized is %s"%(fp.name))

        fullfile=os.path.join(directory,fn)

        # If JPEG, then resize
        ext=os.path.splitext(fullfile)[1].lower()
        if ext=='.jpg':
            isJPG=True
        else:
            isJPG=False

        # If already in DB, remove first, then overwrite
        if fn in db:
            pid=db[fn]['photoid']
            if resize_request and isJPG:
                logger.info("fb: Resize request for %s",fn)
                if self._already_resized_on_fb(fullfile,pid,_megapixels):
                    logger.debug("%s - Already in DB and resized, skipping",fn)
                    return True
            elif movealbum_request:
                logger.info("fb: Move album request for %s",fn)
                if self._already_in_album(fullfile,pid,_album_id):
                    logger.debug("%s - Already in DB and in correct album, skipping",fn)
                    return True
            elif changetitle_request:
                logger.info("fb: Change title request for %s",fn)
                if self._title_uptodate(fullfile,pid,_title):
                    logger.debug("%s - Already in DB and title up to date, skipping",fn)
                    return True


            # --- If we are here it means photo should be updated.
            # With FB graph API this means removing the photo
            # and uploading with new meta data.
            logger.debug("%s - Already in DB, removing first",fn)
            if not self._remove_media(directory,fn):
                logger.error("%s - fb: couldn't replace (remove) file\n",fn)
                return False

        # Do we have to resize?
        if _megapixels and isJPG:
            if pusher_utils.resize_image(fullfile,fullfile_resized,_megapixels):
                logger.debug("%s resized to %s successfully"\
                        %(fullfile,fullfile_resized))
                fullfile=fullfile_resized
            else:
                logger.warning("%s couldn't resize, uploading original"\
                        %(fullfile))

        logger.debug("Upload %s to fb, album=%s, title='%s'",\
                fn,_album_id,_title)

        # We can get a place id by doing a search
        # http://graph.facebook.com/search?type=city&center=37,-122&distance=1000

        # Do the actual upload
        resp=self.fb.put_photo(open(fullfile),\
                message=_title,album_id=_album_id,\
                )
                #place='106377336067638'\

        logger.debug("%s - Upload response is : %s"%(fn,resp))

        if not resp.has_key('id'):
            print("%s - fb: upload failed", fn)
            return False

        pid=resp['id']
        db[fn]={}
        db[fn]['photoid']=pid
        logger.debug("%s - fb: uploaded with photoid %s",fn,pid);

        self._saveDB(directory,db)
        return True