49 feat add cookies (#98)
* build client side validation and submission * enabled cookies submission, bug fixes
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
node_modules
|
||||
downloads
|
||||
dist
|
||||
package-lock.json
|
||||
pnpm-lock.yaml
|
||||
.pnpm-debug.log
|
||||
.parcel-cache
|
||||
.git
|
||||
src/server/core/*.exe
|
||||
src/server/core/yt-dlp
|
||||
node_modules
|
||||
.env
|
||||
*.mp4
|
||||
*.ytdl
|
||||
*.part
|
||||
*.db
|
||||
build/
|
||||
downloads
|
||||
.DS_Store
|
||||
build/
|
||||
yt-dlp-webui
|
||||
session.dat
|
||||
config.yml
|
||||
cookies.txt
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,3 +14,4 @@ build/
|
||||
yt-dlp-webui
|
||||
session.dat
|
||||
config.yml
|
||||
cookies.txt
|
||||
|
||||
@@ -172,6 +172,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 themeSelector = selector<ThemeNarrowed>({
|
||||
key: 'themeSelector',
|
||||
get: ({ get }) => {
|
||||
|
||||
161
frontend/src/components/CookiesTextField.tsx
Normal file
161
frontend/src/components/CookiesTextField.tsx
Normal file
@@ -0,0 +1,161 @@
|
||||
import { 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 { pipe } from 'fp-ts/lib/function'
|
||||
import { useMemo } from 'react'
|
||||
import { useRecoilState, useRecoilValue } from 'recoil'
|
||||
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs'
|
||||
import { downloadTemplateState } from '../atoms/downloadTemplate'
|
||||
import { cookiesState, 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(' '),
|
||||
E.of,
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f.length === 7,
|
||||
() => `missing parts`
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[0].length > 0,
|
||||
() => 'missing domain'
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[1] === 'TRUE' || f[1] === 'FALSE',
|
||||
() => `invalid include subdomains`
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[2].length > 0,
|
||||
() => 'invalid path'
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[3] === 'TRUE' || f[3] === 'FALSE',
|
||||
() => 'invalid secure flag'
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => isFinite(Number(f[4])),
|
||||
() => 'invalid expiration'
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[5].length > 0,
|
||||
() => 'invalid name'
|
||||
)
|
||||
),
|
||||
E.chain(
|
||||
E.fromPredicate(
|
||||
f => f[6].length > 0,
|
||||
() => 'invalid value'
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
const CookiesTextField: React.FC = () => {
|
||||
const serverAddr = useRecoilValue(serverURL)
|
||||
const [customArgs, setCustomArgs] = useRecoilState(downloadTemplateState)
|
||||
const [savedCookies, setSavedCookies] = useRecoilState(cookiesState)
|
||||
|
||||
const { pushMessage } = useToast()
|
||||
const flag = '--cookies=cookies.txt'
|
||||
|
||||
const cookies$ = useMemo(() => new Subject<string>(), [])
|
||||
|
||||
const submitCookies = (cookies: string) =>
|
||||
ffetch(`${serverAddr}/api/v1/cookies`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
cookies
|
||||
})
|
||||
})()
|
||||
|
||||
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(
|
||||
either,
|
||||
E.matchW(
|
||||
(l) => pushMessage(`Error in line ${i + 1}: ${l}`, 'warning'),
|
||||
() => E.isRight(either)
|
||||
),
|
||||
)),
|
||||
A.filter(Boolean),
|
||||
A.match(
|
||||
() => false,
|
||||
(c) => {
|
||||
pushMessage(`Valid ${c.length} Netscape cookies`, 'info')
|
||||
return true
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
useSubscription(
|
||||
cookies$.pipe(
|
||||
debounceTime(650),
|
||||
distinctUntilChanged()
|
||||
),
|
||||
(cookies) => pipe(
|
||||
cookies,
|
||||
cookies => {
|
||||
setSavedCookies(cookies)
|
||||
return cookies
|
||||
},
|
||||
validateNetscapeCookies,
|
||||
O.fromPredicate(f => f === true),
|
||||
O.match(
|
||||
() => {
|
||||
if (customArgs.includes(flag)) {
|
||||
setCustomArgs(a => a.replace(flag, ''))
|
||||
}
|
||||
},
|
||||
async () => {
|
||||
pipe(
|
||||
await submitCookies(cookies),
|
||||
E.match(
|
||||
(l) => pushMessage(`${l}`, 'error'),
|
||||
() => pushMessage(`Saved Netscape cookies`, 'success')
|
||||
)
|
||||
)
|
||||
if (!customArgs.includes(flag)) {
|
||||
setCustomArgs(a => `${a} ${flag}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
return (
|
||||
<TextField
|
||||
label="Netscape Cookies"
|
||||
multiline
|
||||
maxRows={20}
|
||||
minRows={4}
|
||||
fullWidth
|
||||
defaultValue={savedCookies}
|
||||
onChange={(e) => cookies$.next(e.currentTarget.value)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default CookiesTextField
|
||||
@@ -10,13 +10,13 @@ const Downloads: React.FC = () => {
|
||||
const listView = useRecoilValue(listViewState)
|
||||
const active = useRecoilValue(activeDownloadsState)
|
||||
|
||||
const [, setIsLoading] = useRecoilState(loadingAtom)
|
||||
const [isLoading, setIsLoading] = useRecoilState(loadingAtom)
|
||||
|
||||
useEffect(() => {
|
||||
if (active) {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [active?.length])
|
||||
}, [active?.length, isLoading])
|
||||
|
||||
if (listView) {
|
||||
return (
|
||||
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
serverPortState,
|
||||
themeState
|
||||
} from '../atoms/settings'
|
||||
import CookiesTextField from '../components/CookiesTextField'
|
||||
import { useToast } from '../hooks/toast'
|
||||
import { useI18n } from '../hooks/useI18n'
|
||||
import { useRPC } from '../hooks/useRPC'
|
||||
@@ -298,6 +299,12 @@ export default function Settings() {
|
||||
/>
|
||||
</Stack>
|
||||
</Grid>
|
||||
<Grid sx={{ mr: 1, mt: 3 }}>
|
||||
<Typography variant="h6" color="primary" sx={{ mb: 2 }}>
|
||||
Cookies
|
||||
</Typography>
|
||||
<CookiesTextField />
|
||||
</Grid>
|
||||
<Grid>
|
||||
<Stack direction="row">
|
||||
<Button
|
||||
|
||||
@@ -71,3 +71,8 @@ type DownloadRequest struct {
|
||||
Rename string `json:"rename"`
|
||||
Params []string `json:"params"`
|
||||
}
|
||||
|
||||
// struct representing request of creating a netscape cookies file
|
||||
type SetCookiesRequest struct {
|
||||
Cookies string `json:"cookies"`
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ func ApplyRouter(db *internal.MemoryDB, mq *internal.MessageQueue) func(chi.Rout
|
||||
r.Use(middlewares.Authenticated)
|
||||
r.Post("/exec", h.Exec())
|
||||
r.Get("/running", h.Running())
|
||||
r.Post("/cookies", h.SetCookies())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func (h *Handler) Exec() http.HandlerFunc {
|
||||
|
||||
req := internal.DownloadRequest{}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
if err := json.NewDecoder(r.Body).DecodeContext(r.Context(), &req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func (h *Handler) Exec() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(id)
|
||||
err = json.NewEncoder(w).EncodeContext(r.Context(), id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -48,7 +48,34 @@ func (h *Handler) Running() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(res)
|
||||
err = json.NewEncoder(w).EncodeContext(r.Context(), res)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) SetCookies() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
req := new(internal.SetCookiesRequest)
|
||||
|
||||
err := json.NewDecoder(r.Body).DecodeContext(r.Context(), req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.SetCookies(r.Context(), req.Cookies)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).EncodeContext(r.Context(), "ok")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package rest
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
)
|
||||
@@ -36,3 +37,15 @@ func (s *Service) Running(ctx context.Context) (*[]internal.ProcessResponse, err
|
||||
return s.db.All(), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) SetCookies(ctx context.Context, cookies string) error {
|
||||
fd, err := os.Create("cookies.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer fd.Close()
|
||||
fd.WriteString(cookies)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user