I try to use thread in my script but i get this error:
Unhandled exception in thread started by sys.excepthook is missing
lost sys.stderr
My script:
# -*- coding: utf-8 -*-
import tweepy
import thread
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
def deleteThread(api, objectId):
try:
api.destroy_status(objectId)
print "Deleted:", objectId
except:
print "Failed to delete:", objectId
def oauth_login(consumer_key, consumer_secret):
"""Authenticate with twitter using OAuth"""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth_url = auth.get_authorization_url()
verify_code = raw_input("Authenticate at %s and then enter you verification code here > " % auth_url)
auth.get_access_token(verify_code)
return tweepy.API(auth)
def batch_delete(api):
print "You are about to Delete all tweets from the account #%s." % api.verify_credentials().screen_name
print "Does this sound ok? There is no undo! Type yes to carry out this action."
do_delete = raw_input("> ")
if do_delete.lower() == 'yes':
for status in tweepy.Cursor(api.user_timeline).items():
try:
thread.start_new_thread( deleteThread, (api, status.id, ) )
except:
print "Failed to delete:", status.id
if __name__ == "__main__":
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
print "Authenticated as: %s" % api.me().screen_name
batch_delete(api)
How to solve this problem?
Update
I just solve my problem by adding time.sleep(1) before
thread.start_new_thread( deleteThread, (api, status.id, ) )
Related
I've written folowing code in python for followers scraping:
import tweepy
import time
import csv
import sys
import random
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, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, retry_errors=set([401, 404, 500, 502, 503, 504]))
account = 'setavakfi'
log_file = '{}_followers.csv'.format(account)
retry = 0
errorCount = 0
tweepy_cursor = tweepy.Cursor(api.followers, screen_name=account, count=200, cursor=1574812962976647290).pages()
followers_count = []
while True:
try:
retry =0
user = tweepy_cursor.next()
cursor = tweepy_cursor.next_cursor
followers_count += user
print 'Retrieved {} followers accounts'.format(len(followers_count))
print 'Current cursor: {}'.format(cursor)
with open(log_file, 'ab') as fd:
writer = csv.writer(fd)
for i, user in enumerate(user):
writer.writerow([str("#"+user.screen_name), unicode(user.name).encode('utf-8'), str(user.lang), unicode(user.location).encode('utf-8')])
print "Resting..."
time.sleep(random.randint(60, 70)
except tweepy.TweepError as e:
print "Error code: {} with message: {}".format(e.api_code, e.message[0]['message'])
errorCount +=1
retry += 1
print 'Retrying in {} seconds'.format(60+retry*5)
time.sleep(60+retry*5)
if retry == 10:
break
except StopIteration:
break
print 'Done with {} errors'.format(errorCount)
Problem is that with given cursor (1574812962976647290) and account (#setavakfi) I'm only getting error 503 (code 130). This cursor is stuck exacly at page with 8000 folllowers. Whole account is >60,000 followers. I've tried this code on different accounts with more than 60,000 folowers and it works. Have tried to change ISP, IP address and twitter dev account. Nothing change.
Can you see what could be wrong with this code? Is it a problem with this single account? Is there a way to automaticly jump over problematic cursor to see if other cursors will have same problem?
Thanks in advance.
M.
I think that error is because a server overload on the twitter side with this account... I don't think it's an error with your code.
I'm trying to make a twitter bot using tweepy and python but I can't figure out how to reply to a tweet.
import tweepy
from Keys import keys
import time
CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
auth.secure = True
api = tweepy.API(auth)
message = " Test message"
for tweet in tweepy.Cursor(api.search, q='search_item', lang = 'en').items(1):
try:
print ("Found tweet by:#" + tweet.user.screen_name)
api.update_status('#' + tweet.user.screen_name + message)
print 'responded to #' + tweet.user.screen_name
if tweet.user.following == False:
tweet.user.follow()
print ("following #" + tweet.user.screen_name)
except tweepy.TweepError as e:
print(e.reason)
time.sleep(3)
continue
except tweepy.RateLimitError:
time.sleep(15*60)
except StopIteration:
break
I've got it to post a tweet with #username and then say the message but can't get it to reply.
Well then, it was something simple. I had to specify who the tweet was directed towards using the # notation.
api.update_status('My status update #whoIReplyTo',tweetId)
I figured it out eventually.
The code for replying to a tweet is:
api.update_status(status = "your message here", in_reply_to_status_id = tweet.id_str)
I've been using Tweepy with Python 2.7 to stream tweets and everything has been working fine, except the on_direct_message() method isn't being called when I send the account a direct message. I've updated my permissions and even tried using the on_data() method, but it can't seem to detect direct messages being sent to the account:
import tweepy
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
followed_accounts = ['account', 'account']
followed_ids = []
for account in followed_accounts:
followed_ids.append(str(api.get_user(screen_name=account).id))
class StdOutListener(tweepy.StreamListener):
def on_direct_message(self, status):
author = status.author.screen_name
api.send_direct_message(screen_name=author, text='response')
return True
def on_status(self, status):
author = status.author.screen_name
statusID = status.id
print status.text + "\n"
api.update_status('response')
api.send_direct_message(screen_name='my username', text='Just sent a Tweet')
return True
def on_data(self, status):
print 'Entered on_data()'
print status
return True
def on_error(self, status_code):
print "Error Code: " + str(status_code)
if status_code == 420:
return False
else:
return True
def on_timeout(self):
print('Timeout...')
return True
if __name__ == '__main__':
listener = StdOutListener()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
stream = tweepy.Stream(auth, listener)
stream.filter(follow=followed_ids)
Sending the account a direct message gives no errors, and the account receives the message properly on Twitter.
Guys i wanna save twitter user info like name, statuses, tweet in my file (either json,txt,csv or any other json or text are prefered). I tried this code and some other similar but none of them work. Guys have a look at below code and suggest me what changes should i made??
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os
import json
ckey = '**********'
consumer_secret = '**********'
access_token_key = '**********'
access_token_secret = '**********'
start_time = time.time() #grabs the system time
keyword_list = ['twitter'] #track list
#Listener Class Override
class listener(StreamListener):
def __init__(self, start_time, time_limit=60):
self.time = start_time
self.limit = time_limit
def on_data(self, data):
while (time.time() - self.time) < self.limit:
try:
all_data = json.loads["text"]
username = all_data["user"]["name"]
tweets = all_date["user"]["statuses"]
saveFile = open('raw_tweets29.json', 'a')
saveFile.write(username)
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,', str(e)
time.sleep(5)
pass
exit()
def on_error(self, status):
print statuses
auth = OAuthHandler(ckey, consumer_secret) #OAuth object
auth.set_access_token(access_token_key, access_token_secret)
twitterStream = Stream(auth, listener(start_time, time_limit=20))
twitterStream.filter(track=['twitter'])
when i run below code this give me error -
failed ondata, 'function' object has no attribute '__getitem__'
I would greatly appreciate any help you can give me in working this problem
I am doing some mistake, now i figure it out there is no need of temp variable 'text' what i need to do is load actual data.
there is one more thing require is encoding.
thanks everyone for your time.
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os,sys
import json
ckey = '***'
consumer_secret = '***'
access_token_key = '***'
access_token_secret = '***'
start_time = time.time()
class listener(StreamListener):
def __init__(self, start_time, time_limit=300):
self.time = start_time
self.limit = time_limit
def on_data(self, data):
while (time.time() - self.time) < self.limit:
try:
tweet = json.loads(data)
user_name = tweet['user']['name']
tweet_count = tweet['user']['statuses_count']
text = tweet['text']
saveFile = open('user_tweets29.json', 'a')
saveFile.write(text.encode('utf8'))
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,', str(e)
time.sleep(5)
pass
exit()
def on_error(self, status):
print statuses
auth = OAuthHandler(ckey, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
twitterStream = Stream(auth, listener(start_time, time_limit=60))
twitterStream.filter(track=['twitter'])
I am attempting to make a script that searches in the user timeline, then favorites tweets. For some reason, it isnt working.
I wrote this code:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import *
import tweepy, json
class StdOutListener(StreamListener):
def on_data(self, data):
data = json.loads(data)
try:
api.create_favorite(data[id])
except:
pass
print 'Favoriting tweet id ' + data[id] + ' in twitter timeline...'
return True
def on_error(self, status):
print status
l = StdOutListener()
auth = tweepy.OAuthHandler('x', 'x')
auth.set_access_token('x-x', 'x')
api = tweepy.API(auth)
stream = Stream(auth, l)
userz = api.followers_ids(screen_name='smileytechguy')
keywords = ['ebook', 'bot']
stream.filter(track=keywords, follow=userz)
But I am getting this Error message
Traceback (most recent call last):
File "FavTL.py", line 27, in <module>
stream.filter(track=keywords, follow=userz)
File "build\bdist.win-amd64\egg\tweepy\streaming.py", line 310, in filter
AttributeError: 'long' object has no attribute 'encode'
any idea on how can I fix it.
This code should work. Don't forget to enable writing through your API-keys
consumer_key = '..'
consumer_secret = '..'
access_token = '..'
access_secret = '..'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class StdOutListener(StreamListener):
def on_data(self, data):
# Twitter returns data in JSON format - we need to decode it first
decoded = json.loads(data)
tweet_id = decoded['id']
api.create_favorite(tweet_id)
print 'Favoriting tweet id ' + str(tweet_id) + ' in twitter timeline...'
time.sleep(65)
return True
def on_error(self, status):
if(status == 420):
print "Twitter is limiting this account."
else:
print "Error Status "+ str(status)
l = StdOutListener()
api = tweepy.API(auth)
stream = Stream(auth, l)
userz = api.followers_ids('smileytechguy')
keywords = ['ebook', 'bot']
stream.filter(track=keywords, follow=str(userz))