Script in Ruby for validating rapidshare links

This is script which I wrote for testing if file exist on rapidshare hosting service, it use Rapidshare API

require 'net/http'
require 'uri'
require 'optparse'

class FalseException < Exception
end


def check(file, quiet=nil)
  file.each_line {|url|
    begin
      url = url.stript
      #skip blank lines, comments and invalid urls
      if url =~ /^\s*$/ or url =~ /^\s*#/ or
         not url =~ /^http:\/\// or 
         not url =~ /files\/([0-9]*)\/(.*)/
        next
      else
        fileid = $1
        filename = $2
        query = "?sub=download_v1&fileid=#{fileid}&filename=#{filename}"
        apiurl = URI.parse('http://api.rapidshare.com/cgi-bin/rsapi.cgi')
        page = Net::HTTP.new(apiurl.host).get(apiurl.path + query).body
        if page =~ /File deleted/ or 
           page =~ /This file is marked as illegal/ or
           page =~ /Filename invalid./ or
           page =~ /File not found/
          if quiet
            raise FalseException
          end
          if RUBY_PLATFORM =~ /(:?mswin|mingw)/i
            puts "#{url} [FAIL]"
          else
            #display red FAIL on n*ix system
            system("echo \"#{url} [\x1b[31m\x1b[1m\"FAIL\"\x1b[0m]\"")
          end
        else
          if quiet == nil
            if RUBY_PLATFORM =~ /(:?mswin|mingw)/i
              puts "#{url} [OK]"
            else
              #display green OK on *nix system
              system("echo \"#{url} [\x1b[32m\x1b[1m\"OK\"\x1b[0m]\"")
            end
          end
        end
      end
    rescue Timeout::Error, Errno::ETIMEDOUT
      puts "Timeout Error, try again"
      redo
    end
  }
end

def usage()
  puts "rapidtest.rb [-f (filename | - )] [-h]"
end

opts = ARGV.getopts('f:hq')

if opts['h']
  usage
  exit(0)
end

filename = opts['f']
if filename
  begin
    if filename == '-'
      check(ARGF)
    else
      File.open(filename) {|file|
        check(file, opts['q'])
      }
    end
  rescue Interrupt, Errno::EINTR
    exit(1)
  rescue FalseException
    exit(1)
  end
end
exit(0)

It will display url and [FAIL] or [OK] (on unix like system it display in color), or return true or false when used with -q option.

You can use it with -f option which must be filename with urls or – to read from stdin

If you using linux you can use xclip program to get data from clipboard.

If you have some urls in clipboard you can use:

xclip -o -sel clip | rapidtest.rb -f -

You can download it from here.

you can test if all files are valid with:

xclip -o -sel clip | rapidtest -f- && echo 'all fine' || echo 'some are invalid'

You can put this script in panel and display GUI message dialogs with zenity

function ok() {
  zenity --info --title "rapidtest" --text "all links are valid"
}
function error() {
  zenity --error --title "rapidtest" --text "some links are invalid"
}
xclip -o -sel clip | rapidtest.rb -q -f- && ok || eror
About these ads

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.