Posts Tagged ruby

[Ruby] Wyświetlanie definicji ze “Słownika Wyrazów Obcych Kopalinskiego”

To jest kod skryptu w Ruby do wyświetlania definicji ze “Słownika Wyrazów Obcych Kopalińskiego”. Pobierz plik kopalinski.rb.

Żeby użyć tego skryptu musisz mieć zainstalowanego Ruby-ego.

W terminalu wpisz:

kopalinski.rb Słownik

Skrypt wyświetli definicje słowa “Słownik”. Jeśli w słowniku Kopalińskiego nie ma jakiegoś słowa skrypt nie wyświetla nic.

Advertisements

,

3 Comments

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

Ś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

File Hosting Downloader Project

I’ve create my first github project Downloader. It’s a Ruby script for download files from file hosting services. You can download files from 4shared, rapidshare, przeklej, filesonic and wrzuta. You can download it from github or from here.

You can use it:


download.rb <url>
or
download.rb -f <file with urls>

If you download files from rapidshare and you have Orange livebox, you can provide livebox possword for reconnection (change dynamic ADSL IP)


download.rb -l <livebox password> -f <filename>

If you download files from przeklej you can provide you user name and password if you want to download file larger than 50MB


download.rb -u <user> -p <password> -f <filename>

, , , , , ,

Leave a comment

Md5 bruteforce in Ruby

Here is the code for bruteforce md5 hash. It check only small letters.

require 'digest/md5'
def crack(hash, max_length)
  for i in 1..max_length
    for word in ('a'*i..'z'*i)
      if Digest::MD5.new(word) == hash
        return word
      end
    end
  end
end
puts crack(ARGV[0], 5)

, ,

Leave a comment

%d bloggers like this: