def track_sources(self, tweet):
        global sources

        source = tweet.source
        if source in sources:
            sources[source] += 1
        else:
            sources[source] = 1

        save_to_file(sources, filename="sources.json")
    def track_countries(self, tweet):
        global countries

        location = tweet.user.location
        if location is not None:
            if location in countries:
                countries[location] += 1
            else:
                countries[location] = 1

        save_to_file(countries, filename="countries.json")
    def track_retweets(self, tweet):
        global most_retweeted

        if hasattr(tweet, "retweeted_status"):  # Check if Retweet
            id_rt = tweet.retweeted_status.id_str
            if id_rt in most_retweeted:
                most_retweeted[id_rt]["count"] += 1
            else:
                most_retweeted[id_rt] = {}
                most_retweeted[id_rt]["count"] = 1
                most_retweeted[id_rt][
                    "link"] = "https://twitter.com/i/web/status/" + id_rt

            save_to_file(most_retweeted, filename="most_retweeted.json")
Beispiel #4
0
    def perform(self, keyword):
        auth = tweepy.OAuthHandler(os.getenv('consumer_key'),
                                   os.getenv('consumer_secret'))
        auth.set_access_token(os.getenv('access_token'),
                              os.getenv('access_token_secret'))

        api = tweepy.API(auth)
        myStreamListener = MyStreamWordCounter()

        myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
        save_to_file({})

        print('Iniciando')
        print(keyword)

        myStream.filter(track=[keyword], languages=['en'], is_async=True)
        return myStream
    def on_status(self, status):
        global tweets_total

        text = self.extract_text(status)
        self.track_retweets(status)
        self.track_sources(status)
        self.track_countries(status)

        tweets_total += 1
        save_to_file({'count': tweets_total}, filename="number_tweets.json")

        tweet_without_symbols = ' '.join(
            re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ",
                   text).split())

        result = self.tokenize_using_spacy(tweet_without_symbols, 'NOUN')
        save_to_file(result)
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output

from analysis import Analysis
from lib.tools import save_to_file
from lib.tools import load_from_file

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

streamer = None

# Init files
save_to_file({'count': 0}, filename="number_tweets.json")
save_to_file({}, filename="most_retweeted.json")
save_to_file({}, filename="sources.json")
save_to_file({}, filename="countries.json")

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.H1(children='Tweet-Analysis'),
    html.Div([
        dcc.Input(id='input-on-submit', type='text'),
        html.Button('INICIAR', id='submit-val', n_clicks=0),
    ]),
    html.Div(id='container-button-basic',
             children='Enter a value and press submit'),
    html.H3(id='number_tweets', children=''),