Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Last active March 19, 2020 15:44
Show Gist options
  • Save rehmatworks/1fbcd73a291eb22fb23a8540eaa4a396 to your computer and use it in GitHub Desktop.
Save rehmatworks/1fbcd73a291eb22fb23a8540eaa4a396 to your computer and use it in GitHub Desktop.
A simple Python script to download files from Mediafire to PC or server.

Download Mediafire files to PC or Server

The script relies on two dependencies:

  1. Python Requests
  2. BeautifulSoup4

Install the dependencies by running:

pip3 install requests
pip3 install bs4

Downloading files

Using default filename:

python3 mediafire-downloader.py --url https://www.mediafire.com/file/0uzo1dfbxgzwhlr/WindowsInstaller-KB893803-v2-x86.exe/file

This will save downloaded file to WindowsInstaller-KB893803-v2-x86.exe in current directory.

Using custom filename:

python3 mediafire-downloader.py --url https://www.mediafire.com/file/0uzo1dfbxgzwhlr/WindowsInstaller-KB893803-v2-x86.exe/file --saveas filename.exe

This will save downloaded file to filename.exe in current directory.

Using custom filename & path:

python3 mediafire-downloader.py --url https://www.mediafire.com/file/0uzo1dfbxgzwhlr/WindowsInstaller-KB893803-v2-x86.exe/file --saveas /home/rehmat/filename.exe

This will save downloaded file to /home/rehmat/filename.exe.

If this GIST is useful and you need this to be packaged as a PIP package, star it or comment below and I'll package it for easy installation through PyPi.

import requests
import argparse
from bs4 import BeautifulSoup
import sys
def sizeof_fmt(num):
for unit in ['', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']:
if abs(num) < 1024.0:
return "%3.1f%s" % (num, unit)
num /= 1024.0
return "%.1f%s" % (num, 'Yi')
parser = argparse.ArgumentParser(description='Download files from Mediafire.')
parser.add_argument('--url', dest='url', help='Mediafire URL.', required=True)
parser.add_argument('--saveas', dest='saveas',
help='Filename to save.', required=False)
args = parser.parse_args()
if args.url:
try:
url = args.url
if not args.saveas:
saveas = url[url.rfind("/")+1:]
if saveas == "file":
saveas = url.rsplit('/', 2)[1]
else:
saveas = args.saveas
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
links = soup.findAll("a", {"class": "popsok"})
link = links[0]
dllink = link.attrs["href"]
sys.stdout.write("Saving file to %s \n" % saveas)
dl = 0
response = requests.get(dllink, stream=True)
total_length = int(response.headers['Content-Length'])
with response as r:
with open(saveas, 'wb') as f:
chunk_size = 32 * (1 << 10)
for chunk in r.iter_content(chunk_size=chunk_size):
dl += len(chunk)
f.write(chunk)
done = int(50 * dl / total_length)
sys.stdout.write(
"\r[%s%s] (%s/%s)" % ('*' * done, ' ' * (50-done), sizeof_fmt(dl), sizeof_fmt(total_length)))
sys.stdout.write("\nFile downloaded to %s" % saveas)
sys.stdout.flush()
except Exception as e:
sys.stderr.write('Download failed: %s' % str(e))
sys.stderr.flush()
else:
parser.help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment