示例#1
0
    def index(self):
	gd_client = gdata.photos.service.PhotosService()
	#token = request.GET.getone('token')
	parameters = cgi.FieldStorage()
	token = parameters['token']
	gd_client.auth_token = token
	gd_client.UpgradeToSessionToken()

        session['picasa_token'] = token
        session.save()

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(picasa_token=token).first()

        if user:
            user.picasa_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            user = User(picasa_token=token)
            Session.add(user)
            Session.commit()
            user = Session.query(User).filter_by(picasa_token=picasa_token).first()

        session['user_id'] = user.id
        session['picasa_token'] = token
        session.save()
        log.info("Logged in user %s", user)
        redirect(url('index'))
示例#2
0
    def index(self):
        code = request.GET.getone('code')
        nexturl = request.GET.get('nexturl')
        if nexturl:
            # we are acting only as an auth server.
            # redirect to the server that wants the auth code
            redirect(nexturl+'?code=%s' % code)
            return

        token = fb.get_access_token(code)
        if not token:
            #lame... this failed for some reason
            h.flash("The login process failed :(")
            redirect(url('index'))
            return
        fbuser = fb.GraphUser(access_token=token)

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(fb_uid=fbuser.id).first()

        if user:
            # the user does have an account, let's update their auth token
            user.fb_uid = fbuser.id
            user.fb_access_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            for attempt in xrange(3):
                user = User(fb_uid=fbuser.id,
                            fb_access_token=token)
                Session.add(user)
                Session.commit()
                user = Session.query(User).filter_by(fb_uid=fbuser.id).first()
                if user:
                    break
                log.error("Failed to create user with fb_uid=%r attempt %r",
                          fbuser.id, attempt)

        if not user:
            log.error("Trying to log in, but couldn't get a user object. "
                      "user=%r code=%r token=%r fbuser=%r",
                      user, code, token, fbuser)

        session['user_id'] = user.id
        session['fb_access_token'] = token
        session.save()
        log.info("Logged in user %s %s: %s",
                 fbuser.first_name,
                 fbuser.last_name,
                 user)
        redirect(url('index'))
示例#3
0
    def index(self):
        code = request.GET.getone('code')
        nexturl = request.GET.get('nexturl')
        if nexturl:
            # we are acting only as an auth server.
            # redirect to the server that wants the auth code
            redirect(nexturl + '?code=%s' % code)
            return

        token = fb.get_access_token(code)
        if not token:
            #lame... this failed for some reason
            h.flash("The login process failed :(")
            redirect(url('index'))
            return
        fbuser = fb.GraphUser(access_token=token)

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(
                id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(fb_uid=fbuser.id).first()

        if user:
            # the user does have an account, let's update their auth token
            user.fb_uid = fbuser.id
            user.fb_access_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            for attempt in xrange(3):
                user = User(fb_uid=fbuser.id, fb_access_token=token)
                Session.add(user)
                Session.commit()
                user = Session.query(User).filter_by(fb_uid=fbuser.id).first()
                if user:
                    break
                log.error("Failed to create user with fb_uid=%r attempt %r",
                          fbuser.id, attempt)

        if not user:
            log.error(
                "Trying to log in, but couldn't get a user object. "
                "user=%r code=%r token=%r fbuser=%r", user, code, token,
                fbuser)

        session['user_id'] = user.id
        session['fb_access_token'] = token
        session.save()
        log.info("Logged in user %s %s: %s", fbuser.first_name,
                 fbuser.last_name, user)
        redirect(url('index'))
示例#4
0
 def submit_advanced(cls, args, kwargs, delay=0, user_id=None):
     task = AsyncTask(user_id=user_id)
     task.set_status(*cls.get_initial_status())
     task.type = cls.get_type()
     Session.add(task)
     Session.commit()
     queue_id = job.submit_advanced(cls, (task.id, args, kwargs), {}, delay=delay)
     task.queue_id = queue_id
     Session.commit()
     return task
示例#5
0
 def submit_advanced(cls, args, kwargs, delay=0, user_id=None):
     task = AsyncTask(user_id=user_id)
     task.set_status(*cls.get_initial_status())
     task.type = cls.get_type()
     Session.add(task)
     Session.commit()
     queue_id = job.submit_advanced(cls, (task.id, args, kwargs), {}, delay=delay)
     task.queue_id = queue_id
     Session.commit()
     return task
示例#6
0
    def index(self):
        gd_client = gdata.photos.service.PhotosService()
        #token = request.GET.getone('token')
        parameters = cgi.FieldStorage()
        token = parameters['token']
        gd_client.auth_token = token
        gd_client.UpgradeToSessionToken()

        session['picasa_token'] = token
        session.save()

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(
                id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(picasa_token=token).first()

        if user:
            user.picasa_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            user = User(picasa_token=token)
            Session.add(user)
            Session.commit()
            user = Session.query(User).filter_by(
                picasa_token=picasa_token).first()

        session['user_id'] = user.id
        session['picasa_token'] = token
        session.save()
        log.info("Logged in user %s", user)
        redirect(url('index'))
示例#7
0
    def index(self):
        flickr = FlickrAPI()
        token = flickr.get_token(request.GET.getone('frob'))

        session['flickr_token'] = token
        session.save()

        flickr = FlickrAPI(token)
        result = flickr.auth_checkToken()
        nsid = result[0][2].get('nsid')

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(
                id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(flickr_nsid=nsid).first()

        if user:
            user.flickr_nsid = nsid
            user.flickr_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            user = User(flickr_nsid=nsid, flickr_token=token)
            Session.add(user)
            Session.commit()
            user = Session.query(User).filter_by(flickr_nsid=nsid).first()

        session['user_id'] = user.id
        session['flickr_token'] = token
        session.save()
        log.info("Logged in user %s", user)
        redirect(url('index'))
示例#8
0
    def index(self):
        flickr = FlickrAPI()
        token = flickr.get_token(request.GET.getone('frob'))

        session['flickr_token'] = token
        session.save()

        flickr = FlickrAPI(token)
        result = flickr.auth_checkToken()
        nsid = result[0][2].get('nsid')

        if session.get('user_id'):
            # a user is already logged in
            user = Session.query(User).filter_by(id=session.get('user_id')).first()
        else:
            # the user is not already logged in, let's see if they have
            # already created an account before
            user = Session.query(User).filter_by(flickr_nsid=nsid).first()

        if user:
            user.flickr_nsid = nsid
            user.flickr_token = token
            Session.commit()
        else:
            # the user does not have an account.  We need to create a new one
            # for them.
            user = User(flickr_nsid=nsid, flickr_token=token)
            Session.add(user)
            Session.commit()
            user = Session.query(User).filter_by(flickr_nsid=nsid).first()

        session['user_id'] = user.id
        session['flickr_token'] = token
        session.save()
        log.info("Logged in user %s", user)
        redirect(url('index'))
示例#9
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)
示例#10
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)