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"])*
Related
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😢.
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!
I am new to tweepy. And tried to implement an api which can listen the new tweets for a #mention or screen name.
tweets = api.user_timeline(screen_name=username)
But it is not listening the new tweets. Could you please help me to understand how it can be implemented so that whenever a tweet comes with '#mention'. the api should listen immediately
Creating a StreamListener
The on_data method of Tweepy’s StreamListener conveniently passes data from statuses to the on_status method.
Create class MyStreamListener inheriting from StreamListener and overriding on_status.
import tweepy
override tweepy.StreamListener to add logic to on_status
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
Creating a Stream
Need an API to stream with the right credentials for authentications. Once we have an API and a status listener we can create our stream object.
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
start stream
myStream.filter(track=['python'])
myStream.filter(follow=["2211149702"])
Async stream
myStream.filter(track=['python'], is_async=True)
handling errors
class MyStreamListener(tweepy.StreamListener):
def on_error(self, status_code):
if status_code == 420:
#returning False in on_error disconnects the stream
return False
# returning non-False reconnects the stream, with backoff.
Firstly, get their twitter user id using this.
Then, use this:
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import StreamListener
listener = StreamListener()
auth = OAuthHandler(config.API_KEY, config.API_SECRET)
auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
stream.filter(follow=['1102105048253849600']) # user ID for random account
def on_data( self, status ):
print("Entered on_data()")
print(status.text, flush = True)
return True
...
user = api.verify_credentials()
# this is just for getting user. without this also connection can be established
if user:
print(user.id)
print('started')
twitter_stream=Stream(auth,StdOutListener())
print('Stream created...')
twitter_stream.filter(follow=[user.id_str], is_async=True)
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
I am trying to make an application for an organization which will required to fetch all past as well present tweets with some particular hashtag like #airtel, #airtel etc, how should I get past tweet, I am able to fetch the present tweet with the following url : "https://api.twitter.com/1.1/search/tweets.json?q=%23airtel"
Thanks
You can get up to a maximum of 100 tweets with the twitter rest api see the following twitter documentation. The best you can do is use the count parameter https://api.twitter.com/1.1/search/tweets.json?q=%23airtel&count=100
After various search on Google I found some useful library for fetching tweets e.g:
TwitterSearch [https://github.com/ckoepp/TwitterSearch], you would I find the twitter profile # https://twitter.com/twittersearch
Tweepy [https://github.com/tweepy/tweepy], you could find more info at http://www.tweepy.org/
I have implemented using both. Tweepy implementation is as follows:
import tweepy
import json
consumer_key = "get"
consumer_secret = "From"
access_token = "Twitter"
access_token_secret = "site"
# Authenticate twitter Api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#made a cursor
c = tweepy.Cursor(api.search, q='%23Coursera')
c.pages(15) # you can change it make get tweets
#Lets save the selected part of the tweets inot json
tweetJson = []
for tweet in c.items():
if tweet.lang == 'en':
createdAt = str(tweet.created_at)
authorCreatedAt = str(tweet.author.created_at)
tweetJson.append(
{'tweetText':tweet.text,
'tweetCreatedAt':createdAt,
'authorName': tweet.author.name,
})
#dump the data into json format
print json.dumps(tweetJson)
If any one have problem, let me know, will provide git repo for this.
Thanks
Krishna