def fb_posts_with_summary(params):

        token = params.token
        limit = ''
        since = ''
        until = ''
        page = 'me'
        try:
            page = str(params.page)
        except AttributeError:
            pass
        try:
            limit = params.limit
        except AttributeError:
            pass
        try:
            since = params.since
        except AttributeError:
            pass
        try:
            until = params.until
        except AttributeError:
            pass
        logger.info('Request received: %s' % params.values())
        try:
            data_ = FB.get_page_posts(token, limit, since, until, page=page)
            data = cmg.format_response(True,data_,'Data successfully processed!')
        except ValueError, err:
            data = cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
def build_word_cloud_fb(params):

        #source = str(params.source)
        try:
            limit = params.limit
        except AttributeError:
            limit = ''
        try:
            since = params.since
        except AttributeError:
            since = ''
        try:
            until = params.until
        except AttributeError:
            until = ''
        try:
            post_ids = ast.literal_eval(params.post_ids)
        except AttributeError:
            post_ids = None
        page = 'me'
        try:
            page = str(params.page)
        except AttributeError:
            pass
        token = params.token

        try:
            data = FB.get_page_posts_comments(token, limit, since, until, page, post_ids)
        except ValueError, err:
            return cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
def fb_promotional_info(params):

        token = params.token
        promotional_name = params.metric_name
        try:
            data_ = FB.get_promotional_info(token, promotional_name)
            data = cmg.format_response(True,data_,'Data successfully processed!')
        except ValueError, err:
            data = cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
def fb_page_user_locations(params):

        token = params.token
        logger.info('Requested received: %s' % params.values())
        try:
            data_ = FB.get_page_fans_city(token)
            data = cmg.format_response(True,data_,'Data successfully processed!')
        except ValueError, err:
            data = cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
            return data
def test(params):
        token = params.token
        limit = ''
        since = ''
        until = ''
        post_ids = None
        page = 'me'
        try:
            post_ids = ast.literal_eval(params.post_ids)
            page = str(params.page)
            limit = params.limit
            since = params.since
            until = params.until
        except AttributeError:
            pass
        logger.info('Request received: %s' % params.values())
        data = FB.get_page_posts_comments(token, limit, since, until, page, post_ids)
        return json.dumps(data)
def fb_overview(params):

        token = params.token
        metric_names = None
        try:
            metric_names = ast.literal_eval(params.metric_names)
        except AttributeError:
            pass
        since = None
        until = None
        try:
            since = params.since
            until = params.until
        except AttributeError:
            pass
        logger.info('Requested received: %s' % params.values())
        # data = json.dumps(fb.insight_metric(token, metric_name, since, until))
        try:
            data_ = FB.get_overview(token, metric_names, since, until)
            data = cmg.format_response(True,data_,'Data successfully processed!')
        except ValueError, err:
            data = cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
def build_bi_partite(params):

        token= ast.literal_eval(params.token)
        #source = str(params.source)
        try:
            limit = params.limit
        except AttributeError:
            limit = ''
        try:
            since = params.since
        except AttributeError:
            since = ''
        try:
            until = params.until
        except AttributeError:
            until = ''
        page = 'me'
        try:
            page = str(params.page)
        except AttributeError:
            pass
        posts_with_users = FB.get_page_posts_comments(token, limit, since, until, page, None)
        print json.dumps(posts_with_users)
        list_of_tuples = []
        for post in posts_with_users:
            post_id = str(post['post_id'])
            if post['comments'] == []:
                tup = (post_id, '0')
                list_of_tuples.append(tup)
            for comment in post['comments']:
                user_name = comment['from']['name'] # TODO send userid to eliminate duplication
                tup = (post_id, user_name)
                list_of_tuples.append(tup)
                print list_of_tuples
        analysed_data = bp.bipartite(list_of_tuples)
        data = cmg.format_response(True,analysed_data,'Data successfully processed!')
        return data
def sentiment_analysis(params):

        try:
            tokens = ast.literal_eval(params.token)
        except ValueError:
            tokens = params.token
        source = str(params.source)
        try:
            limit = params.limit
        except AttributeError:
            limit = ''
        try:
            since = params.since
        except AttributeError:
            since = ''
        try:
            until = params.until
        except AttributeError:
            until = ''
        try:
            post_ids = ast.literal_eval(params.post_ids)
        except AttributeError:
            post_ids = None
        page = 'me'
        try:
            page = str(params.page)
        except AttributeError:
            pass
        try:
            hash_tag = str(params.hash_tag)
        except AttributeError:
            hash_tag = ''
        #analyzed_data = 'Incorrect datasource name provided!'
        if source == 'twitter':
            api = SMAuth.tweepy_auth(tokens['consumer_key'], tokens['consumer_secret'], tokens['access_token'], tokens['access_token_secret'])
            data_ = Tw.hashtag_search(api,hash_tag)
            #lsi.initialize_stream(hash_tag, unique_id, tokens) # if already exits do something
            #analyzed_data = smlf.process_social_media_data(unique_id, hash_tag)
            data = sa.sentiment(data_)
            result = cmg.format_response(True,data,'Data successfully processed!')
            return result

        elif source == 'facebook':
            try:
                data = FB.get_page_posts_comments(tokens, limit, since, until, page, post_ids)
            except ValueError, err:
                data = cmg.format_response(False,err,'Error validating access token: This may be because the user logged out or may be due to a system error.',sys.exc_info())
                return data

            #full_comment = []
            analyzed_data = []

            def _calculate_sentiment(post_id, comment_string):
                full_comment_str = ''
                for j in filtered_comments:
                    for comment in j['comments']:
                        full_comment_str += ' '
                        full_comment_str += comment['message'].encode('UTF8')
                logger.debug(full_comment_str)
                data_ = sa.sentiment(full_comment_str)
                full_comment_str = ''
                data_['post_id'] = post_id
                analyzed_data.append(data_)

            threads = []
            if post_ids is not None:
                for post_id in post_ids:
                    filtered_comments = filter(lambda d: d['post_id'] in post_id, data)
                    t = threading.Thread(target=_calculate_sentiment, args=(post_id, filtered_comments))
                    t.start()
                    print "Thread started to calculate sentiment analysis for post_id: {0}".format(post_id)
                    threads.append(t)
                    #full_comment_str.join(full_comment)
                    #analysed_data = sa.sentiment(full_comment_str.join(filtered_comments))
                for t in threads:
                    try:
                        t.join()
                    except Exception, err:
                        print err

                data = cmg.format_response(True,analyzed_data,'Data successfully processed!')
                return data