Why does printing a module make the code function? - python

I was working with this code. Until I included print tweepy in the code the page on GAE returned as blank; no error, or anything. It worked fine from the command line. Why would this happen? Is there a better way to make this work?
import tweepy
print tweepy
CONSUMER_KEY = "mine"
CONSUMER_SECRET = "mine"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
try:
print auth.get_authorization_url()
except Exception,e: print str(e)

Related

Status is a duplicate error. I’m trying to make my bot post a tweet, but it’s not working. (Python)

import tweepy
CONSUMER_KEY = 'My key'
CONSUMER_SECRET = 'My secret'
ACCESS_KEY = 'my key'
ACCESS_SECRET = 'my secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication OK")
except:
print("Error during authentication")
api.update_status("Hello")
Obviously at the top I don’t actually have my secret, my key, etc. up there, but I have the actual keys and it connects just fine. The problem is I keep getting an error whenever I try to send the tweet hello. Can anyone help?
You need to catch such exception.
I bet that the api throws DuplicateError (or similar as you don't show the stacktrace) when the status you set is identical.
something like:
from contextlib import suppress
from <your api module> import DuplicateError
...
with suppress(DuplicateError):
api.update_status("Hello")
...

Using Tweepy to get the results from saved searches on Twitter

I am trying to get the tweets from my saved searches using tweepy with python. I am not super great with Python, but the documentation seems pretty good, however, it is just returning empty lists. I know there are results for the searches because I can load them up in a browser. However, tweepy doesn't seem to see them.
import tweepy
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)
saved_searches = api.saved_searches()
for search in saved_searches:
saved_search = api.get_saved_search(search.id)
results = api.search(q=str(saved_search.query))
for result in results:
print(result.text)
Can anyone help me figure out what I am missing here?

tweepy unable to connect to twitter- No error returned

I am a beginner at python . I'm trying to get the follower counts of a given user handle from twitter. The issue is that tweepy is not connecting to twitter and is not even returning any error. The terminal just stays blank. Please help on this.
import tweepy
import pymysql
import time
#insert your Twitter keys here
consumer_key =''
consumer_secret=''
access_token=''
access_secret=''
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
global conn
conn=pymysql.connect(db='twitter', user='root' , host= 'localhost' , port=3307)
global cursor
cursor=conn.cursor()
print("entering loop")
while True:
query=cursor.execute("select twitter_name from timj_users where found_followers is null and twitter_name is not null order by id asc limit 1")
if query>0:
results=cursor.fetchone()
timj_handle=results[0]
user = tweepy.Cursor(api.followers, screen_name=timj_handle).items()
try:
followers=user.follower_count
location=user.location
cursor.execute("update timj_users set followers=%s,location=%s,found_followers=1 where twitter_name=%s" , (followers, location ,handle))
conn.commit()
print("user followers received")
if followers>100:
user.follow()
cursor.execute("update users set followed=1 where twitter_name=%s" , (handle))
conn.commit()
print("User followed")
except:
time.sleep(15*60)
print 'We got a timeout ... Sleeping for 15 minutes'
else:
print("All users processed")
break
If you're not getting an error from python and the console is just "hanging" you did actually connect to the Twitter, but since you have nothing in the code to display any message you get from Twitter you won't receive anything.
You need to include this in the code:
def on_error(self, status_code):
print(status_code)
That code will give you provide you with the number that is related to Twitter's Error Codes & Responses.
To be more clear:
except:
time.sleep(15*60)
print 'We got a timeout ... Sleeping for 15 minutes'
That exception is not being used what you think it is. The exception is raised if there's an error in the code you're writing, not errors you obtain from twitter.
It looks like this line
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
should be
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
However, you have written a really complicated for a beginner. Can I suggest that you run a more basic Tweepy program, and see what you get.
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
(From the Tweepy Documentation)

Skipping over Error?

Hi I was wondering if anyone could help:
I have the code beneath that contacts Twitter's API in order to get retweet count from a Twitter ID no. I was wondering if anyone could point me in the right direction as to how to enter the code so that it skips ID numbers the module (Tweepy) doesnt recognise instead of just stopping altogether?
EDIT: The Error being "TweepError: [{u'message': u'Sorry, that page does not exist', u'code': 34}]"
Thanks!
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)
id_list = ["4000000000000",
"etc.....",
]
def get_retweet_count(tweet_id):
tweet = api.get_status(tweet_id)
return tweet.retweet_count
for id in id_list:
print get_retweet_count(id), id
You want to use exception handling -- i.e. put a try except clause around any code that throws exceptions you think you can handle.
eg.
try:
print get_retweet_count(id)
except TweepError, e:
print e
print "carrying on anyway"
As thecoder mentioned, a try - except block should solve the problem:
for id in id_list:
try:
print get_retweet_count(id), id
except:
pass

How to get number of followers on twitter using tweepy

I am trying to output the number of followers one user has on twitter using tweepy, I have searched high and low to find some answers and I managed to get some code:
import oauth, tweepy, sys, locale, threading
from time import localtime, strftime, sleep
def init():
global api
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)
user = tweepy.api.get_user('...')
print user.screen_name
print user.followers_count
when I run this in python, i get errors of bad authentication,
could someone please explain why this is?
Thanks
You create the api object with the authentication, but then you don't use it and call tweepy directly.
This line:
user = tweepy.api.get_user('...')
Should be:
user = api.get_user('...')

Categories