def test_gigabyte(self): self.assertEquals('2.00 Gb', size2text(2*1024*1024*1024)) self.assertEquals('2.20 Gb', size2text(2.2*1024*1024*1024))
def test_megabyte(self): self.assertEquals('3.00 Mb', size2text(3*1024*1024)) self.assertEquals('3.30 Mb', size2text(3.3*1024*1024))
def download(self): bean = self.bean update = self.update if not bean or not bean.path: return None opener = FancyURLopener() remote = opener.open(bean.path) remote_size = 0 if "Content-Length" in remote.headers: remote_size = int(remote.headers["Content-Length"]) bean.size = size2text(remote_size) block_size = 4096 block_count = 0 ext = get_file_extension(bean.path) path = FC().online_save_to_folder if not os.path.isdir(path): os.makedirs(path) if bean.save_to: to_file = os.path.join(bean.save_to, bean.text + ".mp3") else: to_file = get_bean_download_path(bean, FC().online_save_to_folder) if not os.path.exists(os.path.dirname(to_file)): os.makedirs(os.path.dirname(to_file)) to_file_tmp = to_file + ".tmp" if os.path.exists(to_file_tmp): bean.status = DOWNLOAD_STATUS_INACTIVE bean.to_file = to_file update(bean) return None if os.path.exists(to_file): bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file update(bean) return None bean.save_to = to_file with open(to_file_tmp, "wb") as tmp_file: data = True """begin download""" self.bean.status = DOWNLOAD_STATUS_DOWNLOADING self.bean.path = to_file self.update(self.bean) while data: data = remote.read(block_size) if data: block_count += 1 tmp_file.write(data) #time.sleep(0.1) persent = block_count * block_size * 100.0 / remote_size if block_count % 50 == 0: bean.persent = persent update(bean) time.sleep(0.5) """update file info on finish""" logging.debug("rename %s - %s" % (to_file_tmp, to_file)) os.rename(to_file_tmp, to_file) bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file bean.persent = 100 update(bean)
def test_kilobyte(self): self.assertEquals('4.00 Kb', size2text(4*1024)) self.assertEquals('4.40 Kb', size2text(4.4*1024))
def test_gigabyte(self): self.assertEquals('2.00 Gb', size2text(2 * 1024 * 1024 * 1024)) self.assertEquals('2.20 Gb', size2text(2.2 * 1024 * 1024 * 1024))
def test_kilobyte(self): self.assertEquals('4.00 Kb', size2text(4 * 1024)) self.assertEquals('4.40 Kb', size2text(4.4 * 1024))
def test_megabyte(self): self.assertEquals('3.00 Mb', size2text(3 * 1024 * 1024)) self.assertEquals('3.30 Mb', size2text(3.3 * 1024 * 1024))
def download(self): bean = self.bean update = self.update if not bean or not bean.path: return None opener = FancyURLopener() remote = opener.open(bean.path) remote_size = 0 if "Content-Length" in remote.headers: remote_size = int(remote.headers["Content-Length"]) bean.size = size2text(remote_size) block_size = 4096 block_count = 0 ext = get_file_extension(bean.path) path = FC().online_save_to_folder if not os.path.isdir(path): os.makedirs(path) if bean.artist: bean.artist = bean.artist.replace("/", "-") bean.artist = bean.artist.replace("\\", "-") to_file = os.path.join(FC().online_save_to_folder, bean.artist, bean.get_display_name() + ext) if not os.path.isdir(os.path.dirname(to_file)): os.makedirs(os.path.dirname(to_file)) else: to_file = os.path.join(path, bean.get_display_name() + ext) to_file_tmp = to_file + ".tmp" if os.path.exists(to_file_tmp): bean.status = DOWNLOAD_STATUS_INACTIVE bean.to_file = to_file update(bean) return None if os.path.exists(to_file): bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file update(bean) return None bean.save_to = to_file file = open(to_file_tmp, "wb") data = True """begin download""" self.bean.status = DOWNLOAD_STATUS_DOWNLOADING self.update(self.bean) while data: data = remote.read(block_size) if data: block_count += 1 file.write(data) #time.sleep(0.1) persent = block_count * block_size * 100.0 / remote_size if block_count % 50 == 0: bean.persent = persent update(bean) """update file info on finish""" os.rename(to_file_tmp, to_file) bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file bean.persent = 100 update(bean)
def download(self): bean = self.bean update = self.update if not bean or not bean.path: return None opener = FancyURLopener() remote = opener.open(bean.path) remote_size = 0 if "Content-Length" in remote.headers: remote_size = int(remote.headers["Content-Length"]) bean.size = size2text(remote_size) block_size = 4096 block_count = 0 ext = get_file_extension(bean.path) path = FC().online_save_to_folder if not os.path.isdir(path): os.makedirs(path) if bean.save_to: to_file = os.path.join(bean.save_to, bean.text + ".mp3") else: to_file = get_bean_download_path(bean, FC().online_save_to_folder) if not os.path.exists(os.path.dirname(to_file)): os.makedirs(os.path.dirname(to_file)) to_file_tmp = to_file + ".tmp" if os.path.exists(to_file_tmp): bean.status = DOWNLOAD_STATUS_INACTIVE bean.to_file = to_file update(bean) return None if os.path.exists(to_file): bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file update(bean) return None bean.save_to = to_file with file(to_file_tmp, "wb") as tmp_file: data = True """begin download""" self.bean.status = DOWNLOAD_STATUS_DOWNLOADING self.bean.path = to_file self.update(self.bean) while data: data = remote.read(block_size) if data: block_count += 1 tmp_file.write(data) #time.sleep(0.1) persent = block_count * block_size * 100.0 / remote_size if block_count % 50 == 0: bean.persent = persent update(bean) time.sleep(0.5) """update file info on finish""" logging.debug("rename %s - %s" % (to_file_tmp, to_file)) os.rename(to_file_tmp, to_file) bean.status = DOWNLOAD_STATUS_COMPLETED bean.to_file = to_file bean.persent = 100 update(bean)