Beispiel #1
0
        c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
        c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
        c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
        conn.commit()

    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)
result_KW = api.trends_place(23424870)


def trending():
    i = 0

    print("Trend in Kuwait:")
    for trend in result_KW[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
        else:
            pass
Beispiel #2
0
        conn.commit()

    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

trend_df_sa = pd.DataFrame()

result_SA = api.trends_place(23424938)


def trending():

    i = 0

    # trend from Saudi Arabia
    print("Trend in Saudi Arabia:")
    for trend in result_SA[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
Beispiel #3
0
trend_df_ae = pd.DataFrame()
trend_df_om = pd.DataFrame()
trend_df_qa = pd.DataFrame()
trend_df_bh = pd.DataFrame()
trend_df_kw = pd.DataFrame()

# create empty DataFrame to insert (data based on top trend) from each gulf state.
df_SA = pd.DataFrame()
df_AE = pd.DataFrame()
df_OM = pd.DataFrame()
df_QA = pd.DataFrame()
df_BH = pd.DataFrame()
df_KW = pd.DataFrame()

# connect to country id.
result_SA = api.trends_place(23424938)
result_AE = api.trends_place(23424738)
result_OM = api.trends_place(23424898)
result_QA = api.trends_place(23424930)
result_BH = api.trends_place(23424753)
result_KW = api.trends_place(23424870)

# create empty dataFrame to save all clean data gulf state in single DataFrame
clean_SA = pd.DataFrame()
clean_AE = pd.DataFrame()
clean_OM = pd.DataFrame()
clean_QA = pd.DataFrame()
clean_BH = pd.DataFrame()
clean_KW = pd.DataFrame()

clean_ALL = pd.DataFrame()
Beispiel #4
0
        c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
        conn.commit()


    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

result_BH = api.trends_place(23424753)


def trending():
    i = 0

    # trend from Saudi Arabia
    print("Trend in Bahrain:")
    for trend in result_BH[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
        else:
    """
    A listener handles tweets are the received from the stream.
    This is a basic listener that just prints received tweets to stdout.
    """

    def on_data(self, data):
        data = json.loads(data)
        data_hashtags = data['entities']['hashtags']
        for i in data_hashtags:
            hashtag = '#{}'.format(i['text'])
            if hashtag in hashtags:
                print(hashtag)
                # DON'T FORGET TO FLUSH!!!!
                stdout.flush()
                return True
        return True

    def on_error(self, status):
        print(status)


if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    api = API(auth_handler=auth)
    hashtags = [i['name'] for i in api.trends_place(id=44418)[0]['trends']]
    stream.filter(track=hashtags)
        c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
        c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
        c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
        conn.commit()

    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)
result_QA = api.trends_place(23424930)


def trending():
    i = 0

    print("Trend in Qatr:")
    for trend in result_QA[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
        else:
            pass
        c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
        c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
        conn.commit()

    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

result_AE = api.trends_place(23424738)


def trending():
    i = 0

    print("Trend in United Arab Emirates:")
    for trend in result_AE[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
        else:
            pass
        c.execute("CREATE INDEX fast_unix ON sentiment(unix)")
        c.execute("CREATE INDEX fast_tweet ON sentiment(tweet)")
        c.execute("CREATE INDEX fast_sentiment ON sentiment(sentiment)")
        conn.commit()

    except Exception as e:
        print(str(e))


create_table()

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)
result_OM = api.trends_place(23424898)


def trending():
    i = 0

    print("Trend in oman:")
    for trend in result_OM[0]["trends"][:5]:
        print("\t", trend["name"])
        print(i, end='\r')

        i += 1
        if i == 5:
            break
        else:
            pass
Beispiel #9
0
            print('type error: ', str(e))
        return True

    def on_error(self, status):
        if status.reason[0]['code'] == "420":
            print('error: ', status)
            time.sleep(5)


while True:
    print('while')
    try:
        print('auth')
        auth = OAuthHandler(kays_twitter.consumer_key, kays_twitter.consumer_secret)  # for authenticate
        auth.set_access_token(kays_twitter.access_key, kays_twitter.access_secret)    # set authentication
        print('Stream')
        twitterStream = Stream(auth, listener(), tweet_mode='extended')   # assign streaming

        # extract top trend
        api = API(auth)
        result_SA = api.trends_place(23424938)   # location of Saudi Arabia

        for trend in result_SA[0]["trends"][:1]:
            print(trend['name'])
            SA = trend['name']
        print(SA)
        twitterStream.filter(track=[SA], languages='ar')
    except Exception as e:
        print(str(e))
        time.sleep(5)