Channels now have Channel.channel_downloads. A Channel can have many Downloads.

This commit is contained in:
Roland 2024-06-25 22:12:13 -06:00
parent 91d4d0d15b
commit 50d95dad19
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
43 changed files with 1401 additions and 658 deletions

View file

@ -1,20 +1,21 @@
import React, {useState} from "react";
import {Dimmer, Dropdown, Grid, StatisticLabel, StatisticValue, TableCell, TableRow,} from "semantic-ui-react";
import {
AccordionContent,
AccordionTitle,
Grid,
StatisticLabel,
StatisticValue,
TableCell,
TableRow,
} from "semantic-ui-react";
import {createChannel, deleteChannel, downloadChannel, refreshChannel, updateChannel, validateRegex} from "../api";
createChannel,
createChannelDownload,
deleteChannel,
deleteDownload,
refreshChannel,
updateChannel,
updateChannelDownload
} from "../api";
import {
APIButton,
BackButton,
DirectoryInput,
ErrorMessage,
frequencyOptions,
HelpHeader,
humanFileSize,
RequiredAsterisk,
SearchInput,
@ -22,18 +23,31 @@ import {
secondsToFullDuration,
Toggle,
useTitle,
validURL,
WROLModeMessage
} from "./Common";
import {Link, useNavigate, useParams} from "react-router-dom";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
import {useChannel, useChannels, useOneQuery} from "../hooks/customHooks";
import {useChannel, useChannels, useOneQuery, useWROLMode} from "../hooks/customHooks";
import _ from "lodash";
import {Accordion, Button, Form, FormField, FormGroup, FormInput, Header, Loader, Segment, Statistic} from "./Theme";
import Dropdown from "semantic-ui-react/dist/commonjs/modules/Dropdown";
import {
Button,
Form,
FormField,
FormGroup,
FormInput,
Header,
Loader,
Modal,
ModalContent,
Segment,
Statistic
} from "./Theme";
import {Media, ThemeContext} from "../contexts/contexts";
import {SortableTable} from "./SortableTable";
import {toast} from "react-semantic-toasts-2";
import {RecurringDownloadsTable} from "./admin/Downloads";
import {TagsSelector, useTags} from "../Tags";
function ChannelStatistics({statistics}) {
@ -65,13 +79,13 @@ function ChannelStatistics({statistics}) {
function ChannelPage({create, header}) {
const [disabled, setDisabled] = useState(false);
const [validRegex, setValidRegex] = useState(true);
const [activeIndex, setActiveIndex] = useState(-1);
const [errors, setErrors] = useState({});
const [error, setError] = useState(false);
const [success, setSuccess] = useState(false);
const [messageHeader, setMessageHeader] = useState();
const [messageContent, setMessageContent] = useState();
const [downloadModalOpen, setDownloadModalOpen] = useState(false);
const [rssDownloadOpen, setRssDownloadOpen] = useState(false);
const navigate = useNavigate();
const {channelId} = useParams();
@ -87,12 +101,6 @@ function ChannelPage({create, header}) {
return <Loader active/>;
}
const checkRegex = async (e, {value}) => {
changeValue('match_regex', value);
const valid = await validateRegex(value);
setValidRegex(valid);
}
const handleCheckbox = (e, {name, checked}) => {
if (e) {
e.preventDefault();
@ -130,8 +138,6 @@ function ChannelPage({create, header}) {
directory: channel.directory,
mkdir: channel.mkdir,
url: channel.url,
download_frequency: channel.download_frequency,
match_regex: channel.match_regex,
};
setDisabled(true);
@ -208,29 +214,6 @@ function ChannelPage({create, header}) {
}
}
const handleAdvancedClick = (e, {index}) => {
setActiveIndex(activeIndex === index ? -1 : index);
}
const handleDownloadChannel = async (e) => {
if (e) {
e.preventDefault();
}
try {
const response = await downloadChannel(channelId);
if (response.status === 204) {
toast({
type: 'success',
title: 'Download Created',
description: 'Channel download has been started.',
time: 5000,
});
}
} catch (e) {
console.error(e);
}
}
const handleRefreshChannel = async (e) => {
if (e) {
e.preventDefault();
@ -257,6 +240,13 @@ function ChannelPage({create, header}) {
}
}
let channelDownloads = channel && channel.channel_downloads ? channel.channel_downloads : null;
const afterNewDownloadSave = async () => {
await fetchChannel();
setDownloadModalOpen(false);
}
return <>
<BackButton/>
{!create &&
@ -326,7 +316,6 @@ function ChannelPage({create, header}) {
label="URL"
name="url"
type="url"
disabled={disabled}
placeholder='https://example.com/channel/videos'
error={errors.url}
value={channel.url}
@ -335,51 +324,6 @@ function ChannelPage({create, header}) {
</FormField>
</FormGroup>
<FormGroup>
<FormField>
<label>Download Frequency</label>
<Dropdown selection clearable
name='download_frequency'
placeholder='Frequency'
error={errors.download_frequency}
value={channel.download_frequency}
disabled={disabled || !channel.url}
options={frequencyOptions}
onChange={handleInputChange}
/>
</FormField>
</FormGroup>
<Accordion style={{marginBottom: '1em'}}>
<AccordionTitle
onClick={handleAdvancedClick}
index={0}
active={activeIndex === 0}
>
<Icon name='dropdown'/>
Advanced Settings
</AccordionTitle>
<AccordionContent active={activeIndex === 0}>
<Segment secondary>
<Header as="h4">
The following settings are encouraged by default, modify them at your own risk.
</Header>
<FormField>
<FormInput
label="Title Match Regex"
name="match_regex"
type="text"
disabled={disabled}
error={!validRegex}
placeholder='.*([Nn]ame Matching).*'
value={channel.match_regex}
onChange={checkRegex}
/>
</FormField>
</Segment>
</AccordionContent>
</Accordion>
<Message error
header={messageHeader}
content={messageContent}
@ -401,18 +345,14 @@ function ChannelPage({create, header}) {
confirmButton='Delete'
onClick={handleDelete}
obeyWROLMode={true}
style={{marginTop: '1em'}}
>Delete</APIButton>
<APIButton
color='green'
size='small'
onClick={handleDownloadChannel}
obeyWROLMode={true}
>Download</APIButton>
<APIButton
color='blue'
size='small'
onClick={handleRefreshChannel}
obeyWROLMode={true}
style={{marginTop: '1em'}}
>Refresh</APIButton>
</>
}
@ -433,12 +373,218 @@ function ChannelPage({create, header}) {
</Form>
</Segment>
{!create && <Segment>
<Grid columns={2}>
<Grid.Row>
<Grid.Column>
<Header as='h1'>Downloads</Header>
</Grid.Column>
<Grid.Column>
<Button floated='right'
onClick={() => setDownloadModalOpen(!downloadModalOpen)}
>
New Download
</Button>
</Grid.Column>
</Grid.Row>
</Grid>
<Modal open={downloadModalOpen} closeIcon onClose={() => setDownloadModalOpen(false)}>
<ModalContent>
<Header as='h2'>New Channel Download</Header>
<ChannelDownloadForm
channel_id={channelId}
afterSave={afterNewDownloadSave}
closeModal={() => setDownloadModalOpen(false)}
/>
</ModalContent>
</Modal>
<RecurringDownloadsTable downloads={channelDownloads} fetchDownloads={fetchChannel}/>
</Segment>}
<div style={{marginTop: '2em'}}>
{channel.statistics && <ChannelStatistics statistics={channel.statistics}/>}
</div>
</>
}
export function ChannelDownloadForm({channel_id, afterSave, closeModal, download}) {
download = download || {};
channel_id = download ? download.channel_id || channel_id : channel_id;
const editing = download && !_.isEmpty(download);
const settings = download.settings ? download.settings : {};
const oldTagNames = download && download.settings && download.settings.tag_names ? download.settings.tag_names : [];
const [state, setState] = React.useState({
url: download ? download.url : '',
frequency: download.frequency ? download.frequency : 604800,
title_match: settings.title_match ? settings.title_match : '',
title_exclude: settings.title_exclude ? settings.title_exclude : '',
})
const [disabled, setDisabled] = React.useState(useWROLMode());
const [loading, setLoading] = React.useState(false);
const [urlValid, setUrlValid] = React.useState(true);
const [tagNames, setTagNames] = React.useState(oldTagNames);
const handleInputChange = (e, {name, value}) => {
if (e) {
e.preventDefault();
}
if (name === 'url') {
setUrlValid(validURL(value));
}
setState({...state, [name]: value});
}
const handleSubmit = async () => {
setLoading(true);
setDisabled(true);
try {
let response;
if (editing) {
response = await updateChannelDownload(
channel_id,
download.id,
state.url,
state.frequency,
state.title_match,
state.title_exclude,
tagNames,
);
} else {
response = await createChannelDownload(
channel_id,
state.url,
state.frequency,
state.title_match,
state.title_exclude,
tagNames,
);
}
if (!response.ok) {
throw 'Creating download failed';
}
if (afterSave) {
await afterSave();
}
} finally {
setLoading(false);
setDisabled(false);
}
}
const handleClose = (e) => {
if (e) e.preventDefault();
closeModal();
}
const handleDelete = async () => {
await deleteDownload(download.id);
if (afterSave) {
afterSave();
}
if (closeModal) {
closeModal();
}
};
const handleAddTag = (tagName) => {
setTagNames([...tagNames, tagName]);
}
const handleUntag = (tagName) => {
setTagNames(tagNames.filter(i => i !== tagName));
}
const deleteDownloadButton = <APIButton
color='red'
floated='left'
onClick={handleDelete}
confirmContent='Are you sure you want to delete this download?'
confirmButton='Delete'
disabled={disabled}
obeyWROLMode={true}
>Delete</APIButton>;
return <Form onSubmit={handleSubmit}>
{loading && <Dimmer active><Loader/></Dimmer>}
<Grid columns={2} stackable>
<Grid.Row>
<Grid.Column width={12}>
<FormInput
label='URL'
name='url'
type='url'
value={state.url}
placeholder='https://example.com/videos'
onChange={handleInputChange}
error={!urlValid}
/>
</Grid.Column>
<Grid.Column width={4}>
<FormField>
<label>Download Frequency</label>
<Dropdown selection
name='frequency'
placeholder='Frequency'
value={state.frequency}
disabled={disabled}
options={frequencyOptions.slice(1)}
onChange={handleInputChange}
/>
</FormField>
</Grid.Column>
</Grid.Row>
<Grid.Row>
<Grid.Column>
<HelpHeader
headerSize='h4'
headerContent='Title Match Words'
popupContent='List of words, separated by commas, that Video titles must contain to be downloaded.'
popupPosition='bottom center'
/>
<FormInput
name="title_match"
type="text"
disabled={disabled}
placeholder='Shelter,Solar Power'
value={state.title_match}
onChange={handleInputChange}
/>
</Grid.Column>
<Grid.Column>
<HelpHeader
headerSize='h4'
headerContent='Title Exclusion Words'
popupContent='List of words, separated by commas, that may not appear in video titles to be downloaded.'
popupPosition='bottom center'
/>
<FormInput
name="title_exclude"
type="text"
disabled={disabled}
placeholder='Giveaway,Prize'
value={state.title_exclude}
onChange={handleInputChange}
/>
</Grid.Column>
</Grid.Row>
<Grid.Row columns={1}>
<Grid.Column>
<TagsSelector selectedTagNames={tagNames} onAdd={handleAddTag} onRemove={handleUntag}/>
</Grid.Column>
</Grid.Row>
<Grid.Row columns={1}>
<Grid.Column textAlign='right'>
{editing && deleteDownloadButton}
<Button onClick={handleClose}>Cancel</Button>
<Button color='violet' disabled={!urlValid}>Save</Button>
</Grid.Column>
</Grid.Row>
</Grid>
</Form>
}
export function ChannelEditPage(props) {
return <ChannelPage header="Edit Channel" {...props}/>;
}
@ -463,7 +609,7 @@ function ChannelRow({channel}) {
{channel.video_count}
</TableCell>
<TableCell>
{channel.url && channel.download_frequency ? secondsToFrequency(channel.download_frequency) : null}
{channel.url && channel.minimum_frequency ? secondsToFrequency(channel.minimum_frequency) : null}
</TableCell>
<TableCell>
{channel.size ? humanFileSize(channel.size) : null}
@ -532,7 +678,7 @@ export function ChannelsPage() {
const headers = [
{key: 'name', text: 'Name', sortBy: 'name', width: 8},
{key: 'video_count', text: 'Videos', sortBy: 'video_count', width: 2},
{key: 'download_frequency', text: 'Download Frequency', sortBy: 'download_frequency', width: 2},
{key: 'download_frequency', text: 'Download Frequency', sortBy: 'minimum_frequency', width: 2},
{key: 'size', text: 'Size', sortBy: 'size', width: 2},
{key: 'manage', text: 'Manage', width: 2},
];

View file

@ -735,23 +735,23 @@ export function CardPoster({to, file}) {
}
}
export function HelpPopup({icon, size, content, position, iconSize = 'small'}) {
export function HelpPopup({icon = 'question', size = null, content, position = 'left center', iconSize = 'small'}) {
return <Popup
content={content}
size={size || null}
position={position || 'left center'}
trigger={<Icon circular link name={icon || 'question'} size={iconSize}
size={size}
position={position}
trigger={<Icon circular link name={icon} size={iconSize}
style={{marginLeft: '0.25em', marginRight: '0.25em'}}
/>}
/>
}
export function HelpHeader({icon, headerSize, iconSize, headerContent, popupContent}) {
export function HelpHeader({icon, headerSize = 'h2', iconSize, headerContent, popupContent, popupPosition = 'top center'}) {
return <div className='inline-header'>
<Header as={headerSize || 'h2'}>{headerContent}</Header>
<Header as={headerSize}>{headerContent}</Header>
<span>
<HelpPopup content={popupContent} size={iconSize} icon={icon}/>
</span>
<HelpPopup content={popupContent} size={iconSize} icon={icon} position={popupPosition}/>
</span>
</div>
}
@ -1788,3 +1788,9 @@ export function Breadcrumbs({crumbs, size = undefined}) {
))}
</Breadcrumb>
}
const validUrlRegex = /^(http|https):\/\/[^ "]+$/;
export function validURL(url) {
return !(url && !validUrlRegex.test(url));
}

View file

@ -1,6 +1,14 @@
import React, {useContext, useState} from "react";
import {getDownloaders, postDownload} from "../api";
import {APIButton, DirectorySearch, frequencyOptions, HelpPopup, rssFrequencyOptions, WROLModeMessage} from "./Common";
import {
APIButton,
DirectorySearch,
frequencyOptions,
HelpPopup,
rssFrequencyOptions,
validURL,
WROLModeMessage
} from "./Common";
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import {ThemeContext} from "../contexts/contexts";
@ -11,8 +19,6 @@ import {TagsSelector} from "../Tags";
import {toast} from "react-semantic-toasts-2";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
const validUrl = /^(http|https):\/\/[^ "]+$/;
class Downloader extends React.Component {
constructor(props) {
super(props);
@ -58,9 +64,7 @@ class Downloader extends React.Component {
let urls = this.state.urls.split(/\r?\n/);
let valid = true;
urls.forEach((url) => {
if (valid && url && !validUrl.test(url)) {
valid = false;
}
valid = validURL(url);
})
this.setState({valid});
}
@ -192,7 +196,7 @@ class ChannelDownload extends React.Component {
handleUrlChange = (e, {value}) => {
e.preventDefault();
let ready = value && validUrl.test(value);
let ready = value && validURL(value);
this.setState({url: value, ready, success: false});
}

View file

@ -242,7 +242,7 @@ function VideosStatistics() {
];
const commentsNames = [
{key: 'have_comments', label: 'Have Comments'},
{key: 'no_comments', label: 'Missing Comments'},
{key: 'missing_comments', label: 'Missing Comments'},
{key: 'failed_comments', label: 'Failed Comments'},
];
const historicalNames = [

View file

@ -4,6 +4,7 @@ import {
deleteDownload,
deleteOnceDownloads,
killDownload,
putDownload,
restartDownload,
retryOnceDownloads
} from "../../api";
@ -12,13 +13,18 @@ import {
APIButton,
DisableDownloadsToggle,
ErrorMessage,
frequencyOptions,
isoDatetimeToElapsedPopup,
secondsToFrequency,
useTitle,
validURL,
WROLModeMessage
} from "../Common";
import {
Button as SButton,
Dimmer,
Dropdown,
Grid,
Label,
Loader,
PlaceholderLine,
@ -29,9 +35,23 @@ import {
TableHeaderCell,
TableRow
} from "semantic-ui-react";
import {Button, Header, Modal, ModalActions, ModalContent, ModalHeader, Placeholder, Segment, Table} from "../Theme";
import {useDownloads} from "../../hooks/customHooks";
import {
Button,
Form,
FormField,
FormInput,
Header,
Modal,
ModalActions,
ModalContent,
ModalHeader,
Placeholder,
Segment,
Table
} from "../Theme";
import {useDownloads, useWROLMode} from "../../hooks/customHooks";
import {toast} from "react-semantic-toasts-2";
import {ChannelDownloadForm} from "../Channels";
function ClearDownloadsButton({callback}) {
async function localClearDownloads() {
@ -91,11 +111,134 @@ function DeleteOnceDownloadsButton({callback}) {
>Delete</APIButton>
}
export function DownloadEditForm({afterSave, closeModal, download}) {
const [state, setState] = React.useState({
url: download.url || '',
frequency: download.frequency,
excluded_urls: download.excluded_urls || '',
})
const [disabled, setDisabled] = React.useState(useWROLMode());
const [loading, setLoading] = React.useState(false);
const [urlValid, setUrlValid] = React.useState(true);
const handleInputChange = (e, {name, value}) => {
if (e) {
e.preventDefault();
}
if (name === 'url') {
setUrlValid(validURL(value));
}
setState({...state, [name]: value});
}
const handleSubmit = async () => {
setLoading(true);
setDisabled(true);
try {
const response = await putDownload(
[state.url],
download.id,
download.downloader,
download.sub_downloader,
state.frequency,
state.excluded_urls,
);
if (!response.ok) {
throw 'Updating download failed';
}
} finally {
setLoading(false);
setDisabled(false);
if (afterSave) {
afterSave();
}
}
}
const handleClose = (e) => {
if (e) e.preventDefault();
closeModal();
}
const handleDelete = async () => {
await deleteDownload(download.id);
if (afterSave) {
afterSave();
}
if (closeModal) {
closeModal();
}
};
const deleteDownloadButton = <APIButton
color='red'
floated='left'
onClick={handleDelete}
confirmContent='Are you sure you want to delete this download?'
confirmButton='Delete'
disabled={disabled}
obeyWROLMode={true}
>Delete</APIButton>;
return <Form onSubmit={handleSubmit}>
{loading && <Dimmer active><Loader/></Dimmer>}
<Grid columns={2} stackable>
<Grid.Row>
<Grid.Column width={12}>
<FormInput
label='URL'
name='url'
type='url'
value={state.url}
placeholder='https://example.com/videos'
onChange={handleInputChange}
error={!urlValid}
/>
</Grid.Column>
<Grid.Column width={4}>
<FormField>
<label>Download Frequency</label>
<Dropdown selection
name='frequency'
placeholder='Frequency'
value={state.frequency}
disabled={disabled}
options={frequencyOptions.slice(1)}
onChange={handleInputChange}
/>
</FormField>
</Grid.Column>
</Grid.Row>
<Grid.Row>
<Grid.Column>
<FormInput
label='Excluded URLs'
name='excluded_urls'
type='text'
value={state.excluded_urls}
onChange={handleInputChange}
/>
</Grid.Column>
</Grid.Row>
<Grid.Row columns={1}>
<Grid.Column textAlign='right'>
{deleteDownloadButton}
<Button onClick={handleClose}>Cancel</Button>
<Button color='violet' disabled={!urlValid}>Save</Button>
</Grid.Column>
</Grid.Row>
</Grid>
</Form>
}
class RecurringDownloadRow extends React.Component {
constructor(props) {
super(props);
this.state = {
errorModalOpen: false,
editModalOpen: false,
}
}
@ -133,7 +276,17 @@ class RecurringDownloadRow extends React.Component {
}
render() {
let {url, frequency, last_successful_download, status, location, next_download, error} = this.props;
let {
url,
frequency,
last_successful_download,
status,
location,
next_download,
error,
downloader,
channel_id
} = this.props;
const {errorModalOpen} = this.state;
const link = location ?
@ -167,6 +320,10 @@ class RecurringDownloadRow extends React.Component {
/>
</>;
const editButton = <>
<Button icon='edit' onClick={() => this.setState({editModalOpen: true})}/>
</>
const restartButton = <>
<APIButton
color='green'
@ -184,6 +341,28 @@ class RecurringDownloadRow extends React.Component {
next = isoDatetimeToElapsedPopup(next_download);
}
const handleClose = () => this.setState({editModalOpen: false});
const afterSave = () => {
if (this.props.fetchDownloads) {
this.props.fetchDownloads();
}
handleClose();
}
const editModal = <Modal closeIcon
open={this.state.editModalOpen}
onClose={handleClose}
>
<ModalHeader>Edit Download</ModalHeader>
<ModalContent>
{downloader === 'video_channel' ?
<ChannelDownloadForm download={this.props} closeModal={handleClose}
afterSave={afterSave}/>
: <DownloadEditForm download={this.props} closeModal={handleClose}
afterSave={afterSave}/>
}
</ModalContent>
</Modal>;
return <TableRow>
<TableCell className='column-ellipsis'>
{link(url)}
@ -194,11 +373,12 @@ class RecurringDownloadRow extends React.Component {
{status === 'pending' ? <Loader active inline size='tiny'/> : null}
</TableCell>
<TableCell>{next}</TableCell>
<TableCell>
<TableCell textAlign='right'>
{error && errorModal}
{deleteButton}
{editButton}
{restartButton}
</TableCell>
{editModal}
</TableRow>
}
}
@ -348,14 +528,14 @@ export function OnceDownloadsTable({downloads, fetchDownloads}) {
export function RecurringDownloadsTable({downloads, fetchDownloads}) {
if (downloads && downloads.length >= 1) {
return <Table className='table-ellipsis'>
return <Table compact className='table-ellipsis'>
<TableHeader>
<TableRow>
<TableHeaderCell width={8}>URL</TableHeaderCell>
<TableHeaderCell width={2}>Download Frequency</TableHeaderCell>
<TableHeaderCell width={2}>Completed At</TableHeaderCell>
<TableHeaderCell width={1}>Next</TableHeaderCell>
<TableHeaderCell width={3}>Control</TableHeaderCell>
<TableHeaderCell width={3} textAlign='right'>Control</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>