Compare commits

..

1 Commits

Author SHA1 Message Date
a4ba8cea27 initial support for playlist modifiers
supported modifiers are --playlist-start, --playlist-end, --playlist-reverse, --max-downloads
2025-02-06 11:24:09 +01:00
20 changed files with 105 additions and 513 deletions

1
.gitignore vendored
View File

@@ -29,4 +29,3 @@ frontend/.yarn/install-state.gz
livestreams.dat
.vite/deps
archive.txt
web_config.yml

View File

@@ -18,12 +18,11 @@
"@mui/icons-material": "^6.2.0",
"@mui/material": "^6.2.0",
"fp-ts": "^2.16.5",
"jotai": "^2.10.3",
"jotai-cache": "^0.5.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-router-dom": "^6.23.1",
"react-virtuoso": "^4.7.11",
"jotai": "^2.10.3",
"rxjs": "^7.8.1"
},
"devDependencies": {

View File

@@ -32,9 +32,6 @@ importers:
jotai:
specifier: ^2.10.3
version: 2.10.3(@types/react@19.0.1)(react@19.0.0)
jotai-cache:
specifier: ^0.5.0
version: 0.5.0(jotai@2.10.3(@types/react@19.0.1)(react@19.0.0))
react:
specifier: ^19.0.0
version: 19.0.0
@@ -740,11 +737,6 @@ packages:
is-core-module@2.12.1:
resolution: {integrity: sha512-Q4ZuBAe2FUsKtyQJoQHlvP8OvBERxO3jEmy1I7hcRXcJBGGHFh/aJBswbXuS9sgrDH2QUO8ilkwNPHvHMd8clg==}
jotai-cache@0.5.0:
resolution: {integrity: sha512-29pUuEfSXL7Ba6lxZmiNDARc73TspWzAzCy0jCkk2uEOnFJ6kaUBZTp/AZSwnIsh1ndfUfM9/QpbLU7uJAQL0A==}
peerDependencies:
jotai: '>=2.0.0'
jotai@2.10.3:
resolution: {integrity: sha512-Nnf4IwrLhNfuz2JOQLI0V/AgwcpxvVy8Ec8PidIIDeRi4KCFpwTFIpHAAcU+yCgnw/oASYElq9UY0YdUUegsSA==}
engines: {node: '>=12.20.0'}
@@ -1520,10 +1512,6 @@ snapshots:
dependencies:
has: 1.0.3
jotai-cache@0.5.0(jotai@2.10.3(@types/react@19.0.1)(react@19.0.0)):
dependencies:
jotai: 2.10.3(@types/react@19.0.1)(react@19.0.0)
jotai@2.10.3(@types/react@19.0.1)(react@19.0.0):
optionalDependencies:
'@types/react': 19.0.1

View File

@@ -16,13 +16,13 @@ import ListItemButton from '@mui/material/ListItemButton'
import ListItemIcon from '@mui/material/ListItemIcon'
import ListItemText from '@mui/material/ListItemText'
import Toolbar from '@mui/material/Toolbar'
import Typography from '@mui/material/Typography'
import { grey } from '@mui/material/colors'
import { useAtomValue } from 'jotai'
import { useMemo, useState } from 'react'
import { Link, Outlet } from 'react-router-dom'
import { settingsState } from './atoms/settings'
import AppBar from './components/AppBar'
import { AppTitle } from './components/AppTitle'
import Drawer from './components/Drawer'
import Footer from './components/Footer'
import Logout from './components/Logout'
@@ -76,7 +76,15 @@ export default function Layout() {
>
<Menu />
</IconButton>
<AppTitle />
<Typography
component="h1"
variant="h6"
color="inherit"
noWrap
sx={{ flexGrow: 1 }}
>
{settings.appTitle}
</Typography>
</Toolbar>
</AppBar>
<Drawer variant="permanent" open={open}>

View File

@@ -1,12 +1,12 @@
import { getOrElse } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import { atomWithCache } from 'jotai-cache'
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'
import { ffetch } from '../lib/httpClient'
import { CustomTemplate } from '../types'
import { serverSideCookiesState, serverURL } from './settings'
export const cookiesTemplateState = atomWithCache<Promise<string>>(async (get) =>
export const cookiesTemplateState = atom<Promise<string>>(async (get) =>
await get(serverSideCookiesState)
? '--cookies=cookies.txt'
: ''
@@ -22,7 +22,7 @@ export const filenameTemplateState = atomWithStorage(
localStorage.getItem('lastFilenameTemplate') ?? ''
)
export const savedTemplatesState = atomWithCache<Promise<CustomTemplate[]>>(async (get) => {
export const savedTemplatesState = atom<Promise<CustomTemplate[]>>(async (get) => {
const task = ffetch<CustomTemplate[]>(`${get(serverURL)}/api/v1/template/all`)
const either = await task()
@@ -30,4 +30,5 @@ export const savedTemplatesState = atomWithCache<Promise<CustomTemplate[]>>(asyn
either,
getOrElse(() => new Array<CustomTemplate>())
)
})
}
)

View File

@@ -1,9 +1,9 @@
import { pipe } from 'fp-ts/lib/function'
import { matchW } from 'fp-ts/lib/TaskEither'
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'
import { ffetch } from '../lib/httpClient'
import { prefersDarkMode } from '../utils'
import { atomWithStorage } from 'jotai/utils'
import { atom } from 'jotai'
export const languages = [
'catalan',
@@ -121,15 +121,11 @@ export const appTitleState = atomWithStorage(
export const serverAddressAndPortState = atom((get) => {
if (get(servedFromReverseProxySubDirState)) {
return `${get(serverAddressState)}/${get(servedFromReverseProxySubDirState)}/`
.replaceAll('"', '') // XXX: atomWithStorage uses JSON.stringify to serialize
.replaceAll('//', '/') // which puts extra double quotes.
}
if (get(servedFromReverseProxyState)) {
return `${get(serverAddressState)}`
.replaceAll('"', '')
}
return `${get(serverAddressState)}:${get(serverPortState)}`
.replaceAll('"', '')
})
export const serverURL = atom((get) =>
@@ -139,12 +135,14 @@ export const serverURL = atom((get) =>
export const rpcWebSocketEndpoint = atom((get) => {
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
return `${proto}//${get(serverAddressAndPortState)}/rpc/ws`
})
}
)
export const rpcHTTPEndpoint = atom((get) => {
const proto = window.location.protocol
return `${proto}//${get(serverAddressAndPortState)}/rpc/http`
})
}
)
export const serverSideCookiesState = atom<Promise<string>>(async (get) => await pipe(
ffetch<Readonly<{ cookies: string }>>(`${get(serverURL)}/api/v1/cookies`),
@@ -182,4 +180,5 @@ export const settingsState = atom<SettingsState>((get) => ({
listView: get(listViewState),
servedFromReverseProxy: get(servedFromReverseProxyState),
appTitle: get(appTitleState)
}))
})
)

View File

@@ -1,33 +0,0 @@
import { Typography } from '@mui/material'
import { useAtom } from 'jotai'
import { useEffect } from 'react'
import { appTitleState } from '../atoms/settings'
import useFetch from '../hooks/useFetch'
export const AppTitle: React.FC = () => {
const [appTitle, setAppTitle] = useAtom(appTitleState)
const { data } = useFetch<{ title: string }>('/webconfig')
useEffect(() => {
if (data?.title) {
setAppTitle(
data.title.startsWith('"')
? data.title.substring(1, data.title.length - 1)
: data.title
)
}
}, [data])
return (
<Typography
component="h1"
variant="h6"
color="inherit"
noWrap
sx={{ flexGrow: 1 }}
>
{appTitle.startsWith('"') ? appTitle.substring(1, appTitle.length - 1) : appTitle}
</Typography>
)
}

View File

@@ -15,7 +15,7 @@ import {
Typography
} from '@mui/material'
import { TransitionProps } from '@mui/material/transitions'
import { matchW } from 'fp-ts/lib/TaskEither'
import { matchW } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import { useAtomValue } from 'jotai'
import { forwardRef, startTransition, useState } from 'react'
@@ -52,16 +52,21 @@ const SubscriptionsDialog: React.FC<Props> = ({ open, onClose }) => {
const baseURL = useAtomValue(serverURL)
const submit = async (sub: Omit<Subscription, 'id'>) => pipe(
ffetch<void>(`${baseURL}/subscriptions`, {
const submit = async (sub: Omit<Subscription, 'id'>) => {
const task = ffetch<void>(`${baseURL}/subscriptions`, {
method: 'POST',
body: JSON.stringify(sub)
}),
})
const either = await task()
pipe(
either,
matchW(
(l) => pushMessage(l, 'error'),
(_) => onClose()
)
)()
)
}
return (
<Dialog

View File

@@ -6,12 +6,6 @@ import { serverURL } from '../atoms/settings'
import { ffetch } from '../lib/httpClient'
import { useToast } from './toast'
/**
* Wrapper hook for ffetch. Handles data retrieval and cancellation signals.
* If R type is set to void it doesn't perform deserialization.
* @param resource path of the resource. serverURL is prepended
* @returns JSON decoded value, eventual error and refetcher as an object to destruct.
*/
const useFetch = <R>(resource: string) => {
const base = useAtomValue(serverURL)
@@ -32,10 +26,7 @@ const useFetch = <R>(resource: string) => {
)()
useEffect(() => {
const controller = new AbortController()
fetcher()
return () => controller.abort()
}, [])
return { data, error, fetcher }

View File

@@ -1,9 +1,6 @@
import { tryCatch } from 'fp-ts/TaskEither'
import * as J from 'fp-ts/Json'
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/lib/function'
async function fetcher(url: string, opt?: RequestInit, controller?: AbortController): Promise<string> {
async function fetcher<T>(url: string, opt?: RequestInit): Promise<T> {
const jwt = localStorage.getItem('token')
if (opt && !opt.headers) {
@@ -17,25 +14,17 @@ async function fetcher(url: string, opt?: RequestInit, controller?: AbortControl
headers: {
...opt?.headers,
'X-Authentication': jwt ?? ''
},
signal: controller?.signal
}
})
if (!res.ok) {
throw await res.text()
}
return res.text()
return res.json() as T
}
export const ffetch = <T>(url: string, opt?: RequestInit, controller?: AbortController) => tryCatch(
async () => pipe(
await fetcher(url, opt, controller),
J.parse,
E.match(
(l) => l as T,
(r) => r as T
)
),
export const ffetch = <T>(url: string, opt?: RequestInit) => tryCatch(
() => fetcher<T>(url, opt),
(e) => `error while fetching: ${e}`
)

View File

@@ -17,9 +17,7 @@ import {
Typography,
capitalize
} from '@mui/material'
import { pipe } from 'fp-ts/lib/function'
import { matchW } from 'fp-ts/lib/TaskEither'
import { useAtom, useAtomValue } from 'jotai'
import { useAtom } from 'jotai'
import { Suspense, useCallback, useEffect, useMemo, useState } from 'react'
import {
Subject,
@@ -36,9 +34,9 @@ import {
accentState,
accents,
appTitleState,
autoFileExtensionState,
enableCustomArgsState,
fileRenamingState,
autoFileExtensionState,
formatSelectionState,
languageState,
languages,
@@ -47,14 +45,12 @@ import {
servedFromReverseProxySubDirState,
serverAddressState,
serverPortState,
serverURL,
themeState
} from '../atoms/settings'
import CookiesTextField from '../components/CookiesTextField'
import UpdateBinaryButton from '../components/UpdateBinaryButton'
import { useToast } from '../hooks/toast'
import { useI18n } from '../hooks/useI18n'
import { ffetch } from '../lib/httpClient'
import Translator from '../lib/i18n'
import { validateDomain, validateIP } from '../utils'
@@ -74,7 +70,7 @@ export default function Settings() {
const [pollingTime, setPollingTime] = useAtom(rpcPollingTimeState)
const [language, setLanguage] = useAtom(languageState)
const [appTitle, setAppTitle] = useAtom(appTitleState)
const [appTitle, setApptitle] = useAtom(appTitleState)
const [accent, setAccent] = useAtom(accentState)
const [theme, setTheme] = useAtom(themeState)
@@ -85,11 +81,7 @@ export default function Settings() {
const { pushMessage } = useToast()
// TODO: change name
const derivedServerURL = useAtomValue(serverURL)
const baseURL$ = useMemo(() => new Subject<string>(), [])
const appTitle$ = useMemo(() => new Subject<string>(), [])
const serverAddr$ = useMemo(() => new Subject<string>(), [])
const serverPort$ = useMemo(() => new Subject<string>(), [])
@@ -142,25 +134,6 @@ export default function Settings() {
return () => sub.unsubscribe()
}, [])
// TODO: refactor out of component. maybe use withAtomEffect from jotai/effect package.
useEffect(() => {
const sub = appTitle$
.pipe(debounceTime(500))
.subscribe(title => {
pipe(
ffetch(`${derivedServerURL}/webconfig/title`, {
method: 'PATCH',
body: JSON.stringify(title)
}),
matchW(
(l) => pushMessage(l, 'error'),
(_) => setAppTitle(title)
)
)()
})
return () => sub.unsubscribe()
}, [])
/**
* Language toggler handler
*/
@@ -221,7 +194,7 @@ export default function Settings() {
fullWidth
label={i18n.t('appTitle')}
defaultValue={appTitle}
onChange={(e) => appTitle$.next(e.target.value)}
onChange={(e) => setApptitle(e.currentTarget.value)}
error={appTitle === ''}
/>
</Grid>
@@ -245,7 +218,7 @@ export default function Settings() {
{ value: 500, label: '500 ms' },
{ value: 750, label: '750 ms' },
{ value: 1000, label: '1000 ms' },
{ value: 2000, label: '2 s' },
{ value: 2000, label: '2000 ms' },
]}
onChange={(_, value) => typeof value === 'number'
? setPollingTime(value)

View File

@@ -1,58 +0,0 @@
package archive
import (
"bufio"
"bytes"
"context"
"os"
"os/exec"
"path/filepath"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
)
// Perform a search on the archive.txt file an determines if a download
// has already be done.
func DownloadExists(ctx context.Context, url string) (bool, error) {
cmd := exec.CommandContext(
ctx,
config.Instance().DownloaderPath,
"--print",
"%(extractor)s %(id)s",
url,
)
stdout, err := cmd.Output()
if err != nil {
return false, err
}
extractorAndURL := bytes.Trim(stdout, "\n")
fd, err := os.Open(filepath.Join(config.Instance().Dir(), "archive.txt"))
if err != nil {
return false, err
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
// search linearly for lower memory usage...
// the a pre-sorted with hashed values version of the archive.txt file can be loaded in memory
// and perform a binary search on it.
for scanner.Scan() {
if bytes.Equal(scanner.Bytes(), extractorAndURL) {
return true, nil
}
}
// data, err := io.ReadAll(fd)
// if err != nil {
// return false, err
// }
// slices.BinarySearchFunc(data, extractorAndURL, func(a []byte, b []byte) int {
// return hash(a).Compare(hash(b))
// })
return false, nil
}

View File

@@ -1,104 +0,0 @@
package configurator
import (
"log/slog"
"os"
"path/filepath"
"sync"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
"gopkg.in/yaml.v3"
)
// A singleton holding configuration of the frontend component
// with optional persistence on a file.
type AppConfig struct {
Title string `yaml:"title" json:"title"`
BaseURL string `yaml:"base_url" json:"base_url"`
Language string `yaml:"language" json:"language"`
RPCPollingTime int `yaml:"rpc_polling_time" json:"rpc_polling_time"`
}
type Configurator struct {
mu sync.RWMutex
Config AppConfig
}
var (
instance *Configurator
instanceOnce sync.Once
)
func Instance() *Configurator {
instanceOnce.Do(func() {
if instance == nil {
instance = &Configurator{}
// TODO: move out of initialization
err := instance.Load()
if err != nil {
slog.Error("failed initializating configurator", slog.Any("err", err))
}
}
})
return instance
}
func (c *Configurator) Load() error {
fd, err := getConfigurationFile()
if err != nil {
return err
}
defer fd.Close()
if err := yaml.NewDecoder(fd).Decode(&c.Config); err != nil {
return err
}
return nil
}
func (c *Configurator) Persist() error {
fd, err := getConfigurationFile()
if err != nil {
return err
}
defer fd.Close()
if err := yaml.NewEncoder(fd).Encode(c.Config); err != nil {
return err
}
return nil
}
func (c *Configurator) setAppConfig(ac *AppConfig) {
c.mu.RLock()
defer c.mu.RUnlock()
// TODO: better validaitons
if ac.BaseURL != "" {
c.Config.BaseURL = ac.BaseURL
}
if ac.Language != "" {
c.Config.Language = ac.Language
}
if ac.Title != "" {
c.Config.Title = ac.Title
}
if ac.RPCPollingTime >= 250 && ac.RPCPollingTime <= 2000 {
c.Config.RPCPollingTime = ac.RPCPollingTime
}
}
func getConfigurationFile() (*os.File, error) {
fd, err := os.OpenFile(
filepath.Join(config.Instance().Dir(), "web_config.yml"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644,
)
if err != nil {
return nil, err
}
return fd, nil
}

View File

@@ -1,103 +0,0 @@
package configurator
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
)
// App configurator REST handlers
func GetConfig(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(Instance().Config); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func SetConfig(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
var req AppConfig
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Instance().setAppConfig(&req)
if err := Instance().Persist(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode("ok")
}
func setAppTitle(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
var req string
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
editField(w, func(c *AppConfig) {
if req != "" {
c.Title = req
}
})
json.NewEncoder(w).Encode("ok")
}
func setBaseURL(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
var req string
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
editField(w, func(c *AppConfig) {
if req != "" {
c.BaseURL = req
}
})
json.NewEncoder(w).Encode("ok")
}
func editField(w http.ResponseWriter, editFunc func(c *AppConfig)) {
editFunc(&Instance().Config)
if err := Instance().Persist(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func ApplyRouter() func(r chi.Router) {
return func(r chi.Router) {
r.Get("/", GetConfig)
r.Post("/", SetConfig)
r.Patch("/title", setAppTitle)
r.Patch("/baseURL", setBaseURL)
}
}

View File

@@ -76,7 +76,7 @@ func (m *Monitor) Status() LiveStreamStatus {
// Persist the monitor current state to a file.
// The file is located in the configured config directory
func (m *Monitor) Persist() error {
fd, err := os.Create(filepath.Join(config.Instance().SessionFilePath, "livestreams.dat"))
fd, err := os.Create(filepath.Join(config.Instance().Dir(), "livestreams.dat"))
if err != nil {
return err
}
@@ -95,7 +95,7 @@ func (m *Monitor) Persist() error {
// Restore a saved state and resume the monitored livestreams
func (m *Monitor) Restore() error {
fd, err := os.Open(filepath.Join(config.Instance().SessionFilePath, "livestreams.dat"))
fd, err := os.Open(filepath.Join(config.Instance().Dir(), "livestreams.dat"))
if err != nil {
return err
}

View File

@@ -88,10 +88,10 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error {
proc.Info.URL = meta.URL
time.Sleep(time.Millisecond)
db.Set(proc)
mq.Publish(proc)
proc.Info.CreatedAt = meta.CreatedAt
}
return nil

View File

@@ -22,7 +22,6 @@ import (
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/archive"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/archiver"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/configurator"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/dbutil"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/filebrowser"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/internal"
@@ -236,9 +235,6 @@ func newServer(c serverConfig) *http.Server {
// Subscriptions
r.Route("/subscriptions", subscription.Container(c.db, cronTaskRunner).ApplyRouter())
// Frontend config store
r.Route("/webconfig", configurator.ApplyRouter())
return &http.Server{Handler: r}
}

View File

@@ -48,7 +48,10 @@ func (h *RestHandler) Delete() http.HandlerFunc {
return
}
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode("ok"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

View File

@@ -53,7 +53,6 @@ func toDB(dto *domain.Subscription) data.Subscription {
// Delete implements domain.Service.
func (s *Service) Delete(ctx context.Context, id string) error {
s.runner.StopTask(id)
return s.r.Delete(ctx, id)
}

View File

@@ -7,9 +7,9 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/archive"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/internal"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/subscription/domain"
@@ -19,12 +19,10 @@ import (
type TaskRunner interface {
Submit(subcription *domain.Subscription) error
Spawner(ctx context.Context)
StopTask(id string) error
Recoverer()
}
type monitorTask struct {
Done chan struct{}
type taskPair struct {
Schedule cron.Schedule
Subscription *domain.Subscription
}
@@ -33,22 +31,21 @@ type CronTaskRunner struct {
mq *internal.MessageQueue
db *internal.MemoryDB
tasks chan monitorTask
tasks chan taskPair
errors chan error
running map[string]*monitorTask
}
func NewCronTaskRunner(mq *internal.MessageQueue, db *internal.MemoryDB) TaskRunner {
return &CronTaskRunner{
mq: mq,
db: db,
tasks: make(chan monitorTask),
tasks: make(chan taskPair),
errors: make(chan error),
running: make(map[string]*monitorTask),
}
}
const commandTemplate = "-I1 --flat-playlist --print webpage_url $1"
var argsSplitterRe = regexp.MustCompile(`(?mi)[^\s"']+|"([^"]*)"|'([^']*)'`)
func (t *CronTaskRunner) Submit(subcription *domain.Subscription) error {
@@ -57,8 +54,7 @@ func (t *CronTaskRunner) Submit(subcription *domain.Subscription) error {
return err
}
job := monitorTask{
Done: make(chan struct{}),
job := taskPair{
Schedule: schedule,
Subscription: subcription,
}
@@ -68,110 +64,54 @@ func (t *CronTaskRunner) Submit(subcription *domain.Subscription) error {
return nil
}
// Handles the entire lifecylce of a monitor job.
func (t *CronTaskRunner) Spawner(ctx context.Context) {
for req := range t.tasks {
t.running[req.Subscription.Id] = &req // keep track of the current job
go func() {
ctx, cancel := context.WithCancel(ctx) // inject into the job's context a cancellation singal
fetcherEvents := t.doFetch(ctx, &req) // retrieve the channel of events of the job
for {
select {
case <-req.Done:
slog.Info("stopping cron job and removing schedule", slog.String("url", req.Subscription.URL))
cancel()
return
case <-fetcherEvents:
slog.Info("finished monitoring channel", slog.String("url", req.Subscription.URL))
}
}
}()
}
}
// Stop a currently scheduled job
func (t *CronTaskRunner) StopTask(id string) error {
task := t.running[id]
if task != nil {
t.running[id].Done <- struct{}{}
delete(t.running, id)
}
return nil
}
// Start a fetcher and notify on a channel when a fetcher has completed
func (t *CronTaskRunner) doFetch(ctx context.Context, req *monitorTask) <-chan struct{} {
completed := make(chan struct{})
// generator func
for task := range t.tasks {
go func() {
for {
sleepFor := t.fetcher(ctx, req)
completed <- struct{}{}
slog.Info("fetching latest video for channel", slog.String("channel", task.Subscription.URL))
time.Sleep(sleepFor)
}
}()
return completed
}
// Perform the retrieval of the latest video of the channel.
// Returns a time.Duration containing the amount of time to the next schedule.
func (t *CronTaskRunner) fetcher(ctx context.Context, req *monitorTask) time.Duration {
slog.Info("fetching latest video for channel", slog.String("channel", req.Subscription.URL))
nextSchedule := time.Until(req.Schedule.Next(time.Now()))
fetcherParams := strings.Split(strings.Replace(commandTemplate, "$1", task.Subscription.URL, 1), " ")
cmd := exec.CommandContext(
ctx,
config.Instance().DownloaderPath,
"-I1",
"--flat-playlist",
"--print", "webpage_url",
req.Subscription.URL,
fetcherParams...,
)
stdout, err := cmd.Output()
if err != nil {
t.errors <- err
return time.Duration(0)
return
}
latestVideoURL := string(bytes.Trim(stdout, "\n"))
// if the download exists there's not point in sending it into the message queue.
exists, err := archive.DownloadExists(ctx, latestVideoURL)
if exists && err == nil {
return nextSchedule
}
latestChannelURL := string(bytes.Trim(stdout, "\n"))
p := &internal.Process{
Url: latestVideoURL,
Params: append(
argsSplitterRe.FindAllString(req.Subscription.Params, 1),
[]string{
"--break-on-existing",
Url: latestChannelURL,
Params: append(argsSplitterRe.FindAllString(task.Subscription.Params, 1), []string{
"--download-archive",
filepath.Join(config.Instance().Dir(), "archive.txt"),
}...),
AutoRemove: true,
}
t.db.Set(p) // give it an id
t.mq.Publish(p) // send it to the message queue waiting to be processed
t.db.Set(p)
t.mq.Publish(p)
nextSchedule := time.Until(task.Schedule.Next(time.Now()))
slog.Info(
"cron task runner next schedule",
slog.String("url", req.Subscription.URL),
slog.String("url", task.Subscription.URL),
slog.Any("duration", nextSchedule),
)
return nextSchedule
time.Sleep(nextSchedule)
}
}()
}
}
func (t *CronTaskRunner) Recoverer() {
panic("unimplemented")
panic("Unimplemented")
}