Exemple #1
0
def update_graph_live(value, n_intervals):

    mongo = MongoWrapper()

    neg_sentiment, neutral_sentiment, pos_sentiment = mongo.get_polarity_tweets_of_stock(
        value)

    negs = neg_sentiment.count()
    poss = pos_sentiment.count()
    neuu = neutral_sentiment.count()
    tots = negs + poss + neuu

    posPercentage = (poss / (tots)) * 100
    neuPercentage = (neuu / (tots)) * 100
    negPercentage = (negs / (tots)) * 100

    fig = ({
        'data': [{
            'type': 'pie',
            'labels': ['Positive', 'Neutral', 'Negative'],
            'values': [posPercentage, neuPercentage, negPercentage],
            'marker': {
                'colors': ['#32C85A', '#4C93B1', '#FA4632'],
                # 'values': [posPercentage, negPercentage],
                # 'marker': {'colors': ['#32C85A', '#FA4632'],
            },
        }],
        'layout': {
            'title': {
                'text': "Twitter Sentiment for {}\n".format(value),
            },
        }
    })

    return fig
def update_graph_live(value):

    mongo = MongoWrapper()

    neg_sentiment, neutral_sentiment, pos_sentiment = mongo.get_polarity_tweets_of_stock(value)

    print(neg_sentiment)

    negs = neg_sentiment.count()
    poss = pos_sentiment.count()
    neuu = neutral_sentiment.count()
    tots = negs+poss+neuu

    posPercentage = (poss/(tots))*100
    # neuPercentage = (neuu/(tots))*100
    negPercentage = (negs/(tots))*100

    # print(posPercentage)
    # print(neuPercentage)
    # print(negPercentage)
    # print(tots)
    

    bigGraph = dcc.Graph(
        animate=False,
        figure={
        'data': [{
                'type': 'pie',
                'labels': ['Positive', 'Neutral' ,'Negative'],
                # 'values': [posPercentage, neuPercentage, negPercentage],
                # 'marker': {'colors': ['#32C85A', '#4C93B1', '#FA4632'],
                'values': [posPercentage, negPercentage],
                'marker': {'colors': ['#32C85A', '#FA4632'],
                    },
            }],

            'layout':{
                'title':"Twitter Sentiment for {}\n".format(value)
            }
        }
    ),

    afterText = html.Div('Total Tweets pulled for searchword: {}.\n'.format(tots))

    return (bigGraph, afterText)
def mainFunction(value):

    mongo = MongoWrapper()

    neg_sentiment, neutral_sentiment, pos_sentiment = mongo.get_polarity_tweets_of_stock(
        value)

    negs = neg_sentiment.count()
    poss = pos_sentiment.count()
    neuu = neutral_sentiment.count()
    tots = negs + poss + neuu

    posPercentage = (poss / (tots)) * 100
    neuPercentage = (neuu / (tots)) * 100
    negPercentage = (negs / (tots)) * 100

    # print(posPercentage, neuPercentage, negPercentage)

    return dcc.Graph(
        figure={
            'data': [{
                'type': 'pie',
                'labels': ['Positive', 'Neutral', 'Negative'],
                'values': [posPercentage, neuPercentage, negPercentage],
                'marker': {
                    'colors': ['#32C85A', '#4C93B1', '#FA4632'],
                    # 'values': [posPercentage, negPercentage],
                    # 'marker': {'colors': ['#32C85A', '#FA4632'],
                },
            }],
            'layout': {
                'title': {
                    'text': "Twitter Sentiment for {}\n".format(value),
                },
            }
        }), html.Div(
            children='Total Tweets pulled for searchword: {}.\n'.format(tots)),
import tweepy
from WatchDogs_MongoWrapper import MongoWrapper

CONSUMER_KEY = "lQu7vXPH6hMNQ92LLVOFD5K8b"
CONSUMER_SECRET = "NwoUxJgXZkA6Fm0IJpNC6S0bavZy3YVcblKrQQbna7R4lwj39X"
ACCESS_TOKEN = "1170146904-NfXboXT6cjI8zcThUiixJp1AztyJH4Hw8wdkDwj"
ACCESS_SECRET = "rpRprMkactMVuKiJsESZGjEUmbixPABB3NjIKOxymJT7K"


def twitter_setup():
    try:
        auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
        api = tweepy.API(auth,
                         wait_on_rate_limit=True,
                         wait_on_rate_limit_notify=True)
        return api
    except:
        print("Error: Authentication Failed")


if __name__ == "__main__":
    pullTweets = twitter_setup()
    tweets = pullTweets.search(q=["Boeing"], count=200, tweet_mode="extended")
    mongo = MongoWrapper()
    mongo.get_polarity_tweets_of_stock("Aramark")
 def test_get_stocks_polarity(self):
     mongo = MongoWrapper()
     print(mongo.get_polarity_tweets_of_stock("3D Systems Corporation"))
Exemple #6
0
class RedisWrapper():
    def __init__(self, decrypt_key):
        self.redicclient = redis.StrictRedis(host='35.236.16.13', port=6379, db=0, decode_responses=True)
        self.mng = MongoWrapper(decrypt_key)

    def get_logger(self, logger_name):
        return self.mng.get_logger(logger_name)

    def redis_update_json(self, api_string, key):
        if api_string == 'get_tweets_with_lat_long/':

            json_data = self.mng.get_tweets_with_lat_long(key)
            self.redicclient.execute_command('JSON.SET', api_string+key, '.', json_data)

        elif api_string == 'get_polarity_tweets_of_stock/':

            json_data = self.mng.get_polarity_tweets_of_stock(key)
            self.redicclient.execute_command('JSON.SET', api_string + key, '.', json_data)


    def redis_insert_tweet(self, key, tweet):
        """
        Either insert a single tweet or multiple tweets and this def will update the redis cache accordingly
        :param key:
        :param tweets:
        :return:
        """
        try:
            lat_long_list = tweet['Geo']['coordinates']
            has_lat_long = True
        except:
            has_lat_long = False
            lat_long_list = ['None', 'None']

        if has_lat_long:
            sentiment_value = float(tweet["Sentiment_Value"])
            full_text = tweet["Text"]
            root_json_path = {}
            root_json_path["Latitude"] = lat_long_list[0]
            root_json_path["Longitude"] = lat_long_list[1]
            root_json_path["Tweet_Text"] = full_text
            root_json_path["Sentiment_Value"] = sentiment_value
            api_string = 'get_tweets_with_lat_long/'
            self.redicclient.execute_command('JSON.ARRAPPEND', api_string+key, '.', json.dumps(root_json_path))

        sentiment_polarity = int(tweet["Sentiment_Polarity"])
        full_text = tweet["Text"]
        root_json_path = {}
        root_json_path["Latitude"] = lat_long_list[0]
        root_json_path["Longitude"] = lat_long_list[1]
        root_json_path["Tweet_Text"] = full_text
        root_json_path["Sentiment_Polarity"] = sentiment_polarity
        api_string = 'get_polarity_tweets_of_stock/'
        if sentiment_polarity == -1:
            root_path = '.Negative_Tweets'
        elif sentiment_polarity == 0:
            root_path = '.Neutral_Tweets'
        elif sentiment_polarity == 1:
            root_path = '.Positive_Tweets'
        self.redicclient.execute_command('JSON.ARRAPPEND', api_string + key, root_path, json.dumps(root_json_path))

    def redis_get_json(self, api_string, key):
        return self.redicclient.execute_command('JSON.GET', api_string+key)

    def redis_flush_all(self):
        """
        Danger. This flushes the whole DB. Careful
        :return:
        """
        self.redicclient.flushdb()