#1866: Support deviantart source rewriting

* html work page -> full image
* thumbnail -> full image
This commit is contained in:
Toks
2013-11-26 16:44:29 -05:00
parent 100f3a666b
commit 6fbd373873
7 changed files with 12452 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ module Downloads
module Strategies
class Base
def self.strategies
[Pixiv, Twitpic]
[Pixiv, Twitpic, DeviantArt]
end
def rewrite(url, headers)

View File

@@ -0,0 +1,34 @@
module Downloads
module Strategies
class DeviantArt < Base
def rewrite(url, headers)
if url =~ /https?:\/\/(?:\w+\.)?deviantart\.(?:com|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)
if url =~ %r{^http://\w+\.deviantart\.com/art/\w+}
source = ::Sources::Strategies::DeviantArt.new(url)
source.get
return [source.image_url, headers]
else
return [url, headers]
end
end
def rewrite_thumbnails(url, headers)
if url =~ %r{^(http://\w+.deviantart.net/\w+/)200H/}
match = $1
url.sub!(match + "200H/", match)
end
return [url, headers]
end
end
end
end

View File

@@ -4,7 +4,7 @@ module Sources
delegate :get, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :to => :strategy
def self.strategies
[Strategies::NicoSeiga, Strategies::Pixiv]
[Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt]
end
def initialize(url)

View File

@@ -0,0 +1,68 @@
module Sources
module Strategies
class DeviantArt < Base
def self.url_match?(url)
url =~ /^https?:\/\/(?:\w+\.)?deviantart\.(?:com|net)/
end
def site_name
"Deviant Art"
end
def unique_id
profile_url =~ /https?:\/\/(\w+)\.deviantart\.com/
"deviantart" + $1
end
def get
agent.get(URI.parse(normalized_url)) do |page|
@artist_name, @profile_url = get_profile_from_page(page)
@image_url = get_image_url_from_page(page)
@tags = []
end
end
protected
def get_profile_from_page(page)
links = page.search("div.dev-title-container a.username")
if links.any?
profile_url = links[0]["href"]
artist_name = links[0].text
else
profile_url = nil
artist_name = nil
end
return [artist_name, profile_url].compact
end
def get_image_url_from_page(page)
image = page.search("div.dev-view-deviation img.dev-content-normal")
if image.any?
image[0]["src"]
else
nil
end
end
def normalized_url
@normalized_url ||= begin
if url =~ %r{\Ahttp://(?:fc|th)\d{2}\.deviantart\.net/.+/[a-z0-9_]+_by_[a-z0-9_]+-d([a-z0-9]+)\.}i
"http://fav.me/d#{$1}"
elsif url =~ %r{deviantart\.com/art/}
url
else
nil
end
end
end
def agent
@agent ||= Mechanize.new
end
end
end
end

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
require 'test_helper'
module Downloads
class DeviantArtTest < ActiveSupport::TestCase
context "a download for a deviant art html page" do
setup do
@source = "http://mochikko.deviantart.com/art/RESOLUTION-339610451"
@tempfile = Tempfile.new("danbooru-test")
@download = Downloads::File.new(@source, @tempfile.path)
VCR.use_cassette("download-deviant-art-html", :record => :new_episodes) do
@download.download!
end
end
should "set the direct image link as the source" do
assert_equal("http://fc03.deviantart.net/fs71/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
end
should "work" do
assert_equal(255_683, ::File.size(@tempfile.path))
end
end
context "a download for a deviant art thumbnail" do
setup do
@source = "http://fc03.deviantart.net/fs71/200H/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg"
@tempfile = Tempfile.new("danbooru-test")
@download = Downloads::File.new(@source, @tempfile.path)
VCR.use_cassette("download-deviant-art-thumb", :record => :new_episodes) do
@download.download!
end
end
should "instead download the original version" do
assert_equal("http://fc03.deviantart.net/fs71/f/2012/330/e/7/resolution_by_mochikko-d5m713n.jpg", @download.source)
end
should "work" do
assert_equal(255_683, ::File.size(@tempfile.path))
end
end
end
end