0

I have the following code which is almost working correctly. Three URLs are shortened then put into the content of three different tweets which are then submitted to twitter. However each time the URL is shortened the shortened URL is the same. Because of this the tweets keep being caught by the twitter spam filter.

Is there a way to randomise the appearance of the shortened URLs to stop this from happening, either using import tinyurl or some other method entirely?

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!!!")

Thanks

Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • possible duplicate of [Struggling with a series of variables](http://stackoverflow.com/questions/22898046/struggling-with-a-series-of-variables) – Matthew Trevor Apr 07 '14 at 22:29

1 Answers1

0

When you loop through the result of tinyurl.create, you're assigning it to all three linkvar variables each time, so when the loop has ended all three will be equal to the last value of u.

If you are always going to process the same number of URLs, you can just assign them explicitly to the variables:

linkvar1, linkvar2, linkvar3 = 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',
)

If the number of URLs may change, then you're better off using a list and indexing the result you want:

linkvars = 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',
)
status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvars[0] + " #propellerhead #synapse")
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvars[1] + " #propellerhead #reason #guitar")
...
Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
  • sorry, the site was allowing me to edit the post for some reason once i realised they needed indenting...basically they should be intented within the for loop. that way the first instance of u gets fed to the first tweet, the second to the second tweet and third to the third tweet. the problem is that the url generated by tinyurl is always the same because the link being shortened is always the same. there is a custom domain option on the site itself that would get around this problem but i cannot find any example syntax on how to code that. – gdogg371 Apr 07 '14 at 17:36
  • If the code you've provided isn't correct, please update it. As it is, you're looping through three shortened urls: the first one you assign to all 3 `linkvars`, then the second you assign to all 3 `linkvars` etc. When the loop is completed, all 3 `linkvars` are equal to the _last_ shortened url. Have you tried the first suggestion I provided above? When I run that, I get 3 different tinyurl's assigned to the variables. – Matthew Trevor Apr 07 '14 at 22:26
  • @matthewtrevor...as above i cant...for some reason i can no longer edit code on SO...i dont know if it is a browser issue or a site problem. – gdogg371 Apr 07 '14 at 22:49