Skip to content

Commit

Permalink
Merge pull request #37 from r888888888/master
Browse files Browse the repository at this point in the history
Merg
  • Loading branch information
Iratu committed Jun 21, 2017
2 parents 30f4fc2 + 73f479e commit d39393a
Show file tree
Hide file tree
Showing 27 changed files with 528 additions and 204 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -17,7 +17,7 @@ gem "delayed_job_active_record"
gem "simple_form"
gem "mechanize"
gem "whenever", :require => false
gem "sanitize", "~> 3.1.0"
gem "sanitize"
gem 'rmagick'
gem 'net-sftp'
gem 'term-ansicolor', :require => "term/ansicolor"
Expand All @@ -44,6 +44,7 @@ gem 'memoist'
gem 'daemons'
gem 'oauth2'
gem 'bootsnap'
gem 'addressable'

# needed for looser jpeg header compat
gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes"
Expand Down
21 changes: 11 additions & 10 deletions Gemfile.lock
Expand Up @@ -199,7 +199,7 @@ GEM
metaclass (0.0.4)
method_source (0.8.2)
mime-types (2.99.3)
mini_portile2 (2.1.0)
mini_portile2 (2.2.0)
minitest (5.10.1)
mocha (1.2.1)
metaclass (~> 0.0.1)
Expand All @@ -218,11 +218,11 @@ GEM
net-ssh (2.9.2)
netrc (0.10.3)
newrelic_rpm (3.13.0.299)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
nokogiri (1.7.0.1-x64-mingw32)
mini_portile2 (~> 2.1.0)
nokogumbo (1.2.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
nokogiri (1.8.0-x64-mingw32)
mini_portile2 (~> 2.2.0)
nokogumbo (1.4.13)
nokogiri
ntlm-http (0.1.1)
oauth2 (1.3.0)
Expand Down Expand Up @@ -292,10 +292,10 @@ GEM
rmagick (2.16.0)
ruby-prof (0.15.8)
rubyzip (1.1.7)
sanitize (3.1.2)
crass (~> 1.0.1)
sanitize (4.5.0)
crass (~> 1.0.2)
nokogiri (>= 1.4.4)
nokogumbo (= 1.2.0)
nokogumbo (~> 1.4.1)
sass (3.4.23)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
Expand Down Expand Up @@ -387,6 +387,7 @@ PLATFORMS
x64-mingw32

DEPENDENCIES
addressable
awesome_print
aws-sdk (~> 2)
bcrypt-ruby
Expand Down Expand Up @@ -429,7 +430,7 @@ DEPENDENCIES
ruby-imagespec!
ruby-prof
rubyzip
sanitize (~> 3.1.0)
sanitize
sass-rails
shoulda-context
shoulda-matchers
Expand Down
3 changes: 0 additions & 3 deletions app/controllers/comments_controller.rb
Expand Up @@ -81,7 +81,6 @@ def index_by_post
@posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC NULLS LAST").paginate(params[:page], :limit => 5, :search_count => params[:search])
@posts.each # hack to force rails to eager load
respond_with(@posts) do |format|
format.html {render :action => "index_by_post"}
format.xml do
render :xml => @posts.to_xml(:root => "posts")
end
Expand All @@ -91,10 +90,8 @@ def index_by_post
def index_by_comment
@comments = Comment.search(params[:search]).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@comments) do |format|
format.html {render :action => "index_by_comment"}
format.atom do
@comments = @comments.includes(:post, :creator).load
render :action => "index"
end
format.xml do
render :xml => @comments.to_xml(:root => "comments")
Expand Down
2 changes: 2 additions & 0 deletions app/logical/downloads/rewrite_strategies/base.rb
Expand Up @@ -5,6 +5,8 @@
module Downloads
module RewriteStrategies
class Base
attr_reader :url

def initialize(url = nil)
@url = url
end
Expand Down
16 changes: 6 additions & 10 deletions app/logical/downloads/rewrite_strategies/pawoo.rb
@@ -1,17 +1,13 @@
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 = response.image_url
if Sources::Strategies::Pawoo.url_match?(url)
source = Sources::Strategies::Pawoo.new(url)
source.get
url = source.image_url
elsif url =~ %r!\Ahttps?://img\.pawoo\.net/media_attachments/files/(\d+/\d+/\d+)/small/([a-z0-9]+\.\w+)\z!i
url = "https://img.pawoo.net/media_attachments/files/#{$1}/original/#{$2}"
end

return [url, headers, data]
Expand Down
15 changes: 14 additions & 1 deletion app/logical/note_sanitizer.rb
Expand Up @@ -73,7 +73,20 @@ def self.sanitize(text)
at_rules: [],
protocols: [],
properties: ALLOWED_PROPERTIES,
}
},
:transformers => method(:relativize_links),
)
end

def self.relativize_links(node:, **env)
return unless node.name == "a" && node.attribute("href")

href = node.attribute("href")
url = Addressable::URI.parse(href.value).normalize

if url.authority.in?(Danbooru.config.hostnames)
url.site = nil
href.value = url.to_s
end
end
end
165 changes: 88 additions & 77 deletions app/logical/pawoo_api_client.rb
@@ -1,81 +1,92 @@
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
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

def tags
json["tags"].map { |tag| [tag["name"], tag["url"]] }
end

def commentary
commentary = ""
commentary << "<p>#{json["spoiler_text"]}</p>" if json["spoiler_text"].present?
commentary << json["content"]
commentary
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?

Cache.get("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
6 changes: 3 additions & 3 deletions app/logical/pixiv_api_client.rb
Expand Up @@ -94,8 +94,8 @@ def initialize(json)
@user_id = json["user"]["id"]
@moniker = json["user"]["account"]
@page_count = json["page_count"].to_i
@artist_commentary_title = json["title"]
@artist_commentary_desc = json["caption"]
@artist_commentary_title = json["title"].to_s
@artist_commentary_desc = json["caption"].to_s
@tags = [json["tags"], json["tools"]].flatten.compact.reject {|x| x =~ /^http:/}

if page_count > 1
Expand Down Expand Up @@ -173,4 +173,4 @@ def access_token
access_token
end
end
end
end
4 changes: 3 additions & 1 deletion app/logical/sources/site.rb
Expand Up @@ -55,7 +55,9 @@ def translated_tags
end
end
untranslated_tags.reject! {|x| x.blank?}
WikiPage.other_names_equal(untranslated_tags).map{|wiki_page| [wiki_page.title, wiki_page.category_name]}
wikis = WikiPage.title_in(untranslated_tags)
wikis += WikiPage.other_names_equal(untranslated_tags)
wikis.uniq.map{|wiki_page| [wiki_page.title, wiki_page.category_name]}
end

def to_h
Expand Down

0 comments on commit d39393a

Please sign in to comment.