Compare commits
8 Commits
326-securi
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c06485880 | ||
|
|
ccb6bbe3e6 | ||
| 9ca7bb9377 | |||
| bce696fc67 | |||
| 22caf8899b | |||
| 2a11f64935 | |||
|
|
f4a0f688af | ||
|
|
14a03d6a77 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -29,3 +29,4 @@ frontend/.yarn/install-state.gz
|
||||
livestreams.dat
|
||||
.vite/deps
|
||||
archive.txt
|
||||
twitch-monitor.dat
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "yt-dlp-webui",
|
||||
"version": "3.2.5",
|
||||
"version": "3.2.6",
|
||||
"description": "Frontend compontent of yt-dlp-webui",
|
||||
"scripts": {
|
||||
"dev": "vite --host 0.0.0.0",
|
||||
@@ -18,11 +18,11 @@
|
||||
"@mui/icons-material": "^6.2.0",
|
||||
"@mui/material": "^6.2.0",
|
||||
"fp-ts": "^2.16.5",
|
||||
"jotai": "^2.10.3",
|
||||
"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": {
|
||||
|
||||
@@ -28,6 +28,7 @@ import Footer from './components/Footer'
|
||||
import Logout from './components/Logout'
|
||||
import SocketSubscriber from './components/SocketSubscriber'
|
||||
import ThemeToggler from './components/ThemeToggler'
|
||||
import TwitchIcon from './components/TwitchIcon'
|
||||
import { useI18n } from './hooks/useI18n'
|
||||
import Toaster from './providers/ToasterProvider'
|
||||
import { getAccentValue } from './utils'
|
||||
@@ -154,6 +155,19 @@ export default function Layout() {
|
||||
<ListItemText primary={i18n.t('subscriptionsButtonLabel')} />
|
||||
</ListItemButton>
|
||||
</Link>
|
||||
<Link to={'/twitch'} style={
|
||||
{
|
||||
textDecoration: 'none',
|
||||
color: mode === 'dark' ? '#ffffff' : '#000000DE'
|
||||
}
|
||||
}>
|
||||
<ListItemButton>
|
||||
<ListItemIcon>
|
||||
<TwitchIcon />
|
||||
</ListItemIcon>
|
||||
<ListItemText primary={"Twitch"} />
|
||||
</ListItemButton>
|
||||
</Link>
|
||||
<Link to={'/monitor'} style={
|
||||
{
|
||||
textDecoration: 'none',
|
||||
|
||||
@@ -81,3 +81,6 @@ keys:
|
||||
editButtonLabel: 'Edit'
|
||||
newSubscriptionButton: New subscription
|
||||
clearCompletedButton: 'Clear completed'
|
||||
twitchIntegrationInfo: |
|
||||
To enable monitoring Twitch streams follow this wiki page.
|
||||
https://github.com/marcopiovanello/yt-dlp-web-ui/wiki/Twitch-integration
|
||||
@@ -32,7 +32,6 @@ const HomeSpeedDial: React.FC<Props> = ({ onDownloadOpen, onEditorOpen }) => {
|
||||
ariaLabel="Home speed dial"
|
||||
sx={{ position: 'absolute', bottom: 64, right: 24 }}
|
||||
icon={<SpeedDialIcon />}
|
||||
onClick={onDownloadOpen}
|
||||
>
|
||||
<SpeedDialAction
|
||||
icon={listView ? <ViewAgendaIcon /> : <FormatListBulleted />}
|
||||
|
||||
22
frontend/src/components/TwitchIcon.tsx
Normal file
22
frontend/src/components/TwitchIcon.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useAtomValue } from 'jotai'
|
||||
import { settingsState } from '../atoms/settings'
|
||||
|
||||
const TwitchIcon: React.FC = () => {
|
||||
const { theme } = useAtomValue(settingsState)
|
||||
|
||||
return (
|
||||
<svg
|
||||
role="img"
|
||||
viewBox="0 0 24 24"
|
||||
width={24}
|
||||
height={24}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
style={{ fill: theme === 'dark' ? '#fff' : '#757575' }}
|
||||
>
|
||||
<title>Twitch</title>
|
||||
<path d="M11.571 4.714h1.715v5.143H11.57zm4.715 0H18v5.143h-1.714zM6 0L1.714 4.286v15.428h5.143V24l4.286-4.286h3.428L22.286 12V0zm14.571 11.143l-3.428 3.428h-3.429l-3 3v-3H6.857V1.714h13.714Z" />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default TwitchIcon
|
||||
140
frontend/src/components/twitch/TwitchDialog.tsx
Normal file
140
frontend/src/components/twitch/TwitchDialog.tsx
Normal file
@@ -0,0 +1,140 @@
|
||||
import CloseIcon from '@mui/icons-material/Close'
|
||||
import {
|
||||
Alert,
|
||||
AppBar,
|
||||
Box,
|
||||
Button,
|
||||
Container,
|
||||
Dialog,
|
||||
Grid,
|
||||
IconButton,
|
||||
Paper,
|
||||
Slide,
|
||||
TextField,
|
||||
Toolbar,
|
||||
Typography
|
||||
} from '@mui/material'
|
||||
import { TransitionProps } from '@mui/material/transitions'
|
||||
import { matchW } from 'fp-ts/lib/Either'
|
||||
import { pipe } from 'fp-ts/lib/function'
|
||||
import { useAtomValue } from 'jotai'
|
||||
import { forwardRef, startTransition, useState } from 'react'
|
||||
import { serverURL } from '../../atoms/settings'
|
||||
import { useToast } from '../../hooks/toast'
|
||||
import { useI18n } from '../../hooks/useI18n'
|
||||
import { ffetch } from '../../lib/httpClient'
|
||||
|
||||
type Props = {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
const Transition = forwardRef(function Transition(
|
||||
props: TransitionProps & {
|
||||
children: React.ReactElement
|
||||
},
|
||||
ref: React.Ref<unknown>,
|
||||
) {
|
||||
return <Slide direction="up" ref={ref} {...props} />
|
||||
})
|
||||
|
||||
const TwitchDialog: React.FC<Props> = ({ open, onClose }) => {
|
||||
const [channelURL, setChannelURL] = useState('')
|
||||
|
||||
const { i18n } = useI18n()
|
||||
const { pushMessage } = useToast()
|
||||
|
||||
const baseURL = useAtomValue(serverURL)
|
||||
|
||||
const submit = async (channelURL: string) => {
|
||||
const task = ffetch<void>(`${baseURL}/twitch/user`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
user: channelURL.split('/').at(-1)
|
||||
})
|
||||
})
|
||||
const either = await task()
|
||||
|
||||
pipe(
|
||||
either,
|
||||
matchW(
|
||||
(l) => pushMessage(l, 'error'),
|
||||
(_) => onClose()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
fullScreen
|
||||
open={open}
|
||||
onClose={onClose}
|
||||
TransitionComponent={Transition}
|
||||
>
|
||||
<AppBar sx={{ position: 'relative' }}>
|
||||
<Toolbar>
|
||||
<IconButton
|
||||
edge="start"
|
||||
color="inherit"
|
||||
onClick={onClose}
|
||||
aria-label="close"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
<Typography sx={{ ml: 2, flex: 1 }} variant="h6" component="div">
|
||||
{i18n.t('subscriptionsButtonLabel')}
|
||||
</Typography>
|
||||
</Toolbar>
|
||||
</AppBar>
|
||||
<Box sx={{
|
||||
backgroundColor: (theme) => theme.palette.background.default,
|
||||
minHeight: (theme) => `calc(99vh - ${theme.mixins.toolbar.minHeight}px)`
|
||||
}}>
|
||||
<Container sx={{ my: 4 }}>
|
||||
<Grid container spacing={2}>
|
||||
<Grid item xs={12}>
|
||||
<Paper
|
||||
elevation={4}
|
||||
sx={{
|
||||
p: 2,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
}}
|
||||
>
|
||||
<Grid container gap={1.5}>
|
||||
<Grid item xs={12}>
|
||||
<Alert severity="info">
|
||||
{i18n.t('twitchIntegrationInfo')}
|
||||
</Alert>
|
||||
</Grid>
|
||||
<Grid item xs={12} mt={1}>
|
||||
<TextField
|
||||
multiline
|
||||
fullWidth
|
||||
label={i18n.t('subscriptionsURLInput')}
|
||||
variant="outlined"
|
||||
placeholder="https://www.twitch.tv/a_twitch_user_that_exists"
|
||||
onChange={(e) => setChannelURL(e.target.value)}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<Button
|
||||
sx={{ mt: 2 }}
|
||||
variant="contained"
|
||||
disabled={channelURL === ''}
|
||||
onClick={() => startTransition(() => submit(channelURL))}
|
||||
>
|
||||
{i18n.t('startButton')}
|
||||
</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Paper>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Container>
|
||||
</Box>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
export default TwitchDialog
|
||||
@@ -6,6 +6,7 @@ import Terminal from './views/Terminal'
|
||||
|
||||
const Home = lazy(() => import('./views/Home'))
|
||||
const Login = lazy(() => import('./views/Login'))
|
||||
const Twitch = lazy(() => import('./views/Twitch'))
|
||||
const Archive = lazy(() => import('./views/Archive'))
|
||||
const Settings = lazy(() => import('./views/Settings'))
|
||||
const LiveStream = lazy(() => import('./views/Livestream'))
|
||||
@@ -111,6 +112,14 @@ export const router = createHashRouter([
|
||||
</Suspense >
|
||||
)
|
||||
},
|
||||
{
|
||||
path: '/twitch',
|
||||
element: (
|
||||
<Suspense fallback={<CircularProgress />}>
|
||||
<Twitch />
|
||||
</Suspense >
|
||||
)
|
||||
},
|
||||
]
|
||||
},
|
||||
])
|
||||
77
frontend/src/views/Twitch.tsx
Normal file
77
frontend/src/views/Twitch.tsx
Normal file
@@ -0,0 +1,77 @@
|
||||
import {
|
||||
Chip,
|
||||
Container,
|
||||
Paper
|
||||
} from '@mui/material'
|
||||
import { matchW } from 'fp-ts/lib/Either'
|
||||
import { pipe } from 'fp-ts/lib/function'
|
||||
import { useAtomValue } from 'jotai'
|
||||
import { useState, useTransition } from 'react'
|
||||
import { serverURL } from '../atoms/settings'
|
||||
import LoadingBackdrop from '../components/LoadingBackdrop'
|
||||
import NoSubscriptions from '../components/subscriptions/NoSubscriptions'
|
||||
import SubscriptionsSpeedDial from '../components/subscriptions/SubscriptionsSpeedDial'
|
||||
import TwitchDialog from '../components/twitch/TwitchDialog'
|
||||
import { useToast } from '../hooks/toast'
|
||||
import useFetch from '../hooks/useFetch'
|
||||
import { ffetch } from '../lib/httpClient'
|
||||
|
||||
const TwitchView: React.FC = () => {
|
||||
const { pushMessage } = useToast()
|
||||
|
||||
const baseURL = useAtomValue(serverURL)
|
||||
|
||||
const [openDialog, setOpenDialog] = useState(false)
|
||||
|
||||
const { data: users, fetcher: refetch } = useFetch<Array<string>>('/twitch/users')
|
||||
|
||||
const [isPending, startTransition] = useTransition()
|
||||
|
||||
const deleteUser = async (user: string) => {
|
||||
const task = ffetch<void>(`${baseURL}/twitch/user/${user}`, {
|
||||
method: 'DELETE',
|
||||
})
|
||||
const either = await task()
|
||||
|
||||
pipe(
|
||||
either,
|
||||
matchW(
|
||||
(l) => pushMessage(l, 'error'),
|
||||
() => refetch()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<LoadingBackdrop isLoading={!users || isPending} />
|
||||
|
||||
<SubscriptionsSpeedDial onOpen={() => setOpenDialog(s => !s)} />
|
||||
|
||||
<TwitchDialog open={openDialog} onClose={() => {
|
||||
setOpenDialog(s => !s)
|
||||
refetch()
|
||||
}} />
|
||||
|
||||
{
|
||||
!users || users.length === 0 ?
|
||||
<NoSubscriptions /> :
|
||||
<Container maxWidth="xl" sx={{ mt: 4, mb: 8 }}>
|
||||
<Paper sx={{
|
||||
p: 2.5,
|
||||
minHeight: '80vh',
|
||||
}}>
|
||||
{users.map(user => (
|
||||
<Chip
|
||||
label={user}
|
||||
onDelete={() => startTransition(async () => await deleteUser(user))}
|
||||
/>
|
||||
))}
|
||||
</Paper>
|
||||
</Container>
|
||||
}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default TwitchView
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -31,6 +32,11 @@ type Config struct {
|
||||
OpenIdEmailWhitelist []string `yaml:"openid_email_whitelist"`
|
||||
FrontendPath string `yaml:"frontend_path"`
|
||||
AutoArchive bool `yaml:"auto_archive"`
|
||||
Twitch struct {
|
||||
ClientId string `yaml:"client_id"`
|
||||
ClientSecret string `yaml:"client_secret"`
|
||||
CheckInterval time.Duration `yaml:"check_interval"`
|
||||
} `yaml:"twitch"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -42,6 +48,7 @@ func Instance() *Config {
|
||||
if instance == nil {
|
||||
instanceOnce.Do(func() {
|
||||
instance = &Config{}
|
||||
instance.Twitch.CheckInterval = time.Minute * 5
|
||||
})
|
||||
}
|
||||
return instance
|
||||
|
||||
@@ -100,6 +100,7 @@ func (p *Process) Start() {
|
||||
templateReplacer.Replace(downloadTemplate),
|
||||
"--progress-template",
|
||||
templateReplacer.Replace(postprocessTemplate),
|
||||
"--no-exec",
|
||||
}
|
||||
|
||||
// if user asked to manually override the output path...
|
||||
|
||||
21
server/middleware/utils.go
Normal file
21
server/middleware/utils.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/openid"
|
||||
)
|
||||
|
||||
func ApplyAuthenticationByConfig(next http.Handler) http.Handler {
|
||||
handler := next
|
||||
|
||||
if config.Instance().RequireAuth {
|
||||
handler = Authenticated(handler)
|
||||
}
|
||||
if config.Instance().UseOpenId {
|
||||
handler = openid.Middleware(handler)
|
||||
}
|
||||
|
||||
return handler
|
||||
}
|
||||
@@ -164,7 +164,7 @@ func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
|
||||
|
||||
func (s *Service) GetVersion(ctx context.Context) (string, string, error) {
|
||||
//TODO: load from realease properties file, or anything else outside code
|
||||
const CURRENT_RPC_VERSION = "3.2.5"
|
||||
const CURRENT_RPC_VERSION = "3.2.6"
|
||||
|
||||
result := make(chan string, 1)
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/status"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/subscription"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/subscription/task"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/twitch"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/user"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
@@ -51,6 +52,7 @@ type serverConfig struct {
|
||||
db *sql.DB
|
||||
mq *internal.MessageQueue
|
||||
lm *livestream.Monitor
|
||||
tm *twitch.Monitor
|
||||
}
|
||||
|
||||
// TODO: change scope
|
||||
@@ -115,17 +117,33 @@ func RunBlocking(rc *RunConfig) {
|
||||
go lm.Schedule()
|
||||
go lm.Restore()
|
||||
|
||||
srv := newServer(serverConfig{
|
||||
tm := twitch.NewMonitor(
|
||||
twitch.NewAuthenticationManager(
|
||||
config.Instance().Twitch.ClientId,
|
||||
config.Instance().Twitch.ClientSecret,
|
||||
),
|
||||
)
|
||||
go tm.Monitor(
|
||||
context.TODO(),
|
||||
config.Instance().Twitch.CheckInterval,
|
||||
twitch.DEFAULT_DOWNLOAD_HANDLER(mdb, mq),
|
||||
)
|
||||
go tm.Restore()
|
||||
|
||||
scfg := serverConfig{
|
||||
frontend: rc.App,
|
||||
swagger: rc.Swagger,
|
||||
mdb: mdb,
|
||||
mq: mq,
|
||||
db: db,
|
||||
lm: lm,
|
||||
})
|
||||
tm: tm,
|
||||
}
|
||||
|
||||
go gracefulShutdown(srv, mdb)
|
||||
go autoPersist(time.Minute*5, mdb, lm)
|
||||
srv := newServer(scfg)
|
||||
|
||||
go gracefulShutdown(srv, &scfg)
|
||||
go autoPersist(time.Minute*5, mdb, lm, tm)
|
||||
|
||||
var (
|
||||
network = "tcp"
|
||||
@@ -188,12 +206,7 @@ func newServer(c serverConfig) *http.Server {
|
||||
|
||||
// Filebrowser routes
|
||||
r.Route("/filebrowser", func(r chi.Router) {
|
||||
if config.Instance().RequireAuth {
|
||||
r.Use(middlewares.Authenticated)
|
||||
}
|
||||
if config.Instance().UseOpenId {
|
||||
r.Use(openid.Middleware)
|
||||
}
|
||||
r.Use(middlewares.ApplyAuthenticationByConfig)
|
||||
r.Post("/downloaded", filebrowser.ListDownloaded)
|
||||
r.Post("/delete", filebrowser.DeleteFile)
|
||||
r.Get("/d/{id}", filebrowser.DownloadFile)
|
||||
@@ -235,10 +248,18 @@ func newServer(c serverConfig) *http.Server {
|
||||
// Subscriptions
|
||||
r.Route("/subscriptions", subscription.Container(c.db, cronTaskRunner).ApplyRouter())
|
||||
|
||||
// Twitch
|
||||
r.Route("/twitch", func(r chi.Router) {
|
||||
r.Use(middlewares.ApplyAuthenticationByConfig)
|
||||
r.Get("/users", twitch.GetMonitoredUsers(c.tm))
|
||||
r.Post("/user", twitch.MonitorUserHandler(c.tm))
|
||||
r.Delete("/user/{user}", twitch.DeleteUser(c.tm))
|
||||
})
|
||||
|
||||
return &http.Server{Handler: r}
|
||||
}
|
||||
|
||||
func gracefulShutdown(srv *http.Server, db *internal.MemoryDB) {
|
||||
func gracefulShutdown(srv *http.Server, cfg *serverConfig) {
|
||||
ctx, stop := signal.NotifyContext(context.Background(),
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
@@ -250,7 +271,9 @@ func gracefulShutdown(srv *http.Server, db *internal.MemoryDB) {
|
||||
slog.Info("shutdown signal received")
|
||||
|
||||
defer func() {
|
||||
db.Persist()
|
||||
cfg.mdb.Persist()
|
||||
cfg.lm.Persist()
|
||||
cfg.tm.Persist()
|
||||
|
||||
stop()
|
||||
srv.Shutdown(context.Background())
|
||||
@@ -258,8 +281,14 @@ func gracefulShutdown(srv *http.Server, db *internal.MemoryDB) {
|
||||
}()
|
||||
}
|
||||
|
||||
func autoPersist(d time.Duration, db *internal.MemoryDB, lm *livestream.Monitor) {
|
||||
func autoPersist(
|
||||
d time.Duration,
|
||||
db *internal.MemoryDB,
|
||||
lm *livestream.Monitor,
|
||||
tm *twitch.Monitor,
|
||||
) {
|
||||
for {
|
||||
time.Sleep(d)
|
||||
if err := db.Persist(); err != nil {
|
||||
slog.Warn("failed to persisted session", slog.Any("err", err))
|
||||
}
|
||||
@@ -267,7 +296,10 @@ func autoPersist(d time.Duration, db *internal.MemoryDB, lm *livestream.Monitor)
|
||||
slog.Warn(
|
||||
"failed to persisted livestreams monitor session", slog.Any("err", err.Error()))
|
||||
}
|
||||
if err := tm.Persist(); err != nil {
|
||||
slog.Warn(
|
||||
"failed to persisted twitch monitor session", slog.Any("err", err.Error()))
|
||||
}
|
||||
slog.Debug("sucessfully persisted session")
|
||||
time.Sleep(d)
|
||||
}
|
||||
}
|
||||
|
||||
75
server/twitch/auth.go
Normal file
75
server/twitch/auth.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package twitch
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
const authURL = "https://id.twitch.tv/oauth2/token"
|
||||
|
||||
type AuthResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
TokenType string `json:"token_type"`
|
||||
}
|
||||
|
||||
type AccessToken struct {
|
||||
Token string
|
||||
Expiry time.Time
|
||||
}
|
||||
|
||||
type AuthenticationManager struct {
|
||||
clientId string
|
||||
clientSecret string
|
||||
accesToken *AccessToken
|
||||
}
|
||||
|
||||
func NewAuthenticationManager(clientId, clientSecret string) *AuthenticationManager {
|
||||
return &AuthenticationManager{
|
||||
clientId: clientId,
|
||||
clientSecret: clientSecret,
|
||||
accesToken: &AccessToken{},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AuthenticationManager) GetAccessToken() (*AccessToken, error) {
|
||||
if a.accesToken != nil && a.accesToken.Token != "" && a.accesToken.Expiry.After(time.Now()) {
|
||||
return a.accesToken, nil
|
||||
}
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("client_id", a.clientId)
|
||||
data.Set("client_secret", a.clientSecret)
|
||||
data.Set("grant_type", "client_credentials")
|
||||
|
||||
resp, err := http.PostForm(authURL, data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("errore richiesta token: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("status non OK: %s", resp.Status)
|
||||
}
|
||||
|
||||
var auth AuthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&auth); err != nil {
|
||||
return nil, fmt.Errorf("errore decoding JSON: %w", err)
|
||||
}
|
||||
|
||||
token := &AccessToken{
|
||||
Token: auth.AccessToken,
|
||||
Expiry: time.Now().Add(time.Duration(auth.ExpiresIn) * time.Second),
|
||||
}
|
||||
|
||||
a.accesToken = token
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (a *AuthenticationManager) GetClientId() string {
|
||||
return a.clientId
|
||||
}
|
||||
91
server/twitch/client.go
Normal file
91
server/twitch/client.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package twitch
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const twitchAPIURL = "https://api.twitch.tv/helix"
|
||||
|
||||
type Client struct {
|
||||
authenticationManager AuthenticationManager
|
||||
}
|
||||
|
||||
func NewTwitchClient(am *AuthenticationManager) *Client {
|
||||
return &Client{
|
||||
authenticationManager: *am,
|
||||
}
|
||||
}
|
||||
|
||||
type streamResp struct {
|
||||
Data []struct {
|
||||
ID string `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
Title string `json:"title"`
|
||||
GameName string `json:"game_name"`
|
||||
StartedAt string `json:"started_at"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(endpoint string, params map[string]string) ([]byte, error) {
|
||||
token, err := c.authenticationManager.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reqURL := twitchAPIURL + endpoint
|
||||
req, err := http.NewRequest("GET", reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
for k, v := range params {
|
||||
q.Set(k, v)
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
req.Header.Set("Client-Id", c.authenticationManager.GetClientId())
|
||||
req.Header.Set("Authorization", "Bearer "+token.Token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func (c *Client) PollStream(channel string, liveChannel chan<- *StreamInfo) error {
|
||||
body, err := c.doRequest("/streams", map[string]string{"user_login": channel})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var sr streamResp
|
||||
if err := json.Unmarshal(body, &sr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(sr.Data) == 0 {
|
||||
liveChannel <- &StreamInfo{UserName: channel, IsLive: false}
|
||||
return nil
|
||||
}
|
||||
|
||||
s := sr.Data[0]
|
||||
started, _ := time.Parse(time.RFC3339, s.StartedAt)
|
||||
|
||||
liveChannel <- &StreamInfo{
|
||||
ID: s.ID,
|
||||
UserName: s.UserName,
|
||||
Title: s.Title,
|
||||
GameName: s.GameName,
|
||||
StartedAt: started,
|
||||
IsLive: true,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
149
server/twitch/monitor.go
Normal file
149
server/twitch/monitor.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package twitch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"iter"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/config"
|
||||
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/internal"
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
liveChannel chan *StreamInfo
|
||||
monitored map[string]*Client
|
||||
lastState map[string]bool
|
||||
mu sync.RWMutex
|
||||
authenticationManager *AuthenticationManager
|
||||
}
|
||||
|
||||
func NewMonitor(authenticationManager *AuthenticationManager) *Monitor {
|
||||
return &Monitor{
|
||||
liveChannel: make(chan *StreamInfo, 16),
|
||||
monitored: make(map[string]*Client),
|
||||
lastState: make(map[string]bool),
|
||||
authenticationManager: authenticationManager,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Monitor) Add(user string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.monitored[user] = NewTwitchClient(m.authenticationManager)
|
||||
slog.Info("added user to twitch monitor", slog.String("user", user))
|
||||
}
|
||||
|
||||
func (m *Monitor) Monitor(ctx context.Context, interval time.Duration, handler func(url string) error) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
m.mu.RLock()
|
||||
for user, client := range m.monitored {
|
||||
u := user
|
||||
c := client
|
||||
|
||||
go func() {
|
||||
if err := c.PollStream(u, m.liveChannel); err != nil {
|
||||
slog.Error("polling failed", slog.String("user", u), slog.Any("err", err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
|
||||
case stream := <-m.liveChannel:
|
||||
wasLive := m.lastState[stream.UserName]
|
||||
if stream.IsLive && !wasLive {
|
||||
slog.Info("stream went live", slog.String("user", stream.UserName))
|
||||
if err := handler(fmt.Sprintf("https://www.twitch.tv/%s", stream.UserName)); err != nil {
|
||||
slog.Error("handler failed", slog.String("user", stream.UserName), slog.Any("err", err))
|
||||
}
|
||||
}
|
||||
m.lastState[stream.UserName] = stream.IsLive
|
||||
|
||||
case <-ctx.Done():
|
||||
slog.Info("stopping twitch monitor")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Monitor) GetMonitoredUsers() iter.Seq[string] {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return maps.Keys(m.monitored)
|
||||
}
|
||||
|
||||
func (m *Monitor) DeleteUser(user string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
delete(m.monitored, user)
|
||||
delete(m.lastState, user)
|
||||
}
|
||||
|
||||
func DEFAULT_DOWNLOAD_HANDLER(db *internal.MemoryDB, mq *internal.MessageQueue) func(url string) error {
|
||||
return func(url string) error {
|
||||
p := &internal.Process{
|
||||
Url: url,
|
||||
Livestream: true,
|
||||
Params: []string{"--downloader", "ffmpeg", "--no-part"},
|
||||
}
|
||||
db.Set(p)
|
||||
mq.Publish(p)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Monitor) Persist() error {
|
||||
filename := filepath.Join(config.Instance().SessionFilePath, "twitch-monitor.dat")
|
||||
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
enc := gob.NewEncoder(f)
|
||||
users := make([]string, 0, len(m.monitored))
|
||||
|
||||
for user := range m.monitored {
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return enc.Encode(users)
|
||||
}
|
||||
|
||||
func (m *Monitor) Restore() error {
|
||||
filename := filepath.Join(config.Instance().SessionFilePath, "twitch-monitor.dat")
|
||||
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
dec := gob.NewDecoder(f)
|
||||
var users []string
|
||||
if err := dec.Decode(&users); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.monitored = make(map[string]*Client)
|
||||
for _, user := range users {
|
||||
m.monitored[user] = NewTwitchClient(m.authenticationManager)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
65
server/twitch/rest.go
Normal file
65
server/twitch/rest.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package twitch
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type addUserReq struct {
|
||||
User string `json:"user"`
|
||||
}
|
||||
|
||||
func MonitorUserHandler(m *Monitor) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req addUserReq
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
m.Add(req.User)
|
||||
|
||||
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetMonitoredUsers(m *Monitor) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
it := m.GetMonitoredUsers()
|
||||
|
||||
users := slices.Collect(it)
|
||||
if users == nil {
|
||||
users = make([]string, 0)
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(users); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteUser(m *Monitor) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user := chi.URLParam(r, "user")
|
||||
|
||||
if user == "" {
|
||||
http.Error(w, "empty user", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
m.DeleteUser(user)
|
||||
|
||||
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
20
server/twitch/types.go
Normal file
20
server/twitch/types.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package twitch
|
||||
|
||||
import "time"
|
||||
|
||||
type StreamInfo struct {
|
||||
ID string
|
||||
UserName string
|
||||
Title string
|
||||
GameName string
|
||||
StartedAt time.Time
|
||||
IsLive bool
|
||||
}
|
||||
|
||||
type VodInfo struct {
|
||||
ID string
|
||||
Title string
|
||||
URL string
|
||||
Duration string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
Reference in New Issue
Block a user