#! /usr/bin/ruby -w #================================================================ # wgettsv # Collect WWW resources and generate TSV data #================================================================ require 'open-uri' require 'iconv' require 'kconv' require 'date' require 'time' require 'cgi' def main seeds = [] hist = {} filters = [] max = 1 << 30 lim = 1 << 20 wait = 0 ndf = false i = 0 while i < ARGV.length if seeds.length < 1 && ARGV[i] =~ /^-/ if ARGV[i] == '-allow' usage if (i += 1) >= ARGV.length regex = Regexp::new(ARGV[i]) filters.push([true, regex]) if regex elsif ARGV[i] == '-deny' usage if (i += 1) >= ARGV.length regex = Regexp::new(ARGV[i]) filters.push([false, regex]) if regex elsif ARGV[i] == '-max' usage if (i += 1) >= ARGV.length max = ARGV[i].to_i elsif ARGV[i] == '-lim' usage if (i += 1) >= ARGV.length lim = ARGV[i].to_i elsif ARGV[i] == '-wait' usage if (i += 1) >= ARGV.length wait = ARGV[i].to_f elsif ARGV[i] == '-ndf' ndf = true else usage end else if ARGV[i] =~ /^http:\/\//i seeds.push(ARGV[i]) hist[ARGV[i]] = true else usage end end i += 1 end usage if seeds.length < 1 if !ndf filters.push([false, /\.(txt|text|asc|c|cc|cxx|cpp|h|hxx|hpp|in)$/i]) filters.push([false, /\.(css|js|csv|tsv|log|md5|crc|conf|ini|inf|lnk|sys|tmp|bak)$/i]) filters.push([false, /\.(xml|xsl|xslt|rdf|rss|dtd|sgml|sgm)$/i]) filters.push([false, /\.(pgp|sig|cer|csr|pem|key|b64|uu|uue|[0-9])$/i]) filters.push([false, /\.(rtf|pdf|ps|eps|ai|doc|xls|ppt|sxw|sxc|sxi|xdw|jtd|oas|swf)$/i]) filters.push([false, /\.(zip|tar|tgz|gz|bz2|tbz2|z|lha|lzh)?$/i]) filters.push([false, /\.(7z|lzo|lzma|cpio|shar|cab|rar|sit|ace|hqx)?$/i]) filters.push([false, /\.(bin|o|a|so|exe|dll|lib|obj|ocx|class|jar|war)?$/i]) filters.push([false, /\.(rpm|deb|qdb|qdb|dbx|dbf|dat|msi|bat|com|iso)?$/i]) filters.push([false, /\.(png|gif|jpg|jpeg|tif|tiff|bmp|ico|pbm|pgm|ppm|xbm|xpm|dvi)$/i]) filters.push([false, /\.(au|snd|mid|midi|kar|smf|mp2|mp3|m3u|wav|wma|wmp|asx|at3|aif)$/i]) filters.push([false, /\.(mpg|mpeg|qt|mov|avi|wmv|wvx|asf|ram|rm)$/i]) filters.push([false, /\.(tch|tdb|tdf|tct)$/i]) filters.push([false, /\.idx\./i]) filters.push([false, /(core|casket|Makefile|README|NEWS|COPYING|LISENCE)($|\/)/i]) end return proc(seeds, hist, filters, max, lim, wait) ? 0 : 1 end def usage STDERR.printf("%s: collect WWW resources and generate TSV data\n", $progname) STDERR.printf("\n") STDERR.printf("usage:\n") STDERR.printf(" %s [-allow regex] [-deny regex] [-max num] [-lim num] [-wait num]" + " url ...\n", $progname) STDERR.printf("\n") exit(1) end def proc(seeds, hist, filters, max, lim, wait) cnt = 0 while (url = seeds.shift) && cnt < max STDERR.printf("%d: getting: %s\n", cnt + 1, url) begin opts = {} OpenURI.open_uri(url, 0, 0, opts) do |sio| baseuri = sio.base_uri if baseuri && baseuri.to_s != url url = baseuri.to_s hist[url] = true end size = sio.size raise "invalid size" if size > lim || size < 3 type = sio.content_type type = "text/plain" if !type str = sio.read head = str[0,2048] if (head[0] == 0xfe && head[1] == 0xff) || (head[0] == 0xff && head[1] == 0xfe) str = Kconv::kconv(str, Kconv::UTF8, Kconv::UTF16) charset = "UTF-8" elsif str.include?(0) raise "binary data" end raise "not HTML" if type != "text/html" && head !~ / 0 if charset !~ /^UTF-?8$/i begin nstr = Iconv.conv("UTF-8", charset, str) str = nstr if nstr && nstr.length > 0 rescue str = str.toutf8 end end else str = str.toutf8 end body = str.gsub(/.*
]*>/im, "") body = body.gsub(/<\/body>.*/im, "") body = htmltotext(body) if str =~ /