refactor screenshots to work for all pages

This commit is contained in:
Kyle Drake 2014-04-20 19:04:41 -07:00
parent 72c971ae98
commit b488cf4347
7 changed files with 70 additions and 70 deletions

View file

@ -1,34 +1,40 @@
require 'selenium-webdriver'
require 'RMagick'
class ScreenshotWorker
REQUIRED_RESOLUTIONS = ['235x141', '105x63', '270x162']
SCREENSHOTS_PATH = File.join DIR_ROOT, 'public', 'site_screenshots'
include Sidekiq::Worker
sidekiq_options queue: :screenshots, retry: 3, backtrace: true
def perform(username)
def perform(username, filename)
screenshot = Tempfile.new 'neocities_screenshot'
screenshot.close
screenshot_output_path = screenshot.path+'.png'
caps = Selenium::WebDriver::Remote::Capabilities.htmlunit javascript_enabled: true, takesScreenshot: true
f = Screencap::Fetcher.new("http://#{username}.neocities.org/#{filename}")
f.fetch(
output: screenshot_output_path,
width: 1280,
height: 720
)
driver = Selenium::WebDriver.for :remote, url: $config['phantomjs_url'][rand($config['phantomjs_url'].length)], desired_capabilities: caps
driver.manage.window.resize_to 1280, 720
wait = Selenium::WebDriver::Wait.new(timeout: 10) # seconds
wait.until {
driver.navigate.to "http://#{username}.neocities.org"
driver.save_screenshot screenshot.path
}
driver.quit
img_list = Magick::ImageList.new
img_list.read screenshot.path
img_list.from_blob File.read(screenshot_output_path)
screenshot.unlink
File.unlink screenshot_output_path
img_list.new_image(img_list.first.columns, img_list.first.rows) { self.background_color = "white" }
img = img_list.reverse.flatten_images
img.crop!(0, 0, 1280, 720)
img.resize! 208, 125
img.write File.join(DIR_ROOT, 'public', 'site_screenshots', "#{username}.jpg")
user_screenshots_path = File.join SCREENSHOTS_PATH, username
FileUtils.mkdir_p user_screenshots_path
REQUIRED_RESOLUTIONS.each do |res|
img.scale(*res.split('x').collect {|r| r.to_i}).write(File.join(user_screenshots_path, "#{filename}.#{res}.jpg")) {
self.quality = 90
}
end
end
end
end