Packages

Professional Elixir integration for first-last-frame. Provides utilities for building links and integrating with https://supermaker.ai/video/first-last-frame/.

Current section

Files

Jump to
first_last_frame lib first_last_frame.ex
Raw

lib/first_last_frame.ex

defmodule FirstLastFrame do
@moduledoc """
Provides functions for generating URLs and extracting information
related to the first and last frames of a video on Supermaker.ai.
"""
@base_url "https://supermaker.ai/video"
@doc """
Builds a URL for a video's first/last frame based on its ID and optional parameters.
## Examples
iex> FirstLastFrame.build_url("abc123xyz")
"https://supermaker.ai/video/abc123xyz"
iex> FirstLastFrame.build_url("abc123xyz", frame: "first")
"https://supermaker.ai/video/abc123xyz?frame=first"
iex> FirstLastFrame.build_url("abc123xyz", frame: "last", timestamp: 1000)
"https://supermaker.ai/video/abc123xyz?frame=last&timestamp=1000"
"""
def build_url(video_id, opts \\ []) when is_binary(video_id) do
@base_url
|> Path.join(video_id)
|> build_query_string(opts)
end
defp build_query_string(base_url, opts) when is_list(opts) do
query_params =
opts
|> Enum.reject(fn {_, value} -> is_nil(value) end)
|> Enum.map(fn {key, value} -> "#{key}=#{value}" end)
|> Enum.join("&")
case query_params do
"" ->
base_url
_ ->
"#{base_url}?#{query_params}"
end
end
@doc """
Determines if a URL is a Supermaker video URL.
## Examples
iex> FirstLastFrame.is_supermaker_url?("https://supermaker.ai/video/abc123xyz")
true
iex> FirstLastFrame.is_supermaker_url?("https://example.com/video/abc123xyz")
false
"""
def is_supermaker_url?(url) when is_binary(url) do
String.starts_with?(url, @base_url)
end
@doc """
Extracts the video ID from a Supermaker video URL.
Returns `nil` if the URL is not a valid Supermaker video URL.
## Examples
iex> FirstLastFrame.extract_video_id("https://supermaker.ai/video/abc123xyz")
"abc123xyz"
iex> FirstLastFrame.extract_video_id("https://example.com/video/abc123xyz")
nil
"""
def extract_video_id(url) when is_binary(url) do
case String.replace_prefix(url, @base_url <> "/", "") do
remaining_path when String.length(remaining_path) > 0 ->
case String.split(remaining_path, "?") do
[video_id | _] -> video_id
_ -> nil
end
_ ->
nil
end
end
@doc """
Generates a timestamp parameter for a given number of seconds.
## Examples
iex> FirstLastFrame.timestamp_param(10)
[timestamp: 10]
iex> FirstLastFrame.timestamp_param(0)
[timestamp: 0]
"""
def timestamp_param(seconds) when is_integer(seconds) do
[timestamp: seconds]
end
end