From cd0aa75dbc1e719ab9521951ecce76a0d998705c Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 24 Feb 2010 16:00:52 -0500 Subject: [PATCH] added pixiv proxy, report mailer --- app/logical/pixiv_proxy.rb | 94 +++++++++++++++++++++++++++++++++++++ app/models/comment.rb | 3 +- app/models/note.rb | 4 +- app/models/post.rb | 15 ++++++ app/models/report_mailer.rb | 7 +++ 5 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 app/logical/pixiv_proxy.rb create mode 100644 app/models/report_mailer.rb diff --git a/app/logical/pixiv_proxy.rb b/app/logical/pixiv_proxy.rb new file mode 100644 index 000000000..9496776f3 --- /dev/null +++ b/app/logical/pixiv_proxy.rb @@ -0,0 +1,94 @@ +class PixivProxy + def self.is_pixiv?(url) + url =~ /pixiv\.net/ + end + + def self.get(url) + if url =~ /\/(\d+)(_m)?\.(jpg|jpeg|png|gif)/i + url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{$1}" + get_single(url) + elsif url =~ /member_illust\.php/ && url =~ /illust_id=/ + get_single(url) + # elsif url =~ /member_illust\.php/ && url =~ /id=/ + # get_listing(url) + # elsif url =~ /member\.php/ && url =~ /id=/ + # get_profile(url) + else + {} + end + end + + def self.get_profile(url) + url = URI.parse(url).request_uri + mech = create_mechanize + hash = {} + mech.get(url) do |page| + hash[:artist] = page.search("div#profile/div/a/img").attr("alt") + hash[:listing_url] = "/member_illust.php?id=" + url[/id=(\d+)/, 1] + end + hash + end + + def self.get_single(url) + url = URI.parse(url).request_uri + mech = create_mechanize + hash = {} + mech.get(url) do |page| + if page.search("div#profile/div/a/img") + hash[:artist] = page.search("div#profile/div/a/img").attr("alt") + hash[:image_url] = page.search("div#profile/div/a/img").attr("src").sub("_m.", ".") + hash[:profile_url] = page.search("div#profile/div/a").attr("href") + hash[:jp_tags] = page.search("div#tag_area/span#tags/a").map do |node| + [node.inner_text, node.attribute("href").to_s] + end + else + hash[:artist] = "?" + hash[:image_url] = "?" + hash[:profile_url] = "?" + hash[:jp_tags] = [] + end + end + hash + end + + def self.get_listing(url) + mech = create_mechanize + p = 1 + url = URI.parse(url).request_uri.sub(/&p=\d+/, "") + "&p=1" + more = true + images = [] + + while more + mech.get(url) do |page| + links = page.search("div#illust_c4/ul/li/a") + + if links.empty? + more = false + else + images += links.map do |node| + image_src = node.child.attribute("src").to_s + [image_src, image_src.sub("_s.", "."), node.attribute("href").to_s] + end + end + + p += 1 + url.sub!(/&p=\d+/, "&p=#{p}") + end + end + + images + end + + def self.create_mechanize + mech = WWW::Mechanize.new + + mech.get("http://www.pixiv.net") do |page| + page.form_with(:action => "index.php") do |form| + form.pixiv_id = "uroobnad" + form.pass = "uroobnad556" + end.click_button + end + + mech + end +end diff --git a/app/models/comment.rb b/app/models/comment.rb index 78bea1844..1d193ae96 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,8 @@ class Comment < ActiveRecord::Base attr_accessor :do_not_bump_post scope :recent, :order => "comments.id desc", :limit => 6 - scope :search_body, lambda {|query| {:conditions => ["body_index @@ plainto_tsquery(?)", query], :order => "id desc"}} + scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query)} + scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)} def update_last_commented_at return if do_not_bump_post diff --git a/app/models/note.rb b/app/models/note.rb index db794d122..113fd6f30 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -42,9 +42,9 @@ class Note < ActiveRecord::Base def update_post if Note.exists?(["is_active = ? AND post_id = ?", true, post_id]) - Post.update(post_id, :last_noted_at => updated_at, :updater_id => updater_id, :updater_ip_addr => updater_ip_addr) + execute_sql("UPDATE posts SET last_noted_at = ? WHERE id = ?", updated_at, post_id) else - Post.update(post_id, :last_noted_at => nil, :updater_id => updater_id, :updater_ip_addr => updater_ip_addr) + execute_sql("UPDATE posts SET last_noted_at = NULL WHERE id = ?", post_id) end end diff --git a/app/models/post.rb b/app/models/post.rb index eaf1caf39..2959d2f60 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -499,6 +499,20 @@ class Post < ActiveRecord::Base end end + module CountMethods + def fast_count(tags) + Cache.get("pfc:#{Cache.sanitize(tags)}", 24.hours) do + Post.find_by_tags(tags).count + end + end + + def fast_delete_count(tags) + Cache.get("pfdc:#{Cache.sanitize(tags)}", 24.hours) do + Post.find_by_tags("#{tags} status:deleted").count + end + end + end + include FileMethods include ImageMethods include ModerationMethods @@ -510,6 +524,7 @@ class Post < ActiveRecord::Base include PoolMethods extend SearchMethods include VoteMethods + extend CountMethods def reload(options = nil) super diff --git a/app/models/report_mailer.rb b/app/models/report_mailer.rb new file mode 100644 index 000000000..5f8bad38b --- /dev/null +++ b/app/models/report_mailer.rb @@ -0,0 +1,7 @@ +class ReportMailer < ActionMailer::Base + default :host => Danbooru.config.server_host, :from => Danbooru.config.contact_email, :content_type => "text/html" + + def moderator_report(email) + mail(:to => email, :subject => "#{Danbooru.config.app_name} - Moderator Report") + end +end