top of page

Machine Learning

Public·3 members

Create an application which uses the Twitter Search API (Links to an external site.)


Create an application which uses the Twitter Search API (Links to an external site.) to ingest tweets about the following list of celebrities from April 1 to the day before the project is due. Your application should be able to determine which of these celebrities are the most tweeted about and of the most tweeted celebrity, who has tweeted the most about that celebrity in this time frame.


a.) The query (or queries) to compute which celebrity has been the most tweeted about in the specified time frame

i.e.) Barack Obama has been mentioned the most with 12,000 times from August 16 - 17, 2019.


b.) The query (queries) to compute for the most celebrity tweeted about, who has tweeted the most about that celebrity in the time specified

i.e.) @XXXXX mentioned Barack Obama the most with 5 times from August 16 - 17, 2019.

c.) The results of executing these queries. Additionally, in a table, show the total number of tweets for each of these celebrities.


Tweets | Celebrity | Mentioned


0 | Barack Obama | 12,000


1 | Ariana Grande | 10,000




Solutions:

import pandas as pd
import tweepy
import datetime
import time
from textblob import TextBlob

Setting api key


consumer_key = 'Your consumer key'
consumer_secret = 'Your consumer secret'
access_token = 'Your access token'
access_token_secret = 'Your access token secret'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
startDate = datetime.datetime(2019, 8, 19, 0, 0, 0)
endDate = datetime.datetime(2019, 8, 21, 0, 0, 0)



def getUser_tweets(api, user):
    # tweets from a specific user
    try:
        #item = api.get_user(screen_name = user,since =startDate, until = endDate)
        #return item.statuses_count
    
        tmpTweets = api.user_timeline(screen_name = user)
        tweets = []
        for tweet in tmpTweets:
            if tweet.created_at < endDate and tweet.created_at > startDate:
                tweets.append(tweet)
                
        return len(tweets)
    
    except tweepy.error.TweepError:
        pass
    
    
def getMost_tweetted(api, user):
    query = user
    max_tweets = 100000

    searched_tweets = []

    last_id = -1
    while len(searched_tweets) < max_tweets:
        count = max_tweets - len(searched_tweets)
        try:
            new_tweets = api.search(q=query, count=count, max_id=str(last_id - 1))
            
            if not new_tweets:
                break
                
            
            tweets = []
            #print("new_tweets",len(new_tweets))
            for tweet in new_tweets:
                if tweet.created_at < endDate and tweet.created_at > startDate:
                    tweets.append(tweet)
                    
            #print("Tweets ",len(tweets))
            searched_tweets.extend(tweets)
            last_id = new_tweets[-1].id
        except tweepy.TweepError as e:
            # depending on TweepError.code, one may want to retry or wait
            # to keep things simple, we will give up on an error
            break
    return len(searched_tweets)


            
def getMaxTweettedUser(api, user):
    query = user
    max_tweets = 100000

    searched_tweets = []
    
    user_mentioned = {}

    last_id = -1
    
    while len(searched_tweets) < max_tweets:
        count = max_tweets - len(searched_tweets)
        try:
            new_tweets = api.search(q=query, count=count, max_id=str(last_id - 1))
            if not new_tweets:
                break
                
            tweets = []
            for tweet in new_tweets:
                if tweet.created_at < endDate and tweet.created_at > startDate:
                    tweets.append(tweet)
                
            searched_tweets.extend(tweets)
            
            last_id = new_tweets[-1].id
            
        except tweepy.TweepError as e:
            break
            
    for tweet in searched_tweets:
        
        if tweet.author.name in user_mentioned:
            user_mentioned[tweet.author.name]  = user_mentioned[tweet.author.name] + 1
        else:
            user_mentioned[tweet.author.name] = 1
            
            
    s = [(k, user_mentioned[k]) for k in sorted(user_mentioned, key=user_mentioned.get, reverse=True)]
    # return tuple with value (count,id) example (5, '@katyperry')
    return s[0]
            
    
            

    
users = ["@katyperry", "@justinbieber", "@BarackObama", "@rihanna", "@taylorswift13", "@ladygaga", "@TheEllenShow", "@cristiano", "@timberlake", "@ArianaGrande"]

1. Finding no of tweets for every user


for user in users:
    print(user, " has tweeted ", getUser_tweets(api, user), " times")

Output:

@katyperry  has tweeted  1  times
@justinbieber  has tweeted  0  times
@BarackObama  has tweeted  0  times
@rihanna  has tweeted  0  times
@taylorswift13  has tweeted  3  times
@ladygaga  has tweeted  0  times
@TheEllenShow  has tweeted  12  times
@cristiano  has tweeted  1  times
@timberlake  has tweeted  0  times
@ArianaGrande  has tweeted  1  times


a.) The query (or queries) to compute which celebrity has been the most tweeted about in the specified time frame i.e.) Barack Obama has been mentioned the most with 12,000 times from August 16 - 17, 2019


    for user in users:
        print(user, " has been mentioned ", getMost_tweetted(api, user), " times")
    


b.) The query (queries) to compute for the most celebrity tweeted about, who has tweeted the most about that celebrity in the time specified i.e.) @XXXXX mentioned Barack Obama the most with 5 times from August 16 - 17, 2019.


#taylorswift13 has most tweeted
user = '@katyperry'
most_twitted = getMaxTweettedUser(api, user)

print(most_twitted[0], "mentioned", user, most_twitted[1],"from August 19 - 21, 2019")


c.) The results of executing these queries. Additionally, in a table, show the total number of tweets for each of these celebrities. | Celebrity | Mentioned


for user in users:
    print(getUser_tweets(api, user)," | ", user, " | ", getMost_tweetted(api, user))

Output:

1  |  @katyperry  |  1
0  |  @justinbieber  |  5
0  |  @BarackObama  |  8
0  |  @rihanna  |  0
3  |  @taylorswift13  |  41
1  |  @ladygaga  |  6
5  |  @TheEllenShow  |  4
0  |  @cristiano  |  1
0  |  @timberlake  |  0
9  |  @ArianaGrande  |  24


if you need help simply reply to this message, we are online and ready to help or send Assignment requirement at codersarts@gmail.com . directly


contact us for twitter search api assignment help.

19 Views
bottom of page