Python tweepy find user of search - python

import time
import tweepy
from auth import consumer_key, consumer_secret, access_token, access_token_secret
string_to_search = 'new car ideas'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
past_status_ids = set()
while True:
tweets = api.search(q=string_to_search)
tweet_id_list = set([tweet.id for tweet in tweets])
new_tweet_ids = tweet_id_list - past_status_ids
past_status_ids = tweet_id_list | past_status_ids
for tweet_id in new_tweet_ids:
print "Retweeting " + str(tweet_id)
api.retweet(tweet_id)
#username = tweets.user.screen_name
#api.create_friendship(username)
#print "Followed " + str(username)
limits = api.rate_limit_status()
remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']
print("Limit left is " + str(remain_search_limits))
print("")
time.sleep(150)
In a nutshell I am trying to find a string, then take that string retweet on my feed and follow that person.
My issue is how am I to find the Twitter ID to follow them via the search string?
I have been searching all around and can't find too many examples in Python tweepy for this.

If you want the User ID of the user who tweeted the tweet with status id tweet_id.
Use this
tweet = api.get_status(tweet_id)
user_id = tweet.user.id
# Now follow that user
api.create_friendship(user_id)

Related

There is a problem using "since" in Tweety function to extract Covaxin related hashtag tweets from the starting time of Covid'19

It is saying unexpected parameter :since when running the below code
import tweepy
# Enter your own credentials obtained
# from your developer account
consumer_key = "wwww"
consumer_secret = "xxxx"
access_key = "yyyy"
access_secret = "zzzz"
# The above keys are mentioned correctly in the programming code
# Twitter authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# Creating an API object
api = tweepy.API(auth)
hashtag_tweets = tweepy.Cursor(api.search_tweets, q="#Covaxin", tweet_mode='extended').items(5)
date_tweets = tweepy.Cursor(api.search_tweets, q="#Covaxin", since="2020-03-31", until="2022-01-09",tweet_mode='extended').items(5)
list = []
for tweet in date_tweets:
text = tweet._json["full_text"]
#print(text)
refined_tweet = {'text' : text,
'favorite_count' : tweet.favorite_count,
'retweet_count' : tweet.retweet_count,
'created_at' : tweet.created_at}
list.append(refined_tweet)
#print(list)
import pandas as pd
df = pd.DataFrame(list)
print(df)
df.to_csv('refined_tweets.csv')
It is saying unexpected parameter :since when running the code
I was trying to get the output for all tweets satisfying date query for the particular hashtag Covaxin from the starting of Covid days till now.

twitter app closes after opening(This App has violated Twitter Rules and policies)

I have a twitter app for many months
yesterday after adding "reply" feature for my app ,twitter limited my app
so i decided delete the app and creat a new one but when i set and replace api keys and passwords in my code ,my code still not work and closes after opening it in terminal
how can i solve this issue,i tried send my issue to twitter support but i have'n any helpful email
also for this issue i don't recive any email before that.
import tweepy
import time
consumer_key = "xxxxxxxxx"
consumer_secret = "xxxxxxxxx"
access_token = "xxxxxxxxxx"
access_token_secret = "xxxxxxxxxxx"
bearer_token = "xxxxxxxxxxx"
client_secret= "xxxxxxxxxxxxx"
client_id = "xxxxxxxxxxxxxxxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def on_status(self, tweet):
print(tweet.text)
try:
search = tweet.text
res = any(ele in search for ele in filter_words)
if res == True:
pass
else:
api.create_favorite(tweet.id)
if "#" in str(tweet.text):
if "مرز خسروی" in str(tweet.text):
status_id = tweet.user
username = "#" + status_id.screen_name
api.update_status(status = f"{username}مقدمتان گرامی باد،زیارتتان قبول",in_reply_to_status_id = tweet.id)
else:
pass
else:
api.retweet(tweet.id)
if "مرز خسروی" in str(tweet.text):
status_id = tweet.user
username = "#" + status_id.screen_name
api.update_status(status = f"{username}مقدمتان گرامی باد،زیارتتان قبول",in_reply_to_status_id = tweet.id)
else:
pass
except tweepy.TweepError as e:
print(e.reason)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
keywords = ["کرمانشاه","kermanshah","فرامان","کرماشان","کرمانشاهی","برناج","هرسین","کنگاور","پاوه",
"روانسر","جوانرود","نوسود","نودشه","هجیج","ژاپه","هوره","سرپل ذهاب","قصرشیرین","ثلاث باباجانی",
"تنگه مرصاد","کرند غرب","تاریکه بازار","مرز خسروی","دوکل","پاساژ ارگ","طاق بستان","بیستون","کوزران",
"نان برنجی","نان خرمایی","خورشت خلال","نوبهار پلازا","بازی دراز","بزره دماغ","پارک کوهستان",
"بستنی نوبهار","آبشوران","دالاهو","تاق وه سان","سراب قنبر","کرناچی","قره سو","چقامیرزا",
"کوچه لکا","دنده کباب","ترخینه","نواله","کلانه","پراو","فرخشاد","مرز پرویزخان","چیامیرزا","چیاکو",
"تنگه کنشت","سنقر","لشگر ۸۱","ماهیدشت","نیوی","جشن انار","جوانشیر",
"پاتاق","میدان تاجگذاری","چهارراه شیرخورشید","شاباد","گرگگه","قسقوان","شهرام ناظری","حسین صفامنش",
"پوران درخشنده","رحیم معینی کرمانشاهی","کزاری","میر جلال الدین کزاری","باختران","پرتو کرمانشاهی",
"یداله بهزاد","آیت الله نجومی","تکیه معاون الملک","تکیه بیگلربیگی","هورامان","مسجد جامع شافعی",
"خانه خدیوی","کلیسای پنطی کاستی","پل چهر","تاریکه بازار","مسجد عمادالدوله","مسجد حاج شهباز خان",
"مسجد شهباز خان","آرامگاه ویس","طاق گرا","فرهاد تراش","شکارگاه خسروپرویز","معبد آناهیتا",
"مجسمه هرکول","سان رستم","مادها","دو اشکفت","گاکیه","گودین تپه","آش عباسعلی","سورانه","پاغازه",
"ونوشک","خورشت کنگر","قزانچی","سراب نیلوفر","سراب یاوری","کوه شاهو","رودخانه سیروان","مدلل","لیلیوم",
"کسگم","سراب صحنه","چهارراه نوبهار","گوران","قلخانی","عید یاران"]
filter_words = ["بازنشستگان","#تجمع_اعتراضی","احمق","بازداشت","بپاخیز","اعتراض"
,"تجمع","گوه","سگ","عوضی","بیشعور","الدنگ","الاع","جنایتکار","گاو","خر","پوفیوز","زندان","کیر","سگ",
"سیاسی","اعدام","مرگ","رژیم","آخوند","کوس","کون","جنده","مادر","حروم زاده","حرام زاده","سرنگونی",
"قیام","رجوی","شاهزاده","شورشی","کولبر","حقوق","بیشرف","ریت","سرکوبگر","شکنجه","معتاد","ایدز",
"دیکتاتور","ناموس","غارتگر","#اعتراضات_سراسری","سکس","خامنه ای","خمینی","تظاهرات","فراخوان",
"سلیمانی","جوانان","شعار","هموطن","آبادان#","خیزش","خیابان","قیام_نهایی#","بپاخاسته","مسعود رجوی",
"اسلام","آخوند","آبادان","بپاخواسته","قیام#","کومله","دموکرات","صیغه","پژاک","خاله","قتل","دستگیر",
"کور","بی سواد","حزب","دادگستری","دادستان","قوه قضاییه","تجمعات","فاشیست","کسشر","کصشر","دوزاری",
"فحش","سپاه","اظلاعات","بازجویی","مامور","اوین","جرم","سنگسار"]
myStream.filter(track=keywords,languages=["fa"])

How to specify more than one coordinates for geocode parameter of API.search in Tweepy

I want to search tweets based on more than one coordinates. So, I tried this but it doesn't return any results:
total = 0
for status in tweepy.Cursor(api.search, q='cricket', lang="en",
geocode="24.8607,67.0011,25mi OR 40.7128,74.0060,20mi"
).items(10):
total+=1
print(total)
It's been a while since #Tayyap Mazhar shared the post, but just in case, the code below will work as expected. Just remember, do not put comma while declaring geoc variable!
import tweepy
import pandas as pd
CONSUMER_KEY = "?"
CONSUMER_SECRET = "?"
OAUTH_TOKEN = "?"
OAUTH_TOKEN_SECRET = "?"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
auth.secure = True
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
if (not api):
print ("Can’t Authenticate")
sys.exit(-1)
tweet_lst=[]
geoc=[('41.0,28.9499962,1km'),('41.1062629083,29.0264182277,1km'),('41.072833042,29.022833242,1km'),('41.05,28.91,1km')]
for geocode in geoc:
for tweet in tweepy.Cursor(api.search,geocode=geocode).items(1000):
tweetDate = tweet.created_at.date()
if(tweet.coordinates !=None):
tweet_lst.append([tweetDate,tweet.id,tweet.
coordinates['coordinates'][0],
tweet.coordinates['coordinates'][1],
tweet.user.screen_name,
tweet.user.name, tweet.text,
tweet.user._json['geo_enabled']])
tweet_df = pd.DataFrame(tweet_lst, columns=['tweet_dt', 'id', 'long','lat','username', 'name', 'tweet','geo'])```

Why is Tweepy saying a retweet is a quote?

Here's a simplified version of the code to reproduce the problem:
import tweepy
from tweepy import OAuthHandler
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_secret = '...'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
tweepy_api = tweepy.API(auth)
status_id = 999280226994671616
status = tweepy_api.get_status(status_id, tweet_mode='extended')
print(status.user.screen_name) # Prints 'VP' as it should
if status.retweeted: # Is false, even though it's a retweet
print('RETWEET:', status.retweeted_status.full_text)
elif status.is_quote_status: # Is true
qstatus = tweepy_api.get_status(status.quoted_status_id, tweet_mode='extended')
print(status.full_text, '\nQUOTE:', qstatus.full_text) # Doesn't print full text for either status
else:
print(status.full_text)
I'm wondering if the reason is_quote_status is true is because the retweet contains a quote. How do I detect whether the status is a retweet, then?
This is a secondary problem, but as my comments point out, it's also not displaying the full text (it does just fine when the status isn't a retweet or quote). What am I doing wrong?
A retweet is a tweet, and a tweet may quote another tweet, so a retweet can quote a tweet.
In your case :
The tweet id 999280226994671616 is from #vp.
It is a retweet of the tweet id 999275458796687360 from #SecondLady (https://twitter.com/SecondLady/status/999275458796687360).
And this tweet is quoting the tweet id 999013390528937991 from #SenEvanBayh (https://twitter.com/SenEvanBayh/status/999013390528937991)
To know if a tweet is a RT, check if the status has the attribute retweeted_status.
This :
status_id = 999280226994671616
status = tweepy_api.get_status(status_id, tweet_mode='extended')
print ('status.id_str:' + status.id_str)
print ('status.user.screen_name:#' + status.user.screen_name)
print()
if hasattr(status, 'retweeted_status'):
print ('status.retweeted_status.user.screen_name:' + status.retweeted_status.user.screen_name)
print ('status.retweeted_status.id_str:' + status.retweeted_status.id_str)
print()
qstatus = tweepy_api.get_status(status.retweeted_status.quoted_status_id, tweet_mode='extended')
print ('qstatus.id_str:' + qstatus.id_str)
print ('qstatus.user.screen_name:#' + qstatus.user.screen_name)
print()
will print :
status.id_str:999280226994671616
status.user.screen_name:#VP
status.retweeted_status.user.screen_name:SecondLady
status.retweeted_status.id_str:999275458796687360
qstatus.id_str:999013390528937991
qstatus.user.screen_name:#SenEvanBayh
Hope it helps.

python twitter crawling_how can I extract tweet date or tweet time?

Now the code extracts only the content of the tweet.
ex: 13703 : {'text': 'RT #11111: hi hi hi hi hi~~'}
I want to extract the date(2017.04.16) or time(21:40) of tweet.Is it possible?
import tweepy
import time
import os
import json
import simplejson
search_term = 'word1'
search_term2= 'word2'
search_term3='word3'
lat = "xxxx"
lon = "xxxx"
radius = "xxxx"
location = "%s,%s,%s" % (lat, lon, radius)
API_key = "xxxx"
API_secret = "xxxx"
Access_token = "xxxx"
Access_token_secret = "xxxx"
auth = tweepy.OAuthHandler(API_key, API_secret)
auth.set_access_token(Access_token, Access_token_secret)
api = tweepy.API(auth)
c=tweepy.Cursor(api.search,
q="{}+OR+{}".format(search_term, search_term2, search_term3),
rpp=1000,
geocode=location,
include_entities=True)
data = {}
i = 1
for tweet in c.items():
data['text'] = tweet.text
print(i, ":", data)
i += 1
time.sleep(1)
I submitted a bot TwEater today on GitHub. It can collect all possible attributes of tweets, such as date, time, text, username, id, hashtags, mentions, links, even emojis, and the list of all its replies and so on. You can even get tweets earlier than past 7 days, no 3200 tweets limit. Go try it and have fun!

Categories