Files
danbooru/app/logical/source/url/lofter.rb
evazion 3aa5cab2aa sources: refactor normalize_for_source.
`normalize_for_source` was used to convert image URLs to page URLs when displaying sources
on the post show page. Move all the code for converting image URLs to page URLs from
`Sources::Strategies#normalize_for_source` to `Source::URL#page_url`.

Before we had to be very careful in source strategies not to make any network calls in
`normalize_for_source`, since it was used in the view for the post show page. Now all the
code for generating page URLs is isolated in Source::URL, which makes source strategies
simpler. It also makes it easier to check if a source is an image URL or page URL, and if
the image URL is convertible to a page URL, which will make autotagging bad_link or
bad_source feasible.

Finally, this fixes it to generate better page URLs in a handful of cases:

* https://www.artstation.com/artwork/qPVGP instead of https://anubis1982918.artstation.com/projects/qPVGP
* https://yande.re/post/show?md5=b4b1d11facd1700544554e4805d47bb6s instead of https://yande.re/post?tags=md5:b4b1d11facd1700544554e4805d47bb6
* http://gallery.minitokyo.net/view/365677 instead of http://gallery.minitokyo.net/download/365677
* https://valkyriecrusade.fandom.com/wiki/File:Crimson_Hatsune_H.png instead of https://valkyriecrusade.wikia.com/wiki/File:Crimson_Hatsune_H.png
* https://rule34.paheal.net/post/view/852405 instead of https://rule34.paheal.net/post/list/md5:854806addcd3b1246424e7cea49afe31/1
2022-03-23 01:34:04 -05:00

63 lines
1.9 KiB
Ruby

# frozen_string_literal: true
class Source::URL::Lofter < Source::URL
RESERVED_SUBDOMAINS = %w[www.lofter.com i.lofter.com]
attr_reader :username, :work_id, :unescaped_tag
def self.match?(url)
url.domain.in?(%w[lofter.com 127.net lf127.net])
end
def parse
case [host, *path_segments]
# https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0
# https://imglf3.lf127.net/img/S1d2QlVsWkJhSW1qcnpIS0ZSa3ZJSzFCWFlnUWgzb01DcUdpT1lreG5yQjJVMkhGS09HNGR3PT0.png
# http://imglf0.nosdn.127.net/img/cHl3bXNZdDRaaHBnNWJuN1Y4OXBqR01CeVBZSVNmU2FWZWtHc1h4ZTZiUGxlRzMwZnFDM1JnPT0.jpg (404)
in /127\.net$/, "img", _
nil
# https://www.lofter.com/front/blog/home-page/noshiqian
in "www.lofter.com", "front", "blog", "home-page", username
@username = username
# http://www.lofter.com/app/xiaokonggedmx
# http://www.lofter.com/blog/semblance
in "www.lofter.com", ("app" | "blog"), username
@username = username
# https://gengar563.lofter.com/post/1e82da8c_1c98dae1b
in /^([a-z0-9-]+)\.lofter\.com$/, "post", work_id unless host.in?(RESERVED_SUBDOMAINS)
@username = $1
@work_id = work_id
# https://gengar563.lofter.com/tag/%E5%BA%9F%E5%BC%83%E7%9B%90%E9%85%B8%E5%A4%84%E7%90%86%E5%8E%82
in /^([a-z0-9-]+)\.lofter\.com$/, "tag", tag
@unescaped_tag = CGI.unescape(tag)
# http://gengar563.lofter.com
in /^([a-z0-9-]+)\.lofter\.com$/, *rest unless host.in?(RESERVED_SUBDOMAINS)
@username = $1
else
end
end
def image_url?
url.domain.in?(%w[lf127.net 127.net])
end
def full_image_url
"#{site}#{path}" if image_url?
end
def page_url
"https://#{username}.lofter.com/post/#{work_id}" if username.present? && work_id.present?
end
def profile_url
"https://#{username}.lofter.com" if username.present?
end
end