Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Ritesh's Rough Work

docs/ritesh_roughwork.livemd

Ritesh’s Rough Work

Root Section – Common Utils

defmodule R do
  def recompile() do
    Mix.Task.reenable("app.start")
    Mix.Task.reenable("compile")
    Mix.Task.reenable("compile.all")
    compilers = Mix.compilers()
    Enum.each(compilers, &Mix.Task.reenable("compile.#{&1}"))
    Mix.Task.run("compile.all")
  end
end
voice_id = "b1db78be-0bdf-443c-afdf-3221bac38758"
loaded_voice = Vyasa.Repo.get(Vyasa.Medium.Voice, voice_id)
|> Vyasa.Medium.get_voices!()
|> List.first()
|> Vyasa.Medium.Store.hydrate()

loaded_voice.events

module = VyasaWeb.Context.Read
    module
    |> Atom.to_string()          # Convert the module atom to a string
    |> String.split(".")        # Split the string by "."
    |> List.last()              # Take the last element (the actual module name)
    |> Inflex.underscore()       # Convert from PascalCase to snake_case
    |> String.replace("_", "-")  # Replace underscores with hyphens to get
alias VyasaWeb.ModeLive.{UiState}

mod = UiState
mod.__info__(:functions)
    # Get the function names
    |> Enum.map(&elem(&1, 0))
    # Convert to strings
    |> Enum.map(&Atom.to_string/1)

modules = [mod]

  modules
    |> Enum.flat_map(fn module ->
      case module.__info__(:functions) do
        functions when is_list(functions) ->
          Enum.map(functions, fn {name, _arity} ->
            "#{module}.#{name}"  # Format as "ModuleName.function_name"
          end)
        _ -> []
      end
    end)

Explore Youtube dlp

video_id = "pEskP0ulPlA"

{url, _} =
  System.cmd("youtube-dl", ["-g", "-f", "best", "https://www.youtube.com/watch?v=#{video_id}"])
url

Image Gen Round 2

defmodule Thumb do
  @doc """
  Returns a thumbnail with simple text overlaid onto the logo with gradient & project stamp.
  """
  @priv_dir :code.priv_dir(:vyasa)
  @base_url Path.join([@priv_dir, "static", "images"])
  @img_bg_file_url Path.join([@base_url, "logo_with_gradient_and_stamp_1200x630.png"])
  @fallback_url Path.join([@base_url, "the_vyasa_project_1.png"])

  def create_thumbnail(text) when is_binary(text) do
    # overall config
    dims = "1200x630"
    # caption config
    font = "Gotu"
    font_weight = 800
    caption_width = 800
    caption_height = 400
    font_size = 70
    caption_text_color = "brown"

    {:ok, base} = Image.open(@img_bg_file_url)

    {:ok, thumbed} =
      base
      |> Image.thumbnail!(dims)
      |> Image.blur(sigma: 0.1)

    {:ok, txt_img} =
      Image.Text.simple_text(
        text,
        align: :left,
        font_size: font_size,
        text_fill_color: caption_text_color,
        x: :right,
        y: :top,
        autofit: true,
        width: caption_width,
        height: caption_height,
        font: font,
        font_weight: font_weight
      )

    thumbed
    |> Image.compose!(txt_img)
  end
end

Thumb.create_thumbnail("
Arjun Viṣhād Yog | अर्जुनविषादयोग
Chapter 1 - Arjuna's Dilemma
")
alias VyasaWeb.SourceLive.ImageGenerator
alias Vix.Vips.Operation
dims = "1200x630"
base_url = Path.join([:code.priv_dir(:vyasa), "static", "images"])
# caption config
file = Path.join([base_url, "logo_with_gradient_and_stamp_1200x630.png"])

{:ok, base} = Image.open(file)

{:ok, thumbed} =
  base
  |> Image.thumbnail!(dims)
  |> Image.blur(sigma: 0.1)

caption_width = 800
caption_height = 400
font_size = 70
caption_text_color = "brown"

text_input_ =
  "Gita Chapter 1: Arjun Viṣhād Yog | अर्जुनविषादयोग Gita Chapter 1: Arjun Viṣhād Yog | अर्जुनविषादयोगGita Chapter 1: Arjun Viṣhād Yog | अर्जुनविषादयोगGita Chapter 1: Arjun Viṣhād Yog | अर्जुनविषादयोग"

text_input = "Gita Chapter 1: Arjun Viṣhād Yog | अर्जुनविषादयोग"

{:ok, txt_img} =
  Image.Text.simple_text(
    text_input,
    align: :left,
    background_fill_color: "#FF00FF",
    background_fill_opacity: 1.0,
    font_size: font_size,
    text_fill_color: caption_text_color,
    x: :right,
    y: :top,
    # padding: [10,5],
    autofit: true,
    width: caption_width,
    height: caption_height,
    font: "Gotu",
    font_weight: 800
  )

{:ok}

{:ok, rect} =
  Image.Shape.rect(caption_width * 1.2, caption_height * 1.2, opacity: 0.5, fill_color: "gray")

# {:ok, written} = 

final =
  thumbed
  # |> Image.compose!(rect)
  |> Image.compose!(txt_img)
base_url = Path.join([:code.priv_dir(:vyasa), "static", "images"])
# caption config
file = Path.join([base_url, "logo_with_gradient_and_stamp_1200x630.png"])

JIT Thumbnail Generation from context-encoded-filename

Exploring the use of Module name

Key idea:

  1. file name shall encode where that share location was
  2. since every module can have a module name which is a string, we should explore the use of module name for this encoding.
  3. since fn application can be done by just knowing the strinfied name of the module that the function is, then each module may have its own “generate_og_content” function. This way, every selectable thing can have its own og content that can be auto-generated. The og controller shall merely call this function when attempting to generate the og image
R.recompile()
alias VyasaWeb.OgImageController

# testdecode and content extraction:
uri = "http://localhost:4000/og/images/Elixir.VyasaWeb.SourceLive.Chapter.Index~gita~1.png"
test_filename = "Elixir.VyasaWeb.SourceLive.Chapter.Index~gita~12.png"
IO.inspect(URI.parse(uri), label: "check uri obj")

# error input to be handled:
# test_filename = "bruh4.png" # shall give fallback img

url = OgImageController.get_url_for_img_file(test_filename)
{:ok, img} = Image.open(url)

img

Generate the default thumbnail here:

spaced_om = "                       ॐ"
text = "Come explore Indic wisdom; distilled into words."

fallback_url = "/Users/ritesh/desktop/vyasa_fallback.png"
OgImageController.create_thumbnail(spaced_om <> "\n" <> text <> "\n" <> spaced_om)
# filename encoding:
x = ["a", "b", "c"]
delim = "~"

Enum.join(x, delim)

module = "foo"
params = ["a", "b", "c"]
param_delim = "~"
ext = ".png"

[module | params]
|> Enum.join(param_delim)
|> (&amp;(&amp;1 <> ext)).()