python - Struggling with a series of variables -


to avoid tweets becoming caught in twitter spam filter have code goes tinyurl , creates new short url each time code run each of original urls. want each time 'u' printed, it's value should passed variable 'linkvar1', 'linkvar2', 'linkvar3' etc. passed tweet submission later in code:

import simplejson import httplib2 import twitter import tinyurl  print("python attempt submit tweets twitter...")  try:      api = twitter.api(consumer_key='',                       consumer_secret='',                       access_token_key='',                       access_token_secret='')      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 everyone!" + linkvar2 + " #propellerhead #reason #guitar") status = api.postupdate("free metal , rock drum samples!" + linkvar3 + " #propellerhead #reason)   print("tweets submitted successfully!") 

except exception,e: print str(e)
print("twitter submissions have failed!!!")

however @ minute use tinyurl generated last tweets submitted. i'm sure easy fix i'm being stupid about, know how want?

thanks

your issue aren't doing linkvar variables through each loop. thus, each time loop runs, getting overwritten.

you have few options

option 1: make linkvars list append each loop

linkvar1 = [] u in ...    ...    linkvar1.append(u)  # post twitter p in linkvar1:     status = api.postupdate("the new synapse antidote rack extension:" + p + " #propellerhead #synapse")     status = api.postupdate("free propellerhead guitar patches everyone!" + p + " #propellerhead #reason #guitar")     status = api.postupdate("free metal , rock drum samples!" + p + " #propellerhead #reason) 

at end of first loop, have values in linkvar variable. not sure why using three, chopped down single instance. can loop through using for loop, or passed whole-sale own function handle them appropriately. in either case, of urls in list in each of these variables

option 2: call function perform on each loop

for u in ...    ...    mytwitterfunction(u)  def mytwitterfunction(url):     status = api.postupdate("the new synapse antidote rack extension:" + url + " #propellerhead #synapse")     status = api.postupdate("free propellerhead guitar patches everyone!" + url + " #propellerhead #reason #guitar")     status = api.postupdate("free metal , rock drum samples!" + url + " #propellerhead #reason) 

each time loop iterated over, mytwitterfunction called value of u.

option 3: pull posting code directly for loop

for u in ...     status = api.postupdate("the new synapse antidote rack extension:" + u + " #propellerhead #synapse")     status = api.postupdate("free propellerhead guitar patches everyone!" + u + " #propellerhead #reason #guitar")     status = api.postupdate("free metal , rock drum samples!" + u + " #propellerhead #reason) 

this eliminates need linkvar variables , for loop. can post directly loop in urls created.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -