Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

My first Python program!


  • Please log in to reply
1 reply to this topic

#1 ViralFrost

ViralFrost

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 28 January 2012 - 10:46 AM

Hey everyone, I just joined this board and look forward to being a part of the community here. Last night I was bored and decided I'd learn some Python, so I created my first program last night...so here it is. Don't be too hard on me, as this is my first program (I'm sure it could be optimized and could handle exceptions and input errors)... but anyways, the program is for downloading sequenced images from websites. Here's a sample of some input:

URL of image folder:
http://www.gamersglo....uk/screenshot/

Image prefix:
world-of-warcraft-pc-0

First image number:
1

Last image number:
15

Leading 0 (this is for images 1-9 if for example image 1 is seen as 01) (y/n):
y

Extension:
jpg



The default download folder is C:\Images
if the folder doesn't already exist, the program creates it.

There is an error right now, where if there is a leading 0, it will download 10.jpg and 010.jpg

Also, the program will only work for less than 100 images if there is a leading 0.

Anyways, here's the code:

import urllib;
import os;
import random;

os.chdir('c:\\');

if os.path.exists('C:\\Images'):
	os.chdir('C:\\Images');
else:
	os.system('mkdir Images');
	os.chdir('C:\\Images');

print('Enter URL of image folder: ');
picture_page = raw_input();

print('Enter image prefix: ');
img_prefix = raw_input();

print('Enter starting image number: ');
img_fnum = raw_input();

print('Enter last image number: ');
img_lnum = raw_input();

print('Leading 0 (y/n): ');
lead0 = raw_input();

print('Enter file extension: ');
ext = raw_input();

a = range(int(img_lnum));

for x in a:
	if lead0 == 'y':
		if x < 10:
			a[int(x)] = '0' + str(x+1);
	else:
		a[int(x)] = str(x+1);

rfolder = random.random();
rfolder = rfolder * 7397;
rfolder = str(int(rfolder));
os.system('mkdir ' + rfolder);


for x in a:
		print('Source: ' + picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext);
		print('Destination: ' + os.getcwd() + '\\' + rfolder + '\\' + img_prefix + str(a[int(x)-1]) + '.' + ext);
		urllib.urlretrieve(picture_page + img_prefix + str(a[int(x)-1]) + '.' + ext, os.getcwd() + '\\' + rfolder + '\\' + img_prefix + str(a[int(x)-1]) + '.' + ext);


If you have some ideas as to how to better automate the process, or maybe how to check whether the image actually exists on the server before the program tries to download it, or any other suggestions or tips for me please let me know!
Thanks!
  • 0

#2 ViralFrost

ViralFrost

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 29 January 2012 - 04:03 PM

I've changed the script quite a bit with some help from a couple of people. Let me know what you think. The script has been redesigned to reflect that it can be used to download any file.

Sample input:

Save directory: C:\Downloads\
URL of file folder on server: http://blob.perl.org...beginning-perl/
File prefix: 3145_Chap
Starting file number: 1
Ending file number: 14
Leading 0s: 1
File extension: pdf



Updated code:

01	import urllib
02	import os
03	import random
04	 
05	#get input - local save directory
06	savdir = raw_input('\nEnter the directory to save to: ')
07	 
08	#if local save folder doesn't exist, create it - change directory to local save directory
09	if not os.path.exists(savdir):
10	    os.mkdir(savdir)
11	os.chdir(savdir)
12	 
13	#get input - URL, file prefix, first file number, last file number, number of leading 0s, file extension
14	file_folder = raw_input('Enter URL of containing folder: ')
15	fprefix = raw_input('Enter file prefix: ')
16	ffnum = raw_input('Enter starting file number: ')
17	flnum = raw_input('Enter ending file number: ')
18	num0 = raw_input('Number of leading 0s (on ones column ex: 001 would be 2 0s, 01 would be 1 0): ')
19	ext = raw_input('Enter file extension: ')
20	 
21	#create list of file numbers - add leading 0s
22	fmt = '{0}'
23	fmt = '{0:0' + str(int(num0)+1) + 'd}'
24	a = [fmt.format(x) for x in range(int(ffnum), int(flnum) + 1)]
25	 
26	#create random folder in local save directory to save files to
27	rfolder = str(int(random.random()*7384))
28	os.mkdir(rfolder)
29	 
30	#output file source and file destination - download files from URL and save to local save folder - output number of files downloaded / number of files
31	print('\n----Beginning download of ' + str(int(flnum) - int(ffnum) + 1) + ' files----\n')
32	for x in a:
33	        print('Src: ' + file_folder + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
34	        print('Dst: ' + os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
35	        urllib.urlretrieve(file_folder + fprefix + str(a[int(x) - 1 - int(ffnum)]) + '.' + ext, os.getcwd() + '\\' + rfolder + '\\' + fprefix + str(a[int(x) - int(ffnum)]) + '.' + ext)
36	        print('----Download ' + str(int(x) + 1 - int(ffnum)) + '/' + str(int(flnum) - int(ffnum) + 1) + ' complete----\n')

  • 0