2022-05-20 15:15:31 -06:00
|
|
|
import React, {useState} from "react";
|
2021-12-30 20:45:11 -07:00
|
|
|
import {getDownloaders, postDownload} from "../api";
|
2022-05-20 15:15:31 -06:00
|
|
|
import {frequencyOptions, rssFrequencyOptions, WROLModeMessage} from "./Common";
|
|
|
|
|
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
|
|
|
|
|
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
2022-08-06 05:22:07 +00:00
|
|
|
import {ThemeContext} from "../contexts/contexts";
|
|
|
|
|
import {Button, Form, FormInput, Header, Loader, TextArea} from "./Theme";
|
2022-09-29 14:58:16 -06:00
|
|
|
import {ButtonGroup, FormDropdown} from "semantic-ui-react";
|
2021-12-20 16:08:13 -07:00
|
|
|
|
|
|
|
|
const validUrl = /^(http|https):\/\/[^ "]+$/;
|
|
|
|
|
|
|
|
|
|
class Downloader extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2022-09-29 14:58:16 -06:00
|
|
|
urls: '', valid: true, pending: false, downloader: props.downloader,
|
2021-12-20 16:08:13 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
submitDownload = async () => {
|
2021-12-30 20:45:11 -07:00
|
|
|
let {urls, downloader} = this.state;
|
|
|
|
|
if (urls) {
|
2021-12-21 09:54:38 -07:00
|
|
|
this.setState({pending: true});
|
|
|
|
|
try {
|
2021-12-30 20:45:11 -07:00
|
|
|
let response = await postDownload(urls, downloader);
|
2021-12-21 09:54:38 -07:00
|
|
|
if (response.status === 204) {
|
|
|
|
|
this.setState({urls: '', pending: false});
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
this.setState({pending: false});
|
2021-12-20 16:08:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleInputChange = async (event, {name, value}) => {
|
|
|
|
|
this.setState({[name]: value}, this.validateUrls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validateUrls = async () => {
|
|
|
|
|
// Validate that all URLs in state are valid.
|
|
|
|
|
if (this.state.urls) {
|
|
|
|
|
let urls = this.state.urls.split(/\r?\n/);
|
|
|
|
|
let valid = true;
|
|
|
|
|
urls.forEach((url) => {
|
|
|
|
|
if (valid && url && !validUrl.test(url)) {
|
|
|
|
|
valid = false;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
this.setState({valid});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2022-05-10 22:53:33 -06:00
|
|
|
let disabled = !this.state.urls || !this.state.valid || this.state.pending || !this.state.downloader;
|
2022-05-20 15:15:31 -06:00
|
|
|
const {header} = this.props;
|
2021-12-20 16:08:13 -07:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
return <ThemeContext.Consumer>
|
2022-09-29 14:58:16 -06:00
|
|
|
{({i}) => (<Form onSubmit={this.submitDownload}>
|
|
|
|
|
<WROLModeMessage content='Downloading is disabled while WROL Mode is enabled'/>
|
|
|
|
|
<Header as='h3'>{header}</Header>
|
|
|
|
|
<TextArea required
|
|
|
|
|
placeholder={'Enter one URL per line'}
|
|
|
|
|
name='urls'
|
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
|
value={this.state.urls}
|
|
|
|
|
/>
|
|
|
|
|
<Button content='Cancel' onClick={this.props.clearSelected}/>
|
|
|
|
|
<Button primary style={{marginTop: '1em'}} disabled={disabled}>Download</Button>
|
|
|
|
|
</Form>)}
|
2022-08-06 05:22:07 +00:00
|
|
|
</ThemeContext.Consumer>
|
2021-12-20 16:08:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 15:15:31 -06:00
|
|
|
class ChannelDownload extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2022-09-29 14:58:16 -06:00
|
|
|
url: '', frequency: 604800, pending: false, disabled: false, ready: false, error: null, success: null,
|
2022-05-20 15:15:31 -06:00
|
|
|
};
|
2022-09-29 14:58:16 -06:00
|
|
|
this.freqOptions = [{key: 'once', text: 'Once', value: 0}, ...frequencyOptions,];
|
2022-05-20 15:15:31 -06:00
|
|
|
this.handleUrlChange = this.handleUrlChange.bind(this);
|
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
|
this.handleFrequencyChange = this.handleFrequencyChange.bind(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleUrlChange = (e, {value}) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
let ready = value && validUrl.test(value);
|
|
|
|
|
this.setState({url: value, ready, success: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleFrequencyChange(e, {value}) {
|
|
|
|
|
this.setState({frequency: value, success: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.setState({disabled: true, pending: true, success: null, error: null});
|
|
|
|
|
const {url, frequency} = this.state;
|
|
|
|
|
if (!url) {
|
|
|
|
|
this.setState({error: 'URL is required'});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let response = await postDownload(url, 'video_channel', frequency);
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
this.setState({pending: false, disabled: false, success: true, url: '', ready: false});
|
|
|
|
|
} else {
|
|
|
|
|
const content = await response.json();
|
|
|
|
|
let error = content.message || null;
|
|
|
|
|
this.setState({pending: false, disabled: false, success: false, error});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const {ready, disabled, url, error, frequency, pending, success} = this.state;
|
|
|
|
|
const buttonDisabled = !ready || disabled;
|
|
|
|
|
|
|
|
|
|
const onceMessage = (<Message>
|
|
|
|
|
<Message.Header>Download Once</Message.Header>
|
|
|
|
|
<Message.Content>You have selected a frequency of Once, this is useful when you want to download
|
|
|
|
|
all videos in a Playlist, and when you do not want to download any videos added to the playlist
|
|
|
|
|
in the future.</Message.Content>
|
|
|
|
|
</Message>);
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
return <Form>
|
|
|
|
|
<WROLModeMessage content='Downloading is disabled while WROL Mode is enabled'/>
|
|
|
|
|
<Header as='h4'><Icon name='video'/> Channel / Playlist</Header>
|
|
|
|
|
<FormInput
|
|
|
|
|
required
|
|
|
|
|
label='URL'
|
|
|
|
|
placeholder='https://example.com/channel/videos'
|
|
|
|
|
value={url}
|
|
|
|
|
error={error}
|
|
|
|
|
onChange={this.handleUrlChange}
|
|
|
|
|
/>
|
|
|
|
|
<FormDropdown
|
|
|
|
|
required
|
|
|
|
|
selection
|
|
|
|
|
label='Download Frequency'
|
|
|
|
|
name='download_frequency'
|
|
|
|
|
placeholder='Frequency'
|
|
|
|
|
options={this.freqOptions}
|
|
|
|
|
value={frequency}
|
|
|
|
|
selected={frequency}
|
|
|
|
|
onChange={this.handleFrequencyChange}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{frequency === 0 && onceMessage}
|
|
|
|
|
|
|
|
|
|
<Button content='Cancel' onClick={this.props.clearSelected}/>
|
|
|
|
|
<Button
|
|
|
|
|
color='blue'
|
|
|
|
|
content='Download'
|
|
|
|
|
onClick={this.handleSubmit}
|
|
|
|
|
disabled={buttonDisabled}
|
|
|
|
|
/>
|
|
|
|
|
<Loader active={pending}/>
|
|
|
|
|
{success && <Icon name='check'/>}
|
|
|
|
|
</Form>
|
2022-05-20 15:15:31 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RSSDownload extends ChannelDownload {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
url: '',
|
|
|
|
|
frequency: 604800,
|
|
|
|
|
pending: false,
|
|
|
|
|
disabled: false,
|
|
|
|
|
ready: false,
|
|
|
|
|
error: null,
|
|
|
|
|
success: null,
|
|
|
|
|
downloaders: [],
|
|
|
|
|
downloader: null,
|
|
|
|
|
sub_downloader: null,
|
|
|
|
|
};
|
|
|
|
|
this.freqOptions = rssFrequencyOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
|
await this.fetchDownloaders();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchDownloaders = async () => {
|
|
|
|
|
let {downloaders} = await getDownloaders();
|
|
|
|
|
downloaders = downloaders.map((i) => {
|
|
|
|
|
return {key: i.name, text: i.pretty_name || i.name, value: i.name}
|
|
|
|
|
})
|
|
|
|
|
this.setState({downloaders});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleInputChange = async (event, {name, value}) => {
|
|
|
|
|
this.setState({[name]: value});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.setState({disabled: true, pending: true, success: null, error: null});
|
|
|
|
|
const {url, frequency, sub_downloader} = this.state;
|
|
|
|
|
if (!url) {
|
|
|
|
|
this.setState({error: 'URL is required'});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let response = await postDownload(url, 'rss', frequency, sub_downloader);
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
this.setState({pending: false, disabled: false, success: true, url: '', ready: false});
|
|
|
|
|
} else {
|
|
|
|
|
const content = await response.json();
|
|
|
|
|
let error = content.message || null;
|
|
|
|
|
this.setState({pending: false, disabled: false, success: false, error});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 16:08:13 -07:00
|
|
|
|
|
|
|
|
render() {
|
2022-05-20 15:15:31 -06:00
|
|
|
const {ready, disabled, url, error, frequency, pending, success} = this.state;
|
|
|
|
|
const buttonDisabled = !ready || disabled;
|
|
|
|
|
|
|
|
|
|
const onceMessage = (<Message>
|
|
|
|
|
<Message.Header>Download Once</Message.Header>
|
|
|
|
|
<Message.Content>You have selected a frequency of Once, this is useful when you want to download
|
|
|
|
|
all videos in a Playlist, but you do not want to download any videos added to the playlist
|
|
|
|
|
in the future.</Message.Content>
|
|
|
|
|
</Message>);
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
return <Form>
|
|
|
|
|
<WROLModeMessage content='Downloading is disabled while WROL Mode is enabled'/>
|
|
|
|
|
<Header as='h4'><Icon name='rss'/> RSS Feed</Header>
|
|
|
|
|
<FormInput
|
|
|
|
|
required
|
|
|
|
|
label='URL'
|
|
|
|
|
placeholder='https://example.com/feed'
|
|
|
|
|
value={url}
|
|
|
|
|
error={error}
|
|
|
|
|
onChange={this.handleUrlChange}
|
|
|
|
|
/>
|
|
|
|
|
<FormDropdown
|
|
|
|
|
required
|
|
|
|
|
selection
|
|
|
|
|
label='Download Frequency'
|
|
|
|
|
name='download_frequency'
|
|
|
|
|
placeholder='Frequency'
|
|
|
|
|
options={this.freqOptions}
|
|
|
|
|
value={frequency}
|
|
|
|
|
selected={frequency}
|
|
|
|
|
onChange={this.handleFrequencyChange}
|
|
|
|
|
/>
|
|
|
|
|
{frequency === 0 && onceMessage}
|
|
|
|
|
<FormDropdown selection required
|
|
|
|
|
name='sub_downloader'
|
|
|
|
|
label='Downloader'
|
|
|
|
|
options={this.state.downloaders}
|
|
|
|
|
placeholder='Select a downloader'
|
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Button content='Cancel' onClick={this.props.clearSelected}/>
|
|
|
|
|
<Button
|
|
|
|
|
color='blue'
|
|
|
|
|
content='Download'
|
|
|
|
|
onClick={this.handleSubmit}
|
|
|
|
|
disabled={buttonDisabled}
|
|
|
|
|
/>
|
|
|
|
|
<Loader active={pending}/>
|
|
|
|
|
{success && <Icon name='check'/>}
|
|
|
|
|
</Form>
|
2021-12-20 16:08:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-20 15:15:31 -06:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function DownloadMenu({onOpen}) {
|
2022-05-20 15:15:31 -06:00
|
|
|
const [downloader, setDownloader] = useState();
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
const localOnOpen = (name) => {
|
|
|
|
|
setDownloader(name);
|
|
|
|
|
onOpen(name)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 16:36:39 -06:00
|
|
|
let body = (<>
|
2022-09-29 14:58:16 -06:00
|
|
|
<Button color='blue' content='Videos' onClick={() => localOnOpen('video')} style={{marginBottom: '1em'}}/>
|
|
|
|
|
<Button color='green' content='Archive' onClick={() => localOnOpen('archive')} style={{marginBottom: '1em'}}/>
|
|
|
|
|
<Button color='blue' content='Channel/Playlist' onClick={() => localOnOpen('video_channel')}
|
2022-08-06 05:22:07 +00:00
|
|
|
style={{marginBottom: '1em'}}/>
|
|
|
|
|
<Button content='RSS Feed' onClick={() => localOnOpen('rss')} style={{marginBottom: '1em'}}/>
|
2022-09-29 16:36:39 -06:00
|
|
|
</>);
|
2022-05-20 15:15:31 -06:00
|
|
|
|
|
|
|
|
function clearSelected() {
|
2022-08-06 05:22:07 +00:00
|
|
|
localOnOpen(null);
|
2022-05-20 15:15:31 -06:00
|
|
|
body = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const downloaders = {
|
|
|
|
|
'archive': <Downloader clearSelected={clearSelected} header={<><Icon name='file alternate'/> Archive</>}
|
|
|
|
|
downloader='archive'/>,
|
|
|
|
|
'video': <Downloader clearSelected={clearSelected} header={<><Icon name='video'/> Videos</>}
|
|
|
|
|
downloader='video'/>,
|
|
|
|
|
'video_channel': <ChannelDownload clearSelected={clearSelected}/>,
|
|
|
|
|
'rss': <RSSDownload clearSelected={clearSelected} header={<><Icon name='rss square'/> RSS</>}/>,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
body = downloader in downloaders ? downloaders[downloader] : body;
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
return <>
|
|
|
|
|
<Header as='h2'>Download</Header>
|
|
|
|
|
{body}
|
|
|
|
|
</>
|
2022-05-20 15:15:31 -06:00
|
|
|
}
|