Skip to content

Instantly share code, notes, and snippets.

@morika-t
Forked from pi-chan/slideshare.rb
Created February 10, 2014 22:00
Show Gist options
  • Save morika-t/8925108 to your computer and use it in GitHub Desktop.
Save morika-t/8925108 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'prawn'
url = "http://www.slideshare.net/nippondanji/db-engineerstudyanim"
charset = nil
html = open(url) do |f|
charset = f.charset
f.read
end
doc = Nokogiri::HTML.parse(html,nil,charset)
item = doc.xpath('/html/head/meta[@name="thumbnail"]').first
thumbnail_url = item.attributes["content"].value
xml_url = thumbnail_url.gsub(/phpapp(\d+)?(.+)$/,'phpapp\1').gsub(/ss_thumbnails\//,'') + ".xml"
xml = open(xml_url) do |f|
f.read
end
xml_doc = Nokogiri::XML(xml)
slide_count = xml_doc.xpath("/Show/Slide").count
slide_name = tthumbnail_url.match(/ss_thumbnails\/(.+phpapp\d+)/)[1]
pdf_name = "#{slide_name}.pdf"
pdf_file = Tempfile.new(pdf_name)
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
slide_count.times do |i|
path = "http://image.slidesharecdn.com/#{slide_name}/95/slide-#{i+1}-1024.jpg"
puts "downloading #{path} ..."
filename = File.basename(path)
open(filename, 'wb') do |out|
open(path) do |data|
out.write(data.read)
end
end
end
images = Dir.glob("slide-*.jpg")
images = images.sort_by{|str| str[/\d+/].to_i}
puts "generating #{pdf_file.path} ..."
Prawn::Document.generate(
pdf_file.path,
page_size:[1024,768],
margin:0
) do
images.each do |img|
image(
img,
fit:[1024,768]
)
start_new_page
end
end
end
end
puts pdf_file.path
new_name = pdf_file.path.gsub(/\.pdf(.+)$/,'.pdf')
File.rename(pdf_file.path, new_name)
FileUtils.copy(new_name, "./")
pdf_file.unlink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment