Tạo trình download giống như IDM trên Python

Đây là file thông dịch chạy trên 2.7.3

Chương trình sẽ download toàn bộ các file, thư mục và tất cả các file, thư mục con trên svn ở địa chỉ: http://android-scripting.googlecode.com/svn/trunk/beanshell/

Code:

import urllib,thread,re,time,os
base_url='http://android-scripting.googlecode.com/svn/trunk/'
base_folder='c:/test/'
folder=re.compile('<li><a href=\"(.+?\/)"')
_file=re.compile('<li><a href=\"([^/]+?)">')
max_thread=15
free_thread=max_thread
def get_page(url):
    return urllib.urlopen(base_url+url).read()
def download_folder(url):
    if not os.path.exists(base_folder+url):
        os.mkdir(base_folder+url)
    page=get_page(url)
    fl=_file.findall(page)
    fd=folder.findall(page)
    for i in fl:
        download_file(base_url+url+i,base_folder+url+i)
    for i in fd[1:]:
        download_folder(url+i)
def download_file(url,file_name):
    global free_thread
    print url
    while free_thread<1:
        time.sleep(1)
    free_thread-=1
    thread.start_new(worker,(url,file_name,))
def worker(url,file_name):
    global free_thread
    print url
    urllib.urlretrieve(url,file_name)
    free_thread+=1
download_folder('beanshell/')

2 Responses to “Tạo trình download giống như IDM trên Python”

  1. Hùng Anh Trịnh Says:

    Không ghi copyright.chết nhớ🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: