mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
finish scraper
This commit is contained in:
parent
da09d45c7f
commit
109f3f781a
6 changed files with 67 additions and 2 deletions
|
@ -7,7 +7,7 @@ import { $, $$, hideEl, showEl, makeEl, clearEl } from './utils/dom';
|
|||
import { addTag } from './tagsinput';
|
||||
|
||||
function scrapeUrl(url) {
|
||||
return fetchJson('POST', '/images/scrape_url', { url })
|
||||
return fetchJson('POST', '/images/scrape', { url })
|
||||
.then(handleError)
|
||||
.then(response => response.json());
|
||||
}
|
||||
|
|
17
lib/philomena_web/controllers/image/scrape_controller.ex
Normal file
17
lib/philomena_web/controllers/image/scrape_controller.ex
Normal file
|
@ -0,0 +1,17 @@
|
|||
defmodule PhilomenaWeb.Image.ScrapeController do
|
||||
use PhilomenaWeb, :controller
|
||||
|
||||
alias Philomena.Scrapers
|
||||
|
||||
def create(conn, params) do
|
||||
result =
|
||||
params
|
||||
|> Map.get("url")
|
||||
|> to_string()
|
||||
|> String.trim()
|
||||
|> Scrapers.scrape!()
|
||||
|
||||
conn
|
||||
|> json(result)
|
||||
end
|
||||
end
|
|
@ -14,6 +14,7 @@ defmodule PhilomenaWeb.ImageController do
|
|||
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
||||
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
||||
plug PhilomenaWeb.CaptchaPlug when action in [:create]
|
||||
plug PhilomenaWeb.ScraperPlug, [params_name: "image", params_key: "image"] when action in [:create]
|
||||
|
||||
def index(conn, _params) do
|
||||
query = conn.assigns.compiled_filter
|
||||
|
|
39
lib/philomena_web/plugs/scraper_plug.ex
Normal file
39
lib/philomena_web/plugs/scraper_plug.ex
Normal file
|
@ -0,0 +1,39 @@
|
|||
defmodule PhilomenaWeb.ScraperPlug do
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(conn, opts) do
|
||||
case conn.params do
|
||||
%{"scraper_cache" => url} ->
|
||||
Philomena.Http.get!(url, [], max_body_length: 30_000_000)
|
||||
|> maybe_fixup_params(opts, conn)
|
||||
|
||||
_ ->
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_fixup_params(%HTTPoison.Response{body: body, status_code: 200}, opts, conn) do
|
||||
params_name = Keyword.get(opts, :params_name, "image")
|
||||
params_key = Keyword.get(opts, :params_key, "image")
|
||||
file = Briefly.create!()
|
||||
now = DateTime.utc_now() |> DateTime.to_unix(:microsecond)
|
||||
|
||||
File.write!(file, body)
|
||||
|
||||
fake_upload =
|
||||
%Plug.Upload{
|
||||
path: file,
|
||||
content_type: "application/octet-stream",
|
||||
filename: "scraper-#{now}"
|
||||
}
|
||||
|
||||
updated_form =
|
||||
Map.put(conn.params[params_name], params_key, fake_upload)
|
||||
|
||||
updated_params =
|
||||
Map.put(conn.params, params_name, updated_form)
|
||||
|
||||
%{conn | params: updated_params}
|
||||
end
|
||||
defp maybe_fixup_params(_response, _opts, conn), do: conn
|
||||
end
|
|
@ -84,6 +84,9 @@ defmodule PhilomenaWeb.Router do
|
|||
get "/", ActivityController, :index
|
||||
|
||||
resources "/activity", ActivityController, only: [:index]
|
||||
scope "/images", Image, as: :image do
|
||||
resources "/scrape", ScrapeController, only: [:create]
|
||||
end
|
||||
resources "/images", ImageController, only: [:index, :show, :new, :create] do
|
||||
resources "/comments", Image.CommentController, only: [:index, :show, :create]
|
||||
resources "/tags", Image.TagController, only: [:update], singleton: true
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
h4 Select an image
|
||||
.image-other
|
||||
#js-image-upload-previews
|
||||
p Upload a file from your computer
|
||||
p Upload a file from your computer, or provide a link to the page containing the image and click Fetch.
|
||||
.field
|
||||
= file_input f, :image, class: "input js-scraper"
|
||||
= error_tag f, :image_size
|
||||
|
@ -37,6 +37,11 @@
|
|||
= error_tag f, :image_name
|
||||
= error_tag f, :image_mime_type
|
||||
|
||||
.field.field--inline
|
||||
= url_input f, :scraper_url, class: "input input--wide js-scraper", placeholder: "Link a deviantART page, a Tumblr post, or the image directly"
|
||||
button.button.button--separate-left#js-scraper-preview type="button" title="Fetch the image at the specified URL" data-disable-with="Fetch"
|
||||
' Fetch
|
||||
|
||||
.field-error-js.hidden.js-scraper
|
||||
|
||||
h4 About this image
|
||||
|
|
Loading…
Reference in a new issue