0

I am aware that it is not great code, but this is the current code that I have, I am trying to continuously grab all new tweets that contain one of the anime in my list called animelist.

I need to have a section of code that will take all of the tweets in the tweets list and count which anime has the most mentions in the list of tweets then delete the list to start fresh. Not sure where it will go yet, but I have a example block in the def on_data() function.

I need to do this on a 24-hour basis, so I need to pause the code that will do the analysis and only allow it to turn on every 24 hours(maybe every 23 hours 50 minutes or so, allowing time for analysis).

I don't know if I am just missing the obvious or not thinking of the solution, but I haven't found anything that will do what I want yet.

import tweepy
import time
from tweepy import Stream
from tweepy.streaming import StreamListener

# continuously grabs tweet data on tweets containing any word in the animelist list
class myStreamListener(tweepy.StreamListener):

    def on_data(self, data):
        # data = tweet contents, time tweeted, who tweeted, etc.
        print(data)

        # determines the anime associated with the tweet (some have nothing to do with anime(80-90% are anime-related))
        # prints the anime associated with the tweet(if there is one)
        for anime in animelist:
            if anime.lower() in data.lower():
                print(anime)

            # if anime is found and in animelist, append the tweet data to tweets
            if anime in animelist:
                tweets.append(data)

        #example function(need to put my analysis code here and pause it for around 23 3/4 hours of the day)
        for i in range(100000):
            for i in range(1000000):
                print(i)
            time.sleep(10)

        # prints the length of the tweets list, ie. how many tweets have been grabbed so far
        print(len(tweets))

    # if there is an error, print the reason for the error
    def on_error(self, status):
        print(status)
        if status == 420:
            return False

        else:
            return True


# sets twitter_stream as the continuous stream of new tweets that are created
myStreamListeners = myStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener())

# initializes the class above to grab tweets as they are tweeted, matching any item in the animelist list (track)
myStream.filter(track=[anime for anime in animelist])

The main code for my twitter auth and such is not included for obvious reasons. This code is only the stream function where I will do my analysis.

The anime in the animelist list are scraped from an anime ranking website. Only the first 50 ranked anime are in the list. https://www.ranker.com/crowdranked-list/best-anime-series-all-time

0 Answers0