10449

Struggling with a series of variables

Question:

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)<br /> 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

Answer1:

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 linkvars a list that you append to each loop

linkvar1 = [] for u in ... ... linkvar1.append(u) # Post to twitter for p in linkvar1: status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + p + " #propellerhead #synapse") status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + p + " #propellerhead #reason #guitar") status = api.PostUpdate("Free Metal and Rock drum samples!" + p + " #propellerhead #reason)

At 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 another for 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 variables

Option 2: Call a function to 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 for everyone!" + url + " #propellerhead #reason #guitar") status = api.PostUpdate("Free Metal and Rock drum samples!" + url + " #propellerhead #reason)

Each time the loop is iterated over, the MyTwitterFunction will be called with the value of u.

Option 3: Pull your posting code directly into your for loop

for u in ... status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + u + " #propellerhead #synapse") status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + u + " #propellerhead #reason #guitar") status = api.PostUpdate("Free Metal and Rock drum samples!" + u + " #propellerhead #reason)

This eliminates the need for the linkvar variables and the extra for loop. You can post directly from the loop in which the URLs are created.

Answer2:

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.:

for x in range(5): y = x

Will result in the value of 4 being assigned to y. Did you maybe want to make a list? For example:

y = [] for x in range(5): y.append(x)

which will result in

y = [0,1,2,3,4]

I think this is what you're aiming for with your link1,2,3 variables.

Recommend

  • Struggling with a series of variables
  • Using Tweepy API behind proxy
  • How to collect tweets about an event that are posted on specific date using python?
  • OAuth1 in Clojure
  • Twitter update with media with request.js
  • Curl PHP Run code
  • oAuth in PHP to make 2 legged request
  • How to send direct messages using OAuth?
  • Twitter::Error::Forbidden - Unable to verify your credentials
  • Add fields to Logstash Twitter input and Elasticsearch output
  • How should I use Consumer object in oauth2 v.2 in Python 3.4?
  • Tweepy SSLError regarding ssl certificate
  • How to switch from Twitter API single account use to multiaccount use, keeping it still a private ap
  • Why avoid CDO for sending mail in .NET?
  • Perl or Bash Script - Prefix Lines in text file with text from earlier in file
  • cudaMalloc of a structure and an element of same structure
  • How to filter input data of logstash based on date filed?
  • Delphi TClientSocket replacement using winsock2 and IOCP?
  • WSO2 APIM Custom sequences for non published APIs / Setting variable
  • How to catch tcp client request message in wso2 esb proxy service
  • Stopping Tweepy stream after a duration parameter (# lines, seconds, #Tweets, etc)
  • Ruby get mime-type based on extension
  • Off-screen navigation - right and left - css only
  • How do I move twitter configuration out of the controller? (Rails)
  • POST Method not working in rack-cors rails 4
  • What's the Rails.root equivalent in a Rack application?
  • How to write an install script for ubuntu
  • Subviews for custom UIView with Nib (.xib) don't load?
  • Grails 3 - How to publish to Artifactory
  • HTML 5 video (ogv) and MIME types
  • leaflet marker not displaying in certain contexts
  • Connection pooling with URLConnection?
  • Can't run Appium tests on iOS 10 on real device
  • XSD assert not recognised
  • Sending Content-Type application/x-www-form-urlencoded WSO2 ESB
  • back button function for phonegap windows phone 7
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • JaxB to read class hierarchy