Last active
January 3, 2016 12:59
-
-
Save RoxasShadow/8466220 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'net/ftp' | |
| require 'fileutils' | |
| require 'clipboard' | |
| require 'bitly' | |
| require 'rubyfish' | |
| class String | |
| def fix | |
| gsub(/\\/, ?/).force_encoding('iso-8859-1').encode('utf-8') | |
| end | |
| def last_file | |
| files = Dir["#{self}/**/*"].sort { |a, b| File.ctime(a) <=> File.ctime(b) } | |
| files.select { |f| f =~ /.\.(png|bmp|jpeg|jpg|gif|tiff)$/i }.last | |
| end | |
| def minify | |
| Bitly.use_api_version_3 | |
| Bitly.new('o_4iijurejqj', 'R_1aceafb936a5c248937a73dc8d522007').shorten(self).short_url | |
| end | |
| def to_screenshot | |
| return self unless self.start_with? 'Cattura di schermata' | |
| name = File.basename self | |
| File.join File.dirname(self), "Screenshot_#{name.scan(/\d/).join.to_i}.#{name.split(?.).last}" | |
| end | |
| def ensure(ary) | |
| ary.min { |a, b| a.difference(self) <=> b.difference(self) } | |
| end | |
| def difference(str) | |
| RubyFish::Hamming.distance self, str | |
| end | |
| end | |
| module Net | |
| class FTP | |
| def makepasv | |
| parse229 sendcmd('EPSV') | |
| end | |
| end | |
| end | |
| config = { | |
| :host => 'HOST', | |
| :user => 'USER', | |
| :pass => 'PASSWORD', | |
| :basedir => '/images', | |
| :dir => '/giovannicapuano/htdocs/screenshotto/public/images', | |
| :local => 'D:\Users\Giovanni\Pictures\Screenshots', | |
| :url => 'http://screenshotto.giovannicapuano.net', | |
| :default => 'Random' | |
| } | |
| folders = Net::FTP.open(config[:host], config[:user], config[:pass]) { |ftp| | |
| ftp.chdir config[:dir] | |
| ftp.nlst | |
| } | |
| if ARGV.any? && ARGV.first == '-h' | |
| abort "<screen.rb> [input] [folder]\nIf empty, it takes the last screenshot and 'Random'." if ARGV.any? && ARGV.first == '-h' | |
| elsif ARGV.any? && ARGV.first == '-l' | |
| abort folders.join ', ' | |
| end | |
| file = ARGV.any? ? ARGV.shift.fix : config[:local].fix.last_file | |
| folder = ARGV.pop | |
| folder = folder ? folder.ensure(folders) : config[:default] | |
| abort 'No valid folder found.' unless folder | |
| (ARGV.map { |f| f.fix } << file).each { |f| | |
| path = "#{folder}/#{File.basename f}".to_screenshot | |
| Net::FTP.open(config[:host], config[:user], config[:pass]) { |ftp| | |
| puts "Uploading in #{folder}..." | |
| ftp.passive = true | |
| ftp.putbinaryfile File.new(f), "#{config[:dir]}/#{path}" | |
| ftp.sendcmd "SITE CHMOD 0644 #{config[:dir]}/#{path}" | |
| url = "#{config[:url]}/#{config[:basedir]}/#{path}" | |
| miniurl = url.minify | |
| Clipboard.copy miniurl | |
| puts "URL: #{url }" | |
| puts "Minified URL: #{miniurl}" | |
| puts | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment