Friday, January 13, 2012

A simple command-line Megaupload download manager

Have a bunch of megaupload links and ever felt the need for a simple, lightweight, text-based/command-line and eminently extensible script which does all the downloading for you?

Your search ends here!

Requirements

  1. wget: which is present by default in almost linux distros
  2. ruby and nokogiri: These can be installed as shown here.
Steps
  1. Save the script as megaupload_dm.rb
  2. Make it executable using chmod +x megaupload_dm.rb
  3. Install the dependencies
  4. Dump all your megaupload links to a file named megaupload_list.txt in the current directory. You can also insert any content or comments in this file
  5. Tweak the LinkFile and OutputDirectory variables to your liking.
  6. Limit the download rate by adding --limit-rate=100k to the wget command at line #43 (optional)
  7. Run it using ./megaupload_dm.rb
  8. Your files are downloaded to ~/Downloads directory!


#!/usr/bin/env ruby


# Is the url a valid one?
def valid?(url)
  url =~ %r|^http://www.megaupload.com/\?d=[a-zA-Z0-9]+$|
end

require 'rubygems'
require 'nokogiri'
require 'open-uri'

LinkFile = "./megaupload_list.txt"
OutputDirectory = "/home/vikas/Downloads"
TempFile = "/tmp/megaupload-tmp.html"


File.open(LinkFile, 'r') do |f|
  f.each_line do |link|

    link.strip!

    next unless valid?(link)

    begin

      puts "Fetching information for #{link} ..."
      `wget "#{link}" -U "Firefox/4.0.0" --quiet --output-document #{TempFile}`

      doc = Nokogiri::HTML(File.read(TempFile))

      # Filename and Direct Link
      fname = doc.css("div.downl_main div.download_file_name").first.content
      direct_link = doc.css("div.downl_main div.download_member_bl a").last.attributes['href'].value

      # Actual download
      puts "Downloading #{fname} ... \n"
      `wget -c -P "#{OutputDirectory}" -U "Firefox/3.6.3" "#{direct_link}"`
      puts "*********************************** DONE *******************************************"
      puts "\n\n"

      # Sleep 5 secs. Take some load off megaupload ;)
      sleep 5

    rescue
      puts "ERROR! FileNotFound or otherwise; Skipping this file...."
      puts "\n\n"
      next
    end

  end
end

No comments: