def post(self):
        """."""
        logging.info('tasks/new_gplus')

        gplus_id = self.request.get('gplus_id')
        logging.info(gplus_id)

        # build the service object for the gplus api
        API_KEY = get_server_api_key()
        gplus_service = build('plus', 'v1', developerKey=API_KEY)
        # find out what the last activity date recorded is for the expert
        activities = ActivityPost.query(
            ActivityPost.gplus_id == gplus_id).order(-ActivityPost.date).fetch(1)
        # if this is the first time we are getting activities for a new user
        if len(activities) == 0:
            last_activity_date = date(1990, 1, 1).strftime("%Y-%m-%d %H:%M:%S")
        else:
            last_activity_date = activities[0].date
        logging.info('Last activity date : % s' % last_activity_date)
        # get the last 100 activities from gplus
        # eventually we can also query using the date so we don't
        # have to compare each post with the date we just got
        try:
            result = gplus_service.activities().list(userId=gplus_id,
                                                     collection='public',
                                                     maxResults=100).execute()
        except:
            # try againg
            logging.info('trying to get gplus activities again')
            try:
                result = gplus_service.activities().list(userId=gplus_id,
                                                         collection='public',
                                                         maxResults=100).execute()
            except:
                logging.info('failed again, giving up')

        else:
            # patt0 25102014 sort the items in reverse update date order
            gplus_act = result.get('items', [])
            gplus_activities = sorted(gplus_act, key=lambda k: k['updated'])
            for gplus_activity in gplus_activities:
                if gplus_activity["updated"] > last_activity_date:
                    # ensure this is a gde post
                    if is_gde_post(gplus_activity):

                        # create a new activity
                        new_activity = ActivityPost(id=gplus_activity["id"])
                        new_activity.create_from_gplus_post(gplus_activity)
                        new_activity.put()
                        logging.info('new activity recorded: %s' %
                                     new_activity.url)

                        activity_record = find_or_create_ar(
                            gplus_activity, new_activity)
                        activity_record.add_post(new_activity)
    def post(self):
        """."""
        logging.info('tasks/upd_gplus')

        bad_posts = []
        gplus_id = self.request.get('gplus_id')
        logging.info(gplus_id)

        # build the service object for the gplus api
        API_KEY = get_server_api_key()
        gplus_service = build('plus', 'v1', developerKey=API_KEY)
        # get the activities for the gde
        activities = ActivityPost.query(ActivityPost.gplus_id == gplus_id)

        for activity in activities:
            if activity.deleted:
                continue

            # check if post belongs to deleted activity
            activity_record = ar.find(activity)
            if activity_record is not None:
                if activity_record.deleted:
                    continue

            logging.info(activity_record)

            # find out if the acticity post links to a YouTube video
            video_id = is_youtube_video(activity.links)
            if video_id is not False:
                logging.info('linked YT video found %s', video_id)

                # ensure a metadata entry exist for the video
                meta_found = False
                for meta in activity_record.metadata:
                    if meta.type == '#video' and meta.link == activity_record.activity_link:
                        logging.info('metadata record exist')
                        meta_found = True
                        break

                    if meta.type == '#video' and meta.link is None:
                        meta_found = True
                        logging.info('metadata record incomplete')
                        meta.activity_group = '#content'
                        meta.link = activity_record.activity_link
                        activity_record.put()
                        break

                if not meta_found:
                    logging.info('metadata record NOT found')
                    activity_record.metadata.insert(0, ActivityMetaData())
                    meta = activity_record.metadata[0]
                    meta.type = '#video'
                    meta.activity_group = '#content'
                    meta.link = activity_record.activity_link
                    activity_record.put()

            # get the activity from gplus
            fields = 'id,verb,actor/id,annotation,object(id,actor/id,plusoners/totalItems,replies/totalItems,resharers/totalItems,content)'
            try:
                plus_activity = gplus_service.activities().get(
                    activityId=activity.post_id,
                    fields=fields).execute()
            except:
                # try again
                logging.info('trying to get gplus activities again')
                try:
                    plus_activity = gplus_service.activities().get(
                        activityId=activity.post_id,
                        fields=fields).execute()
                except:
                    logging.info('failed again, giving up')
                    continue

            if not is_gde_post(plus_activity):
                # gde tag has been removed from post
                # This post and associated AR should be deleted
                # For now we just skip any further action on this post
                continue

            updated_activity = self.update_if_changed(activity, plus_activity)

            if updated_activity is not None:
                logging.info('updated gplus post id %s' %
                             updated_activity.post_id)
                activity_record = find_or_create_ar(
                    plus_activity, updated_activity)
                activity_record.add_post(updated_activity)
                updated_activity.put()

            # does the activity have the necessary hashtags? NOT -> bad_posts
            if len(activity.product_group) == 0 or len(activity.activity_type) == 0:
                # check associated AR as well
                really_bad = False
                activity_record = ar.find(activity)
                if activity_record is None:
                    really_bad = True
                elif len(activity_record.activity_types) == 0 or len(activity_record.product_groups) == 0:
                    really_bad = True

                if really_bad:
                    logging.info('bad post spotted')
                    bad_posts.append(activity.url)

        update_video_views(gplus_id)

        if len(bad_posts) > 0:
            self.send_update_mail(bad_posts, gplus_id)