Script for reconnect adsl in d-link router

In work I have wireless network with D-Link router “DSL-2640B”, and I need to reconnect ADSL service to change IP adress, so I write this ruby script, which use telnet standard libary.

#!/usr/bin/ruby
require 'net/telnet'
require 'optparse'

opts = ARGV.getopts('h:p:')

passwd = opts['-p']
host = opts['-h']

server = Net::Telnet::new('Host'=>host,
                          'Port'=> 23,
                          'Timeout'=>25,
                          'Prompt'=> />/)
server.waitfor(/ogin:/)
server.print("admin\n")
server.waitfor(/ssword:/)
server.print("#{passwd}\n")
server.cmd("adsl connection --down")
server.cmd("adsl connection --up")
server.cmd("logout")

You can use it in terminal by typing:

reconnect.rb -p <password> -h <host>

You must wait couple of seconds before your d-link router connect to ADSL.

, ,

Leave a comment

Script for searching files on rapidshare

This is a script in Ruby, that I wrote, which use filestube.com to search files on rapidshare (It don’t use filestube API — it just fetch and parse html).

#!/usr/bin/ruby

require 'net/http'
require 'uri'
require 'optparse'
require 'rexml/document'

def get(url, data=nil)
  url = URI.parse(url)
  http = Net::HTTP.new(url.host)
  if data
    data = data.map{|k,v| "#{k}=#{url_escape(v)}"}.reduce {|a,b|
        "#{a}&#{b}"
    }
    path = url.path + '?' + data
  else
    path = url.path
  end
  res = http.get(path, {'Cookie' => 'usr_timeoffset=0'})
  res.body
end

def url_escape(o)
  if o.class == ''.class
    o.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
      '%' + $1.unpack('H2' * $1.size).join('%').upcase
    }.tr(' ', '+')
  else
    return o
  end 
end

def filestube_urls(query, page=nil, sort=nil, size=nil)
  url = "http://www.filestube.com/search.html"
  data = {
    'q' => query,
    'select' => 'All',
    'hosting' => 1
  }
  if size
    data['size'] = size
  end
  if sort
    data['sort'] = sort
  end
  regex = /<a href="([^"]*)" class="resultsLink">.*?<\/a>/
  if page
    data['page'] = page
  end
  get(url, data).scan(regex).map {|i| i[0]}
end

def extract_urls(page)
  if page =~ /<pre id="copy_paste_links"[^>]*>(.*)<\/pre>/m
     $1.strip.split("\n").each{|url|
       yield url
     }
  end
end

def rapidshare_urls(query, num=nil, sort=nil, size=nil)
  if num
    if num / 10 > 1
      result = []
      (2..num/10+1).each {|page|
         filestube_urls(query, page, sort, size).each {|url|
            extract_urls(get(url)) {|url|
              yield url
            }
         }
      }
    else
      filestube_urls(query, nil, sort, size)[0..num-1].each {|url|
        extract_urls(get(url)) {|url|
          yield url
        }
      }
    end
  else
    filestube_urls(query, nil, sort, size).each {|page|
      extract_urls(get(page)) {|url|
       yield url
      }
    }
  end
end


def usage()
  puts "usage:"
  puts "rapidsearch.rb [-n number] [-o order] [-s size] [-h]"
  puts 
  puts "-n number of url packages"
  puts "-h this help screen"
  puts "-s size:"
  puts "      1 - <20MB"
  puts "      2 - 20MB - 200MB"
  puts "      3 - 200MB - 1GB"
  puts "      4 - >1GB"
  puts "-o sort by (default relevance):"
  puts "      pd - popularity"
  puts "      dd - date"
  puts "      sd - size"
end

params = ARGV.getopts('n:o:s:h')
if params['h']
  usage
  exit
elsif params['o'] and !["pd", "dd", "sd"].include?(params['o'])
  puts "Bad parameter -o #{params['o']}"
  usage
  exit
elsif params['s'] and ![1,2,3,4].include?(params['s'].to_i)
  puts "Bad size #{params['s']}"
  usage
end

query = ARGV.join(' ')
begin
  if params['n']
    rapidshare_urls(query, params['n'].to_i, params['o'], params['s']) {|url|
      puts url
      $stdout.flush
    }
  else
    rapidshare_urls(query, nil, params['o'], params['s']) {|url|
      puts url
      $stdout.flush
    }
  end
rescue Interrupt, Errno::EINTR
  exit(0)
end

You can use this like this:

rapidsearch.rb -n 10 Matrix

this will display urls for 10 packages containgin word “Matrix”.

-o option is for size of packages (1 – 4)

-s option is for sorting it must be one of (dd – by data, pd – by popularity, sd – by size)

You can download this script from here.

You can combine this script with for validating rapidshare links.


rapidsearch.rb -n 5 -s 3 Matrix | rapidtest.rb -f -

, ,

2 Comments

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

,

Leave a comment

I just got 666 reputation on stackoverflow.

666 reputation on stackoverflow

Leave a comment

Great T-Shirt Design.

This is the best T-Shirt design ever.

.ninja { color: black; visibility: hidden; }

, ,

1 Comment

Ściąganie plików z wrzuty

Aby ściągnąć pliki z wrzuty w Ruby, należy ze strony danego adresu odczytać zmienne javascript-u które zostaną przekazane do odtwarzacza napisanego we flashu.

require 'net/http'
require 'uri'
require 'rexml/document'

def get(url)
  url = URI.parse(url)
  http = Net::HTTP.new(url.host)
  return http.get(url.path).body
end

page = get(url)
#zczytanie zmiennych javascript
if page =~ /'key' : '([^']*)',/
  key = $1
else
  key = '4KWNcfGaCak'
end
if page =~ /'login' : '([^']*)',/
  login = $1
else
  login = 'lifthrasil'
end
if page =~ /'host' : '([^']*)',/
  host = $1
else
  host = 'labs.wrzuta.pl'
end
if page =~ /'site' : '([^']*)',/
  site = $1
else
  site = 'wrzuta.pl'
end
if page =~ /'lang' : '([^']*)',/
  lang = $1
end

Następnie z podanych zmiennych należy utworzyć adres url

rnd = (rand*1000000).floor
url = "http://#{login}.#{host}/xml/#{_local2}/#{key}/sa/#{site}/#{rnd}"

Pobrać daną stronę (jest to plik XML) i wczytać adres pliku

xml = REXML::Document.new(get(url)).root
url = xml.elements['//file/storeIds/fileId'][0]
#pobranie nazwy pliku
filename = xml.elements['//name'][0]

Następnie można użyć programu wget do ściągnięcia pliku

`wget -O #{filename} "#{url}"`

Możesz także skorzystać z Downloadera mój projekt na github, który ściąga pliki z przekleja, rapidshare, 4shared i wrzuty. Jak o używać możesz przeczytać w poście: File hosting downloader project (po angielsku) oraz skrypty do wszukiwania i walidacji linków z rapidshare.

, , ,

Leave a comment

Abelson & Sussman video lectures mirror download

Structure and Interpretation of Computer ProgramsIf you have trouble find all Abelson & Sussman SICP video lectures from MIT, here are direct downloads from archive.org (mp4 256kb).

http://ia361309.us.archive.org/3/items/halmit1a/Lecture-1a_256kb.mp4
http://ia361301.us.archive.org/22/items/halmit1b/Lecture-1b_256kb.mp4
http://ia361301.us.archive.org/19/items/halmit2a/Lecture-2a_256kb.mp4
http://ia361301.us.archive.org/18/items/halmit2b/Lecture-2b_256kb.mp4
http://ia361301.us.archive.org/20/items/halmit3a/Lecture-3a_256kb.mp4
http://ia361301.us.archive.org/17/items/halmit3b/Lecture-3b_256kb.mp4
http://ia361301.us.archive.org/11/items/halmit4a/Lecture-4a_256kb.mp4
http://ia361300.us.archive.org/3/items/halmit4b/Lecture-4b_256kb.mp4
http://ia361300.us.archive.org/11/items/halmit5a/Lecture-5a_256kb.mp4
http://ia361301.us.archive.org/8/items/halmit5b/Lecture-5b_256kb.mp4
http://ia361300.us.archive.org/4/items/halmit6a/Lecture-6a_256kb.mp4
http://ia361301.us.archive.org/7/items/halmit6b/Lecture-6b_256kb.mp4
http://ia361301.us.archive.org/10/items/halmit7a/Lecture-7a_256kb.mp4
http://ia361300.us.archive.org/12/items/halmit7b/Lecture-7b_256kb.mp4
http://ia361308.us.archive.org/3/items/halmit8a/Lecture-8a_256kb.mp4
http://ia361301.us.archive.org/14/items/halmit8b/Lecture-8b_256kb.mp4
http://ia361300.us.archive.org/4/items/halmit9a/Lecture-9a_256kb.mp4
http://ia361300.us.archive.org/16/items/halmit9b/Lecture-9b_256kb.mp4
http://ia361300.us.archive.org/6/items/halmit10a/Lecture-10a_256kb.mp4
http://ia361300.us.archive.org/15/items/halmit10b/Lecture-10b_256kb.mp4

,

2 Comments

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: