nijie: fetch image_urls for batch bookmarklet.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
module Sources
|
module Sources
|
||||||
module Strategies
|
module Strategies
|
||||||
class Nijie < Base
|
class Nijie < Base
|
||||||
|
attr_reader :image_urls
|
||||||
|
|
||||||
def self.url_match?(url)
|
def self.url_match?(url)
|
||||||
url =~ /^https?:\/\/(?:.+?\.)?nijie\.info/
|
url =~ /^https?:\/\/(?:.+?\.)?nijie\.info/
|
||||||
end
|
end
|
||||||
@@ -26,6 +28,10 @@ module Sources
|
|||||||
"nijie" + $1.to_s
|
"nijie" + $1.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def image_url
|
||||||
|
image_urls.first
|
||||||
|
end
|
||||||
|
|
||||||
def get
|
def get
|
||||||
page = agent.get(referer_url)
|
page = agent.get(referer_url)
|
||||||
|
|
||||||
@@ -37,7 +43,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
@artist_name, @profile_url = get_profile_from_page(page)
|
@artist_name, @profile_url = get_profile_from_page(page)
|
||||||
@image_url = get_image_url_from_page(page)
|
@image_urls = get_image_urls_from_page(page)
|
||||||
@tags = get_tags_from_page(page)
|
@tags = get_tags_from_page(page)
|
||||||
@artist_commentary_title, @artist_commentary_desc = get_commentary_from_page(page)
|
@artist_commentary_title, @artist_commentary_desc = get_commentary_from_page(page)
|
||||||
end
|
end
|
||||||
@@ -87,13 +93,12 @@ module Sources
|
|||||||
return [artist_name, profile_url].compact
|
return [artist_name, profile_url].compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_image_url_from_page(page)
|
def get_image_urls_from_page(page)
|
||||||
image = page.search("div#gallery a img")
|
page.search("div#gallery a > img").map do |img|
|
||||||
|
# //pic01.nijie.info/__rs_l120x120/nijie_picture/diff/main/218856_0_236014_20170620101329.png
|
||||||
if image.any?
|
# => https://pic01.nijie.info/__rs_l120x120/nijie_picture/diff/main/218856_0_236014_20170620101329.png
|
||||||
image[0]["src"].try(:sub, %r!^//!, "http://")
|
url = "https:" + img.attr("src")
|
||||||
else
|
normalize_image_url(url)
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -121,6 +126,17 @@ module Sources
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize_image_url(image_url)
|
||||||
|
# http://pic03.nijie.info/__rs_l120x120/nijie_picture/diff/main/218856_0_236014_20170620101329.png
|
||||||
|
# => http://pic03.nijie.info/nijie_picture/diff/main/218856_3_236014_20170620101331.png
|
||||||
|
if image_url =~ %r!\Ahttps?://pic\d+\.nijie\.info/__rs_l120x120/nijie_picture/diff/main/[0-9_]+\.\w+\z!i
|
||||||
|
image_url = image_url.gsub(%r!__rs_l120x120/!i, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
image_url = image_url.gsub(%r!\Ahttp:!i, "https:")
|
||||||
|
image_url
|
||||||
|
end
|
||||||
|
|
||||||
def agent
|
def agent
|
||||||
@agent ||= begin
|
@agent ||= begin
|
||||||
mech = Mechanize.new
|
mech = Mechanize.new
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "get the image url" do
|
should "get the image url" do
|
||||||
assert_equal("http://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
assert_equal("https://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "get the profile" do
|
should "get the profile" do
|
||||||
@@ -40,7 +40,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "get the image url" do
|
should "get the image url" do
|
||||||
assert_equal("http://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
assert_equal("https://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "get the profile" do
|
should "get the profile" do
|
||||||
@@ -59,7 +59,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "get the image url" do
|
should "get the image url" do
|
||||||
assert_equal("http://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
assert_equal("https://pic03.nijie.info/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "get the profile" do
|
should "get the profile" do
|
||||||
@@ -77,6 +77,19 @@ module Sources
|
|||||||
@site.get
|
@site.get
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "get the image urls" do
|
||||||
|
urls = %w[
|
||||||
|
https://pic03.nijie.info/nijie_picture/236014_20170620101426_0.png
|
||||||
|
https://pic01.nijie.info/nijie_picture/diff/main/218856_0_236014_20170620101329.png
|
||||||
|
https://pic01.nijie.info/nijie_picture/diff/main/218856_1_236014_20170620101330.png
|
||||||
|
https://pic01.nijie.info/nijie_picture/diff/main/218856_2_236014_20170620101331.png
|
||||||
|
https://pic03.nijie.info/nijie_picture/diff/main/218856_3_236014_20170620101331.png
|
||||||
|
https://pic03.nijie.info/nijie_picture/diff/main/218856_4_236014_20170620101333.png
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_equal(urls, @site.image_urls)
|
||||||
|
end
|
||||||
|
|
||||||
should "get the dtext-ified commentary" do
|
should "get the dtext-ified commentary" do
|
||||||
desc = <<-EOS.strip_heredoc.chomp
|
desc = <<-EOS.strip_heredoc.chomp
|
||||||
foo [b]bold[/b] [i]italics[/i] [s]strike[/s] red\r
|
foo [b]bold[/b] [i]italics[/i] [s]strike[/s] red\r
|
||||||
|
|||||||
Reference in New Issue
Block a user