mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-03-17 09:00:05 +01:00
Merge pull request #457 from tantabus-oneiros/master
Add CivitAI post scraper
This commit is contained in:
commit
e72adf3d34
2 changed files with 56 additions and 0 deletions
|
@ -22,6 +22,7 @@ defmodule PhilomenaProxy.Scrapers do
|
|||
|
||||
@scrapers [
|
||||
PhilomenaProxy.Scrapers.Bluesky,
|
||||
PhilomenaProxy.Scrapers.Civitai,
|
||||
PhilomenaProxy.Scrapers.Deviantart,
|
||||
PhilomenaProxy.Scrapers.Pillowfort,
|
||||
PhilomenaProxy.Scrapers.Twitter,
|
||||
|
|
55
lib/philomena_proxy/scrapers/civitai.ex
Normal file
55
lib/philomena_proxy/scrapers/civitai.ex
Normal file
|
@ -0,0 +1,55 @@
|
|||
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)
|
||||
|
||||
case json["items"] do
|
||||
[] ->
|
||||
%{
|
||||
source_url: url,
|
||||
author_name: "",
|
||||
description: "",
|
||||
images: []
|
||||
}
|
||||
|
||||
items ->
|
||||
username = hd(items)["username"]
|
||||
|
||||
images =
|
||||
Enum.map(items, fn item ->
|
||||
image_url = item["url"]
|
||||
|
||||
%{
|
||||
url: image_url,
|
||||
camo_url: PhilomenaProxy.Camo.image_url(image_url)
|
||||
}
|
||||
end)
|
||||
|
||||
%{
|
||||
source_url: url,
|
||||
author_name: username,
|
||||
description: "",
|
||||
images: images
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue