2025-03-14 21:26:25 +01:00
|
|
|
defmodule PhilomenaProxy.Scrapers.Civitai do
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
alias PhilomenaProxy.Scrapers.Scraper
|
|
|
|
alias PhilomenaProxy.Scrapers
|
|
|
|
|
|
|
|
@behaviour Scraper
|
|
|
|
|
|
|
|
@url_regex ~r|\Ahttps?://(?:www\.)?civitai\.com/posts/([\d]+)/?|
|
|
|
|
|
|
|
|
@spec can_handle?(URI.t(), String.t()) :: boolean()
|
|
|
|
def can_handle?(_uri, url) do
|
|
|
|
String.match?(url, @url_regex)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec scrape(URI.t(), Scrapers.url()) :: Scrapers.scrape_result()
|
|
|
|
def scrape(_uri, url) do
|
|
|
|
[post_id] = Regex.run(@url_regex, url, capture: :all_but_first)
|
|
|
|
|
|
|
|
api_url = "https://api.civitai.com/v1/images?postId=#{post_id}&nsfw=X"
|
|
|
|
{:ok, %{status: 200, body: body}} = PhilomenaProxy.Http.get(api_url)
|
|
|
|
|
|
|
|
json = Jason.decode!(body)
|
2025-03-15 00:15:13 +01:00
|
|
|
|
2025-03-14 21:26:25 +01:00
|
|
|
case json["items"] do
|
|
|
|
[] ->
|
|
|
|
%{
|
|
|
|
source_url: url,
|
|
|
|
author_name: "",
|
|
|
|
description: "",
|
|
|
|
images: []
|
|
|
|
}
|
2025-03-15 00:15:13 +01:00
|
|
|
|
2025-03-14 21:26:25 +01:00
|
|
|
items ->
|
|
|
|
username = hd(items)["username"]
|
2025-03-15 00:15:13 +01:00
|
|
|
|
2025-03-14 21:26:25 +01:00
|
|
|
images =
|
|
|
|
Enum.map(items, fn item ->
|
|
|
|
image_url = item["url"]
|
2025-03-15 03:05:29 +01:00
|
|
|
|
2025-03-14 21:26:25 +01:00
|
|
|
%{
|
|
|
|
url: image_url,
|
|
|
|
camo_url: PhilomenaProxy.Camo.image_url(image_url)
|
|
|
|
}
|
|
|
|
end)
|
2025-03-15 00:15:13 +01:00
|
|
|
|
2025-03-14 21:26:25 +01:00
|
|
|
%{
|
|
|
|
source_url: url,
|
|
|
|
author_name: username,
|
|
|
|
description: "",
|
|
|
|
images: images
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|