def add_user(self, user):
        db_map = self.application.db['map']

        results = db_map.find_one({'_id': user['id']})

        if results is None:
            # Get statuses from user from Facebook
            graph = facebook.GraphAPI(user["access_token"])
            status_request = """[{"method":"GET","relative_url":"method/fql.query?query=select+message,time+from+status+where+uid=me()"}]"""
            statuses = graph.request("", post_args={"batch": status_request})

            # Generate sentiments
            statuses_json = sentiment.sentimentJSON((statuses[0]["body"]), self.application.classifier)

            # Calculate average sentiment for user
            avg_sentiment = 0
            num_statuses = 0
            for status in statuses_json:
                avg_sentiment += status['sentiment']
                num_statuses += 1
            # Calculate average sentiment
            avg_sentiment /= num_statuses

            # Get location for user
            user_id = user['id']
            geo_lng = float(self.get_cookie("geo_lng"))
            geo_lat = float(self.get_cookie("geo_lat"))
            if geo_lng is not None and geo_lat is not None:
                db_map.insert({'_id': user_id, 'avg_sentiment': avg_sentiment, 'lat': geo_lat, 'lng': geo_lng})
            else:
                db_map.insert({'_id': user_id, 'avg_sentiment': avg_sentiment})
Example #2
0
    def get(self):
        # Get statuses for user from Facebook
        user = json.loads(self.get_current_user())
        graph = facebook.GraphAPI(user["access_token"])
        status_request = """[{"method":"GET","relative_url":"method/fql.query?query=select+message,time+from+status+where+uid=me()"}]"""
        statuses = graph.request("", post_args={"batch": status_request})

        # Generate sentiments
        statuses_json = sentiment.sentimentJSON((statuses[0]["body"]), self.application.classifier)
        min_statuses = []

        # Append week of year and calculate average sentiment for user
        for status in statuses_json:
            new_status = {}

            new_status['status'] = status['message']
            status_date = datetime.datetime.fromtimestamp(int(status['time']))
            new_status['month'] = status_date.strftime("%B")
            status_date = status_date.isocalendar()
            new_status['year'] = status_date[0]
            new_status['week'] = status_date[1]
            new_status['day'] = status_date[2]
            new_status['sentiment'] = status['sentiment']

            min_statuses.append(new_status)

        self.write(json.dumps(min_statuses))
        self.finish()