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

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,208 @@
---
http_interactions:
- request:
method: post
uri: https://ocqPCKeWr01mvWzCIcveJYRTY:8GECMr2Ves7oK3bmAneHYMLSfDPoJvTizh5gSd4pLBNbJfYNz5@api.twitter.com/oauth2/token
body:
encoding: UTF-8
string: grant_type=client_credentials
headers:
Accept:
- "*/*"
User-Agent:
- TwitterRubyGem/5.13.0
Content-Type:
- application/x-www-form-urlencoded; charset=UTF-8
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
response:
status:
code: 200
message: OK
headers:
Cache-Control:
- private, max-age=0, must-revalidate
Content-Disposition:
- attachment; filename=json.json
Content-Length:
- '151'
Content-Type:
- application/json;charset=utf-8
Date:
- Fri, 22 May 2015 23:57:30 GMT
Expires:
- Tue, 31 Mar 1981 05:00:00 GMT
Last-Modified:
- Fri, 22 May 2015 23:57:30 GMT
Ml:
- S
Pragma:
- no-cache
Server:
- tsa_a
Set-Cookie:
- guest_id=v1%3A143233904999810992; Domain=.twitter.com; Path=/; Expires=Sun,
21-May-2017 23:57:30 UTC
Status:
- 200 OK
Strict-Transport-Security:
- max-age=631138519
X-Connection-Hash:
- 5d36414acb4d4d37865904c4cae7542d
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Response-Time:
- '11'
X-Transaction:
- 911384da81767843
X-Tsa-Request-Body-Time:
- '4'
X-Twitter-Response-Tags:
- BouncerCompliant
X-Ua-Compatible:
- IE=edge,chrome=1
X-Xss-Protection:
- 1; mode=block
body:
encoding: UTF-8
string: '{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAPV7eAAAAAAAN7q56yJ8wNHhsagDn06qiJ0Io00%3DfdvXQaVKHFS3q1hd3y8fVQdIbntIHN2iTsBTk0f6jQK7lxcDo5"}'
http_version:
recorded_at: Fri, 22 May 2015 23:57:30 GMT
- request:
method: get
uri: https://api.twitter.com/1.1/statuses/show/566030116182949888.json
body:
encoding: US-ASCII
string: ''
headers:
Accept:
- application/json
User-Agent:
- TwitterRubyGem/5.13.0
Authorization:
- Bearer AAAAAAAAAAAAAAAAAAAAAPV7eAAAAAAAN7q56yJ8wNHhsagDn06qiJ0Io00%3DfdvXQaVKHFS3q1hd3y8fVQdIbntIHN2iTsBTk0f6jQK7lxcDo5
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
response:
status:
code: 200
message: OK
headers:
Cache-Control:
- no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Content-Disposition:
- attachment; filename=json.json
Content-Length:
- '1237'
Content-Type:
- application/json;charset=utf-8
Date:
- Fri, 22 May 2015 23:57:30 GMT
Expires:
- Tue, 31 Mar 1981 05:00:00 GMT
Last-Modified:
- Fri, 22 May 2015 23:57:30 GMT
Pragma:
- no-cache
Server:
- tsa_a
Set-Cookie:
- guest_id=v1%3A143233905025943870; Domain=.twitter.com; Path=/; Expires=Sun,
21-May-2017 23:57:30 UTC
Status:
- 200 OK
Strict-Transport-Security:
- max-age=631138519
X-Access-Level:
- read
X-Connection-Hash:
- 1ca5a3dd668b6b3ec0c298fb2d248479
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Rate-Limit-Limit:
- '180'
X-Rate-Limit-Remaining:
- '176'
X-Rate-Limit-Reset:
- '1432339397'
X-Response-Time:
- '21'
X-Transaction:
- ebb6ec42be36ba79
X-Twitter-Response-Tags:
- BouncerCompliant
X-Xss-Protection:
- 1; mode=block
body:
encoding: UTF-8
string: '{"created_at":"Fri Feb 13 00:24:30 +0000 2015","id":566030116182949888,"id_str":"566030116182949888","text":"What
do you give the Caramel Frappuccino that already has everything? The gift
of mocha drizzle. http:\/\/t.co\/2MWtXbYhf0","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\"
rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":127566622,"id_str":"127566622","name":"Frappuccino","screen_name":"frappuccino","location":"Seattle,
WA","description":"Le yum.","url":"http:\/\/t.co\/fZct7xT8Hn","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/fZct7xT8Hn","expanded_url":"http:\/\/www.Frappuccino.com","display_url":"Frappuccino.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":176886,"friends_count":68023,"listed_count":715,"created_at":"Mon
Mar 29 16:30:39 +0000 2010","favourites_count":3588,"utc_offset":-25200,"time_zone":"Pacific
Time (US & Canada)","geo_enabled":true,"verified":true,"statuses_count":11814,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"9AE4E8","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/486908690591535104\/roRpLqRU.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/486908690591535104\/roRpLqRU.jpeg","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/593460960020402176\/B_NNVW9D_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/593460960020402176\/B_NNVW9D_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/127566622\/1420826895","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDFFCC","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":632,"favorite_count":2385,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[],"media":[{"id":566030112210972672,"id_str":"566030112210972672","indices":[97,119],"media_url":"http:\/\/pbs.twimg.com\/media\/B9rxY1qCcAAbeo0.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/B9rxY1qCcAAbeo0.jpg","url":"http:\/\/t.co\/2MWtXbYhf0","display_url":"pic.twitter.com\/2MWtXbYhf0","expanded_url":"http:\/\/twitter.com\/frappuccino\/status\/566030116182949888\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":191,"resize":"fit"},"medium":{"w":600,"h":337,"resize":"fit"},"large":{"w":1024,"h":576,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":566030112210972672,"id_str":"566030112210972672","indices":[97,119],"media_url":"http:\/\/pbs.twimg.com\/media\/B9rxY1qCcAAbeo0.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/B9rxY1qCcAAbeo0.jpg","url":"http:\/\/t.co\/2MWtXbYhf0","display_url":"pic.twitter.com\/2MWtXbYhf0","expanded_url":"http:\/\/twitter.com\/frappuccino\/status\/566030116182949888\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":191,"resize":"fit"},"medium":{"w":600,"h":337,"resize":"fit"},"large":{"w":1024,"h":576,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"possibly_sensitive_appealable":false,"lang":"en"}'
http_version:
recorded_at: Fri, 22 May 2015 23:57:30 GMT
- request:
method: head
uri: http://pbs.twimg.com/media/B9rxY1qCcAAbeo0.jpg:orig
body:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- crystal/2.80.0
Accept:
- "*/*"
response:
status:
code: 200
message: OK
headers:
Cache-Control:
- max-age=604800, must-revalidate
Content-Md5:
- owAkLGZeD7NZTChJ3z0/EA==
Content-Type:
- image/jpeg
Last-Modified:
- Fri, 13 Feb 2015 00:24:29 GMT
Server:
- tsa_a
X-Connection-Hash:
- ca604a6bf0ac6cf10bfa24318305b274
X-Response-Time:
- '94'
Content-Length:
- '56808'
Accept-Ranges:
- bytes
Date:
- Fri, 22 May 2015 23:57:30 GMT
Via:
- 1.1 varnish
Age:
- '0'
Connection:
- keep-alive
X-Served-By:
- cache-tw-sjc1-cr1-14-TWSJC1
X-Cache:
- MISS
X-Cache-Hits:
- '0'
Expires:
- Sat, 06 Jun 2015 23:57:30 GMT
X-Content-Type-Options:
- nosniff
body:
encoding: UTF-8
string: ''
http_version:
recorded_at: Fri, 22 May 2015 23:57:31 GMT
recorded_with: VCR 2.9.0

View File

@@ -35,6 +35,18 @@ class UploadsControllerTest < ActionController::TestCase
assert_response :success
end
context "for a twitter post" do
setup do
VCR.use_cassette("upload-new-twitter", :record => :once) do
get :new, {:url => "https://twitter.com/frappuccino/status/566030116182949888"}, {:user_id => @user.id}
end
end
should "render" do
assert_response :success
end
end
context "for a post that has already been uploaded" do
setup do
@post = FactoryGirl.create(:post, :source => "aaa")

View File

@@ -79,6 +79,11 @@ module Sources
assert_contains(pixiv_links, /search\.php/)
end
should "get the artist commentary" do
assert_not_nil(@site.artist_commentary_title)
assert_not_nil(@site.artist_commentary_desc)
end
should "convert a page into a json representation" do
assert_nothing_raised do
@site.to_json

View File

@@ -44,6 +44,10 @@ module Sources
assert_equal([], @site.tags)
end
should "get the artist commentary" do
assert_not_nil(@site.artist_commentary_desc)
end
should "convert a page into a json representation" do
assert_nothing_raised do
@site.to_json

View File

@@ -217,6 +217,27 @@ class UploadTest < ActiveSupport::TestCase
end
end
context "with an artist commentary" do
setup do
@upload = FactoryGirl.create(:source_upload,
:rating => "s",
:uploader_ip_addr => "127.0.0.1",
:tag_string => "hoge foo"
)
@upload.include_artist_commentary = "1"
@upload.artist_commentary_title = ""
@upload.artist_commentary_desc = "blah"
end
should "create an artist commentary when processed" do
VCR.use_cassette("upload-test-file", :record => :none) do
assert_difference("ArtistCommentary.count") do
@upload.process!
end
end
end
end
should "process completely for a downloaded image" do
@upload = FactoryGirl.create(:source_upload,
:rating => "s",