From 284141aaceb102fffd82ae042b81912345ab7ce9 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 26 Sep 2011 16:47:22 -0400 Subject: [PATCH] refactored source code, work on pixiv integration --- app/assets/javascripts/uploads.js | 70 +++++++++++++++++--- app/assets/stylesheets/application.css.scss | 16 +++++ app/controllers/sources_controller.rb | 8 ++- app/controllers/uploads_controller.rb | 1 + app/logical/sources/site.rb | 32 +++++++-- app/logical/sources/strategies/base.rb | 24 +++++++ app/logical/sources/strategies/default.rb | 4 ++ app/logical/sources/strategies/fc2.rb | 4 ++ app/logical/sources/strategies/nico_seiga.rb | 4 ++ app/logical/sources/strategies/pixa.rb | 4 ++ app/logical/sources/strategies/pixiv.rb | 18 +++-- app/logical/sources/strategies/tinami.rb | 4 ++ app/views/sources/_info.html.erb | 15 +++++ app/views/sources/show.json.erb | 6 -- app/views/uploads/_image.html.erb | 4 ++ app/views/uploads/_post.html.erb | 6 ++ app/views/uploads/new.html.erb | 15 ++--- config/routes.rb | 2 +- test/unit/pixiv_proxy_test.rb | 9 +-- 19 files changed, 201 insertions(+), 45 deletions(-) create mode 100644 app/views/sources/_info.html.erb delete mode 100644 app/views/sources/show.json.erb create mode 100644 app/views/uploads/_image.html.erb create mode 100644 app/views/uploads/_post.html.erb diff --git a/app/assets/javascripts/uploads.js b/app/assets/javascripts/uploads.js index 2614604d1..335dbbf01 100644 --- a/app/assets/javascripts/uploads.js +++ b/app/assets/javascripts/uploads.js @@ -1,13 +1,63 @@ -$(function() { - var img = $("#image-preview img"); - if (img) { - var height = img.attr("height"); - var width = img.attr("width"); - if (height > 400) { - var ratio = 400.0 / height; - img.attr("height", height * ratio); - img.attr("width", width * ratio); - $("#scale").html("Scaled " + parseInt(100 * ratio) + "%"); +(function() { + Danbooru.Upload = {}; + + Danbooru.Upload.initialize_all = function() { + this.initialize_image(); + this.initialize_info(); + } + + Danbooru.Upload.initialize_info = function() { + $("#c-uploads #source-info ul").hide(); + $("#c-uploads #fetch-data").click(function(e) { + Danbooru.ajax_start(e.target); + $.get(e.target.href).success(function(data) { + var artist_name = data.artist_name; + var profile_url = data.profile_url; + var tags = data.tags; + var danbooru_id = data.danbooru_id; + var danbooru_name = data.danbooru_name; + var tag_html = ""; + $.each(data.tags, function(i, v) { + var name = v[0]; + var url = v[1]; + tag_html += ('' + name + ' '); + }); + + $("#source-artist").html('' + data.artist_name + ''); + $("#source-tags").html(tag_html); + + var new_artist_link = 'new'; + + if (data.danbooru_id) { + $("#source-record").html('' + data.danbooru_name + ' ' + new_artist_link); + } else { + $("#source-record").html(new_artist_link); + } + + $("#source-info p").hide(); + $("#source-info ul").show(); + }).complete(function(data) { + Danbooru.ajax_stop(e.target); + }); + e.preventDefault(); + }); + } + + Danbooru.Upload.initialize_image = function() { + var $image = $("#c-uploads #image"); + if ($image.size() > 0) { + var height = $image.height(); + var width = $image.width(); + if (height > 400) { + var ratio = 400.0 / height; + $image.height(height * ratio); + $image.width(width * ratio); + $("#scale").html("Scaled " + parseInt(100 * ratio) + "%"); + } } } +})(); + +$(function() { + Danbooru.Upload.initialize_all(); }); diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index d09269cf9..d115e177e 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -935,6 +935,22 @@ div#c-uploads { display: inline; } } + + div#source-info { + margin: 1em 0; + padding: 1em; + border: 1px solid gray; + + p { + margin: 0; + } + + ul { + a { + margin-right: 1em; + } + } + } } } diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index 7c927a381..b19f2d1cf 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -4,6 +4,12 @@ class SourcesController < ApplicationController def show @source = Sources::Site.new(params[:url]) - respond_with(@source) + @source.get + + respond_with(@source) do |format| + format.json do + render :json => @source.to_json + end + end end end diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 072062ce4..a1c86a63e 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -6,6 +6,7 @@ class UploadsController < ApplicationController @upload = Upload.new(:rating => "q") if params[:url] @post = Post.find_by_source(params[:url]) + @source = Sources::Site.new(params[:url]) end respond_with(@upload) end diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb index b9efea02c..4f72b9a36 100644 --- a/app/logical/sources/site.rb +++ b/app/logical/sources/site.rb @@ -1,18 +1,36 @@ module Sources class Site attr_reader :url, :strategy - delegate :artist_name, :profile_url, :image_url, :tags, :to => :strategy + delegate :get, :site_name, :artist_name, :artist_alias, :profile_url, :image_url, :tags, :artist_record, :unique_id, :to => :strategy + + def self.strategies + [Strategies::Fc2, Strategies::NicoSeiga, Strategies::Pixa, Strategies::Pixiv, Strategies::Tinami, Strategies::Default] + end def initialize(url) @url = url - case url - when /pixiv\.net/ - @strategy = Strategies::Pixiv.new(url) - - else - @strategy = Strategies::Default.new(url) + Site.strategies.each do |strategy| + if strategy.url_match?(url) + @strategy = strategy.new(url) + break + end end end + + def to_json + return { + :artist_name => artist_name, + :profile_url => profile_url, + :tags => tags, + :danbooru_name => artist_record.first.try(:name), + :danbooru_id => artist_record.first.try(:id), + :unique_id => unique_id + }.to_json + end + + def available? + strategy.present? + end end end diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index 92f427b13..16ae39768 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -3,11 +3,23 @@ module Sources class Base attr_reader :url, :agent + def self.url_match?(url) + false + end + def initialize(url) @url = url @agent = create_agent end + def get + raise NotImplementedError + end + + def site_name + raise NotImplementedError + end + def artist_name raise NotImplementedError end @@ -24,6 +36,18 @@ module Sources raise NotImplementedError end + def artist_alias + nil + end + + def unique_id + artist_name + end + + def artist_record + Artist.other_names_match(artist_name) + end + protected def create_agent raise NotImplementedError diff --git a/app/logical/sources/strategies/default.rb b/app/logical/sources/strategies/default.rb index 8fc4e0e47..3652af595 100644 --- a/app/logical/sources/strategies/default.rb +++ b/app/logical/sources/strategies/default.rb @@ -1,6 +1,10 @@ module Sources module Strategies class Default < Base + def site_name + "?" + end + def artist_name "?" end diff --git a/app/logical/sources/strategies/fc2.rb b/app/logical/sources/strategies/fc2.rb index acf84e2f0..54ecdcb1a 100644 --- a/app/logical/sources/strategies/fc2.rb +++ b/app/logical/sources/strategies/fc2.rb @@ -1,6 +1,10 @@ module Sources module Strategies class Fc2 < Base + def site_name + "FC2" + end + def artist_name "?" end diff --git a/app/logical/sources/strategies/nico_seiga.rb b/app/logical/sources/strategies/nico_seiga.rb index 8f7a8a7de..018f0b93d 100644 --- a/app/logical/sources/strategies/nico_seiga.rb +++ b/app/logical/sources/strategies/nico_seiga.rb @@ -1,6 +1,10 @@ module Sources module Strategies class NicoSeiga < Base + def site_name + "Nico Seiga" + end + def artist_name "?" end diff --git a/app/logical/sources/strategies/pixa.rb b/app/logical/sources/strategies/pixa.rb index 5176419b0..5e6995538 100644 --- a/app/logical/sources/strategies/pixa.rb +++ b/app/logical/sources/strategies/pixa.rb @@ -1,6 +1,10 @@ module Sources module Strategies class Pixa < Base + def site_name + "Pixa" + end + def artist_name "?" end diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 274f80de5..72149b786 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -3,13 +3,21 @@ module Sources class Pixiv < Base attr_reader :artist_name, :profile_url, :image_url, :tags - def initialize(url) - super - get + def self.url_match?(url) + url =~ /^https?:\/\/(?:\w+\.)?pixiv\.net/ end - def is_pixiv? - url =~ /pixiv\.net/ + def initialize(url) + super + end + + def site_name + "Pixiv" + end + + def unique_id + image_url =~ /\/img\/([^\/]+)/ + $1 end def get diff --git a/app/logical/sources/strategies/tinami.rb b/app/logical/sources/strategies/tinami.rb index 6789dbeff..53b010975 100644 --- a/app/logical/sources/strategies/tinami.rb +++ b/app/logical/sources/strategies/tinami.rb @@ -1,6 +1,10 @@ module Sources module Strategies class Tinami < Base + def site_name + "Tinami" + end + def artist_name "?" end diff --git a/app/views/sources/_info.html.erb b/app/views/sources/_info.html.erb new file mode 100644 index 000000000..5ca63ffd4 --- /dev/null +++ b/app/views/sources/_info.html.erb @@ -0,0 +1,15 @@ + + +<% if source.try(:available?) %> +
+

This looks like a <%= source.site_name %> upload. <%= link_to "Fetch data", source_path(:format => "json", :url => params[:url]), :id => "fetch-data" %>.

+ + +
+<% end %> diff --git a/app/views/sources/show.json.erb b/app/views/sources/show.json.erb deleted file mode 100644 index 8d0aaae7d..000000000 --- a/app/views/sources/show.json.erb +++ /dev/null @@ -1,6 +0,0 @@ -({ - "artist_name": "<%= j @source.artist_name %>", - "profile_url": "<%= j @source.profile_url %>", - "image_url": "<%= j @source.image_url %>", - "tags": <%= @source.tags.to_json.html_safe %> -}); diff --git a/app/views/uploads/_image.html.erb b/app/views/uploads/_image.html.erb new file mode 100644 index 000000000..b14bf2a2b --- /dev/null +++ b/app/views/uploads/_image.html.erb @@ -0,0 +1,4 @@ +<% if params[:url] %> + <%= image_tag(params[:url], :title => "Preview", :id => "image") %> +

+<% end %> diff --git a/app/views/uploads/_post.html.erb b/app/views/uploads/_post.html.erb new file mode 100644 index 000000000..8c9b4f9a3 --- /dev/null +++ b/app/views/uploads/_post.html.erb @@ -0,0 +1,6 @@ + +<% if post %> +

This post was probably already uploaded (<%= link_to "post ##{post.id}", posts_path(post), :target => "_blank" %>).

+<% end %> diff --git a/app/views/uploads/new.html.erb b/app/views/uploads/new.html.erb index a32419019..0d5ede8c0 100644 --- a/app/views/uploads/new.html.erb +++ b/app/views/uploads/new.html.erb @@ -6,18 +6,11 @@

Before uploading, please read the <%= link_to "how to upload guide", wiki_pages_path(:title => "howto:upload") %>.

+ <%= render "image" %> + <%= render "post" %> + <%= render "sources/info", :source => @source %> + <%= form_for(@upload, :html => {:multipart => true, :class => "simple_form"}) do |f| %> - <% if params[:url] %> -
- <%= image_tag(params[:url], :title => "Preview") %> -

-
- - <% if @post %> -

This post was probably already uploaded (<%= link_to "post ##{@post.id}", posts_path(@post), :target => "_blank" %>).

- <% end %> - <% end %> -
<%= f.label :file %> <%= f.file_field :file, :size => 50 %> diff --git a/config/routes.rb b/config/routes.rb index 37a0e56e1..93a2cf83b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,7 +32,7 @@ Danbooru::Application.routes.draw do resources :advertisements do resources :hits, :controller => "advertisement_hits", :only => [:create] end - resource :art_site_proxy, :only => [:show] + resource :source, :only => [:show] resources :artists do member do put :revert diff --git a/test/unit/pixiv_proxy_test.rb b/test/unit/pixiv_proxy_test.rb index 50fc6336a..6fe7b31d4 100644 --- a/test/unit/pixiv_proxy_test.rb +++ b/test/unit/pixiv_proxy_test.rb @@ -5,11 +5,12 @@ require 'test_helper' class PixivProxyTest < ActiveSupport::TestCase context "The proxy" do should "get a single post" do - proxy = Sources::Site.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484") + site = Sources::Site.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9646484") + site.get - assert_equal("http://www.pixiv.net/member.php?id=4015", proxy.profile_url) - assert(proxy.tags.size > 0) - first_tag = proxy.tags.first + assert_equal("http://www.pixiv.net/member.php?id=4015", site.profile_url) + assert(site.tags.size > 0) + first_tag = site.tags.first assert_equal(2, first_tag.size) assert(first_tag[0] =~ /./) assert(first_tag[1] =~ /tags\.php\?tag=/)