File size: 1,377 Bytes
b1cded8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import sys
import requests

from bs4 import BeautifulSoup

def Mediafire_Download(url, output=None, filename=None):
    if not filename: filename = url.split('/')[-2]
    if not output: output = os.path.dirname(os.path.realpath(__file__))

    output_file = os.path.join(output, filename)
    sess = requests.session()
    sess.headers.update({"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"})

    try:
        bs4 = BeautifulSoup(
            sess.get(url).content, 
            "html.parser"
        ).find(id="downloadButton").get("href")

        with requests.get(
            bs4, 
            stream=True
        ) as r:
            r.raise_for_status()

            with open(output_file, "wb") as f:
                total_length = int(r.headers.get('content-length'))
                download_progress = 0

                for chunk in r.iter_content(chunk_size=1024):
                    download_progress += len(chunk)
                    f.write(chunk)

                    stdout = f"\r[{filename}]: {int(100 * download_progress / total_length)}% ({round(download_progress / 1024 / 1024, 2)}mb/{round(total_length / 1024 / 1024, 2)}mb)"

                    sys.stdout.write(stdout)
                    sys.stdout.flush()

        sys.stdout.write("\n")
        return output_file
    except Exception as e:
        raise RuntimeError(e)