#1866: Tumblr thumbnail rewriting
This commit is contained in:
@@ -2,12 +2,26 @@ module Downloads
|
||||
module Strategies
|
||||
class Base
|
||||
def self.strategies
|
||||
[Pixiv, Twitpic, DeviantArt]
|
||||
[Pixiv, Twitpic, DeviantArt, Tumblr]
|
||||
end
|
||||
|
||||
def rewrite(url, headers)
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
protected
|
||||
def http_exists?(url, headers)
|
||||
exists = false
|
||||
uri = URI.parse(url)
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
http.request_head(uri.request_uri, headers) do |res|
|
||||
if res.is_a?(Net::HTTPSuccess)
|
||||
exists = true
|
||||
end
|
||||
end
|
||||
end
|
||||
exists
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -47,28 +47,13 @@ module Downloads
|
||||
match = $1
|
||||
repl = match.sub(/_p/, "_big_p")
|
||||
big_url = url.sub(match, repl)
|
||||
if http_exists?(big_url)
|
||||
if http_exists?(big_url, headers)
|
||||
url = big_url
|
||||
end
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
def http_exists?(url)
|
||||
# example: http://img01.pixiv.net/img/as-special/15649262_big_p2.jpg
|
||||
exists = false
|
||||
uri = URI.parse(url)
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
headers = {"Referer" => "http://www.pixiv.net", "User-Agent" => "#{Danbooru.config.app_name}/#{Danbooru.config.version}"}
|
||||
http.request_head(uri.request_uri, headers) do |res|
|
||||
if res.is_a?(Net::HTTPSuccess)
|
||||
exists = true
|
||||
end
|
||||
end
|
||||
end
|
||||
exists
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
27
app/logical/downloads/strategies/tumblr.rb
Normal file
27
app/logical/downloads/strategies/tumblr.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Downloads
|
||||
module Strategies
|
||||
class Tumblr < Base
|
||||
def rewrite(url, headers)
|
||||
if url =~ %r{^http?://(?:(?:\d+\.)\w+\.)?tumblr\.com}
|
||||
url, headers = rewrite_thumbnails(url, headers)
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
protected
|
||||
def rewrite_thumbnails(url, headers)
|
||||
if url =~ %r{^http?://.+\.tumblr\.com/(?:\w+/)?(?:tumblr_)?(\w+_)(250|400|500)\..+$}
|
||||
match = $1
|
||||
size = $2
|
||||
big_url = url.sub(match + size, match + "1280")
|
||||
if http_exists?(big_url, headers)
|
||||
url = big_url
|
||||
end
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
7782
test/fixtures/vcr_cassettes/download-tumblr-sample.yml
vendored
Normal file
7782
test/fixtures/vcr_cassettes/download-tumblr-sample.yml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
43
test/unit/downloads/tumblr_test.rb
Normal file
43
test/unit/downloads/tumblr_test.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
require 'test_helper'
|
||||
|
||||
module Downloads
|
||||
class TumblrTest < ActiveSupport::TestCase
|
||||
context "a download for a tumblr 500 sample" do
|
||||
setup do
|
||||
@source = "http://24.media.tumblr.com/fc328250915434e66e8e6a92773f79d0/tumblr_mf4nshfibc1s0oswoo1_500.jpg"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Downloads::File.new(@source, @tempfile.path)
|
||||
VCR.use_cassette("download-tumblr-sample", :record => :new_episodes) do
|
||||
@download.download!
|
||||
end
|
||||
end
|
||||
|
||||
should "instead change the source to the 1280 version" do
|
||||
assert_equal("http://24.media.tumblr.com/fc328250915434e66e8e6a92773f79d0/tumblr_mf4nshfibc1s0oswoo1_1280.jpg", @download.source)
|
||||
end
|
||||
|
||||
should "instead download the 1280 version" do
|
||||
assert_equal(196_617, ::File.size(@tempfile.path))
|
||||
end
|
||||
end
|
||||
|
||||
context "a download for a tumblr 500 image without a larger size" do
|
||||
setup do
|
||||
@source = "http://25.media.tumblr.com/tumblr_lxbzel2H5y1r9yjhso1_500.jpg"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Downloads::File.new(@source, @tempfile.path)
|
||||
VCR.use_cassette("download-tumblr-sample", :record => :new_episodes) do
|
||||
@download.download!
|
||||
end
|
||||
end
|
||||
|
||||
should "not change the source" do
|
||||
assert_equal("http://25.media.tumblr.com/tumblr_lxbzel2H5y1r9yjhso1_500.jpg", @download.source)
|
||||
end
|
||||
|
||||
should "download the 500 version" do
|
||||
assert_equal(90_122, ::File.size(@tempfile.path))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user