Fixes #32: Setting a default images size in the user profile does not seem to work
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
class PixivProxy
|
||||
class PixivProxy < ActiveRecord::Base
|
||||
def self.is_pixiv?(url)
|
||||
url =~ /pixiv\.net/
|
||||
end
|
||||
@@ -9,24 +9,47 @@ class PixivProxy
|
||||
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("a.avatar_m").attr("title").value
|
||||
hash[:listing_url] = "/member_illust.php?id=" + url[/id=(\d+)/, 1]
|
||||
def self.get_profile_from_page(page)
|
||||
links = page.search("div.front-subContent a").find_all do |node|
|
||||
node["href"] =~ /member\.php/
|
||||
end
|
||||
|
||||
if links.any?
|
||||
profile_url = links[0]["href"]
|
||||
children = links[0].children
|
||||
artist = children[0]["alt"]
|
||||
return [artist, profile_url]
|
||||
else
|
||||
return []
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_image_url_from_page(page)
|
||||
meta = page.search("meta[property=\"og:image\"]").first
|
||||
if meta
|
||||
meta.attr("content").sub(/_m\./, ".")
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_jp_tags_from_page(page)
|
||||
links = page.search("div.pedia li a").find_all do |node|
|
||||
node["href"] =~ /tags\.php/
|
||||
end
|
||||
|
||||
if links.any?
|
||||
links.map do |node|
|
||||
[node.inner_text, node.attr("href")]
|
||||
end
|
||||
else
|
||||
[]
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
def self.get_single(url)
|
||||
@@ -34,58 +57,26 @@ class PixivProxy
|
||||
mech = create_mechanize
|
||||
hash = {}
|
||||
mech.get(url) do |page|
|
||||
if page.search("a.avatar_m")
|
||||
hash[:artist] = page.search("a.avatar_m").attr("title").value
|
||||
hash[:image_url] = page.search("div.works_display/a/img").attr("src").value.sub("_m.", ".")
|
||||
hash[:profile_url] = page.search("a.avatar_m").attr("href").value
|
||||
hash[:jp_tags] = page.search("span#tags/a").map do |node|
|
||||
[node.inner_text, node.attribute("href").to_s]
|
||||
end.reject {|x| x[0].empty?}
|
||||
else
|
||||
hash[:artist] = "?"
|
||||
hash[:image_url] = "?"
|
||||
hash[:profile_url] = "?"
|
||||
hash[:jp_tags] = []
|
||||
end
|
||||
artist, profile_url = get_profile_from_page(page)
|
||||
image_url = get_image_url_from_page(page)
|
||||
jp_tags = get_jp_tags_from_page(page)
|
||||
|
||||
hash[:artist] = artist
|
||||
hash[:profile_url] = profile_url
|
||||
hash[:image_url] = image_url
|
||||
hash[:jp_tags] = jp_tags
|
||||
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 = Mechanize.new
|
||||
|
||||
mech.get("http://www.pixiv.net") do |page|
|
||||
page.form_with(:action => "/login.php") do |form|
|
||||
form.pixiv_id = "uroobnad"
|
||||
form.pass = "uroobnad556"
|
||||
form['mode'] = "login"
|
||||
form['login_pixiv_id'] = "uroobnad"
|
||||
form['pass'] = "uroobnad556"
|
||||
end.click_button
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user