-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Foundations of the CLI * Fixing Finch Behaviour for Escripts * Wayback Fallback * File Storage and Chalisa.json done * Support fork for escripts vs application runtime --------- Co-authored-by: ks0m1c_dharma <sakiyamuni@sams.ara> Co-authored-by: Ritesh Kumar <ritesh@emerald.pink>
- Loading branch information
1 parent
34b43c1
commit df3ea6c
Showing
9 changed files
with
3,756 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Vyasa.Corpus.Engine.Fallback do | ||
@url "https://archive.org/wayback/available?url=" | ||
|
||
def run(path) do | ||
#storage opts | ||
@url | ||
|> fetch_url(path) | ||
|> IO.inspect() | ||
|> fetch_tree() | ||
end | ||
|
||
def fetch_url(url, path \\ "") do | ||
case Req.get(url <> path, conn_opts()) do | ||
{:ok, %{body: %{"archived_snapshots" => | ||
%{"closest" => | ||
%{"url" => url}}}}} -> | ||
{:ok, url} | ||
{:ok, %{body: %{"archived_snapshots" => %{}}}} -> | ||
IO.inspect("Not Found", label: :fallback_err) | ||
{:err, :not_found} | ||
{:error, reason} -> | ||
IO.inspect(reason, label: :fallback_err) | ||
{:err, :fallback_failed} | ||
end | ||
end | ||
|
||
def fetch_tree({:ok, url}) do | ||
url | ||
|> to_https() | ||
|> Req.get!(conn_opts()) | ||
|> Map.get(:body) | ||
end | ||
|
||
def fetch_tree(err), do: err | ||
|
||
defp to_https(url) do | ||
url | ||
|> URI.parse() | ||
|> Map.put(:port, nil) | ||
|> Map.put(:scheme, "https") | ||
|> URI.to_string() | ||
end | ||
|
||
defp conn_opts(), do: [connect_options: [transport_opts: [cacerts: :public_key.cacerts_get()]]] | ||
|
||
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,115 @@ | ||
defmodule Vyasa.Corpus.Engine.Shlokam do | ||
@url "https://shlokam.org/" | ||
alias Vyasa.Corpus.Engine.Fallback | ||
|
||
def run(path, opts \\ []) do | ||
#storage opts | ||
@url | ||
|> fetch_tree(path) | ||
|> scrape() | ||
|> store(path, Keyword.get(opts, :o, nil)) | ||
end | ||
|
||
def fetch_tree(url, path \\ "") do | ||
case Req.get!(url <> path, connect_options: [transport_opts: [cacerts: :public_key.cacerts_get()]]) do | ||
%{body: body} -> | ||
{:ok, body | ||
|> Floki.parse_document!() | ||
|> Floki.find(".uncode_text_column")} | ||
%{status: 301, headers: header} -> | ||
header | ||
|> Keyword.get(:location) | ||
|> fetch_tree() | ||
|
||
error -> | ||
IO.inspect(error, label: :primary_site_err) | ||
Fallback.run(url <> path) | ||
end | ||
end | ||
|
||
defp scrape({:ok, tree}) do | ||
tree | ||
|> Enum.reduce(%{title: nil, description: nil, verses: []}, fn | ||
{"div", _, [{"h3", [], ["Description"]} | para]}, acc -> | ||
# IO.inspect(rem, label: "div") | ||
desc = | ||
para | ||
|> Floki.text() | ||
|
||
%{acc | description: desc} | ||
|
||
{"div", _, [{"h3", _, _} = h3_tree]}, acc -> | ||
title = | ||
h3_tree | ||
|> Floki.text() | ||
|
||
%{acc | title: title} | ||
|
||
{"div", _, [{"div", [{"class", "verse_sanskrit"}], _verse} | _] = verse_tree}, acc -> | ||
[curr | [%{"count" => count} | _] = verses] = | ||
Enum.reduce(verse_tree, [], fn | ||
# n case verse break | ||
{"hr", [{"class", "verse_separator"}], []}, [curr | [%{"count" => c} | _] = acc] -> | ||
[Map.put(curr, "count", c + 1) | acc] | ||
|
||
# init verse break | ||
{"hr", [{"class", "verse_separator"}], []}, [curr | acc] -> | ||
[Map.put(curr, "count", 1) | acc] | ||
|
||
# n case after verse break | ||
{"div", [{"class", class}], _} = c_tree, [%{"count" => _} | _] = acc -> | ||
[%{class => c_tree |> Floki.text()} | acc] | ||
|
||
# n case before verse break | ||
{"div", [{"class", class}], _} = c_tree, [curr | acc] when is_map(curr)-> | ||
[Map.put(curr, class, c_tree |> Floki.text()) | acc] | ||
|
||
# init | ||
{"div", [{"class", class}], _} = c_tree, [] -> | ||
[%{class => c_tree |> Floki.text()}] | ||
|
||
others, acc -> | ||
IO.inspect(others) | ||
acc | ||
end) | ||
|
||
#formatting & tying loose ends | ||
clean_verses = [Map.put(curr, "count", count + 1)| verses] | ||
|> Enum.reverse() | ||
|
||
%{acc | verses: clean_verses} | ||
|
||
_, acc -> | ||
acc | ||
end) | ||
end | ||
|
||
defp scrape(err), do: err | ||
|
||
def store(_text, _tree, "db") do | ||
# TODO parsing logic into text indexer structs and db insert ops | ||
end | ||
|
||
def store(tree, text, nil) do | ||
# TODO parsing logic into text indexer structs and db insert ops | ||
json = Jason.encode!(tree) | ||
Application.app_dir(:vyasa, "priv") | ||
|> Path.join("/static/corpus/shlokam.org/#{text}.json") | ||
|> tap(&File.touch(&1)) | ||
|> tap(&File.write!(&1, json)) | ||
end | ||
|
||
def store(tree, text, file_path) do | ||
# TODO parsing logic into text indexer structs and db insert ops | ||
json = Jason.encode!(tree) | ||
file_path | ||
|> Path.join("/shlokam.org") | ||
|> tap(&File.mkdir(&1)) | ||
|> Path.join("/#{text}.json") | ||
|> tap(&File.touch(&1)) | ||
|> tap(&File.write!(&1, json)) | ||
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,40 @@ | ||
defmodule VyasaCLI do | ||
def main(args \\ []) do | ||
IO.inspect(args) | ||
args | ||
|> parse_args() | ||
|> response() | ||
|> IO.puts() | ||
end | ||
|
||
|
||
defp parse_args([command | ["--" <> _] = args]) do | ||
{opts, _, _} = | ||
args | ||
|> OptionParser.parse(switches: [storage: :string]) | ||
{command, opts} | ||
end | ||
|
||
defp parse_args([command | [arg | _] = args]) do | ||
{opts, _, _} = | ||
args | ||
|> OptionParser.parse(switches: [o: :string, path: :string]) | ||
|
||
{command, arg, opts} | ||
end | ||
defp response({"fetch", "shlokam.org/" <> path, opts}) do | ||
Vyasa.Corpus.Engine.Shlokam.run(path, opts) | ||
end | ||
|
||
defp response({"fetch", _, _}) do | ||
"Unsupported domain | ||
Try one of the following: | ||
shlokam.org/ | ||
" | ||
end | ||
|
||
defp response(_) do | ||
"Command doesnt belong to us " | ||
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
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.