add pixiv id search

This commit is contained in:
albert
2013-03-31 14:43:54 -04:00
parent fc32a0023c
commit e2c2a8309d
7 changed files with 55 additions and 15 deletions

View File

@@ -124,7 +124,7 @@ class PostQueryBuilder
relation = add_range_relation(q[:copyright_tag_count], "posts.tag_count_copyright", relation)
relation = add_range_relation(q[:character_tag_count], "posts.tag_count_character", relation)
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
# relation = add_range_relation(q[:pixiv_id], "substring(posts.source, 'pixiv.net/img.*/([0-9]+)[^/]*$')::integer", relation)
relation = add_range_relation(q[:pixiv_id], "posts.pixiv_id", relation)
if q[:md5]
relation = relation.where(["posts.md5 IN (?)", q[:md5]])

View File

@@ -15,6 +15,7 @@ class Post < ActiveRecord::Base
before_save :update_tag_post_counts
before_save :set_tag_counts
before_validation :initialize_uploader, :on => :create
before_validation :parse_pixiv_id
belongs_to :updater, :class_name => "User"
belongs_to :approver, :class_name => "User"
belongs_to :uploader, :class_name => "User"
@@ -1011,7 +1012,21 @@ class Post < ActiveRecord::Base
q
end
end
module PixivMethods
def parse_pixiv_id
if source =~ %r!http://i\d\.pixiv\.net/img-inf/img/\d+/\d+/\d+/\d+/\d+/\d+/(\d+)_s.jpg!
self.pixiv_id = $1
elsif source =~ %r!http://i\d\.pixiv\.net/img\d+/img/[^\/]+/(\d+)!
self.pixiv_id = $1
elsif source =~ /pixiv\.net/ && source =~ /illust_id=(\d+)/
self.pixiv_id = $1
else
self.pixiv_id = nil
end
end
end
include FileMethods
include ImageMethods
include ApprovalMethods
@@ -1029,6 +1044,7 @@ class Post < ActiveRecord::Base
include NoteMethods
include ApiMethods
extend SearchMethods
include PixivMethods
def reload(options = nil)
super

View File

@@ -1,5 +1,5 @@
class Tag < ActiveRecord::Base
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|-pool|pool|-fav|fav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|score|filesize|source|id|date|order|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv"
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|-pool|pool|-fav|fav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|score|filesize|source|id|date|order|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv_id"
attr_accessible :category
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
@@ -370,8 +370,8 @@ class Tag < ActiveRecord::Base
when "status"
q[:status] = $2.downcase
when "pixiv"
q[:pixiv] = parse_helper($2)
when "pixiv_id"
q[:pixiv_id] = parse_helper($2)
end

View File

@@ -0,0 +1,12 @@
class AddPixivIdToPosts < ActiveRecord::Migration
def up
execute "set statement_timeout = 0"
add_column :posts, :pixiv_id, :integer
execute "create index index_posts_on_pixiv_id on posts (pixiv_id) where pixiv_id is not null"
end
def down
execute "set statement_timeout = 0"
remove_column :posts, :pixiv_id
end
end

View File

@@ -2330,7 +2330,8 @@ CREATE TABLE posts (
has_children boolean DEFAULT false NOT NULL,
is_banned boolean DEFAULT false NOT NULL,
up_score integer,
down_score integer
down_score integer,
pixiv_id integer
);
@@ -5993,7 +5994,7 @@ CREATE INDEX index_posts_on_parent_id ON posts USING btree (parent_id);
-- Name: index_posts_on_pixiv_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_posts_on_pixiv_id ON posts USING btree ((("substring"((source)::text, 'pixiv.net/img.*/([0-9]+)[^/]*$'::text))::integer));
CREATE INDEX index_posts_on_pixiv_id ON posts USING btree (pixiv_id) WHERE (pixiv_id IS NOT NULL);
--
@@ -6410,4 +6411,6 @@ INSERT INTO schema_migrations (version) VALUES ('20130326035904');
INSERT INTO schema_migrations (version) VALUES ('20130328092739');
INSERT INTO schema_migrations (version) VALUES ('20130331180246');
INSERT INTO schema_migrations (version) VALUES ('20130331180246');
INSERT INTO schema_migrations (version) VALUES ('20130331182719');

View File

@@ -2,5 +2,14 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
kv = KeyValue.find_or_create_by_key("ApiCacheGenerator.generate_tag_cache")
kv.update_attribute(:value, "0")
1.upto(2) do |i|
Post.where("source like ?", "http://i#{i}.pixiv.net%").find_each do |post|
post.parse_pixiv_id
post.update_column(:pixiv_id, post.pixiv_id) if post.pixiv_id.present?
end
end
Post.where("source like ?", "http://www.pixiv.net%").find_each do |post|
post.parse_pixiv_id
post.update_column(:pixiv_id, post.pixiv_id) if post.pixiv_id.present?
end

View File

@@ -965,11 +965,11 @@ class PostTest < ActiveSupport::TestCase
assert_equal(1, Post.tag_match("pixiv_id:34551381").count)
end
should "return posts for a pixiv novel id search" do
url = "http://www.pixiv.net/novel/show.php?id=2156088"
post = FactoryGirl.create(:post, :source => url)
assert_equal(1, Post.tag_match("pixiv_novel_id:2156088").count)
end
# should "return posts for a pixiv novel id search" do
# url = "http://www.pixiv.net/novel/show.php?id=2156088"
# post = FactoryGirl.create(:post, :source => url)
# assert_equal(1, Post.tag_match("pixiv_novel_id:2156088").count)
# end
should "return posts for a tag subscription search" do
post1 = FactoryGirl.create(:post, :tag_string => "aaa")