Last active
August 29, 2015 14:17
-
-
Save huseyin/109cb652441dba7ac578 to your computer and use it in GitHub Desktop.
Piget için düzenlenmiş bir betik.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# Piget için düzenlenmiş betik. | |
# | |
import urllib | |
import urllib2 | |
import sys | |
#import pita | |
import socket | |
def HereDownload(adress=None, directory=None): | |
try: | |
pita.remaining(adress, directory) | |
oops = urllib.urlopen(adress) | |
fname = adress.split('/')[-1] | |
size = 0 | |
block0 = 4096 | |
block1 = 8192 | |
info = oops.info() | |
meta = int(info.getheaders("Content-Length")[0]) | |
save = open(directory+fname, "wb") | |
while True: | |
buffer = oops.read(block0) | |
if not buffer: | |
buffer = oops.read(block1) | |
if not buffer: | |
break | |
size += len(buffer) | |
save.write(buffer) | |
size_of = size * 100. / meta | |
if meta >= 1024*1024: | |
mib = meta / float(1024*1024) | |
status = r"[ %s ] [%3.2f MB] [%3.2f%%]" % (fname, (mib), size_of) | |
if mib >= 1024: | |
gib = gib / float(1024) | |
status = r"[ %s ] [%3.2f GB] [%3.2f%%]" % (fname, (gib), size_of) | |
elif meta < 1024*1024: | |
kib = meta / float(1024) | |
status = r"[ %s ] [%3.2f KB] [%3.2f%%]" % (fname, (kib), size_of) | |
status = status + chr(8) * (len(status) + 1) | |
print status, | |
except urllib2.URLError, e: | |
if isinstance(e.reason, socket.timeout): | |
raise """Detaylar: | |
bağlantı hatası [%s] | |
error no: %r""" %(adress, e) | |
except socket.timeout, e: | |
raise "bağlantı zaman aşımına uğradı. [%r]" %e | |
except urllib2.HTTPError, e: | |
sys.exit(0) | |
except KeyboardInterrupt: | |
print "İşlem kullanıcı tarafından iptal edildi." | |
sys.exit(0) | |
except IOError: | |
print "Bilgilendirme: ayarlarınızı kontrol ediniz." | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment