Last active
January 26, 2018 14:19
-
-
Save strviola/aaaa35552137f4ee2155aa2da1e05d68 to your computer and use it in GitHub Desktop.
ニコニコ動画サムネイルダウンローダ
This file contains 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
# thanks to http://swiftfox.blog6.fc2.com/blog-entry-301.html | |
require 'net/http' | |
require 'uri' | |
require 'nokogiri' | |
require 'open-uri' | |
def get_parser(url) | |
uri = URI.parse(url) | |
html = Net::HTTP.get(uri) | |
Nokogiri::HTML.parse(html) | |
end | |
parser = get_parser('http://swiftfox.blog6.fc2.com/blog-entry-301.html') | |
thumb_urls = parser | |
.xpath('//iframe') | |
.map { |iframe| iframe.attribute('src').value } | |
.select { |iframe_url| iframe_url.match %r(http://ext.nicovideo.jp/thumb/sm\d+) } | |
thumb_urls.each do |thumb_url| | |
thumb_parser = get_parser(thumb_url) | |
thumb_img_src = thumb_parser.xpath('//img[@class="video_img"]').attribute('src').value | |
$stderr.puts "Downloading #{thumb_img_src}..." | |
open(thumb_img_src) do |image| | |
file_id = thumb_img_src.scan(/\d+$/)[0] | |
File.open("images/smile_#{file_id}.jpg", 'wb') do |file| | |
file.puts(image.read) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment