To avoid tweets becoming caught in the twitter spam filter I have some code that goes to tinyurl and creates a new short url each time the code is run for each of the original urls. What i want is each time 'u'
is printed, it's value should be passed to a variable 'linkvar1', 'linkvar2', 'linkvar3'
etc. This is the passed to the tweet submission later in the code:
import simplejson
import httplib2
import twitter
import tinyurl
print("Python will now attempt to submit tweets to twitter...")
try:
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='')
for u in tinyurl.create('http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html',
'http://audiotechracy.blogspot.co.uk/2014/03/free-guitar-patches-for-propellerhead.html',
'http://audiotechracy.blogspot.co.uk/2014/02/get-free-propellerhead-rock-and-metal.html',
):
print u
linkvar1 = u
linkvar2 = u
linkvar3 = u
status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvar1 + " #propellerhead #synapse")
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvar2 + " #propellerhead #reason #guitar")
status = api.PostUpdate("Free Metal and Rock drum samples!" + linkvar3 + " #propellerhead #reason)
print("Tweets submitted successfully!")
except Exception,e:
print str(e)
print("Twitter submissions have failed!!!")
However at the minute all this does is use the tinyurl generated last for all tweets submitted. I'm sure this is an easy fix that I'm just being stupid about, but does anyone know how to do what I want?
Thanks
Your issue is that you aren't doing anything with your
linkvar
variables through each loop. Thus, each time the loop runs, they are getting overwritten.You have a few options
Option 1: Make the
linkvar
s a list that you append to each loopAt the end of your first for loop, you will have values in the
linkvar
variable. I am not sure why you are using three, do I chopped it down to just the single instance. You can then loop through using anotherfor
loop, or passed whole-sale to your own function that will handle them appropriately. In either case, all of your urls are now in a list in each of these variablesOption 2: Call a function to perform on each loop
Each time the loop is iterated over, the
MyTwitterFunction
will be called with the value ofu
.Option 3: Pull your posting code directly into your
for
loopThis eliminates the need for the
linkvar
variables and the extrafor
loop. You can post directly from the loop in which the URLs are created.I'm not certain what you mean by "passed to a variable". It looks as though you're assigning the value of u to each of your 3 variables and then over-writing it - e.g.:
Will result in the value of 4 being assigned to y. Did you maybe want to make a list? For example:
which will result in
I think this is what you're aiming for with your link1,2,3 variables.