- 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.
|
#
¿
Feb 23, 2008 15:17
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 4, 2024 15:58
|
|
- 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"
|
#
¿
Feb 24, 2008 01:29
|
|
- 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?
|
#
¿
Mar 6, 2008 09:41
|
|
- 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.
|
#
¿
Mar 6, 2008 23:22
|
|