-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Foundations of the CLI #24
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2020dfd
Foundations of the CLI
db7516d
Fixing Finch Behaviour for Escripts
d62f868
Wayback Fallback
3679bca
File Storage and Chalisa.json done
7f78236
Support fork for escripts vs application runtime
2a6f3d9
Merge branch 'master' into video-IR/patch/corpusengine
rtshkmr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in Livebook not ported to the db clause on the CLI for Shlokam