Exception Error Pass Issues - python

I am trying to make the program check the csv file every 3 seconds for a line to tweet, and if it finds duplicates, it terminates, so i am trying to figure out how to pass the duplicate error correctly
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
argfile = str(sys.argv[1])
CONSUMER_KEY = 'example'
CONSUMER_SECRET = 'example'
ACCESS_KEY = 'example'
ACCESS_SECRET = 'example'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
filename=open(argfile,'r')
f=filename.readlines()
filename.close()
for line in f:
try:
api.update_status(line)
except tweepy.error.TweepError:
pass
time.sleep(3)#Tweet every 3 seconds
idk if its because of the indentation on the error section, but it will not pass the error. i get invalid syntax and indentation error for the except and pass lines, can't figure out how to do it correctly.

This line has an extra single quote before the string:
ACCESS_SECRET = ' 'example'
Remove one of them before the "example".

Related

Twitter bot that replies to all the tweets of my friend. Python

Hello I am searching how to create a Twitter bot that replies to all the tweets of a specific user in Python.
I already created a developer's account and I am a beginner in Python.
First, visit https://dev.twitter.com, and create a new application.
head your venv or anaconda and execute
pip install tweepy
Now, in your development directory, create a file, keys.py, and add the following code:
#!/usr/bin/env python
#keys.py
#visit https://dev.twitter.com to create an application and get your keys
keys = dict(
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
)
Replace the ‘x’ fields with your keys and tokens from your newly created Twitter application
create a file, replybot.py, in the same directory as keys.py, and add the following code:
#!/usr/bin/env python
import tweepy
#from our keys module (keys.py), import the keys dictionary
from keys import keys
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)
api = tweepy.API(auth)
twts = api.search(q="Hello World!")
#list of specific strings we want to check for in Tweets
t = ['Hello world!',
'Hello World!',
'Hello World!!!',
'Hello world!!!',
'Hello, world!',
'Hello, World!']
for s in twt:
for i in t:
if i == s.text:
sn = s.user.screen_name
m = "#%s Hello!" % (sn)
s = api.update_status(m, s.id)
Check if your API is working . sleep is you ensure you are not asked to validate you are a bot python <program.py> .txt
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
argfile = str(sys.argv[1])
#enter the corresponding information from your Twitter application:
CONSUMER_KEY = '1234abcd...'#keep the quotes, replace this with your consumer key
CONSUMER_SECRET = '1234abcd...'#keep the quotes, replace this with your consumer secret key
ACCESS_KEY = '1234abcd...'#keep the quotes, replace this with your access token
ACCESS_SECRET = '1234abcd...'#keep the quotes, replace this with your access token secret
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
filename=open(argfile,'r')
f=filename.readlines()
filename.close()
for line in f:
api.update_status(line)
time.sleep(900)#Tweet every 15 minutes
To reply to specific twitter user
toReply = "someonesTwitterName" #user to get most recent tweet
api = tweepy.API(auth)
#get the most recent tweet from the user
tweets = api.user_timeline(screen_name = toReply, count=1)
for tweet in tweets:
api.update_status("#" + toReply + " This is what I'm replying with", in_reply_to_status_id = tweet.id)
you can code whatever logic you want

Can't send tweets with python using Twitter API (foreign letters)

Sooo yeah, pretty much what the title says. Can't seem to tweet foreign letter's to twitter, not quite sure why though, so i'm hoping if anybody can help me there. The foreign letters are "õüöä" Here is my .py file:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
argfile = str(sys.argv[1])
#API details
CONSUMER_KEY = 'xx'
CONSUMER_SECRET = 'xx'
ACCESS_KEY = 'xx'
ACCESS_SECRET = 'xx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
str.decode('utf-8')
filename=open(argfile,'r')
f=filename.readlines()
filename.close()
for line in f:
api.update_status(line)
time.sleep(120)
My .txt file where tweets are included:
Üks
Kaks
Kolm
Neli
Viis
Kuus
Seitse
Kaheksa
Üheksa
Kümme

Authorization in tweepy

I'm followed a tutorial for using tweepy to post something to twitter, everything works but i gave the code to a friend so he could post something using my twitter app(with my credentials consumer key and secret). mine works because i already have my acces token, but how can he get the his access token?
i have tried using the Oauth tutorial, but every example ask for the PIN, but i can't manage to get that PIN.
here's my code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
argfile = str(sys.argv[1])
#enter the corresponding information from your Twitter application:
CONSUMER_KEY = 'MY TOKEN'#keep the quotes, replace this with your consumer key
CONSUMER_SECRET = 'MY TOKEN'#keep the quotes, replace this with your consumer secret key
ACCESS_KEY = '?????????????????????????'#keep the quotes, replace this with your access token
ACCESS_SECRET = '?????????????????????????'#keep the quotes, replace this with your access token secret
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
filename=open(argfile,'r')
f=filename.readlines()
filename.close()
for line in f:
api.update_status(line)
time.sleep(180)#Tweet every 3 minutes
Nevermind, just leave callback blank and it will give you a PIN.
Also with another code and the PIN i can store the tokens and use it in my app.
SOLVED.

Post tweet with tweepy

I'm trying to post a tweet with the tweepy library. I use this code:
import tweepy
CONSUMER_KEY ="XXXX"
CONSUMER_SECRET = "XXXX"
ACCESS_KEY = "XXXX"
ACCESS_SECRET = "XXXX"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('Updating using OAuth authentication via Tweepy!')
But when I run the application, I receive this error:
raise TweepError(error_msg, resp)
TweepError: Read-only application cannot POST.
How can I fix this?
In the application's settings, set your Application Type to "Read and Write". Then renegotiate your access token.
the code works for me with only
api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
You have to set your app to read and write
After that, you'll be able to run your code.
the following python script will tweet a line from a text file. if you want to tweet multiple tweets just put them on a new line separated by a blank line.
import tweepy
from time import sleep
# import keys from cregorg.py
from credorg import *
client = tweepy.Client(bearer_token, 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)
print("Tweet-TXT-Bot v1.0 by deusopus (deusopus#gmail.com)")
# Open text file tweets.txt (or your chosen file) for reading
my_file = open('tweets.txt', 'r')
# Read lines one by one from my_file and assign to file_lines variable
file_lines = my_file.readlines()
# Close file
my_file.close()
# Tweet a line every 5 minutes
def tweet():
for line in file_lines:
try:
print(line)
if line != '\n':
api.update_status(line)
sleep(300)
else:
pass
except tweepy.errors.TweepyException as e:
print(e)
while True:
tweet()

Why does printing a module make the code function?

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)

Categories