4dd9d837a3
* Updated package.json (and made an excuse to make a branch) * Video filepath parser (#6) * Restructured files; Added parser placeholder * More restructuring * Added basic parser for hydrating template strings * Improved docs * More docs * Initial implementation of media profiles (#7) * [WIP] Added basic video download method * [WIP] Very-WIP first steps at parsing options and downloading * Made my options safe by default and removed special safe versions * Ran html generator for mediaprofile model - leaving as-is for now * Addressed a bunch of TODO comments * Add "channel" type Media Source (#8) * [WIP] Working on fetching channel metadata in yt-dlp backend * Finished first draft of methods to do with querying channels * Renamed CommandRunnerMock to have a more descriptive name * Ran the phx generator for the channel model * Renamed Downloader namespace to MediaClient * [WIP] saving before attempting LiveView * LiveView did not work out but here's a working controller how about * Index a channel (#9) * Ran a MediaItem generator; Reformatted to my liking * [WIP] added basic index function * setup oban * Added basic Oban job for indexing * Added in workers for indexing; hooked them into record creation flow * Added a task model with a phx generator * Tied together tasks with jobs and channels * Download indexed videos (#10) * Clarified documentation * more comments * [WIP] hooked up basic video downloading; starting work on metadata * Added metadata model and parsing Adding the metadata model made me realize that, in many cases, yt-dlp returns undesired input in stdout, breaking parsing. In order to get the metadata model working, I had to change the way in which the app interacts with yt-dlp. Now, output is written as a file to disk which is immediately re-read and returned. * Added tests for video download worker * Hooked up video downloading to the channel indexing pipeline * Adds tasks for media items * Updated video metadata parser to extract the title * Ran linting
149 lines
3.8 KiB
Elixir
149 lines
3.8 KiB
Elixir
defmodule Pinchflat.Tasks do
|
|
@moduledoc """
|
|
The Tasks context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Pinchflat.Repo
|
|
|
|
alias Pinchflat.Tasks.Task
|
|
alias Pinchflat.Media.MediaItem
|
|
alias Pinchflat.MediaSource.Channel
|
|
|
|
@doc """
|
|
Returns the list of tasks. Returns [%Task{}, ...]
|
|
"""
|
|
def list_tasks do
|
|
Repo.all(Task)
|
|
end
|
|
|
|
@doc """
|
|
Returns the list of tasks for a given record type and ID. Optionally allows you to specify
|
|
which job states to include.
|
|
|
|
Returns [%Task{}, ...]
|
|
"""
|
|
def list_tasks_for(attached_record_type, attached_record_id, job_states \\ Oban.Job.states()) do
|
|
stringified_states = Enum.map(job_states, &to_string/1)
|
|
|
|
Repo.all(
|
|
from t in Task,
|
|
join: j in assoc(t, :job),
|
|
where: field(t, ^attached_record_type) == ^attached_record_id,
|
|
where: j.state in ^stringified_states
|
|
)
|
|
end
|
|
|
|
@doc """
|
|
Returns the list of pending tasks for a given record type and ID.
|
|
|
|
Returns [%Task{}, ...]
|
|
"""
|
|
def list_pending_tasks_for(attached_record_type, attached_record_id) do
|
|
list_tasks_for(
|
|
attached_record_type,
|
|
attached_record_id,
|
|
[:available, :scheduled, :retryable]
|
|
)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single task.
|
|
|
|
Returns %Task{}. Raises `Ecto.NoResultsError` if the Task does not exist.
|
|
"""
|
|
def get_task!(id), do: Repo.get!(Task, id)
|
|
|
|
@doc """
|
|
Creates a task.
|
|
|
|
Accepts map() | %Oban.Job{}, %Channel{} | %Oban.Job{}, %MediaItem{}.
|
|
Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}.
|
|
"""
|
|
def create_task(attrs) do
|
|
%Task{}
|
|
|> Task.changeset(attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
# This function's signature is designed to help simplify
|
|
# usage of `create_job_with_task/2`
|
|
def create_task(%Oban.Job{} = job, attached_record) do
|
|
attached_record_attr =
|
|
case attached_record do
|
|
%Channel{} = channel -> %{channel_id: channel.id}
|
|
%MediaItem{} = media_item -> %{media_item_id: media_item.id}
|
|
end
|
|
|
|
%Task{}
|
|
|> Task.changeset(Map.merge(%{job_id: job.id}, attached_record_attr))
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Creates a job from given attrs, creating a task with an attached record
|
|
if successful. Returns an error if the job already exists.
|
|
|
|
Returns {:ok, %Task{}} | {:error, :duplicate_job} | {:error, %Ecto.Changeset{}}.
|
|
"""
|
|
def create_job_with_task(job_attrs, task_attached_record) do
|
|
case Repo.insert_unique_job(job_attrs) do
|
|
{:ok, job} -> create_task(job, task_attached_record)
|
|
{:duplicate, _} -> {:error, :duplicate_job}
|
|
err -> err
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Deletes a task. Also cancels any attached job.
|
|
|
|
Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}.
|
|
"""
|
|
def delete_task(%Task{} = task) do
|
|
:ok = Oban.cancel_job(task.job_id)
|
|
|
|
Repo.delete(task)
|
|
end
|
|
|
|
@doc """
|
|
Deletes all tasks attached to a given record, cancelling any attached jobs.
|
|
|
|
Returns :ok
|
|
"""
|
|
def delete_tasks_for(attached_record) do
|
|
tasks =
|
|
case attached_record do
|
|
%Channel{} = channel -> list_tasks_for(:channel_id, channel.id)
|
|
%MediaItem{} = media_item -> list_tasks_for(:media_item_id, media_item.id)
|
|
end
|
|
|
|
Enum.each(tasks, fn task ->
|
|
delete_task(task)
|
|
end)
|
|
end
|
|
|
|
@doc """
|
|
Deletes all _pending_ tasks attached to a given record, cancelling any attached jobs.
|
|
|
|
Returns :ok
|
|
"""
|
|
def delete_pending_tasks_for(attached_record) do
|
|
tasks =
|
|
case attached_record do
|
|
%Channel{} = channel -> list_pending_tasks_for(:channel_id, channel.id)
|
|
%MediaItem{} = media_item -> list_pending_tasks_for(:media_item_id, media_item.id)
|
|
end
|
|
|
|
Enum.each(tasks, fn task ->
|
|
delete_task(task)
|
|
end)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking task changes.
|
|
"""
|
|
def change_task(%Task{} = task, attrs \\ %{}) do
|
|
Task.changeset(task, attrs)
|
|
end
|
|
end
|