Пример #1
0
    def test_download(self):
        obj = SmartDL(self.default_7za920_mirrors, dest=self.dl_dir, progress_bar=False)
        obj.start()

        data = obj.get_data(binary=True, bytes=2)
        
        self.assertEqual(data, 'PK')    
Пример #2
0
def largeNzedbPre():
    if os.path.isfile('predb_dump-062714.csv.gz'):
        fileExists = True
    else:
        try:
            url = "https://www.dropbox.com/s/btr42dtzzyu3hh3/predb_dump-062714.csv.gz?dl=1"
            dest = "."

            print("pre-import: File predb_dump-062714.csv not found, attempt to download - may take a while, its 300mb")

            obj = SmartDL(url, dest)
            obj.start()

            fileExists = True
        except:
            print("pre-import: Error downloading/unzipping. Please try again.")
            exit(0)

    if fileExists:
        dirtyChunk = pandas.read_table('predb_dump-062714.csv.gz', compression='gzip', sep='\t', header=None,
                                       na_values='\\N', usecols=[0, 8, 10, 14, 16, 18, 20, 22], names=COLNAMES,
                                       chunksize=10000, engine='c', error_bad_lines=False, warn_bad_lines=False)
    else:
        print("pre-import: File predb_dump-062714.csv not found, please try again.")
        exit(0)

    i = 0
    for chunk in dirtyChunk:
        process(chunk)
        print("pre-import: Imported chunk {}".format(i))
        i += 1
Пример #3
0
class Downloader:

    def __init__(self, url):
        self.url = url
        self.dest = '~/Downloads/'
        self.dloader = SmartDL(self.url, self.dest)

    def start(self):
        self.dloader.start()
Пример #4
0
    def run(self):
        while dlQueue.not_empty:
            index = dlQueue.get()
            logging.info('Downloader : Grabbed an item. {0} remaining'.format(dlQueue.qsize()))

            path = ".%s%s" % (os.sep, '{title}.mp4'.format(title=foundEpTitles[index]))
            if os.path.isfile(path):
                logging.info("Downloader : The file at %s already exists. Skipping..." % (path))
                foundEpFileStatus[index] = "Grabbed"
                dlQueue.task_done()
            else:
                logging.info('Downloader : Getting {file}'.format(file=foundEpTitles[index]))
                obj = SmartDL(foundEpFilelinks[index], path, progress_bar=False)
                try:
                    obj.start(blocking=True)
                    if obj.isSuccessful():
                        foundEpFileStatus[index] = "Grabbed"
                        logging.info("Downloader : Grabbed '%s' in %s" % (obj.get_dest(), obj.get_dl_time(human=True)))
                        dlQueue.task_done()
                    else:
                        foundEpFilelinks[index] = 'new'
                        logging.info("Downloader : Error grabbing '%s'" % obj.get_dest())
                        dlQueue.task_done()
                        for e in obj.get_errors():
                                logging.error(str(e))
                except KeyboardInterrupt:
                    obj.stop()

            time.sleep(self.interval)
Пример #5
0
def download(url, name, quiet=False):
    path = ".%s%s" % (os.sep, name)
    if os.path.isfile(path):
        if not quiet:
            print "Skipping %s" % name
        return
    else:
        obj = SmartDL(url, path)
        try:
            obj.start()
        except KeyboardInterrupt:
            obj.stop()
Пример #6
0
  def lunch(rsqt,filename,appname,execuable):
    import os,os.path
    d='%s%s\\'%(getTempDir('app'),filename.replace('.','_'))
    if execu(d,execuable):
      return

    filePath='%s%s'%(d,filename)
    if not os.path.exists(filePath):
      url='http://%s/blaze/download/%s'%(remoteHost,filename)
      from pySmartDL import SmartDL
      sdl = SmartDL(url, d,progress_bar=True)
      sdl.start()
      filePath = sdl.get_dest()

    import zip
    zip.extract(filePath, d)
    execu(d,execuable)
Пример #7
0
	def start_component_update(self):
		if not os.path.exists(config.ext_bin_path):
			os.makedirs(config.ext_bin_path)

		d = Main.WebParser.WebServices.get_components_data()

		for i, component in enumerate(self.components):
			urls, archive_hash, file_to_extract, file_hash = d[component]
			fn = urls[0].split('/')[-1]
			
			log.debug("Downloading Component %s [%d/%d]..." % (component, i+1, len(self.components)))
			self.label1.setText(tr("Downloading %s...") % component)
			self.label2.setText(tr("File: <i>%s</i> [%d/%d]") % (fn, i+1, len(self.components)))
			
			obj = SmartDL(urls, logger=log)
			obj.add_hash_verification('sha256', archive_hash)
			obj.start(blocking=False)
			
			b = True
			while not obj.isFinished():
				if b:
					self.label1.setText(tr("Downloading %s (%.2f MB)...") % (component, obj.filesize/1024.0**2))
					b = False
				QtGui.QApplication.processEvents()
				self.prg_bar.setValue(int(obj.get_progress()*100))
				time.sleep(0.1)
			if obj._failed:
				QtGui.QMessageBox.critical(self, tr("Error"), tr("The download has failed. It may be a network connection problem. Please try to rerun this application and try again."), QtGui.QMessageBox.Ok)
				self.close()
			self.prg_bar.setValue(100)
				
			self.label1.setText(tr("Unpacking %s...") % component)
					
			ext = os.path.splitext(obj.get_dest())[1].lower()
			if ext == '.zip':
				zipObj = zipfile.ZipFile(obj.get_dest())
				zipObj.extract(file_to_extract, config.ext_bin_path)
			elif ext == '.7z':
				cmd = r'"%s\7za.exe" e "%s" -ir!%s -y -o"%s"' % (config.ext_bin_path, obj.get_dest(), file_to_extract, config.ext_bin_path)
				subprocess.check_call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
			else:
				log.error('could not extract %s archive.' % ext)
				
		self.close()
Пример #8
0
def audios(link):
    print("Enter the Minimum size of file to be downloaded\n")
    size = input('> ')
    size = int(size)

    url = urllib.request.urlopen(link)
    content = url.read()
    soup = BeautifulSoup(content)
    links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.(mp3|wav|ogg|wma|flac)'))]
    print (str(len(links)) + " Audios Found ")
   # print (links)
    print("\n".join(links))


  
   
#For Downloading
    dest = "~/Downloads/" # or '~/Downloads/' on linux
    for i in range(len(links)):
        #a_link = urllib.request.Request(links[i])
        #a_link2 = int(round((links[i].length / 1024),2)) / 1024
        #req = urllib.request.Request(a_link, method='HEAD')
        #resp = urllib.request.urlopen(req)
        #resp = urllib.request.urlopen(a_link)
        #size = int(resp.headers['Content-Length'])
        #size = int(a_link.length)
        #a_link2 = urllib.request.urlopen(a_link)
        #site = urllib.request.urlopen(a_link)
        #size2 = int((a_link.length / 1024) / 1024)

        a_link = links[i]
        fix_link = utils.url_fix(a_link)
        b =(utils.get_filesize(fix_link, timeout=15))


        print (utils.sizeof_human(b))
        #if size >= b:
        obj = SmartDL(fix_link, dest)
        obj.start()

        path = obj.get_dest()
Пример #9
0
def docs(link):

    print("Enter the Minimum size of file to be downloaded\n")
    u_size = input('> ')
    u_size = int(u_size)
    
    url = urllib.request.urlopen(link)
    #link = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    content = url.read()
    soup = BeautifulSoup(content)
    links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.(pdf|doc|docx|odt|xls|ppt)'))]
    print (str(len(links)) + " Documents Found ")
   # print (links)
    print("\n".join(links))

    dest = "~/Downloads/" 
    
    for i in range(len(links)):
        a_link = links[i]
        fix_link = utils.url_fix(a_link)
        #b =(utils.get_filesize(fix_link, timeout=200000))
        b =(utils.get_filesize(fix_link))
        print (utils.sizeof_human(b))

        c_size =(utils.get_filesize(fix_link, timeout=15))
        c_size = (b / 1024)

        #print (round(c_size,2))
        
        
        
        if u_size <= c_size:
            obj = SmartDL(fix_link, dest)
            obj.start()

            path = obj.get_dest()

    main();
Пример #10
0
def test_get_components_data():
	d = Main.WebParser.WebServices.get_components_data()
	assert d
	for name, t in d.items():
		urls, archive_hash, file_to_extract, file_hash = t
		
		for url in urls:
			obj = SmartDL(url)
			obj.add_hash_verification('sha256', archive_hash)
			obj.start()
			
			assert obj.isSuccessful()
			os.unlink(obj.get_dest())
Пример #11
0
def test_get_packages_data():
	d = Main.WebParser.WebServices.get_packages_data()
	assert d
	for name, t in d.items():
		urls, file_hash, install_param = t
		
		for url in urls:
			obj = SmartDL(url)
			obj.add_hash_verification('sha256', file_hash)
			obj.start()
			
			assert obj.isSuccessful()
			os.unlink(obj.get_dest())
Пример #12
0
	def start_package_install(self):
		d = Main.WebParser.WebServices.get_packages_data()
		
		for i, component in enumerate(self.components):
			urls, file_hash, install_param = d[component]
			fn = urls[0].split('/')[-1]
			
			log.debug("Downloading Component %s [%d/%d]..." % (component, i+1, len(self.components)))
			self.label1.setText(tr("Downloading %s...") % component)
			self.label2.setText(tr("File: <i>%s</i> [%d/%d]") % (fn, i+1, len(self.components)))
			
			for j in range(self.retries):
				obj = SmartDL(urls, logger=log)
				obj.start(blocking=False)
				
				b = True
				while not obj.isFinished():
					if b:
						self.label1.setText(tr("Downloading %s (%.2f MB)...") % (component, obj.filesize/1024.0**2))
						b = False
					QtGui.QApplication.processEvents()
					self.prg_bar.setValue(int(obj.get_progress()*100))
					time.sleep(0.1)
				if obj._failed:
					QtGui.QMessageBox.critical(self, tr("Error"), tr("The download has failed. It may be a network connection problem. Please try to rerun this application and try again."), QtGui.QMessageBox.Ok)
					self.close()
				self.prg_bar.setValue(100)
					
				computed_hash = utils.calc_sha256(obj.get_dest())
				if file_hash == computed_hash:
					log.debug('Hash for %s is valid.' % component)
					break
				else:
					log.warning('Hash for %s is NOT valid (%s != %s). Retrying (%d/%d)...' % (component, file_hash, computed_hash, j+1, self.retries))	
				
			if file_hash != computed_hash:
				log.error('Hash for %s is NOT valid (%s != %s).' % (component, file_hash, computed_hash))
				QtGui.QMessageBox.warning(self, tr("Warning"), tr("Hash check failed for %s. Please contact with the program's developer.") % component, QtGui.QMessageBox.Ok)
				self.close()
				return
				
			path = obj.get_dest()
			install_params = [path] + install_param
				
			self.label1.setText(tr("Installing %s...") % component)
			subprocess.call(install_params, shell=True)
			QtGui.QApplication.processEvents()
		
		self.close()
Пример #13
0
def download_file(task, full_path, url):
        logger.info('Downloading file from: %s', full_path)
        global downloader
        downloader = SmartDL(url, full_path, progress_bar=False, logger=logger)
        stat_job = scheduler.scheduler.add_job(get_download_stats, args=(task,), trigger='interval', seconds=1, max_instances=1, next_run_time=datetime.datetime.now())
        downloader.start(blocking=True)
        while not downloader.isFinished():
            print('wrong! waiting for downloader before finished')
        stat_job.remove()
        if downloader.isSuccessful():
            global total_size_downloaded
            total_size_downloaded += downloader.get_dl_size()
            logger.info('Finished downloading file from: %s', full_path)
        else:
            logger.error('Error while downloading file from: %s', full_path)
            for e in downloader.get_errors():
                logger.error(str(e))
Пример #14
0
async def autopic(event):

    await event.edit("colour profile pic have been enabled")

    downloaded_file_name = "./FRIDAY/original_pic.png"

    downloader = SmartDL(Var.DOWNLOAD_PFP_URL_CLOCK,
                         downloaded_file_name,
                         progress_bar=False)

    downloader.start(blocking=False)

    photo = "photo_pfp.png"

    while not downloader.isFinished():

        place_holder = None

    while True:

        #RIP Danger zone Here no editing here plox

        R = random.randint(0, 256)

        B = random.randint(0, 256)

        G = random.randint(0, 256)

        FR = (256 - R)

        FB = (256 - B)

        FG = (256 - G)

        shutil.copy(downloaded_file_name, photo)

        image = Image.open(photo)

        image.paste((R, G, B), [0, 0, image.size[0], image.size[1]])

        image.save(photo)

        #Edit only Below part 🌚 Or esle u will be responsible 🤷‍♂

        current_time = datetime.now().strftime(
            "\n\n Time: %H:%M:%S \n \n Date: %d/%m/%y")

        img = Image.open(photo)

        drawn_text = ImageDraw.Draw(img)

        fnt = ImageFont.truetype(FONT_FILE_TO_USE, 40)

        ofnt = ImageFont.truetype(FONT_FILE_TO_USE, 40)

        drawn_text.text((200, 400), current_time, font=fnt, fill=(FR, FG, FB))

        drawn_text.text((250, 250),
                        f"{DEFAULTUSER}",
                        font=ofnt,
                        fill=(FR, FG, FB))

        img.save(photo)

        file = await event.client.upload_file(photo)  # pylint:disable=E0602

        try:
            await event.client(
                functions.photos.DeletePhotosRequest(
                    await event.client.get_profile_photos("me", limit=1)))
            await event.client(
                functions.photos.UploadProfilePhotoRequest(  # pylint:disable=E0602
                    file))

            os.remove(photo)

            await asyncio.sleep(60)

        except:

            return
Пример #15
0
import time
from pySmartDL import SmartDL

url_100mb_file = ['http://www.debian.co.il/debian-cd/7.5.0/i386/iso-cd/debian-7.5.0-i386-netinst.iso']
# url_100mb_file = "http://httpbin.org/basic-auth/user/passwd"
# url_100mb_file = "http://cachefly.cachefly.net/100mb.test"
url_100mb_file = 'https://r1---sn-ivuoxu-ua8l.googlevideo.com/videoplayback?requiressl=yes&sver=3&signature=B08C02FDEDC76633D28EA9F72121F341BD471250.CD791E123F116CE2E480BCDEED4268DE35934249&itag=84&id=o-ALsVuxK7RU-9cWXebyXGLBf3xiFZN7N6aMkvkpevG9Jm&sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&ms=au&upn=k-0bari8bTI&mt=1399282666&expire=1399304082&mv=m&pcm2=yes&source=youtube&fexp=933905%2C935501%2C939936%2C945033%2C929305%2C932936%2C937417%2C913434%2C923341%2C936923%2C902408%2C945044&ipbits=0&ratebypass=yes&ip=37.142.247.11&key=yt5&mws=yes'
obj = SmartDL(url_100mb_file, progress_bar=False, connect_default_logger=True)
# obj.add_basic_authentication('user', 'passwd')
# obj.fetch_hash_sums()
# import pdb; pdb.set_trace()
obj.start(blocking=False)
import pdb; pdb.set_trace()

while not obj.isFinished():
    print "Speed: %s" % obj.get_speed(human=True)
    print "Already downloaded: %s" % obj.get_dl_size(human=True)
    print "Eta: %s" % obj.get_eta(human=True)
    print "Progress: %d%%" % (obj.get_progress()*100)
    print "Progress bar: %s" % obj.get_progress_bar()
    print "Status: %s" % obj.get_status()
    print "\n"*2+"="*50+"\n"*2
    time.sleep(0.2)

if obj.isSuccessful():
    print "downloaded file to '%s'" % obj.get_dest()
    print "download task took %ss" % obj.get_dl_time(human=True)
    print "File hashes:"
    print " * MD5: %s" % obj.get_data_hash('md5')
    print " * SHA1: %s" % obj.get_data_hash('sha1')
    print " * SHA256: %s" % obj.get_data_hash('sha256')
Пример #16
0
async def download(target_file):
    """For .download command, download files to the userbot's server."""
    await target_file.edit("`Sedang memproses...`")
    input_str = target_file.pattern_match.group(1)
    replied = await target_file.get_reply_message()
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if input_str:
        url = input_str
        file_name = unquote_plus(os.path.basename(url))
        if "|" in input_str:
            url, file_name = input_str.split("|")
            url = url.strip()
            # https://stackoverflow.com/a/761825/4723940
            file_name = file_name.strip()
            head, tail = os.path.split(file_name)
            if head:
                if not os.path.isdir(
                        os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                    os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                    file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            progress_str = "[{0}{1}] `{2}%`".format(
                "".join(["■" for i in range(math.floor(percentage / 10))]),
                "".join(["▨"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = (
                    f"`Nama` : `{file_name}`\n"
                    "Status"
                    f"\n**{status}**... | {progress_str}"
                    f"\n{humanbytes(downloaded)} dari {humanbytes(total_length)}"
                    f" @ {humanbytes(speed)}"
                    f"\n`Perkiraan` -> {estimated_total_time}")

                if round(diff %
                         15.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit(
                "Berhasil diunduh ke `{}`!".format(downloaded_file_name))
        else:
            await target_file.edit("**URL salah**\n{}".format(url))
    elif replied:
        if not replied.media:
            return await target_file.edit("`Balas file atau media`")
        try:
            media = replied.media
            if hasattr(media, "document"):
                file = media.document
                mime_type = file.mime_type
                filename = replied.file.name
                if not filename:
                    if "audio" in mime_type:
                        filename = ("audio_" +
                                    datetime.now().isoformat("_", "seconds") +
                                    ".ogg")
                    elif "video" in mime_type:
                        filename = ("video_" +
                                    datetime.now().isoformat("_", "seconds") +
                                    ".mp4")
                outdir = TEMP_DOWNLOAD_DIRECTORY + filename
                c_time = time.time()
                start_time = datetime.now()
                with open(outdir, "wb") as f:
                    result = await download_file(
                        client=target_file.client,
                        location=file,
                        out=f,
                        progress_callback=lambda d, t: asyncio.get_event_loop(
                        ).create_task(
                            progress(d, t, target_file, c_time, "[UNDUH]",
                                     input_str)),
                    )
            else:
                start_time = datetime.now()
                result = await target_file.client.download_media(
                    media, TEMP_DOWNLOAD_DIRECTORY)
            dl_time = (datetime.now() - start_time).seconds
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            try:
                await target_file.edit(
                    "Berhasil diunduh ke `{}` dalam `{}` detik.".format(
                        result.name, dl_time))
            except AttributeError:
                await target_file.edit(
                    "Berhasil diunduh ke `{}` dalam `{}` detik.".format(
                        result, dl_time))
    else:
        await target_file.edit(
            "Lihat `“.help download”` untuk info lebih lanjut.")
Пример #17
0
async def download(target_file):
    """ .download komutu userbot sunucusuna dosya indirmenizi sağlar. """
    await target_file.edit("İşleniyor...")
    input_str = target_file.pattern_match.group(1)
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            progress_str = "[{0}{1}] {2}%".format(
                ''.join(["▰" for i in range(math.floor(percentage / 10))]),
                ''.join(["▱"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2))
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"{status}..\
                \nBağlantı: {url}\
                \nDosya adı: {file_name}\
                \n{progress_str}\
                \n{humanbytes(downloaded)} of {humanbytes(total_length)}\
                \nTahmini bitiş: {estimated_total_time}"

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit(
                "`{}` konumuna indirme başarılı.".format(downloaded_file_name))
        else:
            await target_file.edit("Geçersiz bağlantı\n{}".format(url))
    elif target_file.reply_to_msg_id:
        try:
            c_time = time.time()
            downloaded_file_name = await target_file.client.download_media(
                await target_file.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, target_file, c_time, "İndiriliyor...")))
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            await target_file.edit(
                "`{}` konumuna indirme başarılı.".format(downloaded_file_name))
    else:
        await target_file.edit("Sunucuma indirmek için bir mesajı yanıtlayın.")
Пример #18
0
 def __init__(self, url):
     self.url = url
     self.dest = '~/Downloads/'
     self.dloader = SmartDL(self.url, self.dest)
Пример #19
0
def downloadMe(url):
	dest = "./"
	obj = SmartDL(url, dest)
	obj.start()
	return obj.get_dest()
Пример #20
0
async def url_download(message: Message, url: str) -> Tuple[str, int]:
    """ download from link """
    await message.edit("`Downloading From URL...`")
    start_t = datetime.now()
    custom_file_name = unquote_plus(os.path.basename(url))
    if "|" in url:
        url, c_file_name = url.split("|", maxsplit=1)
        url = url.strip()
        if c_file_name:
            custom_file_name = c_file_name.strip()
    dl_loc = os.path.join(Config.DOWN_PATH, custom_file_name)
    downloader = SmartDL(url, dl_loc, progress_bar=False)
    downloader.start(blocking=False)
    count = 0
    while not downloader.isFinished():
        if message.process_is_canceled:
            downloader.stop()
            raise ProcessCanceled
        total_length = downloader.filesize if downloader.filesize else 0
        downloaded = downloader.get_dl_size()
        percentage = downloader.get_progress() * 100
        speed = downloader.get_speed(human=True)
        estimated_total_time = downloader.get_eta(human=True)
        progress_str = \
            "__{}__\n" + \
            "```[{}{}]```\n" + \
            "**Progress** : `{}%`\n" + \
            "**URL** : `{}`\n" + \
            "**FILENAME** : `{}`\n" + \
            "**Completed** : `{}`\n" + \
            "**Total** : `{}`\n" + \
            "**Speed** : `{}`\n" + \
            "**ETA** : `{}`"
        progress_str = progress_str.format(
            "trying to download",
            ''.join((Config.FINISHED_PROGRESS_STR
                     for _ in range(math.floor(percentage / 5)))),
            ''.join((Config.UNFINISHED_PROGRESS_STR
                     for _ in range(20 - math.floor(percentage / 5)))),
            round(percentage, 2),
            url,
            custom_file_name,
            humanbytes(downloaded),
            humanbytes(total_length),
            speed,
            estimated_total_time)
        count += 1
        if count >= Config.EDIT_SLEEP_TIMEOUT:
            count = 0
            await message.try_to_edit(progress_str, disable_web_page_preview=True)
        await asyncio.sleep(1)
    return dl_loc, (datetime.now() - start_t).seconds
Пример #21
0
async def uploadtotg(message: Message):
    flags = message.flags
    path_ = message.filtered_input_str
    if not path_:
        await message.edit("invalid input!, check `.help .upload`", del_in=5)
        return
    is_url = re.search(r"(?:https?|ftp)://[^|\s]+\.[^|\s]+", path_)
    del_path = False
    if is_url:
        del_path = True
        await message.edit("`Downloading From URL...`")
        if not os.path.isdir(Config.DOWN_PATH):
            os.mkdir(Config.DOWN_PATH)
        url = is_url[0]
        file_name = os.path.basename(url)
        if "|" in path_:
            file_name = path_.split("|")[1].strip()
        path_ = os.path.join(Config.DOWN_PATH, file_name)
        try:
            downloader = SmartDL(url, path_, progress_bar=False)
            downloader.start(blocking=False)
            count = 0
            while not downloader.isFinished():
                if message.process_is_canceled:
                    downloader.stop()
                    raise Exception('Process Canceled!')
                total_length = downloader.filesize if downloader.filesize else 0
                downloaded = downloader.get_dl_size()
                percentage = downloader.get_progress() * 100
                speed = downloader.get_speed(human=True)
                estimated_total_time = downloader.get_eta(human=True)
                progress_str = \
                    "__{}__\n" + \
                    "```[{}{}]```\n" + \
                    "**Progress** : `{}%`\n" + \
                    "**URL** : `{}`\n" + \
                    "**FILENAME** : `{}`\n" + \
                    "**Completed** : `{}`\n" + \
                    "**Total** : `{}`\n" + \
                    "**Speed** : `{}`\n" + \
                    "**ETA** : `{}`"
                progress_str = progress_str.format(
                    "trying to download",
                    ''.join(["■" for i in range(math.floor(percentage / 10))]),
                    ''.join([
                        "▨" for i in range(10 - math.floor(percentage / 10))
                    ]), round(percentage,
                              2), url, file_name, humanbytes(downloaded),
                    humanbytes(total_length), speed, estimated_total_time)
                count += 1
                if count >= 5:
                    count = 0
                    await message.try_to_edit(progress_str,
                                              disable_web_page_preview=True)
                await asyncio.sleep(1)
        except Exception as d_e:
            await message.err(d_e)
            return
    if "|" in path_:
        path_, file_name = path_.split("|")
        path_ = path_.strip()
        if os.path.isfile(path_):
            new_path = os.path.join(Config.DOWN_PATH, file_name.strip())
            os.rename(path_, new_path)
            path_ = new_path
    try:
        string = Path(path_)
    except IndexError:
        await message.edit("wrong syntax\n`.upload [path]`")
    else:
        await message.delete()
        await explorer(string, message.chat.id, flags, del_path)
Пример #22
0
 def download(self, parent_path, filename, url):
     obj = SmartDL(url, parent_path)
     obj.start()
Пример #23
0
async def imdb_query(event):  # sourcery no-metrics
    """To fetch imdb data about the given movie or series."""
    catevent = await edit_or_reply(event, "`searching........`")
    reply_to = await reply_id(event)
    try:
        movie_name = event.pattern_match.group(1)
        movies = imdb.search_movie(movie_name)
        movieid = movies[0].movieID
        movie = imdb.get_movie(movieid)
        moviekeys = list(movie.keys())
        for i in mov_titles:
            if i in moviekeys:
                mov_title = movie[i]
                break
        for j in reversed(mov_titles):
            if j in moviekeys:
                mov_ltitle = movie[j]
                break
        mov_runtime = movie["runtimes"][0] + " min" if "runtimes" in movie else ""
        if "original air date" in moviekeys:
            mov_airdate = movie["original air date"]
        elif "year" in moviekeys:
            mov_airdate = movie["year"]
        else:
            mov_airdate = ""
        mov_genres = ", ".join(movie["genres"]) if "genres" in moviekeys else "Not Data"
        mov_rating = str(movie["rating"]) if "rating" in moviekeys else "Not Data"
        mov_rating += (
            " (by " + str(movie["votes"]) + ")"
            if "votes" in moviekeys and "rating" in moviekeys
            else ""
        )
        mov_countries = (
            ", ".join(movie["countries"]) if "countries" in moviekeys else "Not Data"
        )
        mov_languages = (
            ", ".join(movie["languages"]) if "languages" in moviekeys else "Not Data"
        )
        mov_plot = (
            str(movie["plot outline"]) if "plot outline" in moviekeys else "Not Data"
        )
        mov_director = await get_cast("director", movie)
        mov_composers = await get_cast("composers", movie)
        mov_writer = await get_cast("writer", movie)
        mov_cast = await get_cast("cast", movie)
        mov_box = await get_moviecollections(movie)
        resulttext = f"""
<b>Title : </b><code>{mov_title}</code>
<b>Imdb Url : </b><a href='https://www.imdb.com/title/tt{movieid}'>{mov_ltitle}</a>
<b>Info : </b><code>{mov_runtime} | {mov_airdate}</code>
<b>Genres : </b><code>{mov_genres}</code>
<b>Rating : </b><code>{mov_rating}</code>
<b>Country : </b><code>{mov_countries}</code>
<b>Language : </b><code>{mov_languages}</code>
<b>Director : </b><code>{mov_director}</code>
<b>Music Director : </b><code>{mov_composers}</code>
<b>Writer : </b><code>{mov_writer}</code>
<b>Stars : </b><code>{mov_cast}</code>
<b>Box Office : </b>{mov_box}
<b>Story Outline : </b><i>{mov_plot}</i>"""
        if "full-size cover url" in moviekeys:
            imageurl = movie["full-size cover url"]
        else:
            imageurl = None
        soup = BeautifulSoup(resulttext, features="html.parser")
        rtext = soup.get_text()
        if len(rtext) > 1024:
            extralimit = len(rtext) - 1024
            climit = len(resulttext) - extralimit - 20
            resulttext = resulttext[:climit] + "...........</i>"
        if imageurl:
            downloader = SmartDL(imageurl, moviepath, progress_bar=False)
            downloader.start(blocking=False)
            while not downloader.isFinished():
                pass
        if os.path.exists(moviepath):
            await event.client.send_file(
                event.chat_id,
                moviepath,
                caption=resulttext,
                reply_to=reply_to,
                parse_mode="HTML",
            )
            os.remove(moviepath)
            return await catevent.delete()
        await catevent.edit(
            resulttext,
            link_preview=False,
            parse_mode="HTML",
        )
    except IndexError:
        await catevent.edit(f"__No movie found with name {movie_name}.__")
    except Exception as e:
        await catevent.edit(f"**Error:**\n__{e}__")
Пример #24
0
async def download(dryb):
    """ For .gdrive command, upload files to google drive. """
    if not dryb.text[0].isalpha() and dryb.text[0] not in ("/", "#", "@", "!"):
        if dryb.fwd_from:
            return
        await dryb.edit("Processing ...")
        input_str = dryb.pattern_match.group(1)
        if CLIENT_ID is None or CLIENT_SECRET is None:
            return false
        if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
            os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
            required_file_name = None
        if "|" in input_str:
            start = datetime.now()
            url, file_name = input_str.split("|")
            url = url.strip()
            # https://stackoverflow.com/a/761825/4723940
            file_name = file_name.strip()
            head, tail = os.path.split(file_name)
            if head:
                if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                    os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                    file_name = os.path.join(head, tail)
            downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
            downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
            downloader.start(blocking=False)
            c_time = time.time()
            while not downloader.isFinished():
                display_message = ""
                total_length = downloader.filesize if downloader.filesize else None
                downloaded = downloader.get_dl_size()
                now = time.time()
                diff = now - c_time
                percentage = downloader.get_progress()*100
                speed = downloader.get_speed()
                elapsed_time = round(diff) * 1000
                progress_str = "[{0}{1}]\nProgress: {2}%".format(
                    ''.join(["█" for i in range(math.floor(percentage / 5))]),
                    ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
                    round(percentage, 2))
                estimated_total_time = downloader.get_eta(human=True)
                try:
                    current_message = f"Downloading...\nURL: {url}\nFile Name: {file_name}\n{progress_str}\n{humanbytes(downloaded)} of {humanbytes(total_length)}\nETA: {estimated_total_time}"
                    if current_message != display_message:
                        await dryb.edit(current_message)
                        display_message = current_message
                except Exception as e:
                    LOGS.info(str(e))
                    pass
            end = datetime.now()
            duration = (end - start).seconds
            if downloader.isSuccessful():
                await dryb.edit(
                    "Downloaded to `{}` in {} seconds.\nNow uploading to GDrive...".format(
                        downloaded_file_name, duration)
                )
                required_file_name = downloaded_file_name
            else:
                await dryb.edit(
                    "Incorrect URL\n{}".format(url)
                )
        elif input_str:
            input_str = input_str.strip()
            if os.path.exists(input_str):
                start = datetime.now()
                end = datetime.now()
                duration = (end - start).seconds
                required_file_name = input_str
                await dryb.edit("Found `{}` in {} seconds, uploading to Google Drive !!".format(input_str, duration))
            else:
                await dryb.edit("File not found in local server. Give me a valid file path !!")
                return False
        elif dryb.reply_to_msg_id:
            start = datetime.now()
            try:
                c_time = time.time()
                downloaded_file_name = await dryb.client.download_media(
                    await dryb.get_reply_message(),
                    TEMP_DOWNLOAD_DIRECTORY,
                    progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                        progress(d, t, dryb, c_time, "Downloading...")
                    )
                )
            except Exception as e: # pylint:disable=C0103,W0703
                await dryb.edit(str(e))
            else:
                end = datetime.now()
                required_file_name = downloaded_file_name
                duration = (end - start).seconds
                await dryb.edit(
                    "Downloaded to `{}` in {} seconds.\nNow uploading to GDrive...".format(
                        downloaded_file_name, duration)
                )
    if required_file_name:
        #
        if G_DRIVE_AUTH_TOKEN_DATA is not None:
            with open(G_DRIVE_TOKEN_FILE, "w") as t_file:
                t_file.write(G_DRIVE_AUTH_TOKEN_DATA)
        # Check if token file exists, if not create it by requesting authorization code
        if not os.path.isfile(G_DRIVE_TOKEN_FILE):
            storage = await create_token_file(G_DRIVE_TOKEN_FILE, dryb)
            http = authorize(G_DRIVE_TOKEN_FILE, storage)
        # Authorize, get file parameters, upload file and print out result URL for download
        http = authorize(G_DRIVE_TOKEN_FILE, None)
        file_name, mime_type = file_ops(required_file_name)
        # required_file_name will have the full path
        # Sometimes API fails to retrieve starting URI, we wrap it.
        try:
            g_drive_link = await upload_file(http, required_file_name, file_name, mime_type, dryb)
            await dryb.edit(f"File:`{required_file_name}`\nwas successfully uploaded to [Google Drive]({g_drive_link})!")
        except Exception as e:
            await dryb.edit(f"Error while uploading to Google Drive\nError Code:\n`{e}`")
Пример #25
0
async def gdrive_upload_function(dryb):
    """ For .gdrive command, upload files to google drive. """
    await dryb.edit("Processing ...")
    if not get_parent_id():
        parent_id = None
    else:
        catparent_id = get_parent_id()
        if len(catparent_id) == 1:
            parent_id = catparent_id[0].cat
        elif len(catparent_id) > 1:
            for fid in catparent_id:
                rmparent_id(fid.cat)
            parent_id = None
    input_str = dryb.pattern_match.group(1)
    if CLIENT_ID is None or CLIENT_SECRET is None:
        return
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
        required_file_name = None
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            elapsed_time = round(diff) * 1000
            progress_str = "[{0}{1}] {2}%".format(
                ''.join(["█" for i in range(math.floor(percentage / 10))]),
                ''.join(["░"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2))
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"{status}...\
                \nURL: {url}\
                \nFile Name: {file_name}\
                \n{progress_str}\
                \n{humanbytes(downloaded)} of {humanbytes(total_length)}\
                \nETA: {estimated_total_time}"

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await dryb.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await dryb.edit(
                "Downloaded to `{}` successfully !!\nInitiating Upload to Google Drive.."
                .format(downloaded_file_name))
            required_file_name = downloaded_file_name
        else:
            await dryb.edit("Incorrect URL\n{}".format(url))
    elif input_str:
        input_str = input_str.strip()
        if os.path.exists(input_str):
            required_file_name = input_str
            await dryb.edit(
                "Found `{}` in local server, Initiating Upload to Google Drive.."
                .format(input_str))
        else:
            await dryb.edit(
                "File not found in local server. Give me a valid file path !")
            return False
    elif dryb.reply_to_msg_id:
        try:
            c_time = time.time()
            downloaded_file_name = await dryb.client.download_media(
                await dryb.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(progress(d, t, dryb, c_time, "Downloading...")))
        except Exception as e:
            await dryb.edit(str(e))
        else:
            required_file_name = downloaded_file_name
            await dryb.edit(
                "Downloaded to `{}` Successfully !!\nInitiating Upload to Google Drive.."
                .format(downloaded_file_name))
    if required_file_name:
        if G_DRIVE_AUTH_TOKEN_DATA is not None:
            with open(G_DRIVE_TOKEN_FILE, "w") as t_file:
                t_file.write(G_DRIVE_AUTH_TOKEN_DATA)
        # Check if token file exists, if not create it by requesting
        # authorization code
        if not os.path.isfile(G_DRIVE_TOKEN_FILE):
            storage = await create_token_file(G_DRIVE_TOKEN_FILE, dryb)
            http = authorize(G_DRIVE_TOKEN_FILE, storage)
        # Authorize, get file parameters, upload file and print out result URL
        # for download
        http = authorize(G_DRIVE_TOKEN_FILE, None)
        file_name, mime_type = file_ops(required_file_name)
        # required_file_name will have the full path
        # Sometimes API fails to retrieve starting URI, we wrap it.
        try:
            g_drive_link = await upload_file(http, required_file_name,
                                             file_name, mime_type, dryb,
                                             parent_id)
            await dryb.edit(
                f"File:`{required_file_name}`\nwas Successfully Uploaded to [Google Drive]({g_drive_link})!"
            )
        except Exception as e:
            await dryb.edit(
                f"Error while Uploading to Google Drive\nError Code:\n`{e}`")
Пример #26
0
async def down_load_media(message: Message):
    await message.edit("`Trying to Download...`")
    if message.reply_to_message and message.reply_to_message.media:
        start_t = datetime.now()
        dl_loc = await message.client.download_media(
            message=message.reply_to_message,
            file_name=Config.DOWN_PATH,
            progress=progress,
            progress_args=(message, "trying to download"))
        if message.process_is_canceled:
            await message.edit("`Process Canceled!`", del_in=5)
        else:
            dl_loc = os.path.join(Config.DOWN_PATH, os.path.basename(dl_loc))
            end_t = datetime.now()
            m_s = (end_t - start_t).seconds
            await message.edit(f"Downloaded to `{dl_loc}` in {m_s} seconds")
    elif message.input_str:
        start_t = datetime.now()
        url = message.input_str
        custom_file_name = unquote_plus(os.path.basename(url))
        if "|" in url:
            url, custom_file_name = url.split("|")
            url = url.strip()
            custom_file_name = custom_file_name.strip()
        download_file_path = os.path.join(Config.DOWN_PATH, custom_file_name)
        try:
            downloader = SmartDL(url, download_file_path, progress_bar=False)
            downloader.start(blocking=False)
            count = 0
            while not downloader.isFinished():
                if message.process_is_canceled:
                    downloader.stop()
                    raise Exception('Process Canceled!')
                total_length = downloader.filesize if downloader.filesize else 0
                downloaded = downloader.get_dl_size()
                percentage = downloader.get_progress() * 100
                speed = downloader.get_speed(human=True)
                estimated_total_time = downloader.get_eta(human=True)
                progress_str = \
                    "__{}__\n" + \
                    "```[{}{}]```\n" + \
                    "**Progress** : `{}%`\n" + \
                    "**URL** : `{}`\n" + \
                    "**FILENAME** : `{}`\n" + \
                    "**Completed** : `{}`\n" + \
                    "**Total** : `{}`\n" + \
                    "**Speed** : `{}`\n" + \
                    "**ETA** : `{}`"
                progress_str = progress_str.format(
                    "trying to download", ''.join(
                        (Config.FINISHED_PROGRESS_STR
                         for i in range(math.floor(percentage / 5)))),
                    ''.join((Config.UNFINISHED_PROGRESS_STR
                             for i in range(20 - math.floor(percentage / 5)))),
                    round(percentage,
                          2), url, custom_file_name, humanbytes(downloaded),
                    humanbytes(total_length), speed, estimated_total_time)
                count += 1
                if count >= 5:
                    count = 0
                    await message.try_to_edit(progress_str,
                                              disable_web_page_preview=True)
                await asyncio.sleep(1)
        except Exception as e:
            await message.err(e)
        else:
            end_t = datetime.now()
            m_s = (end_t - start_t).seconds
            await message.edit(
                f"Downloaded to `{download_file_path}` in {m_s} seconds")
    else:
        await message.edit("Please read `.help download`", del_in=5)
Пример #27
0
async def download(target_file):
    """ For .download command, download files to the userbot's server. """
    await target_file.edit("`Sedang memproses...`")
    input_str = target_file.pattern_match.group(1)
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            progress_str = "[{0}{1}] `{2}%`".format(
                "".join(["■" for i in range(math.floor(percentage / 10))]),
                "".join(["▨"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = (
                    f"`Nama` : `{file_name}`\n"
                    "Status"
                    f"\n**{status}**... | {progress_str}"
                    f"\n{humanbytes(downloaded)} dari {humanbytes(total_length)}"
                    f" @ {humanbytes(speed)}"
                    f"\n`Perkiraan` -> {estimated_total_time}")

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit(
                "Berhasil diunduh ke `{}`!".format(downloaded_file_name))
        else:
            await target_file.edit("URL salah.\n{}".format(url))
    elif target_file.reply_to_msg_id:
        try:
            replied = await target_file.get_reply_message()
            file = replied.document
            file_name = replied.document.attributes[-1].file_name
            outdir = TEMP_DOWNLOAD_DIRECTORY + file_name
            c_time = time.time()
            with open(outdir, "wb") as f:
                result = await download_file(
                    client=target_file.client,
                    location=file,
                    out=f,
                    progress_callback=lambda d, t: asyncio.get_event_loop().
                    create_task(
                        progress(d, t, target_file, c_time, "[UNDUH]",
                                 input_str)),
                )
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            await target_file.edit("Berhasil diunduh ke `{}`!".format(
                result.name))
    else:
        await target_file.edit("`Balas pesan untuk diunduh ke server lokal.`")
Пример #28
0
async def lul(event):
    if event.fwd_from:
        return
    input_str = event.raw_text.split(" ", maxsplit=1)[1]
    mone = await event.edit("**Processing..**")
    start = datetime.now()
    url = input_str
    a = urlparse(input_str)
    file_name = os.path.basename(a.path)
    to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
    downloaded_file_name = os.path.join(to_download_directory, file_name)
    downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
    downloader.start(blocking=False)
    display_message = ""
    c_time = time.time()
    while not downloader.isFinished():
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        now = time.time()
        diff = now - c_time
        percentage = downloader.get_progress() * 100
        speed = downloader.get_speed()
        elapsed_time = round(diff) * 1000
        progress_str = "[{0}{1}]\nProgress: {2}%".format(
            ''.join(["▰" for i in range(math.floor(percentage / 5))]),
            ''.join(["▱" for i in range(20 - math.floor(percentage / 5))]),
            round(percentage, 2))
        estimated_total_time = downloader.get_eta(human=True)
        try:
            current_message = f"trying to download\n"
            current_message += f"URL: {url}\n"
            current_message += f"File Name: {file_name}\n"
            current_message += f"{progress_str}\n"
            current_message += f"{humanbytes(downloaded)} of {humanbytes(total_length)}\n"
            current_message += f"ETA: {estimated_total_time}"
            if round(diff % 10.00) == 0 and current_message != display_message:
                await mone.edit(current_message)
                display_message = current_message
        except Exception as e:
            logger.info(str(e))
    end = datetime.now()
    ms = (end - start).seconds
    if downloader.isSuccessful():
        c_time = time.time()
        lul = await mone.edit("Downloaded to `{}` in {} seconds.".format(
            downloaded_file_name, ms))
        lol_m = await upload_file(
            file_name=file_name,
            client=borg,
            file=open(downloaded_file_name, 'rb'),
            progress_callback=lambda d, t: asyncio.get_event_loop().
            create_task(
                progress(d, t, event, c_time, "Uploading This File.",
                         downloaded_file_name)),
        )
        await borg.send_file(
            event.chat_id,
            lol_m,
            caption=file_name,
            force_document=False,
            allow_cache=False,
        )
        await lul.delete()
        os.remove(downloaded_file_name)
    else:
        await mone.edit("Incorrect URL\n {}".format(input_str))
Пример #29
0
async def mega_downloader(megadl):
    await megadl.edit("`Collecting information...`")
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await megadl.edit("Usage: `.mega` **<MEGA.nz link>**")
    try:
        link = re.findall(r"\bhttps?://.*mega.*\.nz\S+", link)[0]
        """ - Mega changed their URL again - """
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await megadl.edit("`folder download support are removed...`")
            return
    except IndexError:
        await megadl.edit("`MEGA.nz link not found...`")
        return None
    cmd = f"bin/megadown -q -m {link}"
    result = await subprocess_run(megadl, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        await megadl.edit("**JSONDecodeError**: `failed to extract link...`")
        return None
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    temp_file_path = TEMP_DOWNLOAD_DIRECTORY + temp_file_name
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name

    if os.path.isfile(file_path):
        try:
            raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST),
                                  file_path)
        except FileExistsError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
    downloader = SmartDL(file_url, temp_file_path, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        await megadl.edit(f"**HTTPError**: `{str(e)}`")
        return None
    start = time.time()
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        speed = downloader.get_speed(human=True)
        estimated_total_time = round(downloader.get_eta())
        progress_str = "`{}` | [{}{}] `{}%`".format(
            status,
            "".join(["●" for i in range(math.floor(percentage / 10))]),
            "".join(["○" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2),
        )
        diff = time.time() - start
        try:
            current_message = (
                f"`{file_name}`\n\n"
                "Status\n"
                f"{progress_str}\n"
                f"`{humanbytes(downloaded)} of {humanbytes(total_length)}"
                f" @ {speed}`\n"
                f"`ETA` -> {time_formatter(estimated_total_time)}\n"
                f"`Duration` -> {time_formatter(round(diff))}")
            if round(diff % 15.00) == 0 and (display_message != current_message
                                             or total_length == downloaded):
                await megadl.edit(current_message)
                await asyncio.sleep(0.2)
                display_message = current_message
        except Exception:
            pass
        finally:
            if status == "Combining":
                wait = round(downloader.get_eta())
                await asyncio.sleep(wait)
    if downloader.isSuccessful():
        download_time = round(downloader.get_dl_time() + wait)
        try:
            P = multiprocessing.Process(
                target=await decrypt_file(megadl, file_path, temp_file_path,
                                          hex_key, hex_raw_key),
                name="Decrypt_File",
            )
            P.start()
            P.join()
        except FileNotFoundError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
        else:
            await megadl.edit(
                f"`{file_name}`\n\n"
                f"Successfully downloaded in: `{file_path}`.\n"
                f"Download took: {time_formatter(download_time)}.", )

        msg = await megadl.respond("`Uploading to GDrive...`")
        service = await create_app(msg)
        if service is False:
            await asyncio.sleep(2.5)
            await msg.delete()
            return
        mime_type = await get_mimeType(file_path)

        try:
            resultgd = await upload(msg, service, file_path, file_name,
                                    mime_type)
        except CancelProcess:
            msg.edit("`[FILE - CANCELLED]`\n\n"
                     "`Status` : **OK** - received signal cancelled.")
        if resultgd:
            result_output = f"**GDrive Upload**\n\n📄 [{file_name}]({resultgd[1]})"
            if G_DRIVE_INDEX_URL:
                index_url = G_DRIVE_INDEX_URL.rstrip("/") + "/" + quote(
                    file_name)
                result_output += f"\n👥 [Index URL]({index_url})"
            result_output += f"\n__Size : {humanbytes(resultgd[0])}__"
            await msg.edit(
                result_output,
                link_preview=False,
            )

    else:
        await megadl.edit("`Failed to download, "
                          "check heroku Logs for more details.`")
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Пример #30
0
                                             "./downloads")
else:
    # Put your ppe vars here if you are using local hosting
    PLACEHOLDER = None

# Setting Up CloudMail.ru and MEGA.nz extractor binaries,
# and giving them correct perms to work properly.
if not os.path.exists('bin'):
    os.mkdir('bin')

binaries = {
    "https://raw.githubusercontent.com/yshalsager/megadown/master/megadown":
    "bin/megadown",
    "https://raw.githubusercontent.com/yshalsager/cmrudl.py/master/cmrudl.py":
    "bin/cmrudl"
}

for binary, path in binaries.items():
    downloader = SmartDL(binary, path, progress_bar=False)
    downloader.start()
    os.chmod(path, 0o755)

# Global Variables
COUNT_MSG = 0
USERS = {}
COUNT_PM = {}
LASTMSG = {}
CMD_HELP = {}
ISAFK = False
AFKREASON = None
Пример #31
0
 def test_hash(self):
     obj = SmartDL(self.default_7za920_mirrors, progress_bar=False)
     obj.add_hash_verification('sha256' ,'2a3afe19c180f8373fa02ff00254d5394fec0349f5804e0ad2f6067854ff28ac') # good hash
     obj.start(blocking=False) # no exceptions
     obj.wait()
     
     self.assertTrue(obj.isSuccessful())
     
     obj = SmartDL(self.default_7za920_mirrors, progress_bar=False)
     obj.add_hash_verification('sha256' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') # bad hash
     obj.start(blocking=False) # no exceptions
     obj.wait()
     
     self.assertFalse(obj.isSuccessful())
     self.assertTrue(isinstance(obj.get_errors()[-1], HashFailedException))
Пример #32
0
        def download(__url__):
            url = __url__
            self.destination = str(self.destination)
            stopButton['command'] = lambda: terminate(self.downloadObject)
            stopButton['state'] = NORMAL
            pauseButton['command'] = lambda: pauseResume(self.downloadObject)
            pauseButton['state'] = NORMAL

            def doDownload(sem):
                with sem:
                    try:
                        if self.downloadObject:
                            self.downloadObject.start()
                    except Exception as e:
                        print(f"------> {e}")
                        print(
                            f"object error ---> {self.downloadObject.get_errors()}"
                        )
                        self.statusMessage.set(f"   Status: {e}")
                        root.update_idletasks()

            def showProgress(sem):
                with sem:
                    time.sleep(1)
                    startTime = time.perf_counter()
                    if self.downloadObject:
                        while not self.downloadObject.isFinished() and len(
                                self.downloadObject.get_errors()) == 0:
                            self.statusMessage.set(
                                f"   Status: {self.downloadObject.get_status().capitalize()}"
                            )
                            self.speedMessage.set(
                                f"   Speed: {self.downloadObject.get_speed(human=True)}"
                            )
                            self.destinationMessage.set(
                                f"   Working directory: {self.destination}")
                            self.sizeMessage.set(
                                f"   Downloaded so far: {self.downloadObject.get_dl_size(human=True)}"
                            )
                            self.timeMessage.set(
                                f"   Elapsed Time: {round(time.perf_counter() - startTime, 1)}"
                                if self.downloadObject.get_status() != 'paused'
                                else '   Elapsed Time: . . . ')
                            progress[
                                'value'] = 100 * self.downloadObject.get_progress(
                                )
                            time.sleep(0.2)
                            root.update_idletasks()
                        if len(self.downloadObject.get_errors()) == 0:
                            startPoint = time.perf_counter()
                            while time.perf_counter() - startPoint < 2:
                                self.statusMessage.set(
                                    f"   Status: {self.downloadObject.get_status().capitalize()}"
                                )
                                self.speedMessage.set(
                                    f"   Speed: {self.downloadObject.get_speed(human=True)}"
                                )
                                self.destinationMessage.set(
                                    f"   Saved at: {self.downloadObject.get_dest()}"
                                )
                                self.sizeMessage.set(
                                    f"   Total File Size: {self.downloadObject.get_final_filesize(human=True)}"
                                )
                                self.timeMessage.set(
                                    f"   Total Time: {str(self.downloadObject.get_dl_time(human=True))}"
                                )
                                progress[
                                    'value'] = 100 * self.downloadObject.get_progress(
                                    )
                                time.sleep(0.2)
                                root.update_idletasks()
                            if progress['value'] == 100:
                                speak('File Downloaded')
                        else:
                            self.statusMessage.set(
                                f"   Status: Download Failed")
                            self.speedMessage.set(
                                f"   Reason: {self.downloadObject.get_errors()[0]}"
                            )
                            root.update_idletasks()
                            speak('Download Failed')

            if len(url) == 0:
                downloadButton.flash()
            else:
                try:
                    self.downloadObject = SmartDL(url, self.destination)
                except Exception as e:
                    print(f"Error in {e}")
                    self.statusMessage.set(f"   Status: {e}")
                    root.update_idletasks()
                semaphore = threading.Semaphore(2)
                threading.Thread(target=doDownload, args=(semaphore, )).start()
                threading.Thread(target=showProgress,
                                 args=(semaphore, )).start()
Пример #33
0
async def mega_downloader(megadl):
    await megadl.edit("`Bilgi toplanıyor...`")
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await megadl.edit("Kullanımı: `.mega` **<MEGA.nz link>**")
    try:
        link = re.findall(r'\bhttps?://.*mega.*\.nz\S+', link)[0]
        # - Mega changed their URL again -
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await megadl.edit("`klasör indirme desteği kaldırıldı...`")
            return
    except IndexError:
        await megadl.edit("`MEGA.nz bağlantısı bulunamadı...`")
        return None
    cmd = f'bin/megadown -q -m {link}'
    result = await subprocess_run(megadl, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        await megadl.edit("**JSONDecodeError**: `bağlantı çıkarılamadı...`")
        return None
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    temp_file_path = os.path.join(TEMP_DOWNLOAD_DIRECTORY, temp_file_name)
    file_path = os.path.join(TEMP_DOWNLOAD_DIRECTORY, file_name)
    if os.path.isfile(file_path):
        try:
            raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST),
                                  file_path)
        except FileExistsError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
    downloader = SmartDL(file_url, temp_file_path, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        await megadl.edit(f"**HTTPError**: `{str(e)}`")
        return None
    start = time.time()
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        speed = downloader.get_speed(human=True)
        estimated_total_time = round(downloader.get_eta())
        progress_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status, ''.join(["▰" for i in range(math.floor(percentage / 10))]),
            ''.join(["▱" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2))
        diff = time.time() - start
        try:
            current_message = (
                f"**➥dosya adı : **`{file_name}`\n\n"
                "**➥Durum**\n"
                f"{progress_str}\n"
                f"`{humanbytes(downloaded)}` of `{humanbytes(total_length)}`"
                f" @ `{speed}`\n"
                f"**➥ETA -> **`{time_formatter(estimated_total_time)}`\n"
                f"**➥ Süresi -> **`{time_formatter(round(diff))}`")
            if round(diff % 15.00) == 0 and (display_message != current_message
                                             or total_length == downloaded):
                await megadl.edit(current_message)
                await asyncio.sleep(0.2)
                display_message = current_message
        except Exception:
            pass
        finally:
            if status == "Birleştirme":
                wait = round(downloader.get_eta())
                await asyncio.sleep(wait)
    if downloader.isSuccessful():
        download_time = round(downloader.get_dl_time() + wait)
        try:
            P = multiprocessing.Process(target=await
                                        decrypt_file(megadl, file_path,
                                                     temp_file_path, hex_key,
                                                     hex_raw_key),
                                        name="Decrypt_File")
            P.start()
            P.join()
        except FileNotFoundError as e:
            await megadl.edit(f"`{str(e)}`")
            return None
        else:
            await megadl.edit(
                f"**➥ Dosya adı : **`{file_name}`\n\n"
                f"**➥ Başarıyla indirildi : ** `{file_path}`.\n"
                f"**➥İndirme aracı:** {time_formatter(download_time)}.")
            return None
    else:
        await megadl.edit(
            "`İndirme başarısız, "
            "daha fazla ayrıntı için heroku Günlüklerine bakın..`")
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Пример #34
0
 async def upload(self) -> None:
     """Upload from file/folder/link/tg file to GDrive"""
     if not os.path.isdir(Config.DOWN_PATH):
         os.mkdir(Config.DOWN_PATH)
     replied = self._message.reply_to_message
     is_url = re.search(r"(?:https?|ftp)://[^\|\s]+\.[^\|\s]+",
                        self._message.input_str)
     dl_loc = None
     if replied and replied.media:
         await self._message.edit("`Downloading From TG...`")
         c_time = time.time()
         dl_loc = await userge.download_media(
             message=replied,
             file_name=Config.DOWN_PATH,
             progress=progress,
             progress_args=("trying to download", userge, self._message,
                            c_time))
         if self._message.process_is_canceled:
             await self._message.edit("`Process Canceled!`", del_in=5)
             return
         else:
             dl_loc = os.path.join(Config.DOWN_PATH,
                                   os.path.basename(dl_loc))
     elif is_url:
         await self._message.edit("`Downloading From URL...`")
         url = is_url[0]
         file_name = os.path.basename(url)
         if "|" in self._message.input_str:
             file_name = self._message.input_str.split("|")[1].strip()
         dl_loc = os.path.join(Config.DOWN_PATH, file_name)
         try:
             downloader = SmartDL(url, dl_loc, progress_bar=False)
             downloader.start(blocking=False)
             count = 0
             while not downloader.isFinished():
                 if self._message.process_is_canceled:
                     downloader.stop()
                     raise Exception('Process Canceled!')
                 total_length = downloader.filesize if downloader.filesize else 0
                 downloaded = downloader.get_dl_size()
                 percentage = downloader.get_progress() * 100
                 speed = downloader.get_speed(human=True)
                 estimated_total_time = downloader.get_eta(human=True)
                 progress_str = \
                     "__{}__\n" + \
                     "```[{}{}]```\n" + \
                     "**Progress** : `{}%`\n" + \
                     "**URL** : `{}`\n" + \
                     "**FILENAME** : `{}`\n" + \
                     "**Completed** : `{}`\n" + \
                     "**Total** : `{}`\n" + \
                     "**Speed** : `{}`\n" + \
                     "**ETA** : `{}`"
                 progress_str = progress_str.format(
                     "trying to download", ''.join(
                         ["■" for i in range(math.floor(percentage / 10))]),
                     ''.join([
                         "▨"
                         for i in range(10 - math.floor(percentage / 10))
                     ]), round(percentage,
                               2), url, file_name, humanbytes(downloaded),
                     humanbytes(total_length), speed, estimated_total_time)
                 count += 1
                 if count >= 5:
                     count = 0
                     await self._message.try_to_edit(
                         progress_str, disable_web_page_preview=True)
                 await asyncio.sleep(1)
         except Exception as d_e:
             await self._message.err(d_e)
             return
     upload_file_name = dl_loc if dl_loc else self._message.input_str
     if not os.path.exists(upload_file_name):
         await self._message.err("invalid file path provided?")
         return
     await self._message.edit("`Loading GDrive Upload...`")
     pool.submit_thread(self._upload, upload_file_name)
     start_t = datetime.now()
     count = 0
     while not self._is_finished:
         count += 1
         if self._message.process_is_canceled:
             self._cancel()
         if self._progress is not None and count >= 5:
             count = 0
             await self._message.try_to_edit(self._progress)
         await asyncio.sleep(1)
     if dl_loc and os.path.exists(dl_loc):
         os.remove(dl_loc)
     end_t = datetime.now()
     m_s = (end_t - start_t).seconds
     if isinstance(self._output, HttpError):
         out = f"**ERROR** : `{self._output._get_reason()}`"
     elif self._output is not None and not self._is_canceled:
         out = f"**Uploaded Successfully** __in {m_s} seconds__\n\n{self._output}\n\n📂 Index link: <a href='{Config.INDEX_PATH_URL}/{file_name}'>Click here</a>"
     elif self._output is not None and self._is_canceled:
         out = self._output
     else:
         out = "`failed to upload.. check logs?`"
     await self._message.edit(out, disable_web_page_preview=True, log=True)
Пример #35
0
async def mega_downloader(megadl):
    await megadl.edit("`Collecting information...`")
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await megadl.edit("Usage: `.mega` **<MEGA.nz link>**")
    try:
        link = re.findall(r'\bhttps?://.*mega.*\.nz\S+', link)[0]
        """ - Mega changed their URL again - """
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await megadl.edit("`folder download support are removed...`")
            return
    except IndexError:
        return await megadl.edit("`MEGA.nz link not found...`")
    cmd = f'bin/megadown -q -m {link}'
    result = await subprocess_run(megadl, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        return await megadl.edit("`Err: failed to extract link...`\n")
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    temp_file_path = TEMP_DOWNLOAD_DIRECTORY + temp_file_name
    file_path = TEMP_DOWNLOAD_DIRECTORY + file_name
    if os.path.isfile(file_path):
        try:
            raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST),
                                  file_path)
        except FileExistsError as e:
            return await megadl.edit(f"`{str(e)}`")
    downloader = SmartDL(file_url, temp_file_path, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        return await megadl.edit(f"`Err: {str(e)}`")
    start = time.time()
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        speed = downloader.get_speed(human=True)
        estimated_total_time = round(downloader.get_eta())
        progress_str = "`{0}` | [{1}{2}] `{3}%`".format(
            status, ''.join(["■" for i in range(math.floor(percentage / 10))]),
            ''.join(["▨" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2))
        diff = time.time() - start
        try:
            current_message = (
                f"`{file_name}`\n\n"
                "Status\n"
                f"{progress_str}\n"
                f"`{humanbytes(downloaded)} of {humanbytes(total_length)}"
                f" @ {speed}`\n"
                f"`ETA` -> {time_formatter(estimated_total_time)}\n"
                f"`Duration` -> {time_formatter(round(diff))}")
            if round(diff % 10.00) == 0 and (display_message != current_message
                                             or total_length == downloaded):
                await megadl.edit(current_message)
                await asyncio.sleep(0.2)
                display_message = current_message
        except Exception:
            pass
        finally:
            if status == "Combining":
                wait = round(downloader.get_eta())
                await asyncio.sleep(wait)
    if downloader.isSuccessful():
        download_time = round(downloader.get_dl_time() + wait)
        try:
            P = multiprocessing.Process(target=await
                                        decrypt_file(megadl, file_path,
                                                     temp_file_path, hex_key,
                                                     hex_raw_key),
                                        name="Decrypt_File")
            P.start()
            P.join()
        except FileNotFoundError as e:
            return await megadl.edit(f"`{str(e)}`")
        else:
            return await megadl.edit(
                f"`{file_name}`\n\n"
                f"Successfully downloaded in: `{file_path}`.\n"
                f"Download took: {time_formatter(download_time)}.")
    else:
        await megadl.edit("`Failed to download, "
                          "check heroku Logs for more details`.")
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Пример #36
0
	def run(self): # Called by Qt once the thread environment has been set up.
		while self.tasks:
			taskId, songObj, dl_dir = self.tasks.pop(0)
			self.startedTask.emit(taskId)
			
			isVideo = songObj.ext in ['mp4', 'avi', 'flv', 'webm', 'mkv']
			isAudio = songObj.ext in ['mp3', 'm4a', 'wav']
			isMultimediaFile = any([isVideo, isAudio])
			convertNeeded = isMultimediaFile and songObj.ext != 'mp3' and config.downloadAudio
			encode_time = 0
			url = songObj.url
			filesize = songObj.filesize
			
			audio_path = os.path.join(dl_dir, songObj.GetProperFilename('mp3')) # final path
			video_path = os.path.join(dl_dir, songObj.GetProperFilename()) # final path
			dest_audio_path = os.path.join(config.temp_dir, "%s.mp3" % utils.get_rand_string())
			
			if not isMultimediaFile:
				dest_path = os.path.join(config.temp_dir, utils.get_rand_string())
			elif songObj.ext == "mp3": # no convertion needed
				dest_path = dest_audio_path
			else:
				dest_path = os.path.join(config.temp_dir, "%s.%s" % (utils.get_rand_string(), songObj.ext))
			
			dl_obj = SmartDL(url, dest_path, logger=log, fix_urls=False)
			dl_obj.start(blocking=False)
			
			while not dl_obj.isFinished():
				if dl_obj.status == 'combining':
					self.status.emit(tr("Combining Parts..."))
					break
				
				self.downloadProgress.emit(int(dl_obj.get_progress()*100), dl_obj.get_speed(), dl_obj.get_eta(), dl_obj.get_dl_size(), filesize)
				time.sleep(0.1)
			while not dl_obj.isFinished():
				# if we were breaking the last loop, we are waiting for
				# parts to get combined. we shall wait.
				time.sleep(0.1)
			if not dl_obj.isSuccessful:
				log.error("Got DownloadFailedException() for %s" % url)
				self.error.emit(IOError())
				self.terminate()
				return
			self.downloadProgress.emit(100, dl_obj.get_speed(), dl_obj.get_eta(), filesize, filesize)
			
			if convertNeeded:
				t1 = time.time()
				log.debug("Encoding Audio...")
				self.status.emit(tr("Encoding Audio..."))
				est_final_filesize = songObj.final_filesize
				if est_final_filesize:
					print "Encoding: %s (%.2f MB) to %s" % (dest_audio_path, est_final_filesize / 1024.0 / 1024.0, dl_dir)
				else:
					print "Encoding: %s to %s" % (dest_audio_path, dl_dir)
				
				proc = Wrappers.FFMpeg(dest_path, dest_audio_path, config.itag_audio_bitrates[songObj.itag.quality])
				self.encProgress.emit(0)
				for fs_counter in proc:
					if not est_final_filesize:
						continue
					status = r"Encoding: %.2f MB / %.2f MB %s [%3.2f%%]" % (fs_counter / 1024.0, est_final_filesize / 1024.0**2, utils.progress_bar(1.0*fs_counter*1024/est_final_filesize) , fs_counter*1024 * 100.0 / est_final_filesize)
					status = status + chr(8)*(len(status)+1)
					print status,
					self.encProgress.emit(int(fs_counter*1024 * 100.0 / est_final_filesize))
				self.encProgress.emit(100)
				
				t2 = time.time()
				encode_time += t2-t1
				
				if not config.downloadVideo or not isVideo:
					log.debug("Removing %s..." % dest_path)
					os.unlink(dest_path)
			else:
				dest_audio_path = dest_path
					
			if config.downloadAudio and config.trimSilence:
				t1 = time.time()
				
				log.debug("Trimming Silence...")
				self.status.emit(tr("Trimming Silence from edges..."))
				
				temp_audio_trimmed_path = "%s.tmp.mp3" % dest_audio_path
				if os.path.exists(temp_audio_trimmed_path):
					os.unlink(temp_audio_trimmed_path)
				os.rename(dest_audio_path, temp_audio_trimmed_path)
				
				est_final_filesize = songObj.final_filesize
				print "Trimming Silence: %s (%.2f MB) to %s" % (dest_audio_path, est_final_filesize / 1024.0**2, dl_dir)
				self.encProgress.emit(0)
				
				proc = Wrappers.SoX(temp_audio_trimmed_path, dest_audio_path)
				for progress in proc:
					status = r"Trimming Silence: %s" % utils.progress_bar(progress/100.0)
					status = status + chr(8)*(len(status)+1)
					print status,
					self.encProgress.emit(progress)
				self.encProgress.emit(100)
				
				t2 = time.time()
				encode_time += t2-t1
				
				if not os.path.exists(dest_audio_path):
					log.error('SoX failed.')
				
			log.debug("Copying Files...")
			self.status.emit(tr("Copying Files..."))
			
			if isVideo:
				# IMPROVE: this crashes when a video is running in media player, os.unlink removes it, but it is still running in media player.
				if config.downloadAudio:
					log.debug("Moving %s to %s" % (dest_audio_path, audio_path))
					shutil.move(dest_audio_path, audio_path) 
				if config.downloadVideo:
					log.debug("Moving %s to %s" % (dest_path, video_path))
					shutil.move(dest_path, video_path)
			if isAudio:
				log.debug("Moving %s to %s" % (dest_path, audio_path))
				shutil.move(dest_path, audio_path) 
			
			dl_time = dl_obj.get_dl_time()
			dl_time_s = int(dl_time)%60
			dl_time_m = int(dl_time)/60
			if filesize/dl_time/1024**2 > 1: # If dlRate is in MBs
				if dl_time_m:
					stats_str = tr('Download: %d:%.2d (%.2f MB/s)') % (dl_time_m, dl_time_s, filesize/dl_time/1024**2)
				else:
					stats_str = tr('Download: %ds (%.2f MB/s)') % (dl_time, filesize/dl_time/1024**2)
			else:
				if dl_time_m:
					stats_str = tr('Download: %d:%.2d (%.2f KB/s)') % (dl_time_m, dl_time_s, filesize/dl_time/1024)
				else:
					stats_str = tr('Download: %ds (%.2f KB/s)') % (dl_time, filesize/dl_time/1024)
			
			if encode_time:
				stats_str += tr('; Encoded: %ds') % encode_time
			self.status.emit(stats_str)
			self.finishedTask.emit(taskId, dl_time, encode_time)
Пример #37
0
async def _(event):
    if event.fwd_from:
        return
    mone = await edit_or_reply(event, "`Processing ...`")
    input_str = event.pattern_match.group(1)
    if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
    if event.reply_to_msg_id:
        start = datetime.now()
        reply_message = await event.get_reply_message()
        try:
            c_time = time.time()
            downloaded_file_name = await event.client.download_media(
                reply_message,
                Config.TMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, mone, c_time, "trying to download")),
            )
        except Exception as e:  # pylint:disable=C0103,W0703
            await mone.edit(str(e))
        else:
            end = datetime.now()
            ms = (end - start).seconds
            await mone.edit(
                f"**  •  Downloaded in {ms} seconds.**\n**  •  Downloaded to :- ** `{downloaded_file_name}`\n**  •  Downloaded by :-** {DEFAULTUSER}"
            )
    elif input_str:
        start = datetime.now()
        url = input_str
        file_name = os.path.basename(url)
        to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
        if "|" in input_str:
            url, file_name = input_str.split("|")
        url = url.strip()
        file_name = file_name.strip()
        downloaded_file_name = os.path.join(to_download_directory, file_name)
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        while not downloader.isFinished():
            total_length = downloader.filesize or None
            downloaded = downloader.get_dl_size()
            display_message = ""
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            downloader.get_speed()
            progress_str = "`{0}{1} {2}`%".format(
                "".join(["▰" for i in range(math.floor(percentage / 5))]),
                "".join(["▱" for i in range(20 - math.floor(percentage / 5))]),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"Downloading the file\
                                \n\n**URL : **`{url}`\
                                \n**File Name :** `{file_name}`\
                                \n{progress_str}\
                                \n`{humanbytes(downloaded)} of {humanbytes(total_length)}`\
                                \n**ETA : **`{estimated_total_time}``"

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await mone.edit(current_message)
                    display_message = current_message
            except Exception as e:
                logger.info(str(e))
        end = datetime.now()
        ms = (end - start).seconds
        if downloader.isSuccessful():
            await mone.edit(
                f"**  •  Downloaded in {ms} seconds.**\n**  •  Downloaded to :- ** `{downloaded_file_name}`"
            )
        else:
            await mone.edit("Incorrect URL\n {}".format(input_str))
    else:
        await mone.edit("Reply to a message to download to my local server.")
Пример #38
0
async def download(target_file):
    """ For .download command, download files to the userbot's server. """
    await target_file.edit("Processing ...")
    input_str = target_file.pattern_match.group(1)
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            progress_str = "[{0}{1}] `{2}%`".format(
                "".join(["●" for i in range(math.floor(percentage / 10))]),
                "".join(["○"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = (
                    f"`Name` : `{file_name}`\n"
                    "Status"
                    f"\n**{status}**... | {progress_str}"
                    f"\n{humanbytes(downloaded)} of {humanbytes(total_length)}"
                    f" @ {humanbytes(speed)}"
                    f"\n`ETA` -> {estimated_total_time}")

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit("Downloaded to `{}` successfully !!".format(
                downloaded_file_name))
        else:
            await target_file.edit("Incorrect URL\n{}".format(url))
    elif target_file.reply_to_msg_id:
        try:
            replied = await target_file.get_reply_message()
            file = replied.document
            attribs = replied.media.document.attributes
            for attr in attribs:
                if isinstance(attr, DocumentAttributeFilename):
                    filename = attr.file_name
            outdir = TEMP_DOWNLOAD_DIRECTORY + filename
            c_time = time.time()
            start_time = datetime.now()
            with open(outdir, "wb") as f:
                result = await download_file(
                    client=target_file.client,
                    location=file,
                    out=f,
                    progress_callback=lambda d, t: asyncio.get_event_loop().
                    create_task(
                        progress(d, t, target_file, c_time, "[DOWNLOAD]",
                                 input_str)),
                )
            dl_time = (datetime.now() - start_time).seconds
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            await target_file.edit(
                "Downloaded to `{}` in `{}` seconds.".format(
                    result.name, dl_time))
    else:
        await target_file.edit(
            "Reply to a message to download to my local server.")
Пример #39
0
async def mega_downloader(megadl):
    await megadl.edit("`Processing...`")
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await megadl.edit("Usage: `.mega <MEGA.nz link>`")
    try:
        link = re.findall(r'\bhttps?://.*mega.*\.nz\S+', link)[0]
        """ - Mega changed their URL again - """
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await megadl.edit(
                "`Currently support folder download are removed`.")
            return
    except IndexError:
        return await megadl.edit("`No MEGA.nz link found`\n")
    cmd = f'bin/megadown -q -m {link}'
    result = await subprocess_run(megadl, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        return await megadl.edit("`Error: Can't extract the link`\n")
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    downloaded_file_name = "./" + "" + temp_file_name
    downloader = SmartDL(file_url, downloaded_file_name, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        return await megadl.edit("`" + str(e) + "`")
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        progress = downloader.get_progress_bar()
        speed = downloader.get_speed(human=True)
        estimated_total_time = downloader.get_eta(human=True)
        try:
            current_message = (
                "File Name:"
                f"\n`{file_name}`\n\n"
                "Status:"
                f"\n**{status}** | {progress} `{percentage}%`"
                f"\n{humanbytes(downloaded)} of {humanbytes(total_length)}"
                f" @ {speed}"
                f"\nETA: {estimated_total_time}")
            if display_message != current_message:
                await megadl.edit(current_message)
                await asyncio.sleep(0.2)
                display_message = current_message
        except Exception:
            pass
        finally:
            if status == "Combining":
                await asyncio.sleep(float(downloader.get_eta()))
    if downloader.isSuccessful():
        download_time = downloader.get_dl_time(human=True)
        try:
            P = multiprocessing.Process(target=await
                                        decrypt_file(megadl, file_name,
                                                     temp_file_name, hex_key,
                                                     hex_raw_key),
                                        name="Decrypt_File")
            P.start()
            P.join()
        except FileNotFoundError as e:
            return await megadl.edit(str(e))
        else:
            return await megadl.edit(f"`{file_name}`\n\n"
                                     "Successfully downloaded\n"
                                     f"Download took: {download_time}")
    else:
        await megadl.edit("`Failed to download, "
                          "check heroku Logs for more details`")
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Пример #40
0
                    dest='dir',
                    nargs='?',
                    metavar='your Directory',
                    help='Default = current directory')
args = parser.parse_args()
if args.url == None:
    print("Usage: < python3 Download_Manager.py -h/--help >")
    exit(0)
else:
    url = str(args.url)
    if args.dir == None:
        dest = os.getcwd()
    else:
        dest = str(args.dir)

    obj = SmartDL(url, dest)
    print("\n" + "=" * 70 + "\n")
    file_name = url.split("/")[-1]
    obj.start()
    path = obj.get_dest()
    if obj.isSuccessful():
        print("\n" + "=" * 70 + "\n")
        print("File Name: '%s'" % file_name)
        print("downloaded file to '%s'" % obj.get_dest())
        print("Speed: %s" % obj.get_speed(human=True))
        print("Already downloaded: %s" % obj.get_dl_size(human=True))
        print("Eta: %s" % obj.get_eta(human=True))
        print("Progress: %d%%" % (obj.get_progress() * 100))
        print("Progress bar: %s" % obj.get_progress_bar())
        print("Status: %s" % obj.get_status())
        print("\n" * 2 + "=" * 70 + "\n")
Пример #41
0
async def mega_downloader(megadl):
    catevent = await edit_or_reply(megadl, "__Collecting information...__")
    if not os.path.isdir(TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TMP_DOWNLOAD_DIRECTORY)
    msg_link = await megadl.get_reply_message()
    link = megadl.pattern_match.group(1)
    if link:
        pass
    elif msg_link:
        link = msg_link.text
    else:
        return await catevent.edit("Usage: __.mega__ **<MEGA.nz link>**")
    try:
        link = re.findall(r"\bhttps?://.*mega.*\.nz\S+", link)[0]
        # - Mega changed their URL again -
        if "file" in link:
            link = link.replace("#", "!").replace("file/", "#!")
        elif "folder" in link or "#F" in link or "#N" in link:
            await catevent.edit("__folder download support are removed...__")
            return
    except IndexError:
        await catevent.edit("__MEGA.nz link not found...__")
        return None
    cmd = f"bin/megadown -q -m {link}"
    result = await subprocess_run(catevent, cmd)
    try:
        data = json.loads(result[0])
    except json.JSONDecodeError:
        await catevent.edit("**JSONDecodeError**: __failed to extract link...__")
        return None
    except (IndexError, TypeError):
        return
    file_name = data["file_name"]
    file_url = data["url"]
    hex_key = data["hex_key"]
    hex_raw_key = data["hex_raw_key"]
    temp_file_name = file_name + ".temp"
    temp_file_path = os.path.join(TMP_DOWNLOAD_DIRECTORY, temp_file_name)
    file_path = os.path.join(TMP_DOWNLOAD_DIRECTORY, file_name)
    if os.path.isfile(file_path):
        try:
            raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), file_path)
        except FileExistsError as e:
            await catevent.edit(f"__{str(e)}__")
            return None
    downloader = SmartDL(file_url, temp_file_path, progress_bar=False)
    display_message = None
    try:
        downloader.start(blocking=False)
    except HTTPError as e:
        await catevent.edit(f"**HTTPError**: __{str(e)}__")
        return None
    start = time.time()
    while not downloader.isFinished():
        status = downloader.get_status().capitalize()
        total_length = downloader.filesize or None
        downloaded = downloader.get_dl_size()
        percentage = int(downloader.get_progress() * 100)
        speed = downloader.get_speed(human=True)
        estimated_total_time = round(downloader.get_eta())
        progress_str = "__{0}__ | [{1}{2}] __{3}%__".format(
            status,
            "".join(["▰" for i in range(math.floor(percentage / 10))]),
            "".join(["▱" for i in range(10 - math.floor(percentage / 10))]),
            round(percentage, 2),
        )
        diff = time.time() - start
        try:
            current_message = (
                f"**➥file name : **__{file_name}__\n\n"
                "**➥Status**\n"
                f"{progress_str}\n"
                f"__{humanbytes(downloaded)}__ of __{humanbytes(total_length)}__"
                f" @ __{speed}__\n"
                f"**➥ETA -> **__{time_formatter(estimated_total_time)}__\n"
                f"**➥ Duration -> **__{time_formatter(round(diff))}__"
            )
            if round(diff % 15.00) == 0 and (
                display_message != current_message or total_length == downloaded
            ):
                await catevent.edit(current_message)
                await asyncio.sleep(1)
                display_message = current_message
        except Exception as e:
            LOGS.info(str(e))
        finally:
            if status == "Combining":
                wait = round(downloader.get_eta())
                await asyncio.sleep(wait)
    if downloader.isSuccessful():
        download_time = round(downloader.get_dl_time() + wait)
        try:
            P = multiprocessing.Process(
                target=await decrypt_file(
                    catevent, file_path, temp_file_path, hex_key, hex_raw_key
                ),
                name="Decrypt_File",
            )
            P.start()
            P.join()
        except FileNotFoundError as e:
            await catevent.edit(f"__{str(e)}__")
            return None
        else:
            await catevent.edit(
                f"**➥ file name : **__{file_name}__\n\n"
                f"**➥ Successfully downloaded in : ** __{file_path}__.\n"
                f"**➥ Download took :** {time_formatter(download_time)}."
            )
            return None
    else:
        await megadl.edit(
            "__Failed to download, " "check heroku Logs for more details.__"
        )
        for e in downloader.get_errors():
            LOGS.info(str(e))
    return
Пример #42
0
async def download(target_file):
    """ For .download command, download files to the userbot's server. """
    await target_file.edit("**Processing...**")
    input_str = target_file.pattern_match.group(1)
    replied = await target_file.get_reply_message()
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if input_str:
        url = input_str
        file_name = unquote_plus(os.path.basename(url))
        if "|" in input_str:
            url, file_name = input_str.split("|")
            url = url.strip()
            # https://stackoverflow.com/a/761825/4723940
            file_name = file_name.strip()
            head, tail = os.path.split(file_name)
            if head:
                if not os.path.isdir(
                        os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                    os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                    file_name = os.path.join(head, tail)
        try:
            url = get(url).url
        except BaseException:
            return await target_file.edit("**This is not a valid link.**")
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize or None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            progress_str = "[{}{}] `{}%`".format(
                "".join("●" for _ in range(math.floor(percentage / 10))),
                "".join("○" for _ in range(10 - math.floor(percentage / 10))),
                round(percentage, 2),
            )

            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = (
                    f"**Name:** `{file_name}`\n"
                    f"\n**{status}...** | {progress_str}"
                    f"\n{humanbytes(downloaded)} of {humanbytes(total_length)}"
                    f" @ {humanbytes(speed)}"
                    f"\n**ETA:** {estimated_total_time}")

                if round(diff %
                         15.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit(
                f"**Downloaded to** `{downloaded_file_name}` **successfully!**"
            )
        else:
            await target_file.edit(f"**Incorrect URL**\n{url}")
    elif replied:
        if not replied.media:
            return await target_file.edit("**Reply to file or media.**")
        try:
            media = replied.media
            if hasattr(media, "document"):
                file = media.document
                mime_type = file.mime_type
                filename = replied.file.name
                if not filename:
                    if "audio" in mime_type:
                        filename = ("audio_" +
                                    datetime.now().isoformat("_", "seconds") +
                                    ".ogg")
                    elif "video" in mime_type:
                        filename = ("video_" +
                                    datetime.now().isoformat("_", "seconds") +
                                    ".mp4")
                outdir = TEMP_DOWNLOAD_DIRECTORY + filename
                c_time = time.time()
                start_time = datetime.now()
                with open(outdir, "wb") as f:
                    result = await download_file(
                        client=target_file.client,
                        location=file,
                        out=f,
                        progress_callback=lambda d, t: asyncio.get_event_loop(
                        ).create_task(
                            progress(
                                d,
                                t,
                                target_file,
                                c_time,
                                "Telegram - Download",
                                input_str,
                            )),
                    )
            else:
                start_time = datetime.now()
                result = await target_file.client.download_media(
                    media, TEMP_DOWNLOAD_DIRECTORY)
            dl_time = (datetime.now() - start_time).seconds
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            try:
                await target_file.edit(
                    f"**Downloaded to** `{result.name}` **in {dl_time} seconds.**"
                )
            except AttributeError:
                await target_file.edit(
                    f"**Downloaded to** `{result}` **in {dl_time} seconds.**")
    else:
        await target_file.edit("**See** `.help download` **for more info.**")
Пример #43
0
async def _(event):
    if event.fwd_from:
        return
    if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
    thumb = None
    if os.path.exists(thumb_image_path):
        thumb = thumb_image_path
    start = datetime.now()
    input_str = event.pattern_match.group(1)
    url = input_str
    file_name = os.path.basename(url)
    to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
    if "|" in input_str:
        url, file_name = input_str.split("|")
    url = url.strip()
    file_name = file_name.strip()
    downloaded_file_name = os.path.join(to_download_directory, file_name)
    downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
    downloader.start(blocking=False)
    display_message = ""
    c_time = time.time()
    while not downloader.isFinished():
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        now = time.time()
        diff = now - c_time
        percentage = downloader.get_progress() * 100
        speed = downloader.get_speed()
        elapsed_time = round(diff) * 1000
        progress_str = "[{0}{1}]\nProgress: {2}%".format(
            ''.join(["█" for i in range(math.floor(percentage / 5))]),
            ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
            round(percentage, 2))
        estimated_total_time = downloader.get_eta(human=True)
        try:
            current_message = f"trying to download\n"
            current_message += f"URL: {url}\n"
            current_message += f"File Name: {file_name}\n"
            current_message += f"{progress_str}\n"
            current_message += f"{humanbytes(downloaded)} of {humanbytes(total_length)}\n"
            current_message += f"ETA: {estimated_total_time}"
            if round(diff % 10.00) == 0 and current_message != display_message:
                await event.edit(current_message)
                display_message = current_message
        except Exception as e:
            logger.info(str(e))
    end = datetime.now()
    ms_dl = (end - start).seconds
    if downloader.isSuccessful():
        await event.edit("Downloaded to `{}` in {} seconds.".format(
            downloaded_file_name, ms_dl))
        if os.path.exists(downloaded_file_name):
            c_time = time.time()
            await borg.send_file(
                event.chat_id,
                downloaded_file_name,
                force_document=True,
                supports_streaming=False,
                allow_cache=False,
                reply_to=event.message.id,
                thumb=thumb,
                progress_callback=lambda d, t: asyncio.get_event_loop().
                create_task(progress(d, t, event, c_time, "trying to upload")))
            end_two = datetime.now()
            os.remove(downloaded_file_name)
            ms_two = (end_two - end).seconds
            await event.edit(
                "Downloaded in {} seconds. Uploaded in {} seconds.".format(
                    ms_dl, ms_two))
        else:
            await event.edit("File Not Found {}".format(input_str))
    else:
        await event.edit("Incorrect URL\n {}".format(input_str))
Пример #44
0
async def download_telegram(client, message):
      mone = await message.edit("Processing ...") # Reply
      url = message.text[10:]
      if message.reply_to_message:
         start = datetime.now()
         c_time = time.time()
         try:
             downloaded_file_name = await message.reply_to_message.download(
                                    file_name=DOWNLOAD_LOCATION,
                                    progress=progress_for_pyrogram,
                                    progress_args=(
                                                   mone,c_time, "Downloading... "
             )
             )
             downloaded_file_name=downloaded_file_name[4:]
             downloaded_file_name="."+downloaded_file_name
         except Exception as e: 
             await mone.edit(str(e))
         else:
             end = datetime.now()
             ms = (end - start).seconds
             await mone.edit("Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms))
             # time.sleep(100000)
             # await mone.delete()
      
      elif url:
           start = datetime.now()
           file_name = os.path.basename(url)
           to_download_directory = "./DOWNLOADS/"
           if "|" in url:
               url, file_name = url.split("|")
           url = url.strip()
           file_name = file_name.strip()
           downloaded_file_name = os.path.join(to_download_directory, file_name)
           downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
           downloader.start(blocking=False)
           display_message = ""
           c_time = time.time()
           while not downloader.isFinished():
                 total_length = downloader.filesize if downloader.filesize else None
                 downloaded = downloader.get_dl_size()
                 now = time.time()
                 diff = now - c_time
                 percentage = downloader.get_progress() * 100
                 speed = downloader.get_speed()
                 elapsed_time = round(diff) * 1000
                 progress_str = "[{0}{1}]\nProgress: {2}%".format(
                          ''.join(["█" for i in range(math.floor(percentage / 5))]),
                          ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
                          round(percentage, 2))
                 estimated_total_time = downloader.get_eta(human=True)
                 try:
                    current_message = f"trying to download\n"
                    current_message += f"URL: {url}\n"
                    current_message += f"File Name: {file_name}\n"
                    current_message += f"{progress_str}\n"
                    current_message += f"{humanbytes(downloaded)} of {humanbytes(total_length)}\n"
                    current_message += f"ETA: {estimated_total_time}"
                    if round(diff % 10.00) == 0 and current_message != display_message:
                       await mone.edit(current_message)
                       display_message = current_message
                 except Exception as e:
                       await mone.edit(str(e))
           end = datetime.now()
           ms = (end - start).seconds
           if downloader.isSuccessful():
              await mone.edit("Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms))
           else:
              await mone.edit("Incorrect URL\n {}".format(url))
      
      else:
           await mone.edit("Reply to a message to download to my local server.")
           time.sleep(5)
           await mone.delete()
Пример #45
0
async def _(event):
    if event.fwd_from:
        return
    mone = await event.reply("Processing ...")
    input_str = event.pattern_match.group(1)
    if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
    if event.reply_to_msg_id:
        start = datetime.now()
        reply_message = await event.get_reply_message()
        try:
            c_time = time.time()
            downloaded_file_name = await event.client.download_media(
                reply_message,
                Config.TMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop().
                create_task(progress(d, t, mone, c_time, "trying to download"))
            )
        except Exception as e:  # pylint:disable=C0103,W0703
            await mone.edit(str(e))
        else:
            end = datetime.now()
            ms = (end - start).seconds
            await mone.edit("Downloaded to `{}` in {} seconds.".format(
                downloaded_file_name, ms))
    elif input_str:
        start = datetime.now()
        url = input_str
        file_name = os.path.basename(url)
        to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
        if "|" in input_str:
            url, file_name = input_str.split("|")
        url = url.strip()
        file_name = file_name.strip()
        downloaded_file_name = os.path.join(to_download_directory, file_name)
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        while not downloader.isFinished():
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            display_message = ""
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            elapsed_time = round(diff) * 1000
            progress_str = "{0}{1}\nProgress: {2}%".format(
                ''.join(["█" for i in range(math.floor(percentage / 5))]),
                ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
                round(percentage, 2))
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"trying to download\nURL: {url}\nFile Name: {file_name}\n{progress_str}\n{humanbytes(downloaded)} of {humanbytes(total_length)}\nETA: {estimated_total_time}"
                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await mone.edit(current_message)
                    display_message = current_message
            except Exception as e:
                logger.info(str(e))
        end = datetime.now()
        ms = (end - start).seconds
        if downloader.isSuccessful():
            await mone.edit("Downloaded to `{}` in {} seconds.".format(
                downloaded_file_name, ms))
        else:
            await mone.edit("Incorrect URL\n {}".format(input_str))
    else:
        await mone.edit("Reply to a message to download to my local server.")
Пример #46
0
 def test_mirrors(self):
     urls = ["http://totally_fake_website/7za.zip" ,"http://mirror.ufs.ac.za/7zip/9.20/7za920.zip"]
     obj = SmartDL(urls, dest=self.dl_dir, progress_bar=False)
     obj.start()
     
     self.assertTrue(obj.isSuccessful())
Пример #47
0
async def download(target_file):
    """ For .dl command, download files to the userbot's server. """
    await target_file.edit("Processing ...")
    input_str = target_file.pattern_match.group(1)
    if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
        os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
    if "|" in input_str:
        url, file_name = input_str.split("|")
        url = url.strip()
        # https://stackoverflow.com/a/761825/4723940
        file_name = file_name.strip()
        head, tail = os.path.split(file_name)
        if head:
            if not os.path.isdir(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head)):
                os.makedirs(os.path.join(TEMP_DOWNLOAD_DIRECTORY, head))
                file_name = os.path.join(head, tail)
        downloaded_file_name = TEMP_DOWNLOAD_DIRECTORY + "" + file_name
        downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        display_message = None
        while not downloader.isFinished():
            status = downloader.get_status().capitalize()
            total_length = downloader.filesize if downloader.filesize else None
            downloaded = downloader.get_dl_size()
            now = time.time()
            diff = now - c_time
            percentage = downloader.get_progress() * 100
            speed = downloader.get_speed()
            elapsed_time = round(diff) * 1000
            progress_str = "[{0}{1}] {2}%".format(
                ''.join(["▰" for i in range(math.floor(percentage / 10))]),
                ''.join(["▱"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2))
            estimated_total_time = downloader.get_eta(human=True)
            try:
                current_message = f"{status}..\
                \nURL: {url}\
                \nFile Name: {file_name}\
                \n{progress_str}\
                \n{humanbytes(downloaded)} of {humanbytes(total_length)}\
                \nETA: {estimated_total_time}"

                if round(diff %
                         10.00) == 0 and current_message != display_message:
                    await target_file.edit(current_message)
                    display_message = current_message
            except Exception as e:
                LOGS.info(str(e))
        if downloader.isSuccessful():
            await target_file.edit("Downloaded to `{}` successfully !!".format(
                downloaded_file_name))
        else:
            await target_file.edit("Incorrect URL\n{}".format(url))
    elif target_file.reply_to_msg_id:
        try:
            c_time = time.time()
            downloaded_file_name = await target_file.client.download_media(
                await target_file.get_reply_message(),
                TEMP_DOWNLOAD_DIRECTORY,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, target_file, c_time, "Downloading...")))
        except Exception as e:  # pylint:disable=C0103,W0703
            await target_file.edit(str(e))
        else:
            await target_file.edit("Downloaded to `{}` successfully !!".format(
                downloaded_file_name))
    else:
        await target_file.edit(
            "Reply to a message to download to my local server.")
Пример #48
0
 def test_pause_unpause_stop(self):
     obj = SmartDL(self.default_7za920_mirrors, dest=self.dl_dir, progress_bar=False)
     obj.start(blocking=False)
     
     while not obj.get_dl_size():
         time.sleep(0.2)
         
     time.sleep(1)
     obj.pause()
     time.sleep(0.5)
     dl_size = obj.get_dl_size()
     time.sleep(2.5)
     self.assertEqual(dl_size, obj.get_dl_size())
     
     obj.unpause()
     time.sleep(0.5)
     self.assertNotEqual(dl_size, obj.get_dl_size())
     
     obj.stop()
     obj.wait()
     self.assertFalse(obj.isSuccessful())
Пример #49
0
async def _(event):
    if event.fwd_from:
        return
    await event.delete()
    mone = await event.reply("Processing ...")
    input_str = event.pattern_match.group(1)
    sample_url = "https://www.scandit.com/wp-content/themes/bridge-child/wbq_barcode_gen.php?symbology=code128&value={}&size=100&ec=L".format(input_str.replace(" ","-"))
    link = sample_url.rstrip()
    start = datetime.now()
    url = link
    file_name = "barcode.webp"
    to_download_directory = Config.TMP_DOWNLOAD_DIRECTORY
    url = url.strip()
    file_name = file_name.strip()
    downloaded_file_name = os.path.join(to_download_directory, file_name)
    downloader = SmartDL(url, downloaded_file_name, progress_bar=False)
    downloader.start(blocking=False)
    c_time = time.time()
    while not downloader.isFinished():
        # url
        # downloaded_file_name
        # mone
        # c_time
        total_length = downloader.filesize if downloader.filesize else None
        downloaded = downloader.get_dl_size()
        display_message = ""
        now = time.time()
        diff = now - c_time
        percentage = downloader.get_progress() * 100
        speed = downloader.get_speed()
        elapsed_time = round(diff) * 1000
        progress_str = "[{0}{1}]\nProgress: {2}%".format(
            ''.join(["●" for i in range(math.floor(percentage / 5))]),
            ''.join(["○" for i in range(20 - math.floor(percentage / 5))]),
            round(percentage, 2))
        estimated_total_time = downloader.get_eta(human=True)
        try:
            current_message = f"SNAPDRAGON DOWNLOADER\n\nURL: {url}\nFile Name: {file_name}\n{progress_str}\n{humanbytes(downloaded)} of {humanbytes(total_length)}\nETA: {estimated_total_time}"
            if current_message != display_message:
                await mone.edit(current_message)
                display_message = current_message
        except Exception as e:
            logger.info(str(e))
    input_str = Config.TMP_DOWNLOAD_DIRECTORY + file_name
    thumb = None
    if os.path.exists(thumb_image_path):
        thumb = thumb_image_path
    if os.path.exists(input_str):
        start = datetime.now()
        c_time = time.time()
        await borg.send_file(
            event.chat_id,
            input_str,
            force_document=True,
            supports_streaming=False,
            allow_cache=False,
            reply_to=event.message.id,
            thumb=thumb,
            progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                progress(d, t, mone, c_time, "trying to upload")
            )
        )
        end = datetime.now()
        os.remove("barcode.webp")
        ms = (end - start).seconds
        await mone.edit("Barcode generated in {} seconds.".format(ms))