This commit is contained in:
r888888888
2015-05-21 15:05:55 -07:00
parent af86099ac5
commit 2ddc93723c
17 changed files with 3156 additions and 3554 deletions

View File

@@ -8,19 +8,15 @@ class UploadsController < ApplicationController
@upload = Upload.new
if params[:url]
@normalized_url = params[:url]
headers = default_headers()
data = {}
headers = {
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
Downloads::RewriteStrategies::Base.strategies.each do |strategy|
@normalized_url, headers = strategy.new(@normalized_url).rewrite(@normalized_url, headers)
@normalized_url, headers, data = strategy.new(@normalized_url).rewrite(@normalized_url, headers, data)
end
if @normalized_url.nil?
@post = Post.where(source: params[:url]).first
else
@post = Post.where(source: [params[:url], @normalized_url]).first
end
@post = find_post_by_url(@normalized_url)
extract_artist_commentary(@upload, data)
begin
@source = Sources::Site.new(params[:url])
@@ -71,6 +67,27 @@ class UploadsController < ApplicationController
end
protected
def extract_artist_commentary(upload, data)
if data[:artist_commentary_desc]
upload.artist_commentary_title = data[:artist_commentary_title]
upload.artist_commentary_desc = data[:artist_commentary_desc]
end
end
def find_post_by_url(normalized_url)
if normalized_url.nil?
Post.where(source: params[:url]).first
else
Post.where(source: [params[:url], @normalized_url]).first
end
end
def default_headers
{
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
end
def save_recent_tags
if @upload
tags = Tag.scan_tags(@upload.tag_string)

View File

@@ -15,6 +15,8 @@ module Downloads
url, headers = rewrite_thumbnails(url, headers)
url, headers = rewrite_old_small_manga_pages(url, headers)
url, headers = rewrite_to_thumbnails(url, headers) if data.delete(:get_thumbnail)
data[:artist_commentary_title] = source.artist_commentary_title
data[:artist_commentary_desc] = source.artist_commentary_desc
end
# http://i2.pixiv.net/img-zip-ugoira/img/2014/08/05/06/01/10/44524589_ugoira1920x1080.zip

View File

@@ -3,29 +3,35 @@ module Downloads
class Twitter < Base
def rewrite(url, headers, data = {})
if url =~ %r!^https?://(?:mobile\.)?twitter\.com!
url, headers = rewrite_status_page(url, headers)
url, headers = rewrite_status_page(url, headers, data)
elsif url =~ %r{^https?://pbs\.twimg\.com}
url, headers = rewrite_thumbnails(url, headers)
url, headers = rewrite_thumbnails(url, headers, data)
end
return [url, headers, data]
end
protected
def rewrite_status_page(url, headers)
source = ::Sources::Strategies::Twitter.new(url)
source.get
def rewrite_status_page(url, headers, data)
source = build_source(url)
url = source.image_url
return [url, headers]
data[:artist_commentary_desc] = source.artist_commentary_desc
return [url, headers, data]
end
def rewrite_thumbnails(url, headers)
def rewrite_thumbnails(url, headers, data)
if url =~ %r{^(https?://pbs\.twimg\.com/media/[^:]+)}
url = $1 + ":orig"
end
return [url, headers]
end
def build_source(url)
::Sources::Strategies::Twitter.new(url).tap do |x|
x.get
end
end
end
end
end

View File

@@ -7,6 +7,7 @@ class PixivApiClient
class WorksResponse
attr_reader :json, :pages, :moniker, :page_count
attr_reader :artist_commentary_title, :artist_commentary_desc
def initialize(json)
# Sample response:
@@ -91,6 +92,8 @@ class PixivApiClient
@json = json
@moniker = json["user"]["account"]
@page_count = json["page_count"].to_i
@artist_commentary_title = json["title"]
@artist_commentary_desc = json["caption"]
if page_count > 1
@pages = json["metadata"]["pages"].map {|x| x["image_urls"]["large"]}

View File

@@ -3,7 +3,11 @@
module Sources
class Site
attr_reader :url, :strategy
delegate :get, :get_size, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :page_count, :file_url, :ugoira_frame_data, :image_urls, :to => :strategy
delegate :get, :get_size, :referer_url, :site_name, :artist_name,
:profile_url, :image_url, :tags, :artist_record, :unique_id,
:page_count, :file_url, :ugoira_frame_data, :image_urls,
:has_artist_commentary?, :artist_commentary_title,
:artist_commentary_desc, :to => :strategy
def self.strategies
[Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::Nijie, Strategies::Twitter]

View File

@@ -3,6 +3,7 @@ module Sources
class Base
attr_reader :url
attr_reader :artist_name, :profile_url, :image_url, :tags, :page_count
attr_reader :artist_commentary_title, :artist_commentary_desc
def self.url_match?(url)
false
@@ -43,6 +44,11 @@ module Sources
false
end
# Determines whether or not to automatically create an ArtistCommentary
def has_artist_commentary?
false
end
def normalize_for_artist_finder!
url
end

View File

@@ -35,6 +35,10 @@ module Sources
@pixiv_moniker
end
def has_artist_commentary?
@artist_commentary_desc.present?
end
def normalized_for_artist_finder?
url =~ %r!http://img\.pixiv\.net/img/#{MONIKER}/?$!i
end
@@ -47,15 +51,18 @@ module Sources
if has_moniker?
moniker = get_moniker_from_url
else
illust_id = illust_id_from_url(url)
metadata = get_metadata_from_papi(illust_id)
moniker = metadata.moniker
@illust_id = illust_id_from_url(url)
@metadata = get_metadata_from_papi(@illust_id)
moniker = @metadata.moniker
end
"http://img.pixiv.net/img/#{moniker}/"
end
def get
@illust_id = illust_id_from_url(url)
@metadata = get_metadata_from_papi(@illust_id)
page = agent.get(URI.parse(normalized_url))
if page.search("body.not-logged-in").any?
@@ -71,6 +78,8 @@ module Sources
@tags = get_tags_from_page(page)
@page_count = get_page_count_from_page(page)
@gallery_link = get_gallery_link(page)
@artist_commentary_title = @metadata.artist_commentary_title
@artist_commentary_desc = @metadata.artist_commentary_desc
is_manga = @page_count > 1
@@ -122,10 +131,7 @@ module Sources
def rewrite_new_medium_images(thumbnail_url)
if thumbnail_url =~ %r!/c/\d+x\d+/img-master/img/#{TIMESTAMP}/\d+_p\d+_\w+\.jpg!i
page = manga_page_from_url(@url).to_i
illust_id = illust_id_from_url(@url)
metadata = get_metadata_from_papi(illust_id)
thumbnail_url = metadata.pages[page]
thumbnail_url = @metadata.pages[page]
end
thumbnail_url
@@ -154,9 +160,7 @@ module Sources
def rewrite_old_small_and_medium_images(thumbnail_url, is_manga)
if thumbnail_url =~ %r!/img/#{MONIKER}/\d+_[ms]\.#{EXT}!i
if is_manga.nil?
illust_id = illust_id_from_url(@url)
metadata = get_metadata_from_papi(illust_id)
page_count = metadata.page_count
page_count = @metadata.page_count
is_manga = page_count > 1
end
@@ -300,8 +304,7 @@ module Sources
end
def normalized_url
illust_id = illust_id_from_url(@url)
"http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{illust_id}"
"http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{@illust_id}"
end
def get_metadata_from_papi(illust_id)

View File

@@ -18,6 +18,12 @@ module Sources::Strategies
@artist_name = attrs[:user][:name]
@profile_url = "https://twitter.com/" + attrs[:user][:screen_name]
@image_url = attrs[:entities][:media][0][:media_url] + ":orig"
@artist_commentary_title = ""
@artist_commentary_desc = attrs[:text]
end
def has_artist_commentary?
@artist_commentary_desc.present?
end
def image_urls

View File

@@ -4,7 +4,9 @@ require "tmpdir"
class Upload < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size, :as_pending
attr_accessor :file, :image_width, :image_height, :file_ext, :md5,
:file_size, :as_pending, :artist_commentary_title,
:artist_commentary_desc, :include_artist_commentary
belongs_to :uploader, :class_name => "User"
belongs_to :post
before_validation :initialize_uploader, :on => :create
@@ -14,7 +16,11 @@ class Upload < ActiveRecord::Base
validate :uploader_is_not_limited, :on => :create
validate :file_or_source_is_present, :on => :create
validate :rating_given
attr_accessible :file, :image_width, :image_height, :file_ext, :md5, :file_size, :as_pending, :source, :file_path, :content_type, :rating, :tag_string, :status, :backtrace, :post_id, :md5_confirmation, :parent_id, :server
attr_accessible :file, :image_width, :image_height, :file_ext, :md5,
:file_size, :as_pending, :source, :file_path, :content_type, :rating,
:tag_string, :status, :backtrace, :post_id, :md5_confirmation,
:parent_id, :server, :artist_commentary_title,
:artist_commentary_desc, :include_artist_commentary
module ValidationMethods
def uploader_is_not_limited
@@ -117,6 +123,7 @@ class Upload < ActiveRecord::Base
post.distribute_files
if post.save
CurrentUser.increment!(:post_upload_count)
create_artist_commentary(post) if include_artist_commentary
ugoira_service.save_frame_data(post) if is_ugoira?
update_attributes(:status => "completed", :post_id => post.id)
else
@@ -489,6 +496,19 @@ class Upload < ActiveRecord::Base
end
end
module ArtistCommentaryMethods
def create_artist_commentary(post)
post.create_artist_commentary(
:original_title => artist_commentary_title,
:original_description => artist_commentary_desc
)
end
def has_artist_commentary?
artist_commentary_desc.present?
end
end
include ConversionMethods
include ValidationMethods
include FileMethods
@@ -503,6 +523,7 @@ class Upload < ActiveRecord::Base
include VideoMethods
extend SearchMethods
include ApiMethods
include ArtistCommentaryMethods
def presenter
@presenter ||= UploadPresenter.new(self)

View File

@@ -65,6 +65,25 @@
<%= f.text_field :parent_id %>
</div>
<% if @upload.has_artist_commentary? %>
<div class="input">
<%= f.label :artist_commentary_title, "Artist Commentary Title" %>
<%= f.text_field :artist_commentary_title, :value => @upload.artist_commentary_title %>
</div>
<div class="input">
<%= f.label :artist_commentary_desc, "Artist Commentary" %>
<%= f.text_area :artist_commentary_desc, :value => @upload.artist_commentary_desc, :size => "60x5" %>
</div>
<div class="input">
<label for="upload_include_artist_commentary">
<%= f.check_box :include_artist_commentary %>
Include Commentary
</label>
</div>
<% end %>
<% if Danbooru.config.iqdb_hostname_and_port %>
<% if params[:url] %>
<div class="input" id="iqdb-similar">