nijie: fetch image_urls for batch bookmarklet.

This commit is contained in:
evazion
2017-06-20 16:09:57 -05:00
parent 2d5fc191dd
commit 294358b4a6
2 changed files with 40 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
module Sources
module Strategies
class Nijie < Base
attr_reader :image_urls
def self.url_match?(url)
url =~ /^https?:\/\/(?:.+?\.)?nijie\.info/
end
@@ -26,6 +28,10 @@ module Sources
"nijie" + $1.to_s
end
def image_url
image_urls.first
end
def get
page = agent.get(referer_url)
@@ -37,7 +43,7 @@ module Sources
end
@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)
@artist_commentary_title, @artist_commentary_desc = get_commentary_from_page(page)
end
@@ -87,13 +93,12 @@ module Sources
return [artist_name, profile_url].compact
end
def get_image_url_from_page(page)
image = page.search("div#gallery a img")
if image.any?
image[0]["src"].try(:sub, %r!^//!, "http://")
else
nil
def get_image_urls_from_page(page)
page.search("div#gallery a > img").map do |img|
# //pic01.nijie.info/__rs_l120x120/nijie_picture/diff/main/218856_0_236014_20170620101329.png
# => https://pic01.nijie.info/__rs_l120x120/nijie_picture/diff/main/218856_0_236014_20170620101329.png
url = "https:" + img.attr("src")
normalize_image_url(url)
end
end
@@ -121,6 +126,17 @@ module Sources
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
@agent ||= begin
mech = Mechanize.new