What's wrong in my code? Tweepy - python

I want to unfollow all people i follow.
What is wrong in my code?
import tweepy
#unfollow script
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# llölölöälöä
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def unfollow():
print("[+] Unfollowing in progress...")
for user in api.followers():
api.destroyfriendship(user)
test = input("Type ENTER if you want to start.")
test = True
if test is True:
unfollow()
else:
print("Thanks for using my script")
AttributeError: 'API' object has no attribute 'destroyfriendship'

Assuming you are using the latest version of the Tweepy API, you have typo in your method call.
api.destroyfriendship(user)
The correct call should be as follows:
api.destroy_friendship(user)
Please see the api-reference

Related

401 error code being returned when trying to stream live Tweets from a specific account with Tweepy

Anyone have any idea what could be causing the error? All my keys and tokens should be correct. I made sure to double check them. I followed the steps on how to set up the stream pretty arcuately I thought.
import time
import tweepy
import praw
#Variables that contains the credentials to access Twitter API and REDDIT
USERNAME = ""
PASSWORD = "!"
CLIENT_ID = ''
CLIENT_SECRET = ''
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, consumer_key)
api = tweepy.API(auth, wait_on_rate_limit=True)
class MyStreamListener(tweepy.Stream):
def on_status(self, status):
print("ID: {}".format(status.id))
print(status.full_text)
def streamtweets():
myStreamListener = MyStreamListener(consumer_key, consumer_secret,access_token, consumer_key)
myStream = tweepy.Stream(consumer_key, consumer_secret,access_token, consumer_key)
myStream.filter(follow = [''])
streamtweets()
You're passing your consumer_key as the access token secret.

Why is my Twitter bot not authenticating?

I have been following a tutorial directly, as well as cross referencing with tweepy documentation and my code is still not authorizing to Twitters' API. I have quadruple checked my key, secrets, and tokens. I've even changed the which variables go where. Anytime I try to use the authentication methods found in the #graveyard it tells me the authentication failed. Where am I going wrong?
import tweepy
import time
consumer_key = 'xxx'
consumer_secret = 'xxx'
#bearertoken = "xxx"
access_token = 'xxx'
access_token_secret = 'xxx'
#authenticate to twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#create api object
api = tweepy.API(auth, wait_on_rate_limit=True)
##create tweet
#api.update_status('test run good')
#GRAVEYARD
#if api.verify_credentials():
# print("Success")
#else:
# print("authentication failed")
#try:
# api.verify_credentials()
# print("authentication ok")
#except:
# print("authentication failed")

How to stream tweets from private accounts using Tweepy

I have been having problems with the MyStreamListener from tweepy, in where I am unable to fetch tweets from private accounts I follow. I know these tweets can be accessed with the API, because I can fetch them with api.user_timeline. Is there any way to be able to stream my whole timeline? Could I just continuously fetch my timeline in a while loop? Thank you for any help!
import tweepy
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
webhooklink = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print('starting to monitor!')
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
if __name__ == '__main__':
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(
follow='25073877', is_async=True)

Get list of followers and following for group of users tweepy

I was just wondering if anyone knew how to list out the usernames that a twitter user is following, and their followers in two separate .csv cells.
This is what I have tried so far.
import tweepy
import csv
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
csvFile = open('ID.csv', 'w')
csvWriter = csv.writer(csvFile)
users = ['AindriasMoynih1', 'Fiona_Kildare', 'daracalleary', 'CowenBarry', 'BillyKelleherTD', 'BrendanSmithTD']
for user_name in users:
user = api.get_user(screen_name = user_name, count=200)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.followers_id, user.friends_id user.description.encode('utf-8')])
print (user.id)
csvFile.close()
Tweepy is a wrapper around the Twitter API.
According to the Twitter API documentation, you'll need to call the GET friends/ids to get a list of their friends (people they follow), and GET followers/ids to get their followers.
Using the wrapper, you'll invoke those API calls indirectly by calling the corresponding method in Tweepy.
Since there will be a lot of results, you should use the Tweepy Cursor to handle scrolling through the pages of results for you.
Try the code below. I'll leave it to you to handle the CSV aspect, and to apply it to multiple users.
import tweepy
access_token = "1234"
access_token_secret = "1234"
consumer_key = "1234"
consumer_secret = "1234"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
for user in tweepy.Cursor(api.get_friends, screen_name="TechCrunch").items():
print('friend: ' + user.screen_name)
for user in tweepy.Cursor(api.get_followers, screen_name="TechCrunch").items():
print('follower: ' + user.screen_name)

Tweepy: Auto Tweet Images From Folder?

New here, First post aswell.
I'm currently trying to use Tweepy. I've successfully set it up so far and I'm able to tweet single images. So the code runs fine.
The purpose of this is because I run an account that tweets images only, no actual text tweets.
I've a folder of 100's of images I go through everyday to tweet and found out about tweepy, Is it possible to be able to tell Tweepy to go into the folder of the images and select 1 or any 1 at random? I've did extensive searching and couldn't find anything at all.
All help is greatly, greatly appreciated!
Here's the code I've got at the moment (python-2).
import tweepy
from time import sleep
consumer_key = 'Removed'
consumer_secret = 'Removed'
access_token = 'Removed'
access_token_secret = 'Removed'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_with_media('Image')
sleep(900)
print 'Tweeted!'
I'm assuming that you're iterating 100 times, given that you have 100 photos in your dir. I hope you don't mind, I took the liberty of placing your twitter api instantiation/auth in a function (for reusability's sake :) ). For the getPathsFromDir() function, I adapted GoToLoop's solution from processing.org. You might want to check out the link reference/link for more details. Also, practice placing your api.update[_with_media,_status]() in try - except blocks. You'll never know an odd exception would be raised by the api. I hope my implementation works for you!
import tweepy
from time import sleep
folderpath = "/path/to/your/directory/"
def tweepy_creds():
consumer_key = 'Removed'
consumer_secret = 'Removed'
access_token = 'Removed'
access_token_secret = 'Removed'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
def getPathsFromDir(dir, EXTS="extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp"):
return this.listPaths(folder, EXTS)
def tweet_photos(api):
imagePaths = getPathsFromDir(this.dataPath(folderpath))
for x in imagePaths:
status = "tweet text here"
try:
api.update_with_media(filename=x,status=status)
print "Tweeted!"
sleep(900)
except Exception as e:
print "encountered error! error deets: %s"%str(e)
break
if __name__ == "__main__":
tweet_photos(tweepy_creds())
/ogs

Categories

Resources