Вот код:
from tweepy.streaming import StreamListener
# oauth handler deals with the authentication
from tweepy import OAuthHandler
from tweepy import Stream
import twitter_credentials
from tweepy import API
from tweepy import Cursor
import matplotlib.pyplot as plt
import twitter_credentials
import numpy as np
import pandas as pd
# # # # Twitter Client # # # #
class TwitterClient():
def __init__(self, twitter_user = None):
self.auth = TwitterAuthenticator().aunthenticate_twitter_app()
self.twitter_client = API(self.auth)
# allow to specify user
self.twitter_user = twitter_user
def get_twitter_client_api(self):
return self.twitter_client
def get_user_timeline_tweeets(self, num_tweets):
tweets = []
for tweet in Cursor(self.twitter_client.user_timeline, id = self.twitter_user).items(num_tweets):
tweets.append(tweet)
return tweets
def get_friend_list(self, num_friends):
friend_list = []
for friend in Cursor(self.twitter_client.friends, id = self.twitter_user).items(num_friends):
friend_list.append(friend)
return friend_list
def get_home_timeline_tweets(self, num_tweets):
home_timeline_tweets = []
for tweet in Cursor(self.twitter_client.home_timeline, id = self.twitter_user).items(num_tweets):
home_timeline_tweets.append(tweet)
return home_timeline_tweets
# # # # TWITTER AUTHENTICATOR # # # #
class TwitterAuthenticator():
def aunthenticate_twitter_app(self):
auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
return auth
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
# class for streaming and processing live tweets
def __init__(self):
self.twitter_authenticator = TwitterAuthenticator()
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
listener = TwitterListener(fetched_tweets_filename)
auth = self.twitter_authenticator.aunthenticate_twitter_app()
stream = Stream(auth, listener)
stream.filter(track = hash_tag_list)
class TwitterListener(StreamListener):
"""
Basic listener class that just prints receieved textst stdout
"""
def __init__(self, fetched_tweets_filename):
self.fetched_tweets_filename = fetched_tweets_filename
def on_data(self, data):
try:
print(data)
with open(self.fetched_tweets_filename, 'a') as tf:
tf.write(data)
return True
except BaseException as e:
print("error on_data: %s" %str(e))
return True
def on_error(self, status):
# certain twitter errors, like 420, create a cooldown time that grows exponentially the more you do it, we don't want these errors
if status == 420:
return False
print(status)
class TweetAnalyzer():
'''
Function for analyzing and cateogrizing tweet information
'''
def tweets_to_data_frame(self, tweets):
df = pd.DataFrame(data = [tweet.text for tweet in tweets], columns = ['Tweets'])
df['id'] = np.array([tweet.id for tweet in tweets])
df['len'] = np.array([len(tweet.text) for tweet in tweets])
df['date'] = np.array([tweet.created_at for tweet in tweets])
df['source'] = np.array([tweet.source for tweet in tweets])
df['likes'] = np.array([tweet.favorite_count for tweet in tweets])
df['retweet'] = np.array([tweet.retweet_count for tweet in tweets])
return df
if __name__ == "__main__":
twitter_client = TwitterClient()
tweet_analyzer = TweetAnalyzer()
api = twitter_client.get_twitter_client_api()
tweets = api.user_timeline(screen_name = "realDonaldTrump", count = 200)
df = tweet_analyzer.tweets_to_data_frame(tweets)
# print(dir(tweets))
# print(df.head(10))
# np is the numoy command and df is a data frame, which is an array of arrays
print(np.mean(df['len']))
#get the num of likes for the most liked tweet
print(np.max(df['likes']))
# get the earliest date of the tweet
print(np.min(df['date']))
#get the number of retweets of the most retweeted
print(np.max(df['retweet']))
#visualize the data
# time series object to plot
# time_likes = pd.Series(data = df['likes'].values, index = df['date'])
# time_likes.plot(figsize = (16,4), color = 'r')
# plt.show()
#date vs retweets
# time_retweets = pd.Series(data = df['retweet'].values, index = df['date'])
# time_retweets.plot(figsize = (16,4), color = 'r')
# plt.show()
#plotting both on the same graph
time_retweets = pd.Series(data = df['retweet'].values, index = df['date'])
time_retweets.plot(figsize = (16,4), label = "retweets", legend = True)
time_likes = pd.Series(data = df['likes'].values, index = df['date'])
time_likes.plot(figsize = (16,4), label = "likes", legend = True)
plt.show()
Почему я получаю следующую ошибку:
Traceback (последний вызов был последним): Файл "c: / Users / Gokul / Desktop / NLP Stuff / .vscode / TwitterStuff / tweepy_streamer.py ", строка 1, из файла tweepy.streaming import из файла StreamListener" C: \ Users \ Gokul \ Anaconda3 \ lib \ site-packages \ tweepy__init __. Py ", строка 18, из потока импорта tweepy.streaming, файл StreamListener" C: \ Users \ Gokul \ Anaconda3 \ lib \ site-packages \ tweepy \ streaming.py ", строка 13, в файле импорта ssl" C: \ Users \ Gokul \ Anaconda3 \ lib \ ssl.py ", строка 98, в import _ssl #, если мы не можем импортировать его, разрешить распространению ошибки ImportError: Ошибка загрузки DLL: указанный модуль не найден.