版權沒有,翻印不究。

# -*- coding: UTF-8 -*-

import sys, os
from urllib import urlretrieve

def ratio_changed(ratio, last_ratio, tick):
    return int(ratio/tick)-int(last_ratio/tick) > 0

last_ratio = 0.
def report(block, size, total):
    global last_ratio
    ratio = float(block)*size/total*100
    if ratio_changed(ratio, last_ratio, 10):
        sys.stdout.write("%.1f%%"%ratio)
    elif ratio_changed(ratio, last_ratio, 1):
        sys.stdout.write(".")
    last_ratio = ratio

if len(sys.argv) < 2:
    sys.stdout.write("Please specify a url.\n")
    sys.exit()
url = sys.argv[1]
filename = url.split('/')[-1]
os.unlink(filename)
sys.stdout.write("[start]")
urlretrieve(url,filename,report)
sys.stdout.write("[done]\n")
sys.stdout.write("%s saved.\n"%filename)
Posted by yungyuc at 08:30, 0 comment, 0 trackback.
Change to page (10 entries in each page): 1
© hover year to navigate month: powered by django