def index(): if (request.method == 'POST'): query1 = request.form.get('query') api = TwitterClient() tweets = api.get_tweets(query=query1, count=200) # picking positive tweets from tweets ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive'] # percentage of positive tweets posper = 100 * len(ptweets) / len(tweets) # picking negative tweets from tweets ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative'] negper = 100 * len(ntweets) / len(tweets) # percentage of neutral tweets neuper = 100 * (len(tweets) - len(ntweets) - len(ptweets)) / len(tweets) resultdict = { "Positive tweets percentage": int(posper), "negative tweets percentage ": int(negper), "neutral tweets percentage": int(neuper) } return render_template('index.html', result=resultdict, ip=query1, pt=ptweets, nt=ntweets)
def submit(): keyword = request.form['keyword'] count = request.form['count'] # Get the tweets try: api = TwitterClient() tweets = api.get_tweets(query = keyword, count = count) positive_tweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive'] positive_tweets_percentage = round(len(positive_tweets)/len(tweets)*100, 2) negative_tweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative'] negative_tweets_percentage = round(len(negative_tweets)/len(tweets)*100, 2) neutral_percentage = round((len(tweets) - len(positive_tweets) - len(negative_tweets))/len(tweets)*100, 2) context = { 'ptweets': positive_tweets[:5], 'ntweets': negative_tweets[:5], 'ppercent': positive_tweets_percentage, 'npercent': negative_tweets_percentage, 'neutral': neutral_percentage } return render_template("results.html", context = context) except Exception as e: return str(e)
# -*- coding: utf-8 -*- import requests import json from requests_oauthlib import OAuth1Session from twitter import TwitterClient consumer_key = '' consumer_secret = '' access_token = '' access_secret_token = '' twitter_client = TwitterClient(consumer_key, consumer_secret, access_token, access_secret_token) for tweet in twitter_client.get_tweets('naruto', n=100, lang='pt-br'): print(tweet['text'])
from twitter import TwitterClient import pprint CONSUMER_KEY = 'your key' CONSUMER_SECRET = 'your key' ACCESS_TOKEN = 'your token' ACCESS_SECREAT_TOKEN = 'your token' client = TwitterClient(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECREAT_TOKEN) pesquisar = client.search('#naruto') #get_msg = client.post_message(pesquisar) tweets = client.get_tweets(pesquisar, n=100, lang='pt') for tweet in tweets: pprint.pprint(tweet['text'])