Go back to MinaSolution's CodePad.

Filename: ubuntu-megaupload-on-command-line-downloader-1291156718.html
Author: AhhDonuts
Code type: Python
Date: 09:38:38 01/12/2010 (timestamp: 1291156718)
Description:megaupload-dl : to download megaupload files using command line in Ubuntu or Linux distro.

How to install?

How to fix bug? Latest update 01-Dec-2010

Sometimes the code doesn't work well. It's seem like the author doesn't update since March 2009.
MinaSolution has taken over and made some quick fixes. The lastest fix is on 01/11/2010
To fix, after install the original package, simply replace the whole file /usr/bin/megaupload-dl with the code below (p/s: need root access to modify /usr/bin/megaupload-dl)

How to run?

Type "man megaupload-dl" in command line for help.

How to download in batch?

How to run, disown and not show on command line?

Credits

Original author and site: http://mundogeek.net/megaupload-dl/
Updated: mark.nguyen at MinaSolution

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
###
#
# Megaupload Download is the legal property of Raul Gonzalez Duque <zootropo@gmail.com>
# Copyright (c) 2007 Raul Gonzalez Duque
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###

import cookielib, urllib, urllib2
import time, sys
import subprocess
import re
import os
import ConfigParser
import getpass

try:
    import pygtk
    pygtk.require("2.0")
    import gtk
    with_gtk = True
except ImportError:
    with_gtk = False



def log(text):
    """Prints a message with the local time and date."""
    date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    print "[%s] - %s" % (date, text)

def from_megaupload(url):
    """Check if this is a megaupload link"""
    return (url.startswith("megaupload.com") or 
            url.startswith("www.megaupload.com") or 
            url.startswith("http://megaupload.com") or
            url.startswith("http://www.megaupload.com"))


class redirectManager(urllib2.HTTPRedirectHandler):
    """Used to manage the redirects we can find when the user selected direct
    downloads at Megaupload"""
    def __init__(self):
        self.redirect = ""

    def http_error_301(self, req, fp, code, msg, headers):
        """Executed when we find a 301 redirect"""
        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
        result.status = code
        self.redirect = result.geturl()
        return result

    def http_error_302(self, req, fp, code, msg, headers):
        """Executed when we find a 302 redirect"""
        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        result.status = code
        self.redirect = result.geturl()
        return result



# There needs to be at least one argument
if len(sys.argv) < 2:
    print "Too few arguments"
    print "Try --help for more information"
    exit()


# Get the login info from the config file, or ask for it
if os.environ.has_key("APPDATA") and os.path.exists(os.environ["APPDATA"]):
    path = os.environ["APPDATA"] + "/megaupload-dl.ini"
else:
    path = os.path.expanduser("~") + "/.megaupload-dl"

if not os.path.exists(path):
    user = raw_input("Enter your user name: ")
    password = getpass.getpass(prompt="Enter your password: ")
    print
else:
    cfg = ConfigParser.SafeConfigParser()
    cfg.readfp(file(path))
    try:
        user = cfg.get("Login", "user")
        password = cfg.get("Login", "password")
    except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
        print "The config file is corrupt"
        user = raw_input("Enter your user name: ")
	password = getpass.getpass(prompt="Enter your password: ")
        print
        os.remove(path)


# Process the arguments
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
    print "megaupload-dl http://megaupload.com/?d=FILE1 ... http://megaupload.com/?d=FILEN"
    print "  Download one or several megaupload links passed as argument\n"

    print "megaupload-dl http://some-web-site.com"
    print "  Download a list of links from an URL\n"

    print "megaupload-dl links.txt"
    print "  Download a list of links from a file\n"

    print "megaupload-dl -c"
    print "  Download a list of links from the clipboard (PyGTK has to be installed)\n"
    exit()

from_file = False
urls = []
if os.path.exists(sys.argv[1]):    # If this is a file
    from_file = True
    l_file = sys.argv[1]
    log("Reading list of links from the file %s" % l_file)
    print "\n"
    list_f = file(l_file, "r")
    urls = list_f.readlines()
    list_f.close()
elif sys.argv[1].startswith("http://"):    # If this is a URL
    if from_megaupload(sys.argv[1]):
        for i in range(1, len(sys.argv)):
            urls.append(sys.argv[i])
    else:
        log("This is not a Megaupload URL. Trying to retrieve links from " + sys.argv[1])
        try:
            source = "\n".join(urllib.urlopen(sys.argv[1]).readlines())
        except IOError:
            print "Operation timed out"
            exit()
        urls_source = re.findall('megaupload\.com/\?d=[\w]{8}', source)
        urls_source += re.findall('megaupload\.com/[\w]{2}/\?d=[\w]{8}', source)
        for url in urls_source:
            url = "http://" + url + "\n"
            if not url in urls:
                urls.append(url)
        print
        if urls:
            log("I found %s links. Saving to megauploadtmp.txt" % len(urls))
            l_file = "megauploadtmp.txt"
            list_f = file(l_file, "w")
            list_f.write("".join(urls))
            list_f.close()
            from_file = True
            print urls
            print
        else:
            log("I found 0 links.")
            exit()
elif sys.argv[1] == "-c" or sys.argv[1] == "--clipboard":
    if not with_gtk:
        print "PyGTK is not available in your system. This feature cannot be used"
        exit()
    else:
        log("Trying to retrieve links from the clipboard")
        clipboard = gtk.clipboard_get()
        text = clipboard.wait_for_text()
        urls_source = re.findall('megaupload\.com/\?d=[\w]{8}', text)
        for url in urls_source:
            url = "http://" + url + "\n"
            if not url in urls:
                urls.append(url)
        print
        if urls:
            log("I found %s links. Saving to megauploadtmp.txt" % len(urls))
            l_file = "megauploadtmp.txt"
            list_f = file(l_file, "w")
            list_f.write("".join(urls))
            list_f.close()
            from_file = True
            print urls
            print
        else:
            log("I found 0 links.")
            exit()
else:
    print "%s is not a valid argument." % sys.argv[1]
    print "Please use a URL or URLs from Megaupload, a file with a list of Megaupload URLs, or the address of a site with Megaupload URLs."
    print "If this is a URL, please ensure that it starts with http://"
    print "If this is a file, please ensure that it exists"
    exit()


# Log in to get the cookie
cred = urllib.urlencode({"username": user, "password": password, "login": 1})
req = urllib2.urlopen("http://megaupload.com/?c=login", cred)
cookie = req.headers.get("set-cookie", "")

if cookie:
    (cookie,_) = cookie.split(";",1)
    log("Logged in as %s" % user)
    print
    if not os.path.exists(path):
        config = "[Login]\n"
        config += "user = %s\n" % user
        config += "password = %s\n" % password
        config_file = open(path, "w")
        config_file.write(config)
        config_file.close()
else:
    log("Invalid user name or password")
    exit()


# Download the files
current = urls
errors = ""
for url in urls:
    try:
        if url[:-1] == "\n":
            url = url[:-1]

        parts = url.split("/")
        if len(parts) > 4:
            url = "http://megaupload.com/" + parts[-1]

        print url

        req = urllib2.Request(url)
        req.add_header("Cookie", cookie)
        req.add_header("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.8 (Ubuntu-gutsy)")
        gr = redirectManager()
        opener = urllib2.build_opener(gr)
        source = opener.open(req)

        if gr.redirect and url != gr.redirect:
            # If this is a redirect, the user selected Direct Downloads at Megaupload's preferences
            # so we can download the file already
            real_url = gr.redirect
        else:
            # If this is not a redirect, we have to look for the direct download link
            source = source.read()
            prefixes = re.findall("<a href=\"http://www(\d*)..?megaupload.com/files/.*</a>", source)
            if prefixes:
                prefix = prefixes[0]
            else:
                log("This file cannot be downloaded")
                errors = errors + current[0]
                current = current[1:]
                print "\n\n"
                continue

            regEx = "<a href=\"http://www"+prefix+".megaup\.?(.*?)\""
            real_url = re.findall(regEx, source)[0]

            real_url = "www" + prefix + ".megaup" + real_url            
            real_url = urllib.quote(real_url)
                        
        process = subprocess.Popen(["wget", "-c", real_url])
        process.wait()

        if from_file:
            current = current[1:]
            list_f = file(l_file, "w")
            list_f.writelines([errors] + current)
            list_f.close()
            log("List updated")

        print "\n\n"
    except KeyboardInterrupt:
        print "\n\nBye"
        exit()
    except ValueError:
        log("This doesn't look like an url. We won't download it\n\n")
        if from_file:
            errors = errors + current[0]
            current = current[1:]
    except urllib2.URLError, e:
        log("URLError, we can't download this\n\n")
        print e
        if from_file:
            errors = errors + current[0]
            current = current[1:]
    except:
        log("Unexpected error.")
        raise

if errors:
    log("The following files could not be downloaded")
    print errors
else:
    log("All files were downloaded")

log("Finished")