Skip to content

Commit

Permalink
Use cookies saved server side (#188)
Browse files Browse the repository at this point in the history
* retrieve cookies stored server side

fixed netscape cookies validation pipeline

* code refactoring
  • Loading branch information
marcopiovanello authored Aug 26, 2024
1 parent 64df0e0 commit bb4db5d
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 149 deletions.
17 changes: 8 additions & 9 deletions frontend/src/atoms/downloadTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { getOrElse } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import { atom, selector } from 'recoil'
import { CustomTemplate } from '../types'
import { ffetch } from '../lib/httpClient'
import { serverURL } from './settings'
import { pipe } from 'fp-ts/lib/function'
import { getOrElse } from 'fp-ts/lib/Either'
import { CustomTemplate } from '../types'
import { serverSideCookiesState, serverURL } from './settings'

export const cookiesTemplateState = atom({
export const cookiesTemplateState = selector({
key: 'cookiesTemplateState',
default: localStorage.getItem('cookiesTemplate') ?? '',
effects: [
({ onSet }) => onSet(e => localStorage.setItem('cookiesTemplate', e))
]
get: ({ get }) => get(serverSideCookiesState)
? '--cookies=cookies.txt'
: ''
})

export const customArgsState = atom({
Expand Down
19 changes: 12 additions & 7 deletions frontend/src/atoms/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { pipe } from 'fp-ts/lib/function'
import { matchW } from 'fp-ts/lib/TaskEither'
import { atom, selector } from 'recoil'
import { ffetch } from '../lib/httpClient'
import { prefersDarkMode } from '../utils'

export const languages = [
Expand Down Expand Up @@ -187,13 +190,15 @@ export const rpcHTTPEndpoint = selector({
}
})

export const cookiesState = atom({
key: 'cookiesState',
default: localStorage.getItem('yt-dlp-cookies') ?? '',
effects: [
({ onSet }) =>
onSet(c => localStorage.setItem('yt-dlp-cookies', c))
]
export const serverSideCookiesState = selector<string>({
key: 'serverSideCookiesState',
get: async ({ get }) => await pipe(
ffetch<Readonly<{ cookies: string }>>(`${get(serverURL)}/api/v1/cookies`),
matchW(
() => '',
(r) => r.cookies
)
)()
})

const themeSelector = selector<ThemeNarrowed>({
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/atoms/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { pipe } from 'fp-ts/lib/function'
import { of } from 'fp-ts/lib/Task'
import { getOrElse } from 'fp-ts/lib/TaskEither'
import { atom, selector } from 'recoil'
import { ffetch } from '../lib/httpClient'
import { rpcClientState } from './rpc'
import { serverURL } from './settings'

export const connectedState = atom({
key: 'connectedState',
Expand All @@ -22,4 +27,15 @@ export const availableDownloadPathsState = selector({
.catch(() => ({ result: [] }))
return res.result
}
})

export const ytdlpVersionState = selector<string>({
key: 'ytdlpVersionState',
get: async ({ get }) => await pipe(
ffetch<string>(`${get(serverURL)}/api/v1/version`),
getOrElse(() => pipe(
'unknown version',
of
)),
)()
})
105 changes: 60 additions & 45 deletions frontend/src/components/CookiesTextField.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { TextField } from '@mui/material'
import { Button, TextField } from '@mui/material'
import * as A from 'fp-ts/Array'
import * as E from 'fp-ts/Either'
import * as O from 'fp-ts/Option'
import { matchW } from 'fp-ts/lib/TaskEither'
import { pipe } from 'fp-ts/lib/function'
import { useMemo } from 'react'
import { useRecoilState, useRecoilValue } from 'recoil'
import { useRecoilValue } from 'recoil'
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs'
import { cookiesTemplateState } from '../atoms/downloadTemplate'
import { cookiesState, serverURL } from '../atoms/settings'
import { serverSideCookiesState, serverURL } from '../atoms/settings'
import { useSubscription } from '../hooks/observable'
import { useToast } from '../hooks/toast'
import { ffetch } from '../lib/httpClient'

const validateCookie = (cookie: string) => pipe(
cookie,
cookie => cookie.replace(/\s\s+/g, ' '),
cookie => cookie.replaceAll('\t', ' '),
cookie => cookie.split(' '),
cookie => cookie.split('\t'),
E.of,
E.flatMap(
E.fromPredicate(
Expand Down Expand Up @@ -68,13 +66,19 @@ const validateCookie = (cookie: string) => pipe(
),
)

const noopValidator = (s: string): E.Either<string, string[]> => pipe(
s,
s => s.split('\t'),
E.of
)

const isCommentOrNewLine = (s: string) => s === '' || s.startsWith('\n') || s.startsWith('#')

const CookiesTextField: React.FC = () => {
const serverAddr = useRecoilValue(serverURL)
const [, setCookies] = useRecoilState(cookiesTemplateState)
const [savedCookies, setSavedCookies] = useRecoilState(cookiesState)
const savedCookies = useRecoilValue(serverSideCookiesState)

const { pushMessage } = useToast()
const flag = '--cookies=cookies.txt'

const cookies$ = useMemo(() => new Subject<string>(), [])

Expand All @@ -86,28 +90,41 @@ const CookiesTextField: React.FC = () => {
})
})()

const deleteCookies = () => pipe(
ffetch(`${serverAddr}/api/v1/cookies`, {
method: 'DELETE',
}),
matchW(
(l) => pushMessage(l, 'error'),
(_) => {
pushMessage('Deleted cookies', 'success')
pushMessage(`Reload the page to apply the changes`, 'info')
}
)
)()

const validateNetscapeCookies = (cookies: string) => pipe(
cookies,
cookies => cookies.split('\n'),
cookies => cookies.filter(f => !f.startsWith('\n')), // empty lines
cookies => cookies.filter(f => !f.startsWith('# ')), // comments
cookies => cookies.filter(Boolean), // empty lines
A.map(validateCookie),
A.mapWithIndex((i, either) => pipe(
A.map(c => isCommentOrNewLine(c) ? noopValidator(c) : validateCookie(c)), // validate line
A.mapWithIndex((i, either) => pipe( // detect errors and return the either
either,
E.matchW(
(l) => pushMessage(`Error in line ${i + 1}: ${l}`, 'warning'),
() => E.isRight(either)
E.match(
(l) => {
pushMessage(`Error in line ${i + 1}: ${l}`, 'warning')
return either
},
(_) => either
),
)),
A.filter(Boolean),
A.match(
() => false,
(c) => {
pushMessage(`Valid ${c.length} Netscape cookies`, 'info')
return true
}
)
A.filter(c => E.isRight(c)), // filter the line who didn't pass the validation
A.map(E.getOrElse(() => new Array<string>())), // cast the array of eithers to an array of tokens
A.filter(f => f.length > 0), // filter the empty tokens
A.map(f => f.join('\t')), // join the tokens in a TAB separated string
A.reduce('', (c, n) => `${c}${n}\n`), // reduce all to a single string separated by \n
parsed => parsed.length > 0 // if nothing has passed the validation return none
? O.some(parsed)
: O.none
)

useSubscription(
Expand All @@ -117,22 +134,17 @@ const CookiesTextField: React.FC = () => {
),
(cookies) => pipe(
cookies,
cookies => {
setSavedCookies(cookies)
return cookies
},
validateNetscapeCookies,
O.fromPredicate(f => f === true),
O.match(
() => setCookies(''),
async () => {
() => pushMessage('No valid cookies', 'warning'),
async (some) => {
pipe(
await submitCookies(cookies),
await submitCookies(some.trimEnd()),
E.match(
(l) => pushMessage(`${l}`, 'error'),
() => {
pushMessage(`Saved Netscape cookies`, 'success')
setCookies(flag)
pushMessage(`Saved ${some.split('\n').length} Netscape cookies`, 'success')
pushMessage('Reload the page to apply the changes', 'info')
}
)
)
Expand All @@ -142,15 +154,18 @@ const CookiesTextField: React.FC = () => {
)

return (
<TextField
label="Netscape Cookies"
multiline
maxRows={20}
minRows={4}
fullWidth
defaultValue={savedCookies}
onChange={(e) => cookies$.next(e.currentTarget.value)}
/>
<>
<TextField
label="Netscape Cookies"
multiline
maxRows={20}
minRows={4}
fullWidth
defaultValue={savedCookies}
onChange={(e) => cookies$.next(e.currentTarget.value)}
/>
<Button onClick={deleteCookies}>Delete cookies</Button>
</>
)
}

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const Footer: React.FC = () => {
<div style={{ display: 'flex', gap: 4, alignItems: 'center' }}>
{/* TODO: make it dynamic */}
<Chip label="RPC v3.2.0" variant="outlined" size="small" />
<VersionIndicator />
<Suspense>
<VersionIndicator />
</Suspense>
</div>
<div style={{ display: 'flex', gap: 4, 'alignItems': 'center' }}>
<div style={{
Expand Down
27 changes: 2 additions & 25 deletions frontend/src/components/VersionIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { Chip, CircularProgress } from '@mui/material'
import { useEffect, useState } from 'react'
import { useRecoilValue } from 'recoil'
import { serverURL } from '../atoms/settings'
import { useToast } from '../hooks/toast'
import { ytdlpVersionState } from '../atoms/status'

const VersionIndicator: React.FC = () => {
const serverAddr = useRecoilValue(serverURL)

const [version, setVersion] = useState('')
const { pushMessage } = useToast()

const fetchVersion = async () => {
const res = await fetch(`${serverAddr}/api/v1/version`, {
headers: {
'X-Authentication': localStorage.getItem('token') ?? ''
}
})

if (!res.ok) {
return pushMessage(await res.text(), 'error')
}

setVersion(await res.json())
}

useEffect(() => {
fetchVersion()
}, [])
const version = useRecoilValue(ytdlpVersionState)

return (
version
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/lib/rpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export class RPCClient {
: ''

const sanitizedArgs = this.argsSanitizer(
req.args.replace('-o', '').replace(rename, '')
req.args
.replace('-o', '')
.replace(rename, '')
)

if (req.playlist) {
Expand Down Expand Up @@ -177,14 +179,14 @@ export class RPCClient {
}

public killLivestream(url: string) {
return this.sendHTTP<LiveStreamProgress>({
return this.sendHTTP({
method: 'Service.KillLivestream',
params: [url]
})
}

public killAllLivestream() {
return this.sendHTTP<LiveStreamProgress>({
return this.sendHTTP({
method: 'Service.KillAllLivestream',
params: []
})
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/views/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Typography,
capitalize
} from '@mui/material'
import { useEffect, useMemo, useState } from 'react'
import { Suspense, useEffect, useMemo, useState } from 'react'
import { useRecoilState } from 'recoil'
import {
Subject,
Expand Down Expand Up @@ -347,7 +347,9 @@ export default function Settings() {
<Typography variant="h6" color="primary" sx={{ mb: 2 }}>
Cookies
</Typography>
<CookiesTextField />
<Suspense>
<CookiesTextField />
</Suspense>
</Grid>
<Grid>
<Stack direction="row">
Expand Down
9 changes: 4 additions & 5 deletions server/internal/message_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ func (m *MessageQueue) downloadConsumer() {
)

if p.Progress.Status != StatusCompleted {
slog.Info("started process",
slog.String("bus", queueName),
slog.String("id", p.getShortId()),
)
if p.Livestream {
// livestreams have higher priorty and they ignore the semaphore
go p.Start()
} else {
p.Start()
}
}

slog.Info("started process",
slog.String("bus", queueName),
slog.String("id", p.getShortId()),
)
}, false)
}

Expand Down
Loading

0 comments on commit bb4db5d

Please sign in to comment.