缩短的URL需要随机化

时间:2014-04-06 22:26:33

标签: python twitter tinyurl

我有以下几乎正常工作的代码。缩短三个URL然后放入三个不同推文的内容中,然后将其提交给推特。但是,每次缩短URL时,缩短的URL都是相同的。因此,推文被Twitter垃圾邮件过滤器抓住了。

有没有办法随机化缩短网址的外观以阻止这种情况发生,无论是使用import tinyurl还是完全使用其他方法?

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

由于

1 个答案:

答案 0 :(得分:0)

循环遍历tinyurl.create的结果时,每次都将它分配给所有三个linkvar变量,因此当循环结束时,所有三个将等于{的最后一个值{1}}。

如果您总是要处理相同数量的URL,您可以将它们明确地分配给变量:

u

如果网址数量可能会发生变化,那么您最好使用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', ) 并将所需结果编入索引:

list