-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
233 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ defmodule Canary.Index.Document.Shared do | |
:source_id, | ||
:embedding, | ||
:tags, | ||
:is_empty_tags, | ||
:meta | ||
] | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
defmodule Canary.Sources.Webpage.Config do | ||
use Ash.Resource, data_layer: :embedded | ||
|
||
alias Canary.Sources.Webpage.TagDefinition | ||
|
||
attributes do | ||
attribute :start_urls, {:array, :string}, default: [] | ||
attribute :url_include_patterns, {:array, :string}, default: [] | ||
attribute :url_exclude_patterns, {:array, :string}, default: [] | ||
attribute :tag_definitions, {:array, TagDefinition}, default: [] | ||
end | ||
|
||
actions do | ||
defaults [:read] | ||
|
||
create :create do | ||
primary? true | ||
accept [:start_urls, :url_include_patterns, :url_exclude_patterns] | ||
accept [:start_urls, :url_include_patterns, :url_exclude_patterns, :tag_definitions] | ||
end | ||
|
||
update :update do | ||
primary? true | ||
accept [:start_urls, :url_include_patterns, :url_exclude_patterns] | ||
accept [:start_urls, :url_include_patterns, :url_exclude_patterns, :tag_definitions] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,48 @@ | ||
defmodule Canary.Sources.Webpage.FetcherResult do | ||
alias Canary.Scraper.Item | ||
|
||
defstruct [:url, :html, :items] | ||
@type t :: %__MODULE__{url: String.t(), html: String.t(), items: list(Item.t())} | ||
defstruct [:url, :html, :tags, :items] | ||
|
||
@type t :: %__MODULE__{ | ||
url: String.t(), | ||
html: String.t(), | ||
tags: list(String.t()), | ||
items: list(Item.t()) | ||
} | ||
end | ||
|
||
defmodule Canary.Sources.Webpage.Fetcher do | ||
alias Canary.Sources.Source | ||
alias Canary.Sources.Webpage.FetcherResult | ||
alias Canary.Sources.Webpage.Config | ||
alias Canary.Sources.Webpage | ||
|
||
def run(%Source{config: %Ash.Union{type: :webpage, value: %Config{} = config}}) do | ||
def run(%Webpage.Config{} = config) do | ||
case Canary.Crawler.run(config) do | ||
{:ok, stream} -> | ||
results = | ||
stream = | ||
stream | ||
|> Stream.map(fn {url, html} -> | ||
items = Canary.Scraper.run(html) | ||
|
||
tags = | ||
config.tag_definitions | ||
|> Enum.filter(&is_matching_tag?(&1, url)) | ||
|> Enum.map(& &1.name) | ||
|
||
if(length(items) == 0, | ||
do: nil, | ||
else: %FetcherResult{url: url, html: html, items: items} | ||
else: %Webpage.FetcherResult{url: url, html: html, tags: tags, items: items} | ||
) | ||
end) | ||
|> Stream.reject(&is_nil/1) | ||
|> Enum.to_list() | ||
|
||
{:ok, results} | ||
{:ok, stream} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
defp is_matching_tag?(%Webpage.TagDefinition{} = tag, url) do | ||
tag.url_include_patterns | ||
|> Enum.any?(&Canary.Native.glob_match(&1, url)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Canary.Sources.Webpage.TagDefinition do | ||
use Ash.Resource, data_layer: :embedded | ||
|
||
attributes do | ||
attribute :name, :string, allow_nil?: false | ||
attribute :url_include_patterns, {:array, :string}, default: [] | ||
end | ||
|
||
actions do | ||
defaults [:read, :destroy] | ||
|
||
create :create do | ||
primary? true | ||
accept [:name, :url_include_patterns] | ||
end | ||
|
||
update :update do | ||
primary? true | ||
accept [:name, :url_include_patterns] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.