Beispiel #1
0
    def index(self):
        c.user = self.logged_in_user
        c.fb_connect_url = fb.get_authorization_url()
        c.flickr_connect_url = flickr.get_authorization_url()
        if self.logged_in_user:
            if self.logged_in_user.fb_access_token:
                c.fb_user = fb.GraphUser(
                    id=self.logged_in_user.fb_uid,
                    access_token=self.logged_in_user.fb_access_token)
            if self.logged_in_user.flickr_token:
                c.flickr_user = flickr.FlickrUser(
                    token=self.logged_in_user.flickr_token)
            if self.logged_in_user.flickr_token and self.logged_in_user.fb_access_token:
                c.tasks = AsyncTask.get_for_user(
                    limit=2,
                    type=FullSync.get_type()).all()
                c.sync_url = '/sync/full_sync'
                for task in c.tasks:
                    if task.is_completed:
                        c.last_task = task
                    elif task.time_left:
                        c.next_task = task
                    else:
                        c.current_task = task

            bytes_in, bytes_out = SyncRecord.get_bytes_transfered_by_user(self.logged_in_user.id)
            c.bytes_transferred = bytes_in/1024.**2

            # cost is $0.10 per GB transferred in and
            # $0.15 per GB transferred out
            c.cost = bytes_in/1024.**3 * 0.1 + bytes_out/1024.**3 * 0.15

        if c.tasks:
            return render('/homepage.mako')

        return render('/frontpage.mako')

        if session.get('user_id'):
            return self.home()
        else:
            c.fb_connect_url = fb.get_authorization_url([
                'user_photos',
                'publish_stream',
                'offline_access',
                ])
            # Return a rendered template
            return render('/frontpage.mako')
Beispiel #2
0
    def sync_photoset(self, photoset):
        log.debug("Syncing flickr photoset %s to facebook", photoset.get("id"))
        sync = Session.query(SyncRecord).filter_by(flickrid=photoset.get("id"), type=SyncRecord.TYPE_ALBUM).first()
        album = None
        if sync and sync.success:
            # don't resync unless we failed before
            log.debug("skipping... already synced")
            album = fb.GraphAlbum(id=sync.fbid, access_token=self.user.fb_access_token)
            if not album.data:
                album = None
        if not album:
            sync = SyncRecord(SyncRecord.TYPE_ALBUM, self.task.user_id)
            sync.flickrid = photoset.get("id")

            title = photoset.find("title").text
            description = photoset.find("description").text
            album = self.fb_user.albums.add(
                title.encode("utf-8") if title else "",
                description.encode("utf-8") if description else "",
                privacy=self.fb_privacy,
            )

            if album:
                sync.fbid = album.id
                sync.status = SyncRecord.STATUS_SUCCESS
            else:
                sync.status = SyncRecord.STATUS_FAILED
            Session.add(sync)
            Session.commit()
            self.set_status(self.synced_photos, self.total_photos, "Failed to sync %s" % title)
            return

        photos = self.fk.photosets_getPhotos(photoset_id=photoset.get("id"))[0]

        # max out at 200 photos until we figure out what to do with bigger sets
        photos = photos[:200]

        photos_to_sync = []
        for photo in photos:
            log.debug("Syncing flickr photo %s to facebook", photo.get("id"))
            sync = SyncRecord.get_for_flickrid(photo.get("id")).first()
            fb_photo = None
            if sync and sync.fbid and sync.success:
                log.debug("Skipping... already synced")
                fb_photo = fb.GraphPhoto(id=sync.fbid, access_token=self.user.fb_access_token)
                # if not fb_photo.data:
                #    fb_photo = None
                self.synced_photos += 1
            if not fb_photo:
                photos_to_sync.append(photo)

        status = "%s photos from %s already synced" % (self.synced_photos, photoset.find("title").text)
        self.set_status(self.synced_photos, self.total_photos, status)

        if not photos_to_sync:
            return

        def on_progress(processed, total):
            self.set_status(
                processed, total, "Found %s/%s photos in %s..." % (processed, total, photoset.find("title").text)
            )

        fetcher = http.Fetcher(progress_callback=on_progress)
        requests = []
        for photo in photos_to_sync:
            url = flickr.get_url(self.user.flickr_token, method="flickr.photos.getSizes", photo_id=photo.get("id"))
            request = http.JsonRequest(url)
            requests.append((photo, request))
            fetcher.queue(request)
        fetcher.run()

        def on_progress(processed, total):
            self.set_status(
                processed, total, "Downloaded %s/%s images from %s" % (processed, total, photoset.find("title").text)
            )

        fetcher = http.Fetcher(progress_callback=on_progress)
        img_requests = []

        # TODO: make tmp directory path configurable
        if not os.path.exists("/tmp/photosync"):
            os.mkdir("/tmp/photosync")
            os.chmod("/tmp/photosync", 0777)

        for i, (photo, request) in enumerate(requests):
            sync = SyncRecord(SyncRecord.TYPE_PHOTO, self.task.user_id)
            sync.flickrid = photo.get("id")
            Session.add(sync)
            res = request.read_response()
            try:
                img_url = res["sizes"]["size"][-1]["source"]
            except:
                # import pdb; pdb.set_trace()
                raise
            filename = "/tmp/photosync/flickr-" + sync.flickrid + ".jpg"
            log.debug("Downloading image %s to %s", img_url, filename)
            img_request = None
            if not os.path.exists(filename):
                f = open(filename, "wb")
                f.write(urllib2.urlopen(img_url).read())
                f.close()
                on_progress(i, len(requests))
                # TODO: Figure out why curl isn't working here
                # for some reason when we use the code below,
                # the complete file does not get downloaded.
                # img_request = http.Request(img_url, filename=filename)
                # fetcher.queue(img_request)
            img_requests.append((photo, filename, sync, img_request))
        Session.commit()
        fetcher.run()

        for photo, temp_filename, sync, img_request in img_requests:
            sync.transfer_in = os.path.getsize(temp_filename)
            log.debug("Uploading image %s to facebook", temp_filename)
            graph_photo = None
            title = photo.get("title")
            try:
                title = title.encode("utf-8")
            except UnicodeEncodeError, e:
                log.error("Failed to encode %s to utf-8", title)
                log.exception(e)
                # better a photo with no title than no photo at all.
                title = ""
            try:
                graph_photo = album.photos.add(temp_filename, title)
            except TypeError, e:
                log.error("Error uploading image %s", temp_filename)
                log.exception(e)
Beispiel #3
0
    def sync_photoset(self, photoset):
        log.debug("Syncing flickr photoset %s to facebook", photoset.get('id'))
        sync = Session.query(SyncRecord).filter_by(
            flickrid=photoset.get('id'), type=SyncRecord.TYPE_ALBUM).first()
        album = None
        if sync and sync.success:
            # don't resync unless we failed before
            log.debug("skipping... already synced")
            album = fb.GraphAlbum(id=sync.fbid, access_token=self.user.fb_access_token)
            if not album.data:
                album = None
        if not album:
            sync = SyncRecord(SyncRecord.TYPE_ALBUM, self.task.user_id)
            sync.flickrid = photoset.get('id')

            title = photoset.find('title').text
            description = photoset.find('description').text
            album = self.fb_user.albums.add(
                title.encode('utf-8') if title else '',
                description.encode('utf-8') if description else '',
                privacy=self.fb_privacy)

            if album:
                sync.fbid = album.id
                sync.status = SyncRecord.STATUS_SUCCESS
            else:
                sync.status = SyncRecord.STATUS_FAILED
            Session.add(sync)
            Session.commit()
            self.set_status(self.synced_photos, self.total_photos,
                            "Failed to sync %s" % title)
            return

        photos = self.fk.photosets_getPhotos(photoset_id=photoset.get('id'))[0]

        # max out at 200 photos until we figure out what to do with bigger sets
        photos = photos[:200]

        photos_to_sync = []
        for photo in photos:
            log.debug("Syncing flickr photo %s to facebook", photo.get('id'))
            sync = SyncRecord.get_for_flickrid(photo.get('id')).first()
            fb_photo = None
            if sync and sync.fbid and sync.success:
                log.debug("Skipping... already synced")
                fb_photo = fb.GraphPhoto(id=sync.fbid, access_token=self.user.fb_access_token)
                #if not fb_photo.data:
                #    fb_photo = None
                self.synced_photos += 1
            if not fb_photo:
                photos_to_sync.append(photo)

        status = "%s photos from %s already synced" % (self.synced_photos,
                                                       photoset.find('title').text)
        self.set_status(self.synced_photos, self.total_photos, status)

        if not photos_to_sync:
            return

        def on_progress(processed, total):
            self.set_status(
                processed,
                total,
                "Found %s/%s photos in %s..." % (
                    processed, total, photoset.find('title').text))

        fetcher = http.Fetcher(progress_callback=on_progress)
        requests = []
        for photo in photos_to_sync:
            url = flickr.get_url(
                self.user.flickr_token,
                method='flickr.photos.getSizes',
                photo_id=photo.get('id'))
            request = http.JsonRequest(url)
            requests.append((photo, request))
            fetcher.queue(request)
        fetcher.run()

        def on_progress(processed, total):
            self.set_status(
                processed,
                total,
                "Downloaded %s/%s images from %s" % (
                    processed, total, photoset.find('title').text))

        fetcher = http.Fetcher(progress_callback=on_progress)
        img_requests = []

        # TODO: make tmp directory path configurable
        if not os.path.exists('/tmp/photosync'):
            os.mkdir('/tmp/photosync')
            os.chmod('/tmp/photosync', 0777)

        for i, (photo, request) in enumerate(requests):
            sync = SyncRecord(SyncRecord.TYPE_PHOTO, self.task.user_id)
            sync.flickrid = photo.get("id")
            Session.add(sync)
            res = request.read_response()
            try:
                img_url = res['sizes']['size'][-1]['source']
            except:
                #import pdb; pdb.set_trace()
                raise
            filename = '/tmp/photosync/flickr-'+sync.flickrid+'.jpg'
            log.debug("Downloading image %s to %s", img_url, filename)
            img_request = None
            if not os.path.exists(filename):
                f = open(filename, 'wb')
                f.write(urllib2.urlopen(img_url).read())
                f.close()
                on_progress(i, len(requests))
                # TODO: Figure out why curl isn't working here
                # for some reason when we use the code below,
                # the complete file does not get downloaded.
                #img_request = http.Request(img_url, filename=filename)
                #fetcher.queue(img_request)
            img_requests.append((photo, filename, sync, img_request))
        Session.commit()
        fetcher.run()

        for photo, temp_filename, sync, img_request in img_requests:
            sync.transfer_in = os.path.getsize(temp_filename)
            log.debug("Uploading image %s to facebook", temp_filename)
            graph_photo = None
            title = photo.get('title')
            try:
                title = title.encode('utf-8')
            except UnicodeEncodeError, e:
                log.error("Failed to encode %s to utf-8", title)
                log.exception(e)
                # better a photo with no title than no photo at all.
                title = ""
            try:
                graph_photo = album.photos.add(temp_filename, title)
            except TypeError, e:
                log.error("Error uploading image %s", temp_filename)
                log.exception(e)