def wcloud_stream(tweet):
    try:
        logger.info('Getting data from Cache if there is')
        c_data = json.loads(cache.get_data('wordcloud','d_name,data','d_name ="test1"').data)
        logger.info('calc word count for a new tweet')
        dat = wnc.wordcloud_json(tweet)
        n = json.loads(dat)

        logger.info('Agreegate previous and current tweets')
        for t in n:
            if t in c_data:
                c_data[t] = c_data[t] + n[t]
            else:
                c_data.update({t:n[t]})

        logger.info('update MemSql')
        c_data=dict(sorted(c_data.items(), key=lambda x: x[1],reverse=True)[:250])
        update_data(json.dumps(c_data))

    except Exception, err:
        logger.info(err, 'Insert to MemSQL, if dash boardname is ot existing')
        dat = wnc.wordcloud_json(tweet)
        c_data = json.loads(dat)
        c_data=dict(sorted(c_data.items(), key=lambda x: x[1],reverse=True)[:250])
        insert_data(json.dumps(c_data))
def build_word_cloud(params):

        tokens = ast.literal_eval(params.tokens)
        hash_tag = params.hash_tag
        try:
            api = SMAuth.tweepy_auth(tokens['consumer_key'], tokens['consumer_secret'], tokens['access_token'], tokens['access_token_secret'])
            data_ = Tw.hashtag_search(api, hash_tag)
            wc_data = json.loads(wc.wordcloud_json(data_))
            data = cmg.format_response(True,wc_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())
        #full_comment = []
        analyzed_data = []

        if post_ids is not None:
            for post_id in post_ids:
                filtered_comments = filter(lambda d: d['post_id'] in post_id, data)
                for j in filtered_comments:
                   # full_comment.append(str(j['comments']))
                   #p = j['comments']
                   for comment in j['comments']:

                       full_comment_str +=' '
                       full_comment_str += comment['message'].encode('UTF8')
                #print full_comment_str
                logger.info(full_comment_str)
                data_ = json.loads(wc.wordcloud_json(full_comment_str))
                full_comment_str = ''
                data_['post_id'] = post_id
                analyzed_data.append(data_)
            print analyzed_data
                #full_comment_str.join(full_comment)
                #analysed_data = sa.sentiment(full_comment_str.join(filtered_comments))
            data = cmg.format_response(True,analyzed_data,'Data successfully processed!')
            return data
        else:
            for post in data:
                for comments in post['comments']:
                   #comment = comments['message']
                   #full_comment.append(comment)
                    full_comment_str +=' '
                    full_comment_str += comments['message']