Advertisement
Narzew

Link Tool

Dec 23rd, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.46 KB | None | 0 0
  1. require 'open-uri'
  2.  
  3. module LinkTool
  4.  
  5.     def self.analyze_links_t(site,sitename,file)
  6.         sitedata = lambda{open(site){|f| return f.read }}.call
  7.         links = []
  8.         x = sitedata.split("href=")
  9.         size = x.size
  10.         size.times{|y|
  11.             z = x[y].split(">")[0]
  12.             next if (z[0].to_s)[0] != "\""
  13.             r = z.to_s.gsub("http://","start").gsub("\"","")
  14.             if r =~ /\Astart/
  15.                 link = "#{z}".gsub("\"","")
  16.             else
  17.                 link = "#{sitename}/#{z}".gsub("\"","")
  18.             end
  19.             links << link unless links.include?(link)
  20.         }
  21.         File.open(file,'wb'){|f| links.each{|x| f.write(x+"\n") }}
  22.         return links
  23.     end
  24.  
  25.     def self.download_links_t(file,sitename="")
  26.         links = lambda{File.open(file,'rb'){|f |return f.read}}.call
  27.         downcount = [0,0]
  28.         links.each_line{|x|
  29.             downcount[1] += 1
  30.             ok = 1
  31.             name = x.gsub(sitename,"").gsub("http://","").gsub("/", "\\")
  32.             x = x.gsub("\n","")
  33.             name = name.gsub("\n","")
  34.             if name[0] == "\\" || name[0] == "/"
  35.                 name[0] = ""
  36.             end
  37.             lambda{ open(x){|s| File.open(name, "wb"){|f| f.write(s.read)}}}.call rescue ok = 0
  38.             if ok == 0
  39.                 print "[#{downcount[0]}/#{downcount[1]}] #{x} failed.\n"
  40.             else
  41.                 downcount[0] += 1
  42.                 print "[#{downcount[0]}/#{downcount[1]}] #{name} (#{x}) downloaded.\n"
  43.    
  44.             end
  45.         }
  46.         print "[#{downcount[0]}/#{downcount[1]}] files downloaded.\n"
  47.     end
  48.  
  49.     def self.analyze_and_download_t(site,sitename)
  50.         sitedata = lambda{open(site){|f| return f.read }}.call
  51.         links = []
  52.         x = sitedata.split("href=")
  53.         size = x.size
  54.         size.times{|y|
  55.             z = x[y].split(">")[0]
  56.             next if (z[0].to_s)[0] != "\""
  57.             r = z.to_s.gsub("http://","start").gsub("\"","")
  58.             if r =~ /\Astart/
  59.                 link = "#{z}".gsub("\"","")
  60.             else
  61.                 link = "#{sitename}/#{z}".gsub("\"","")
  62.             end
  63.             links << link unless links.include?(link)
  64.         }
  65.         downcount = [0,0]
  66.         links.each{|x|
  67.             downcount[1] += 1
  68.             ok = 1
  69.             name = x.gsub(sitename,"").gsub("http://","").gsub("/", "\\")
  70.             x = x.gsub("\n","")
  71.             name = name.gsub("\n","")
  72.             if name[0] == "\\" || name[0] == "/"
  73.                 name[0] = ""
  74.             end
  75.             lambda{ open(x){|s| File.open(name, "wb"){|f| f.write(s.read)}}}.call rescue ok = 0
  76.             if ok == 0
  77.                 print "[#{downcount[0]}/#{downcount[1]}] #{x} failed.\n"
  78.             else
  79.                 downcount[0] += 1
  80.                 print "[#{downcount[0]}/#{downcount[1]}] #{name} (#{x}) downloaded.\n"
  81.    
  82.             end
  83.         }
  84.         print "[#{downcount[0]}/#{downcount[1]}] files downloaded.\n"
  85.     end
  86.  
  87.     def self.analyze_links
  88.         print "Enter the site you want download links from (for ex. http://mysite.com/download.html): "
  89.         site = $stdin.gets.chomp!
  90.         print "Enter the site base (for ex. http://mysite.com): "
  91.         sitename = $stdin.gets.chomp!
  92.         print "Enter file name: "
  93.         file = $stdin.gets.chomp!
  94.         LinkTool.analyze_links_t(site,sitename,file)
  95.     end
  96.  
  97.     def self.download_links
  98.         print "Enter the file name: "
  99.         file = $stdin.gets.chomp!
  100.         print "Enter the site for names or leave blank: "
  101.         site = $stdin.gets.chomp!
  102.         LinkTool.download_links_t(file,site)
  103.     end
  104.  
  105.     def self.analyze_and_download
  106.         print "Enter the site you want download links from (for ex. http://mysite.com/download.html): "
  107.         site = $stdin.gets.chomp!
  108.         print "Enter the site base (for ex. http://mysite.com): "
  109.         sitename = $stdin.gets.chomp!
  110.         LinkTool.analyze_and_download_t(site,sitename)
  111.     end
  112.  
  113.     def self.start
  114.         LinkTool.print_header
  115.         if ARGV[0] == nil
  116.             LinkTool.start_interactive
  117.         else
  118.             LinkTool.start_terminal
  119.         end
  120.     end
  121.  
  122.     def self.print_header
  123.         print "**LinkTool Web Link Analyzer and Downloader\n**by Narzew\n**16.11.2013\n**v 1.1\n**All rights reserved.\n\n"
  124.     end
  125.  
  126.     def self.start_interactive
  127.         print "0 - analyze links on the site and save to a file\n"
  128.         print "1 - download links contained in a file\n"
  129.         print "2 - analyze and download links on the site\n"
  130.         mode = $stdin.gets.chomp!.to_i
  131.         case mode
  132.         when 0 then LinkTool.analyze_links
  133.         when 1 then LinkTool.download_links
  134.         when 2 then LinkTool.analyze_and_download
  135.         else
  136.             print "Enter the correct choice.\n"
  137.             exit
  138.         end
  139.     end
  140.  
  141.     def self.start_terminal
  142.         if ARGV[0].to_s == "0" || ARGV[0].to_s == "a"
  143.             LinkTool.analyze_links_t(ARGV[1],ARGV[2],ARGV[3])
  144.         elsif ARGV[0].to_s == "1" || ARGV[0].to_s == "d"
  145.             if ARGV.size == 2
  146.                 LinkTool.download_links_t(ARGV[1])
  147.             else
  148.                 LinkTool.download_links_t(ARGV[1],ARGV[2])
  149.             end
  150.         elsif ARGV[0].to_s == "2" || ARGV[0].to_s == "ad"
  151.             LinkTool.analyze_and_download_t(ARGV[1],ARGV[2])
  152.         end
  153.     end
  154.        
  155. end
  156.  
  157. begin
  158.     LinkTool.start
  159. rescue => e
  160.     print "Error: #{e}\n"
  161.     $stdin.gets
  162.     exit
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement