From 9af933421359c73339fe52506eb6387092d9cda1 Mon Sep 17 00:00:00 2001
From: Chaska <166928710+chaskayote@users.noreply.github.com>
Date: Mon, 29 Apr 2024 00:36:08 -0500
Subject: [PATCH] Adding Furbooru

---
 lib/philomena/scrapers.ex          |  1 +
 lib/philomena/scrapers/furbooru.ex | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 lib/philomena/scrapers/furbooru.ex

diff --git a/lib/philomena/scrapers.ex b/lib/philomena/scrapers.ex
index 9cdd578f..5927d667 100644
--- a/lib/philomena/scrapers.ex
+++ b/lib/philomena/scrapers.ex
@@ -9,6 +9,7 @@ defmodule Philomena.Scrapers do
     Philomena.Scrapers.Furaffinity,
     Philomena.Scrapers.Pixiv,
     Philomena.Scrapers.Derpibooru,
+    Philomena.Scrapers.Furbooru,
     Philomena.Scrapers.Raw
   ]
 
diff --git a/lib/philomena/scrapers/furbooru.ex b/lib/philomena/scrapers/furbooru.ex
new file mode 100644
index 00000000..cb315c8d
--- /dev/null
+++ b/lib/philomena/scrapers/furbooru.ex
@@ -0,0 +1,30 @@
+defmodule Philomena.Scrapers.Furbooru do
+  @url_regex ~r|\Ahttps?://furbooru\.org/images/([0-9]+)|
+
+  @spec can_handle?(URI.t(), String.t()) :: true | false
+  def can_handle?(_uri, url) do
+    String.match?(url, @url_regex)
+  end
+
+  def scrape(_uri, url) do
+[_, submission_id] = Regex.run(@url_regex, url, capture: :all)
+    api_url = "https://furbooru.org/api/v1/json/images/#{submission_id}"
+    {:ok, %Tesla.Env{status: 200, body: body}} = Philomena.Http.get(api_url)
+
+    json = Jason.decode!(body)
+    submission = json["image"]
+    
+    tags = submission["tags"]
+        
+    %{
+      source_url: url,
+      tags: tags,
+      sources: submission["source_url"],
+      description: submission["description"],
+      images: [%{
+        url: "#{submission["representations"]["full"]}",
+        camo_url: Camo.Image.image_url(submission["representations"]["medium"])
+      }]
+    }
+  end
+end
\ No newline at end of file