#1866: Add nico seiga support and fix various seiga bugs
* Support rewriting source when user uploads from a thumbnail url or html page url * Fix bug where site did not log in correctly * Fix bug where the image url couldn't be extracted from the page if the image was rated as adults only on seiga * Normalize direct image url to html page url so tags, etc., can be extracted
This commit is contained in:
@@ -2,7 +2,7 @@ module Downloads
|
||||
module Strategies
|
||||
class Base
|
||||
def self.strategies
|
||||
[Pixiv, Twitpic, DeviantArt, Tumblr]
|
||||
[Pixiv, NicoSeiga, Twitpic, DeviantArt, Tumblr]
|
||||
end
|
||||
|
||||
def rewrite(url, headers)
|
||||
|
||||
42
app/logical/downloads/strategies/nico_seiga.rb
Normal file
42
app/logical/downloads/strategies/nico_seiga.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
module Downloads
|
||||
module Strategies
|
||||
class NicoSeiga < Base
|
||||
def rewrite(url, headers)
|
||||
if url =~ %r{https?://lohas\.nicoseiga\.jp} || url =~ %r{https?://seiga\.nicovideo\.jp}
|
||||
url, headers = rewrite_headers(url, headers)
|
||||
url, headers = rewrite_html_pages(url, headers)
|
||||
url, headers = rewrite_thumbnails(url, headers)
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
protected
|
||||
def rewrite_headers(url, headers)
|
||||
headers["Referer"] = "http://seiga.nicovideo.jp"
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
def rewrite_html_pages(url, headers)
|
||||
# example: http://seiga.nicovideo.jp/seiga/im1389842
|
||||
|
||||
if url =~ %r{https?://seiga\.nicovideo\.jp/seiga/im\d+}
|
||||
source = ::Sources::Strategies::NicoSeiga.new(url)
|
||||
source.get
|
||||
return [source.image_url, headers]
|
||||
else
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
|
||||
def rewrite_thumbnails(url, headers)
|
||||
if url =~ %r{/thumb/(\d+)}
|
||||
id = $1
|
||||
url = "http://seiga.nicovideo.jp/image/source?id=" + id
|
||||
end
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -15,7 +15,7 @@ module Sources
|
||||
end
|
||||
|
||||
def get
|
||||
agent.get(url) do |page|
|
||||
agent.get(normalized_url) do |page|
|
||||
@artist_name, @profile_url = get_profile_from_page(page)
|
||||
@image_url = get_image_url_from_page(page)
|
||||
@tags = get_tags_from_page(page)
|
||||
@@ -39,10 +39,10 @@ module Sources
|
||||
end
|
||||
|
||||
def get_image_url_from_page(page)
|
||||
meta = page.search("meta[property='og:image']")
|
||||
link = page.search("a#illust_link")
|
||||
|
||||
if meta.any?
|
||||
meta[0]["content"]
|
||||
if link.any?
|
||||
"http://seiga.nicovideo.jp" + link[0]["href"]
|
||||
else
|
||||
nil
|
||||
end
|
||||
@@ -56,17 +56,37 @@ module Sources
|
||||
end
|
||||
end
|
||||
|
||||
def normalized_url
|
||||
@normalized_url ||= begin
|
||||
if url =~ %r{\Ahttp://lohas\.nicoseiga\.jp/priv/(\d+)\?e=\d+&h=[a-f0-9]+}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{\Ahttp://lohas\.nicoseiga\.jp/priv/[a-f0-9]+/\d+/(\d+)}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{/seiga/im\d+}
|
||||
url
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def agent
|
||||
@agent ||= begin
|
||||
mech = Mechanize.new
|
||||
|
||||
mech.get("https://secure.nicovideo.jp/secure/login_form") do |page|
|
||||
page.form_with do |form|
|
||||
form["mail"] = Danbooru.config.nico_seiga_login
|
||||
form["mail_tel"] = Danbooru.config.nico_seiga_login
|
||||
form["password"] = Danbooru.config.nico_seiga_password
|
||||
end.click_button
|
||||
end
|
||||
|
||||
# This cookie needs to be set to allow viewing of adult works
|
||||
cookie = Mechanize::Cookie.new("skip_fetish_warning", "1")
|
||||
cookie.domain = "seiga.nicovideo.jp"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user