#1866: Support twitpic source rewriting
* html work page -> full image * thumbnail -> full image
This commit is contained in:
@@ -69,7 +69,7 @@ module Downloads
|
||||
if limit == 0 then
|
||||
raise Error.new("Too many redirects")
|
||||
end
|
||||
source = res["location"]
|
||||
@source = res["location"]
|
||||
limit -= 1
|
||||
|
||||
else
|
||||
|
||||
@@ -2,7 +2,7 @@ module Downloads
|
||||
module Strategies
|
||||
class Base
|
||||
def self.strategies
|
||||
[Pixiv]
|
||||
[Pixiv, Twitpic]
|
||||
end
|
||||
|
||||
def rewrite(url, headers)
|
||||
|
||||
36
app/logical/downloads/strategies/twitpic.rb
Normal file
36
app/logical/downloads/strategies/twitpic.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
module Downloads
|
||||
module Strategies
|
||||
class Twitpic < Base
|
||||
def rewrite(url, headers)
|
||||
if url =~ %r{http://twitpic\.com} || url =~ %r{^http://d3j5vwomefv46c\.cloudfront\.net}
|
||||
url, headers = rewrite_html_pages(url, headers)
|
||||
url, headers = rewrite_thumbnails(url, headers)
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
protected
|
||||
def rewrite_html_pages(url, headers)
|
||||
# example: http://twitpic.com/cpprns
|
||||
|
||||
if url =~ %r{http://twitpic\.com/([a-z0-9]+)$}
|
||||
id = $1
|
||||
url = "http://twitpic.com/show/full/#{id}"
|
||||
return [url, headers]
|
||||
else
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
|
||||
def rewrite_thumbnails(url, headers)
|
||||
if url =~ %r{^http://d3j5vwomefv46c\.cloudfront\.net/photos/thumb/(\d+\..+)$}
|
||||
match = $1
|
||||
url.sub!("/thumb/" + match, "/large/" + match)
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
2479
test/fixtures/vcr_cassettes/download-twitpic-html.yml
vendored
Normal file
2479
test/fixtures/vcr_cassettes/download-twitpic-html.yml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2447
test/fixtures/vcr_cassettes/download-twitpic-thumb.yml
vendored
Normal file
2447
test/fixtures/vcr_cassettes/download-twitpic-thumb.yml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
44
test/unit/downloads/twitpic_test.rb
Normal file
44
test/unit/downloads/twitpic_test.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'test_helper'
|
||||
|
||||
module Downloads
|
||||
class TwitpicTest < ActiveSupport::TestCase
|
||||
context "a download for a twitpic html page" do
|
||||
setup do
|
||||
@source = "http://twitpic.com/cpprns"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Downloads::File.new(@source, @tempfile.path)
|
||||
VCR.use_cassette("download-twitpic-html", :record => :new_episodes) do
|
||||
@download.download!
|
||||
end
|
||||
end
|
||||
|
||||
should "set the direct image link as the source" do
|
||||
assert_equal("http://d3j5vwomefv46c.cloudfront.net/photos/large/768786760.jpg?1368245083", @download.source)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
puts ::File.read(@tempfile.path).inspect
|
||||
assert_equal(89_409, ::File.size(@tempfile.path))
|
||||
end
|
||||
end
|
||||
|
||||
context "a download for a twitpic thumbnail" do
|
||||
setup do
|
||||
@source = "http://d3j5vwomefv46c.cloudfront.net/photos/thumb/768786760.jpg?1368245083"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Downloads::File.new(@source, @tempfile.path)
|
||||
VCR.use_cassette("download-twitpic-thumb", :record => :new_episodes) do
|
||||
@download.download!
|
||||
end
|
||||
end
|
||||
|
||||
should "instead download the original version" do
|
||||
assert_equal("http://d3j5vwomefv46c.cloudfront.net/photos/large/768786760.jpg?1368245083", @download.source)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
assert_equal(89_409, ::File.size(@tempfile.path))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user