mirror of https://github.com/Trivernis/whooshy.git
parent
c7507fb4f7
commit
229a6b6ee7
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
npm i
|
||||
git clone https://github.com/trivernis/reddit-riddle ./scripts/reddit-riddle
|
||||
pip3 install -r ./scripts/reddit-riddle/requirements.txt
|
||||
mkdir tmp
|
@ -0,0 +1,79 @@
|
||||
function postLocData(postBody) {
|
||||
let request = new XMLHttpRequest();
|
||||
return new Promise((res, rej) => {
|
||||
|
||||
request.onload = () => {
|
||||
res({
|
||||
status: request.status,
|
||||
data: request.responseText
|
||||
});
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
rej(request.error);
|
||||
};
|
||||
|
||||
request.open('POST', '#', true);
|
||||
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
||||
request.send(JSON.stringify(postBody));
|
||||
});
|
||||
}
|
||||
|
||||
async function startSubredditDownload(subredditName) {
|
||||
let data = await postLocData({
|
||||
subreddit: subredditName
|
||||
});
|
||||
return JSON.parse(data.data);
|
||||
}
|
||||
|
||||
async function getDownloadStatus(downloadId) {
|
||||
let data = await postLocData({
|
||||
id: downloadId
|
||||
});
|
||||
return JSON.parse(data.data);
|
||||
}
|
||||
|
||||
async function refreshDownloadInfo(downloadId) {
|
||||
let response = await getDownloadStatus(downloadId);
|
||||
|
||||
let dlDiv = document.querySelector(`.download-container[dl-id='${downloadId}']`);
|
||||
dlDiv.querySelector('.downloadStatus').innerText = response.status;
|
||||
let subredditName = dlDiv.getAttribute('subreddit-name');
|
||||
|
||||
if (response.status === 'pending') {
|
||||
setTimeout(() => refreshDownloadInfo(downloadId), 1000)
|
||||
} else {
|
||||
let dlLink = document.createElement('a');
|
||||
dlLink.setAttribute('href', response.file);
|
||||
dlLink.setAttribute('filename', `${subredditName}`);
|
||||
for (let cNode of dlDiv.childNodes)
|
||||
dlLink.appendChild(cNode);
|
||||
dlDiv.appendChild(dlLink);
|
||||
setTimeout(() => {
|
||||
dlDiv.remove();
|
||||
}, 30000);
|
||||
}
|
||||
}
|
||||
|
||||
async function submitDownload() {
|
||||
let subredditName = document.querySelector('#subreddit-input').value;
|
||||
let response = await startSubredditDownload(subredditName);
|
||||
|
||||
let dlDiv = document.createElement('div');
|
||||
dlDiv.setAttribute('class', 'download-container');
|
||||
dlDiv.setAttribute('dl-id', response.id);
|
||||
dlDiv.setAttribute('subreddit-name', subredditName);
|
||||
document.querySelector('#download-list').prepend(dlDiv);
|
||||
|
||||
let subnameSpan = document.createElement('span');
|
||||
subnameSpan.innerText = subredditName;
|
||||
subnameSpan.setAttribute('class', 'subredditName');
|
||||
dlDiv.appendChild(subnameSpan);
|
||||
|
||||
let dlStatusSpan = document.createElement('span');
|
||||
dlStatusSpan.innerText = response.status;
|
||||
dlStatusSpan.setAttribute('class', 'downloadStatus');
|
||||
dlDiv.appendChild(dlStatusSpan);
|
||||
|
||||
await refreshDownloadInfo(response.id);
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
const express = require('express'),
|
||||
router = express.Router(),
|
||||
cproc = require('child_process'),
|
||||
fsx = require('fs-extra');
|
||||
|
||||
const rWordOnly = /^\w+$/;
|
||||
|
||||
let downloads = {};
|
||||
|
||||
class RedditDownload {
|
||||
constructor(file) {
|
||||
this.file = file;
|
||||
this.status = 'pending';
|
||||
this.progress = 'N/A';
|
||||
this.process = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an id for a subreddit download.
|
||||
* @param subreddit
|
||||
* @returns {string}
|
||||
*/
|
||||
function generateDownloadId(subreddit) {
|
||||
return Date.now().toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the subreddit download by executing the riddle python file.
|
||||
* @param subreddit {String}
|
||||
* @returns {string}
|
||||
*/
|
||||
function startDownload(subreddit) {
|
||||
if (rWordOnly.test(subreddit)) {
|
||||
let downloadId = generateDownloadId(subreddit);
|
||||
let dlFilePath = `./public/static/${downloadId}.zip`;
|
||||
let dlWebPath = `/static/${downloadId}.zip`;
|
||||
let dl = new RedditDownload(dlWebPath);
|
||||
|
||||
dl.process = cproc.exec(`python -u riddle.py -o ../../public/static/${downloadId} -z --lzma ${subreddit}`,
|
||||
{cwd: './scripts/reddit-riddle', env: {PYTHONIOENCODING: 'utf-8', PYTHONUNBUFFERED: true}},
|
||||
(err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
} else {
|
||||
console.log(`riddle.py: ${stdout}`);
|
||||
}
|
||||
});
|
||||
|
||||
dl.process.on('exit', (code) => {
|
||||
if (code === 0)
|
||||
dl.status = 'finished';
|
||||
else
|
||||
dl.status = 'failed';
|
||||
setTimeout(async () => {
|
||||
await fsx.remove(dlFilePath);
|
||||
delete downloads[downloadId];
|
||||
}, 300000); // delete the file after 5 minutes
|
||||
});
|
||||
|
||||
dl.process.on('message', (msg) => {
|
||||
console.log(msg)
|
||||
});
|
||||
|
||||
downloads[downloadId] = dl;
|
||||
|
||||
return downloadId;
|
||||
}
|
||||
}
|
||||
|
||||
router.use('/files', express.static('./tmp'));
|
||||
|
||||
router.get('/', (req, res, next) => {
|
||||
res.render('riddle');
|
||||
});
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
if (req.body.subreddit) {
|
||||
let id = startDownload(req.body.subreddit);
|
||||
let download = downloads[id];
|
||||
|
||||
res.send({id: id, status: download.status, file: download.file});
|
||||
} else if (req.body.id) {
|
||||
let id = req.body.id;
|
||||
let download = downloads[id];
|
||||
|
||||
if (download) {
|
||||
res.send({
|
||||
id: id,
|
||||
status: download.status,
|
||||
file: download.file
|
||||
});
|
||||
} else {
|
||||
res.send({error: 'Unknown download ID', id: id});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -0,0 +1,10 @@
|
||||
html
|
||||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
script(type='text/javascript', src='/javascripts/riddle-web.js')
|
||||
body
|
||||
h1 Riddle Reddit downloader
|
||||
input(type='text' placeholder='subreddit' id='subreddit-input')
|
||||
button(id='submit-download' onclick='submitDownload()') Download
|
||||
div(id='download-list')
|
Loading…
Reference in New Issue