Trying to use Twitter API but my code wont run anything - twitter

I am trying to use the twitter API in order to stream all tweets that include Michigan State, Spartans, and MSU. After I can figure this out I want to use different big10 key words. However, I run this code and I can't get past
ln (*) no matter how long I wait nothing is happening. Is there any issue with my code? Or how to I get the display of this information so I can analyze it?
THANK YOU!
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access Twitter API
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authentification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'MichiganState', 'Spartans', 'MSU'
stream.filter(track=['MichiganState', 'Spartans', 'MSU'])'

Use on_status(self, status) in the listener class:
class StdOutListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
print status.id

Related

How to get a user's new Tweets with a Telegram Python bot while run_polling?

I'm currently developing a Telegram bot using telegram-python-bot and tweepy.
I want to create a feature that allows users of the bot to add their Twitter ID list via Telegram and have their new Tweets sent to them in real-time.
I want that the bot should be application.run_polling() to receive commands from the user, and at the same time, forwarding new tweets from Twitter users in users individual list.
When I read the tweepy documentation, I realized that I can get real-time tweets with fewer api requests if I fetch them through MyStream(auth=auth, listener=None).
But I don't know how to get both functions to work on the same file at the same time.
version
nest_asyncio-1.5.6 python_telegram_bot-20.0 tweepy-4.12.1
def main() -> None:
application = Application.builder().token("...").build()
add_list = ConversationHandler(
entry_points=[CallbackQueryHandler(input_id, pattern='input_id')],
states={ADD :[MessageHandler(filters.TEXT & ~filters.COMMAND, add)],},
fallbacks=[CallbackQueryHandler(button,pattern='back')])
application.add_handler(CommandHandler("on", on))
application.add_handler(add_list)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("list", list_setting))
application.add_handler(CommandHandler("admin", admin))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CallbackQueryHandler(button))
application.run_polling()
if __name__ == "__main__":
main()
This is my main statement and I made it work until the SIGINT(ctrl+c) came in via application.run_polling().
I want to combine the above code to run and do the following at the same time.
import tweepy
consumer_key = "..." # Twitter API Key
consumer_secret = "..." # Twitter API Secret Key
access_token = "..." # Twitter Access Key
access_token_secret = "..." # Twitter Access Secret Key
usernames = ['...']
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
# Convert screen names to user IDs
user_ids = []
for username in usernames:
user = tweepy.API(auth).get_user(screen_name=username)
user_ids.append(str(user.id))
# Create a custom stream class
class MyStream(tweepy.Stream):
def __init__(self, auth, listener=None):
super().__init__(consumer_key, consumer_secret, access_token, access_token_secret)
def on_status(self, status):
tweet_url = f"https://twitter.com/{status.user.screen_name}/status/{status.id_str}"
print(f"{status.user.screen_name} tweeted: {status.text}\n{tweet_url}")
# send message to telegram
# Create a stream object with the above class and authentication
myStream = MyStream(auth=auth, listener=None)
# Start streaming for the selected users
myStream.filter(follow=user_ids)
I also tried to use thread's interval function or python-telegram-bot's job_queue.run_repeating function,
but these seem problematic for forwarding messages in real time.
I'm desperately looking for someone to help me with this😢.

Empty response from Twitter API v2 using Tweepy with organic_metrics

I am trying to download tweets by a specific user from Twitter API v2 using Tweepy. For some reason, when I add organic_metrics to tweet_fields I get an empty response. If I remove organic_metrics I receive tweets as a response as expected.
Here is an MWE.
import configparser
from pprint import PrettyPrinter
import tweepy
import logging
# Read Twitter authentication information from settings.ini
config = configparser.RawConfigParser()
config.read("settings.ini")
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
pp = PrettyPrinter()
# Setup access to API
def connect_to_twitter_OAuth():
client = tweepy.Client(
consumer_key=config["AUTHENTICATION"]["CONSUMER_KEY"],
consumer_secret=config["AUTHENTICATION"]["CONSUMER_SECRET"],
access_token=config["AUTHENTICATION"]["ACCESS_TOKEN"],
access_token_secret=config["AUTHENTICATION"]["ACCESS_SECRET"],
wait_on_rate_limit=True,
)
_logger.debug("Authenticated with Twitter with user context.")
return client
# Create API object
api = connect_to_twitter_OAuth()
tweet_fields = [
"id",
"text",
"conversation_id",
"created_at",
"in_reply_to_user_id",
"organic_metrics",
]
paginator = tweepy.Paginator(
api.get_users_tweets,
user_id,
max_results=5,
tweet_fields=tweet_fields,
user_auth=True,
)
_logger.debug("Retrieving tweets from user.")
for tweet in paginator.flatten(limit=250):
pp.pprint(tweet)
_logger.debug("Tweet retrieved.")
Any thoughts?
"Non-public, organic, and promoted metrics are only available for Tweets that have been created within the last 30 days."
https://developer.twitter.com/en/docs/twitter-api/metrics
This is why you cannot paginate through responses. Hope this answers some of your confusion!

Get/list all tweets of all users my account follow

I am using python tweepy to connect the twitter end point, and it's very simple to list all of any single user's tweets. It is also possible to read my account's "following" list, so technicly I can get the list of all the tweets by all of my followed users, thing is, it will be lot's and lot's of seperate API calls.
Is there a way to bulk this effectively?
You're not going to be able to get them all in one go, Twitter has rate limits to prevent that, but this script will get as many from each user as possible:
import tweepy
# Put your API keys here
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
# Authenticate to Tweepy and wait if you get rate limited
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
# For each user that you follow.....
for user in tweepy.Cursor(api.friends, screen_name="pigeonburger").items():
# Get each user's username and print out 100 of their tweets
username = user._json['screen_name']
print(api.user_timeline(screen_name = username, count = 100))
# Do what you want with those tweets after

Check if a certain user has tweeted

Is it possible to write a script for twitter that checks when the last time a certain user has tweeted?
Preferably using python.
Yes, it is possible. Here is how using TwitterAPI.
from TwitterAPI import TwitterAPI
SCREEN_NAME = 'justinbieber'
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
ACCESS_TOKEN_KEY = 'XXXX'
ACCESS_TOKEN_SECRET = 'XXXX'
api = TwitterAPI(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN_KEY,
ACCESS_TOKEN_SECRET)
r = api.request('statuses/user_timeline', {'screen_name':SCREEN_NAME, 'count':1})
for item in r:
print(item['created_at'])
There is a python library for accessing the Twitter API called Tweepy
More info on the API can be found here: https://dev.twitter.com
An older post but may be relevant: streaming api with tweepy only returns second last tweet and NOT the immediately last tweet

TWEEPY STREAM: my timeline tweets

I need to catch the tweets I recieve in my timeline from the people I follow.
The code I have is:
*import sys
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
CONSUMER_KEY = 'fgdg'
CONSUMER_SECRET = 'fdgdfgdf'
ACCESS_KEY = 'fgdfgd'
ACCESS_SECRET = 'dfgdfgdfg'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
class listener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["order"])*
But this give me the PUBLIC STREAM. I only want MY TIMELINE STREAM
Instead of the track method of the Stream object, you can use the userstream method of the Stream object. This returns only the data that appears on a user's personal timeline. To further limit the tweets returned, you might want to pass _with='user' to userstream. This limits the returned events to events only concerning the authenticated user, not their followings.
use
twitterStream.userstream(encoding='utf8')
instead of
twitterStream.filter(track=["order"])*

Resources