refactored source code, work on pixiv integration

This commit is contained in:
albert
2011-09-26 16:47:22 -04:00
parent 11b623c66b
commit 284141aace
19 changed files with 201 additions and 45 deletions

View File

@@ -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 += ('<a href="' + url + '">' + name + '</a> ');
});
$("#source-artist").html('<a href="' + data.profile_url + '">' + data.artist_name + '</a>');
$("#source-tags").html(tag_html);
var new_artist_link = '<a href="/artists/new?name=' + data.unique_id + '&other_names=' + data.artist_name + '&urls=' + encodeURIComponent(profile_url) + '+' + encodeURIComponent($("#image").attr("src")) + '">new</a>';
if (data.danbooru_id) {
$("#source-record").html('<a href="/artists/' + data.danbooru_id + '">' + data.danbooru_name + '</a> ' + 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();
});

View File

@@ -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;
}
}
}
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,6 +1,10 @@
module Sources
module Strategies
class Default < Base
def site_name
"?"
end
def artist_name
"?"
end

View File

@@ -1,6 +1,10 @@
module Sources
module Strategies
class Fc2 < Base
def site_name
"FC2"
end
def artist_name
"?"
end

View File

@@ -1,6 +1,10 @@
module Sources
module Strategies
class NicoSeiga < Base
def site_name
"Nico Seiga"
end
def artist_name
"?"
end

View File

@@ -1,6 +1,10 @@
module Sources
module Strategies
class Pixa < Base
def site_name
"Pixa"
end
def artist_name
"?"
end

View File

@@ -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

View File

@@ -1,6 +1,10 @@
module Sources
module Strategies
class Tinami < Base
def site_name
"Tinami"
end
def artist_name
"?"
end

View File

@@ -0,0 +1,15 @@
<!--
- source
-->
<% if source.try(:available?) %>
<div id="source-info">
<p>This looks like a <%= source.site_name %> upload. <%= link_to "Fetch data", source_path(:format => "json", :url => params[:url]), :id => "fetch-data" %>.</p>
<ul>
<li><strong>Artist</strong>: <span id="source-artist"></span></li>
<li><strong>Tags</strong>: <span id="source-tags"></span></li>
<li><strong>Record</strong>: <span id="source-record"></span></li>
</ul>
</div>
<% end %>

View File

@@ -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 %>
});

View File

@@ -0,0 +1,4 @@
<% if params[:url] %>
<%= image_tag(params[:url], :title => "Preview", :id => "image") %>
<p id="scale"></p>
<% end %>

View File

@@ -0,0 +1,6 @@
<!--
- post
-->
<% if post %>
<p>This post was probably already uploaded (<%= link_to "post ##{post.id}", posts_path(post), :target => "_blank" %>).</p>
<% end %>

View File

@@ -6,18 +6,11 @@
<p>Before uploading, please read the <%= link_to "how to upload guide", wiki_pages_path(:title => "howto:upload") %>.</p>
</div>
<%= render "image" %>
<%= render "post" %>
<%= render "sources/info", :source => @source %>
<%= form_for(@upload, :html => {:multipart => true, :class => "simple_form"}) do |f| %>
<% if params[:url] %>
<div id="image-preview">
<%= image_tag(params[:url], :title => "Preview") %>
<p id="scale"></p>
</div>
<% if @post %>
<p>This post was probably already uploaded (<%= link_to "post ##{@post.id}", posts_path(@post), :target => "_blank" %>).</p>
<% end %>
<% end %>
<div class="input">
<%= f.label :file %>
<%= f.file_field :file, :size => 50 %>

View File

@@ -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

View File

@@ -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=/)