Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
FrontLine
Sep 17, 2003
I gave Mr. Adequate hot, steamy man love and all he bought me was a custom title!
I wrote this short program as a cheap excuse to mess around with Python a bit more (only started learning about a week ago) and to screw around with VIM. Just wondering if there's any stupid newbie mistakes I've made or things I should watch out for. Considering it's pretty short I think I'm safe but just to be sure...

code:
#!/usr/bin/python
# Basic python program that will download a user specified number of images from WaffleImages.
# Please be warned that some of the images you may get back from WaffleImages could be NWS. This is unavoidable.

# Imports blah blah
import urllib
import urllib2
import os

# These headers may be redundant but it's what your browser sends when it makes a request so I've included it here just in case.
headers = {'Referer' : 'http://waffleimages.com/random', 'Cookie' : 'tg-visit=7e8303b895110be9868e2af52424643bcaa92c87;' + 
		'__utma=104184454.1558119543.1203770220.1203770220.1203770220.1; __utmb=104184454; __utmc=104184454; __' + 
		'utmz=104184454.1203770220.1.1.utmccn=(organic)|utmcsr=google|utmctr=waffle+images|utmcmd=organic'}

# This block specifys what data will be used when we send our GET to WaffleImages asking for a random image.
randomURL = 'http://waffleimages.com/random'
randomOpener = urllib2.build_opener()
randomRequest = urllib2.Request(randomURL, "", headers)

# Ask the user how many images they want.
iteration = raw_input('How many images do you want to download? ')

# Self explanatory.
print "Downloading... Please wait..."

# This is the work horse for the program and where I'm sure improvements can be made. 
# The loop runs the amount of times the user specified. It sends a GET to WaffleImages asking for a random image to be sent back.
# WaffleImages returns a web page with the location of the random image within. The program hunts down the link, currently located between
# characters 516 - 556 and requests that. The file is then saved as a .jpg. 
for i in xrange(int(iteration)):
	randomWriter = randomOpener.open(randomRequest).read()
	imageURL = 'http://img.waffleimages.com/' + str(randomWriter)[516:556]
	urllib.urlretrieve(imageURL, str(randomWriter)[516:556] + ".jpg")

print "Finished!"

# Things to be improved:
#	- A way of telling the user which file is being downloaded and how many there are left to download.
#	- A better way of finding the image URL. The current method will be prone to breaking if the html is changed by even a single character.
# Any suggestions are very much welcomed.
edit: Apologies for the heavy commenting. I just wanted to make sure what I was trying to do was obvious.

Adbot
ADBOT LOVES YOU

FrontLine
Sep 17, 2003
I gave Mr. Adequate hot, steamy man love and all he bought me was a custom title!

Wedge of Lime posted:

After a quick once over (I've not tested the program) the first thing I noticed was the lack of input validation.

For example, when asked how many images I want to download I type 'five.' what happens?

I've thrown this bit in. Effective?

code:
while True:
	iteration = raw_input('How many images do you want to download? ')
	try:
		checkInt = str(int(iteration))
		break
	except:
		print "Invalid input"

FrontLine
Sep 17, 2003
I gave Mr. Adequate hot, steamy man love and all he bought me was a custom title!

Milde posted:

Don't do "except:", that swallows all exceptions. And I'm not sure why you need to turn it back into a string. And the convention for naming non-class variables in Python is lowercase_with_underscores:
code:
while True:
    image_count = raw_input('Blah blah blah ')
    try:
        image_count = int(image_count)
        break
    except ValueError:
        print 'Whoa man, not cool.'

Thanks for the info :)

The example I found for checking for the int turned it into a string so I didn't think much of it. I also didn't know about python naming conventions, much appreciated :)

FrontLine
Sep 17, 2003
I gave Mr. Adequate hot, steamy man love and all he bought me was a custom title!
Does anybody know how/where to get the Python language pack for VS2005?

FrontLine
Sep 17, 2003
I gave Mr. Adequate hot, steamy man love and all he bought me was a custom title!

deimos posted:

AFAIK you can't integrate it, instead you get to use: IronPython Studio

Shock and awe!

Wikipedia lied to me.

Wikipedia, the lying encyclopedia posted:

Support for other languages such as F#, Python, and Ruby among others has been made available via language services which are to be installed separately.

  • Locked thread