add source + rewrite strategy for pawoo
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -42,6 +42,7 @@ gem 'bigquery', :git => "https://github.com/abronte/BigQuery.git", :ref => "b92b
|
|||||||
gem 'memcache_mock'
|
gem 'memcache_mock'
|
||||||
gem 'memoist'
|
gem 'memoist'
|
||||||
gem 'daemons'
|
gem 'daemons'
|
||||||
|
gem 'oauth2'
|
||||||
|
|
||||||
# needed for looser jpeg header compat
|
# needed for looser jpeg header compat
|
||||||
gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes"
|
gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes"
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ GEM
|
|||||||
mocha (1.2.1)
|
mocha (1.2.1)
|
||||||
metaclass (~> 0.0.1)
|
metaclass (~> 0.0.1)
|
||||||
multi_json (1.12.1)
|
multi_json (1.12.1)
|
||||||
|
multi_xml (0.6.0)
|
||||||
multipart-post (2.0.0)
|
multipart-post (2.0.0)
|
||||||
naught (1.0.0)
|
naught (1.0.0)
|
||||||
net-http-digest_auth (1.4.1)
|
net-http-digest_auth (1.4.1)
|
||||||
@@ -216,6 +217,12 @@ GEM
|
|||||||
nokogumbo (1.2.0)
|
nokogumbo (1.2.0)
|
||||||
nokogiri
|
nokogiri
|
||||||
ntlm-http (0.1.1)
|
ntlm-http (0.1.1)
|
||||||
|
oauth2 (1.3.0)
|
||||||
|
faraday (>= 0.8, < 0.11)
|
||||||
|
jwt (~> 1.0)
|
||||||
|
multi_json (~> 1.3)
|
||||||
|
multi_xml (~> 0.5)
|
||||||
|
rack (>= 1.2, < 3)
|
||||||
os (0.9.6)
|
os (0.9.6)
|
||||||
pg (0.19.0)
|
pg (0.19.0)
|
||||||
pg (0.19.0-x64-mingw32)
|
pg (0.19.0-x64-mingw32)
|
||||||
@@ -401,6 +408,7 @@ DEPENDENCIES
|
|||||||
mocha
|
mocha
|
||||||
net-sftp
|
net-sftp
|
||||||
newrelic_rpm
|
newrelic_rpm
|
||||||
|
oauth2
|
||||||
pg
|
pg
|
||||||
protected_attributes
|
protected_attributes
|
||||||
pry-byebug
|
pry-byebug
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ module Downloads
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.strategies
|
def self.strategies
|
||||||
[Downloads::RewriteStrategies::Pixiv, Downloads::RewriteStrategies::NicoSeiga, Downloads::RewriteStrategies::ArtStation, Downloads::RewriteStrategies::Twitpic, Downloads::RewriteStrategies::DeviantArt, Downloads::RewriteStrategies::Tumblr, Downloads::RewriteStrategies::Moebooru, Downloads::RewriteStrategies::Twitter, Downloads::RewriteStrategies::Nijie]
|
[Downloads::RewriteStrategies::Pixiv, Downloads::RewriteStrategies::NicoSeiga, Downloads::RewriteStrategies::ArtStation, Downloads::RewriteStrategies::Twitpic, Downloads::RewriteStrategies::DeviantArt, Downloads::RewriteStrategies::Tumblr, Downloads::RewriteStrategies::Moebooru, Downloads::RewriteStrategies::Twitter, Downloads::RewriteStrategies::Nijie, Downloads::RewriteStrategies::Pawoo]
|
||||||
end
|
end
|
||||||
|
|
||||||
def rewrite(url, headers, data = {})
|
def rewrite(url, headers, data = {})
|
||||||
|
|||||||
21
app/logical/downloads/rewrite_strategies/pawoo.rb
Normal file
21
app/logical/downloads/rewrite_strategies/pawoo.rb
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
module Downloads
|
||||||
|
module RewriteStrategies
|
||||||
|
class Pawoo < Base
|
||||||
|
attr_accessor :url, :source
|
||||||
|
|
||||||
|
def initialize(url)
|
||||||
|
@url = url
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewrite(url, headers, data = {})
|
||||||
|
if PawooApiClient::Status.is_match?(url)
|
||||||
|
client = PawooApiClient.new
|
||||||
|
response = client.get_status(url)
|
||||||
|
url = client.image_url
|
||||||
|
end
|
||||||
|
|
||||||
|
return [url, headers, data]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
81
app/logical/pawoo_api_client.rb
Normal file
81
app/logical/pawoo_api_client.rb
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
class PawooApiClient
|
||||||
|
extend Memoist
|
||||||
|
|
||||||
|
class MissingConfigurationError < Exception ; end
|
||||||
|
|
||||||
|
class Account
|
||||||
|
attr_reader :json
|
||||||
|
|
||||||
|
def self.is_match?(url)
|
||||||
|
url =~ %r!https://pawoo.net/web/accounts/(\d+)!
|
||||||
|
$1
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(json)
|
||||||
|
@json = get
|
||||||
|
end
|
||||||
|
|
||||||
|
def profile_url
|
||||||
|
json["url"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Status
|
||||||
|
attr_reader :json
|
||||||
|
|
||||||
|
def self.is_match?(url)
|
||||||
|
url =~ %r!https?://pawoo.net/web/statuses/(\d+)! || url =~ %r!https?://pawoo.net/@.+?/(\d+)!
|
||||||
|
$1
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(json)
|
||||||
|
@json = json
|
||||||
|
end
|
||||||
|
|
||||||
|
def account_profile_url
|
||||||
|
json["account"]["url"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def account_name
|
||||||
|
json["account"]["username"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def image_url
|
||||||
|
image_urls.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def image_urls
|
||||||
|
json["media_attachments"].map {|x| x["url"]}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_status(url)
|
||||||
|
if id = Status.is_match?(url)
|
||||||
|
Status.new(JSON.parse(access_token.get("/api/v1/statuses/#{id}").body))
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_access_token
|
||||||
|
raise MissingConfigurationError.new("missing pawoo client id") if Danbooru.config.pawoo_client_id.nil?
|
||||||
|
raise MissingConfigurationError.new("missing pawoo client secret") if Danbooru.config.pawoo_client_secret.nil?
|
||||||
|
|
||||||
|
Rails.cache.fetch("pawoo-token") do
|
||||||
|
result = client.client_credentials.get_token
|
||||||
|
result.token
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def access_token
|
||||||
|
OAuth2::AccessToken.new(client, fetch_access_token)
|
||||||
|
end
|
||||||
|
|
||||||
|
def client
|
||||||
|
OAuth2::Client.new(Danbooru.config.pawoo_client_id, Danbooru.config.pawoo_client_secret, :site => "https://pawoo.net")
|
||||||
|
end
|
||||||
|
|
||||||
|
memoize :client
|
||||||
|
end
|
||||||
@@ -10,7 +10,7 @@ module Sources
|
|||||||
:artist_commentary_desc, :rewrite_thumbnails, :illust_id_from_url, :to => :strategy
|
:artist_commentary_desc, :rewrite_thumbnails, :illust_id_from_url, :to => :strategy
|
||||||
|
|
||||||
def self.strategies
|
def self.strategies
|
||||||
[Strategies::PixivWhitecube, Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::ArtStation, Strategies::Nijie, Strategies::Twitter, Strategies::Tumblr]
|
[Strategies::PixivWhitecube, Strategies::Pixiv, Strategies::NicoSeiga, Strategies::DeviantArt, Strategies::ArtStation, Strategies::Nijie, Strategies::Twitter, Strategies::Tumblr, Strategies::Pawoo]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(url, options = {})
|
def initialize(url, options = {})
|
||||||
|
|||||||
25
app/logical/sources/strategies/pawoo.rb
Normal file
25
app/logical/sources/strategies/pawoo.rb
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
module Sources::Strategies
|
||||||
|
class Pawoo < Base
|
||||||
|
attr_reader :image_urls
|
||||||
|
|
||||||
|
def self.url_match?(url)
|
||||||
|
PawooApiClient::Status.is_match?(url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def site_name
|
||||||
|
"Pawoo"
|
||||||
|
end
|
||||||
|
|
||||||
|
def get
|
||||||
|
response = PawooApiClient.new.get_status(url)
|
||||||
|
@artist_name = response.account_name
|
||||||
|
@profile_url = response.account_profile_url
|
||||||
|
@image_url = response.image_urls.first
|
||||||
|
@image_urls = response.image_urls
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalizable_for_artist_finder?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
class AddUpdatedAtIndexToPools < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_index :pools, :updated_at
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -6682,6 +6682,13 @@ CREATE INDEX index_pools_on_name ON pools USING btree (name);
|
|||||||
CREATE INDEX index_pools_on_name_trgm ON pools USING gin (lower((name)::text) gin_trgm_ops);
|
CREATE INDEX index_pools_on_name_trgm ON pools USING gin (lower((name)::text) gin_trgm_ops);
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Name: index_pools_on_updated_at; Type: INDEX; Schema: public; Owner: -
|
||||||
|
--
|
||||||
|
|
||||||
|
CREATE INDEX index_pools_on_updated_at ON pools USING btree (updated_at);
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: index_post_appeals_on_created_at; Type: INDEX; Schema: public; Owner: -
|
-- Name: index_post_appeals_on_created_at; Type: INDEX; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
@@ -7465,5 +7472,7 @@ INSERT INTO schema_migrations (version) VALUES ('20170414005856');
|
|||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20170414233426');
|
INSERT INTO schema_migrations (version) VALUES ('20170414233426');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20170414233617');
|
||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20170416224142');
|
INSERT INTO schema_migrations (version) VALUES ('20170416224142');
|
||||||
|
|
||||||
|
|||||||
24
test/unit/sources/pawoo_test.rb
Normal file
24
test/unit/sources/pawoo_test.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module Sources
|
||||||
|
class PawooTest < ActiveSupport::TestCase
|
||||||
|
context "The source site for pawoo" do
|
||||||
|
setup do
|
||||||
|
@site = Sources::Site.new("https://pawoo.net/web/statuses/1202176")
|
||||||
|
@site.get
|
||||||
|
end
|
||||||
|
|
||||||
|
should "get the profile" do
|
||||||
|
assert_equal("https://pawoo.net/@9ed00e924818", @site.profile_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "get the artist name" do
|
||||||
|
assert_equal("9ed00e924818", @site.artist_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "get the image url" do
|
||||||
|
assert_equal("https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png?1492461815", @site.image_url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user