tinami: get the full image.

Support grabbing the full image for Tinami uploads, rather than the sample.

Getting the full image requires making a request like this:

    curl -X POST \
    -H 'Referer: https://www.tinami.com/' \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    -H 'Cookie: Tinami2SESSID=<redacted>;' \
    --data-raw 'action_view_original=true&cont_id=1087268&ethna_csrf=<redacted>' \
    https://www.tinami.com/view/1087268

Then scraping the <img> tag from the resulting HTML page.

If the post has multiple images, then we need to scrape and pass the
`sub_id` of the image too.

Fixes #2818.
This commit is contained in:
evazion
2022-03-19 23:22:09 -05:00
parent 0ddc09f011
commit 7f58cfbe5e
4 changed files with 62 additions and 17 deletions

View File

@@ -56,6 +56,7 @@ jobs:
DANBOORU_BARAAG_CLIENT_ID: ${{ secrets.DANBOORU_BARAAG_CLIENT_ID }}
DANBOORU_BARAAG_CLIENT_SECRET: ${{ secrets.DANBOORU_BARAAG_CLIENT_SECRET }}
DANBOORU_FANTIA_SESSION_ID: ${{ secrets.DANBOORU_FANTIA_SESSION_ID }}
DANBOORU_TINAMI_SESSION_ID: ${{ secrets.DANBOORU_TINAMI_SESSION_ID }}
DANBOORU_DISCORD_WEBHOOK_ID: ${{ secrets.DANBOORU_DISCORD_WEBHOOK_ID }}
DANBOORU_DISCORD_WEBHOOK_SECRET: ${{ secrets.DANBOORU_DISCORD_WEBHOOK_SECRET }}
DANBOORU_RAKISMET_KEY: ${{ secrets.DANBOORU_RAKISMET_KEY }}

View File

@@ -12,18 +12,35 @@ module Sources
def image_urls
if parsed_url.image_url?
[url]
# http://www.tinami.com/view/1087268 (single image)
elsif page&.css("img.captify")&.size.to_i == 1
[full_image_url]
# http://www.tinami.com/view/1087270 (multiple images)
elsif image_sub_ids.present?
image_sub_ids.map { |sub_id| full_image_url(sub_id) }
# http://www.tinami.com/view/1087271 (multiple images)
elsif nv_body_image_urls.present?
nv_body_image_urls
# http://www.tinami.com/view/1087267 (no images, text only)
else
# Page type 1: http://www.tinami.com/view/1087268
# Page type 2: http://www.tinami.com/view/1087271
# Page type 3: http://www.tinami.com/view/1087270
# Page type 4: http://www.tinami.com/view/1087267 (no images, text only)
page&.css(".viewbody img.captify, .viewbody .nv_body img").to_a.map do |img|
# img[:src] == "//img.tinami.com/illust2/img/619/6234b647da609.jpg"
"https:#{img[:src]}"
end
[]
end
end
def nv_body_image_urls
page&.css(".viewbody .nv_body img").to_a.map do |img|
"https:#{img[:src]}" # img[:src] == "//img.tinami.com/illust2/img/619/6234b647da609.jpg"
end
end
def image_sub_ids
page&.css(".viewbody #controller_model .thumbnail_list").to_a.map { |td| td.attr("sub_id") }
end
def page_url
parsed_url.page_url || parsed_referer&.page_url
end
@@ -59,6 +76,24 @@ module Sources
Source::URL.parse(url)&.user_id
end
def work_id
parsed_url.work_id || parsed_referer&.work_id
end
def ethna_csrf
page&.at("#open_original_content input[name=ethna_csrf]")&.attr("value")
end
def full_image_url(sub_id = nil)
return nil unless work_id.present? && ethna_csrf.present?
# Note that we have to spoof the Referer here.
response = http.post(page_url, form: { action_view_original: true, cont_id: work_id, sub_id: sub_id, ethna_csrf: ethna_csrf })
return nil unless response.status == 200
response.parse.at("body > div > a > img[src^='//img.tinami.com']")&.attr("src")&.prepend("https:")
end
def page
return nil if page_url.blank?
@@ -68,7 +103,11 @@ module Sources
response.parse
end
memoize :page, :user_id
def http
super.cookies(Tinami2SESSID: Danbooru.config.tinami_session_id).use(:spoof_referrer)
end
memoize :http, :page, :user_id, :work_id, :ethna_csrf, :image_urls, :image_sub_ids, :nv_body_image_urls
end
end
end

View File

@@ -305,6 +305,11 @@ module Danbooru
nil
end
# Your Tinami "Tinami2SESSID" cookie. Login to Tinami then use the devtools to find the "Tinami2SESSID" cookie.
def tinami_session_id
nil
end
# 1. Register app at https://www.tumblr.com/oauth/register.
# 2. Copy "OAuth Consumer Key" from https://www.tumblr.com/oauth/apps.
def tumblr_consumer_key

View File

@@ -9,7 +9,7 @@ module Sources
assert_equal("みぐめ", source.artist_name)
assert_equal("https://www.tinami.com/view/1087268", source.page_url)
assert_equal(["https://img.tinami.com/illust2/img/647/6234fe5588e97.jpg"], source.image_urls)
assert_equal(["https://img.tinami.com/illust2/img/49/6234fe552348b.jpg"], source.image_urls)
assert_equal("https://www.tinami.com/creator/profile/66493", source.profile_url)
assert_equal(%w[横顔 アナログ ボールペン SP], source.tags.map(&:first))
assert_equal("横顔", source.artist_commentary_title)
@@ -42,13 +42,13 @@ module Sources
assert_equal("セラ箱", source.artist_name)
assert_equal("https://www.tinami.com/view/1087270", source.page_url)
assert_equal(%w[
https://img.tinami.com/illust2/img/934/623503bb9891b.jpg
https://img.tinami.com/illust2/img/398/623503bd481bb.jpg
https://img.tinami.com/illust2/img/698/623503bec2105.jpg
https://img.tinami.com/illust2/img/981/623503c029fbf.jpg
https://img.tinami.com/illust2/img/769/623503c187eab.jpg
https://img.tinami.com/illust2/img/847/623503c2dd8d6.jpg
https://img.tinami.com/illust2/img/252/623503c434204.jpg
https://img.tinami.com/illust2/img/399/623503bb2c686.jpg
https://img.tinami.com/illust2/img/505/623503bdd064e.jpg
https://img.tinami.com/illust2/img/140/623503bf50d20.jpg
https://img.tinami.com/illust2/img/986/623503c0940f5.jpg
https://img.tinami.com/illust2/img/954/623503c219ee9.jpg
https://img.tinami.com/illust2/img/655/623503c3646c0.jpg
https://img.tinami.com/illust2/img/401/623503c4b8171.jpg
], source.image_urls)
assert_equal("https://www.tinami.com/creator/profile/38168", source.profile_url)
assert_equal(%w[Re:ゼロから始める異世界生活 レム リゼロ セラ箱 rizero フィギュア リペイント], source.tags.map(&:first))