mirror of
https://github.com/dev-claw/vripper-project.git
synced 2026-08-19 08:35:41 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf2e8c4bb6 | ||
|
|
5d449fa4e5 | ||
|
|
c8b76b2dfb | ||
|
|
e3e9935e15 | ||
|
|
83b41763bd | ||
|
|
ec479cb920 | ||
|
|
4827a88ac4 | ||
|
|
d4980ad6a3 | ||
|
|
f9fc8de3a5 | ||
|
|
9a9df9c8e9 | ||
|
|
46083b6c91 | ||
|
|
8abd8dbf9e | ||
|
|
4a143c349f | ||
|
|
5813dfca3f | ||
|
|
2faefba98e | ||
|
|
d2141d6c10 | ||
|
|
6b7da93562 | ||
|
|
0b3e4ee295 | ||
|
|
42fddde18c | ||
|
|
dcbdaf8e41 | ||
|
|
62bfe72611 | ||
|
|
6c8d3229f1 | ||
|
|
fea9697817 | ||
|
|
74ad8ce8ee | ||
|
|
df5aae8cf3 | ||
|
|
cbf8da2723 | ||
|
|
0d752fa9bd | ||
|
|
b9c9b820ba | ||
|
|
402025fbda | ||
|
|
8d497947a3 | ||
|
|
e5f58e9079 | ||
|
|
d992cb2b92 | ||
|
|
721a61ec72 | ||
|
|
713fbebb51 | ||
|
|
8eb8941df4 | ||
|
|
78c7137e45 |
@@ -0,0 +1,75 @@
|
||||
# Changelog
|
||||
|
||||
## [1.4.7] - 2019-08-13
|
||||
### Changed
|
||||
- Fixed a bug in image filename extensions
|
||||
|
||||
## [1.4.6] - 2019-08-06
|
||||
### Changed
|
||||
- Fixed a bug when creating image file names
|
||||
- Remove unnecessary logs
|
||||
- Fix data.json init
|
||||
|
||||
## [1.4.5] - 2019-08-03
|
||||
### Changed
|
||||
- Add min width and height for electron app
|
||||
### Added
|
||||
- Add download speed to status bar
|
||||
|
||||
## [1.4.4] - 2019-08-01
|
||||
### Changed
|
||||
- Fix names for ImgboxHost
|
||||
- Enhance UI
|
||||
- Minor fixes
|
||||
### Added
|
||||
- Add start and stop button
|
||||
|
||||
## [1.4.3] - 2019-07-14
|
||||
### Changed
|
||||
- Update some messages
|
||||
- Fix scrolling issue
|
||||
|
||||
## [1.4.2] - 2019-07-14
|
||||
### Changed
|
||||
- Fix layout issue in web mode
|
||||
|
||||
## [1.4.1] - 2019-07-14
|
||||
### Changed
|
||||
- Change title's font
|
||||
### Added
|
||||
- Add borders
|
||||
|
||||
## [1.4.0] - 2019-07-14
|
||||
### Changed
|
||||
- Better error handling on startup
|
||||
- Better support for linux AppImage
|
||||
### Added
|
||||
- Clear all completed
|
||||
- Add clipboard support
|
||||
- Add option to remove all posts
|
||||
- Add confirmation on remove
|
||||
|
||||
## [1.3.0-rc0] - 2019-07-04
|
||||
### Changed
|
||||
- Change default setting for autostart false -> true
|
||||
- Change default setting for max concurrent downloads 1 -> 4
|
||||
### Added
|
||||
- Desktop app built with electron
|
||||
- Add the option to remove a post
|
||||
|
||||
## [1.2.0] - 2019-06-23
|
||||
### Added
|
||||
- Add a shutdown button to gracefully shutdown the app
|
||||
- Open and redirect the browser to the app address on startup
|
||||
- Add the option to run the app in headless mode (-Djava.awt.headless=true)
|
||||
|
||||
## [1.1.1] - 2019-06-22
|
||||
### Changed
|
||||
- Fix a stability issue with progress updates when the app is runninh for a long time
|
||||
|
||||
## [1.0.0] - 2019-06-22
|
||||
### Added
|
||||
- Add real time communication for progress updates via websocket (SockJS)
|
||||
- Add settings for download path, max concurrent download, enable/disable integration with vipergirls
|
||||
- Add integration with vipergitls.to (possibility to auto leave thanks after grab post)
|
||||
- Add support for acidimg.cc, imagezilla.net, imgbox.com, imx.to, pixhost.to, turboimagehost.com
|
||||
@@ -0,0 +1,26 @@
|
||||
# Viper Ripper!
|
||||
|
||||
This is my spin for a gallery ripper app for [vipergirls](https://vipergirls.to) website.
|
||||
The purpose of this project is to build a robust and clean application to conveniently download photo galleries using modern web technologies, Java + Spring boot for the back end and angular + electron for the front end.
|
||||
|
||||
## How to build
|
||||
You need a recent version of maven 3.6.1+.
|
||||
|
||||
The application support 2 build modes:
|
||||
- Desktop app (*Partially cross platform): Self contained app, built with electron, depends on Java runtimes.
|
||||
- Server app (Fully cross platform): Depends on Java runtimes, you need a web browser to access the app UI.
|
||||
|
||||
Most people will be interested only on the desktop app, however if you have a nas or a server, the server app is there for you.
|
||||
|
||||
To build the server app:
|
||||
|
||||
mvn clean install
|
||||
To build the desktop app:
|
||||
|
||||
mvn clean install -Pelectron
|
||||
|
||||
Maven will automatically handle front end compilation. However, for development, you will need to install a recent version of nodejs on your system.
|
||||
|
||||
*Partially cross platform:
|
||||
Technically cross platform, but the Desktop app relies on electron builder to make appropriate binaries for each platform. mac binaries can only be generated on a mac, and i don't own one. So i can only provide binaries for windows and linux.
|
||||
The Server app however depends only on Java, so it will work just fine on a mac, you only need a browser to access it.
|
||||
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
<artifactId>vripper</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.4.7</version>
|
||||
<packaging>pom</packaging>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -15,5 +15,6 @@
|
||||
<modules>
|
||||
<module>vripper-server</module>
|
||||
<module>vripper-ui</module>
|
||||
<module>vripper-electron</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# compiled output
|
||||
/dist
|
||||
/tmp
|
||||
/out-tsc
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
|
||||
# profiling files
|
||||
chrome-profiler-events.json
|
||||
speed-measure-plugin.json
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
.history/*
|
||||
|
||||
# misc
|
||||
/.sass-cache
|
||||
/connect.lock
|
||||
/coverage
|
||||
/libpeerconnection.log
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
testem.log
|
||||
/typings
|
||||
|
||||
# System Files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
dist
|
||||
build/vripper-ui
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"esversion": 6,
|
||||
"node": true,
|
||||
"globals": {
|
||||
"BigInt": false
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
@@ -0,0 +1,138 @@
|
||||
const { app, BrowserWindow } = require("electron");
|
||||
const path = require("path");
|
||||
const url = require("url");
|
||||
const getPort = require("get-port");
|
||||
const { spawn } = require("child_process");
|
||||
const { ipcMain } = require("electron");
|
||||
const commandExists = require("command-exists").sync;
|
||||
const { dialog } = require("electron");
|
||||
const axios = require('axios');
|
||||
const appDir = process.env.APPDIR;
|
||||
|
||||
let win;
|
||||
let vripperServer;
|
||||
let serverPort;
|
||||
|
||||
const maxTerminationAttemps = 5;
|
||||
let terminationAttemps = 0;
|
||||
let terminationInteval;
|
||||
let terminated = false;
|
||||
|
||||
process.on("uncaughtException", err => {
|
||||
dialog.showErrorBox(err.message, err.stack);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
if (!commandExists("java")) {
|
||||
dialog.showErrorBox(
|
||||
"Java command is missing",
|
||||
"Java cannot be found on your PATH, make sure to install java before running Viper Ripper"
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
let icon;
|
||||
if(process.platform === "win32") {
|
||||
icon = path.join(__dirname, `icon.ico`);
|
||||
} else {
|
||||
icon = path.join(__dirname, `icon.icns`);
|
||||
}
|
||||
win = new BrowserWindow({
|
||||
width: 1024,
|
||||
height: 768,
|
||||
minWidth: 640,
|
||||
minHeight: 480,
|
||||
frame: false,
|
||||
webPreferences: {
|
||||
nodeIntegration: true
|
||||
},
|
||||
icon: icon
|
||||
});
|
||||
|
||||
win.removeMenu();
|
||||
|
||||
win.loadURL(
|
||||
url.format({
|
||||
pathname: path.join(__dirname, `vripper-ui/index.html`),
|
||||
protocol: "file:",
|
||||
slashes: true
|
||||
})
|
||||
);
|
||||
|
||||
// win.webContents.openDevTools();
|
||||
|
||||
win.on("closed", () => {
|
||||
win = null;
|
||||
});
|
||||
}
|
||||
|
||||
getPort().then(port => {
|
||||
serverPort = port;
|
||||
ipcMain.on("get-port", event => {
|
||||
event.reply("port", port);
|
||||
});
|
||||
vripperServer = spawn("java", [
|
||||
"-Dvripper.server.port=" + port,
|
||||
"-jar",
|
||||
appDir !== undefined ? path.join(appDir, "bin/vripper-server.jar") :
|
||||
path.join(app.getPath('exe'), "../bin/vripper-server.jar")
|
||||
], {
|
||||
stdio: 'ignore'
|
||||
});
|
||||
vripperServer.on('exit', (code, signal) => {
|
||||
console.log(`vripper server terminated, code = ${code}, signal = ${signal}`);
|
||||
terminated = true;
|
||||
app.quit();
|
||||
});
|
||||
});
|
||||
|
||||
const gotTheLock = app.requestSingleInstanceLock();
|
||||
|
||||
if (!gotTheLock) {
|
||||
app.quit();
|
||||
} else {
|
||||
app.on("second-instance", (event, commandLine, workingDirectory) => {
|
||||
if (win) {
|
||||
if (win.isMinimized()) win.restore();
|
||||
win.focus();
|
||||
}
|
||||
});
|
||||
|
||||
app.on("ready", createWindow);
|
||||
|
||||
app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") {
|
||||
axios.post('http://localhost:' + serverPort + '/actuator/shutdown', {}, {
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}).then((response) => {
|
||||
terminationInteval= setInterval(() => {
|
||||
terminationAttemps++;
|
||||
if(terminated) {
|
||||
console.log('viper server terminated');
|
||||
clearInterval(terminationInteval);
|
||||
app.quit();
|
||||
} else if(terminationAttemps > maxTerminationAttemps) {
|
||||
console.log('viper server is not terminated');
|
||||
console.log('Proceed to kill');
|
||||
vripperServer.kill('SIGKILL');
|
||||
clearInterval(terminationInteval);
|
||||
app.quit();
|
||||
}
|
||||
}, 1000);
|
||||
})
|
||||
.catch((error) => {
|
||||
// Terminate immediately
|
||||
vripperServer.kill('SIGKILL');
|
||||
terminated = true;
|
||||
app.quit();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.on("activate", () => {
|
||||
if (win === null) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
}
|
||||
Generated
+3269
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"name": "vripper-electron",
|
||||
"version": "1.4.7",
|
||||
"description": "",
|
||||
"main": "main.js",
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"build": {
|
||||
"appId": "tn.mnlr.vripper",
|
||||
"files": [
|
||||
"**/*",
|
||||
"!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",
|
||||
"!**/node_modules/*/{test,__tests__,tests,powered-test,example,examples}",
|
||||
"!**/node_modules/*.d.ts",
|
||||
"!**/node_modules/.bin",
|
||||
"!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}",
|
||||
"!.editorconfig",
|
||||
"!**/._*",
|
||||
"!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,.gitignore,.gitattributes}",
|
||||
"!**/{__pycache__,thumbs.db,.flowconfig,.idea,.vs,.nyc_output}",
|
||||
"!**/{appveyor.yml,.travis.yml,circle.yml}",
|
||||
"!**/{npm-debug.log,yarn.lock,.yarn-integrity,.yarn-metadata.json}",
|
||||
{
|
||||
"from": "./build/",
|
||||
"to": "."
|
||||
}
|
||||
],
|
||||
"extraFiles": [
|
||||
{
|
||||
"from": "../vripper-server/target/vripper-server-${version}-electron.jar",
|
||||
"to": "bin/vripper-server.jar"
|
||||
}
|
||||
],
|
||||
"win": {
|
||||
"target": "nsis"
|
||||
},
|
||||
"linux": {
|
||||
"target": [
|
||||
"zip"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"dist": "node pre-build.js && build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "^5.0.5",
|
||||
"electron-builder": "^20.44.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"cheerio": "^1.0.0-rc.3",
|
||||
"command-exists": "^1.2.8",
|
||||
"copy-dir": "^1.1.0",
|
||||
"custom-electron-titlebar": "^3.0.10",
|
||||
"electron-context-menu": "^0.13.0",
|
||||
"get-port": "^5.0.0",
|
||||
"rimraf": "^2.6.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
<artifactId>vripper</artifactId>
|
||||
<version>1.4.7</version>
|
||||
</parent>
|
||||
<artifactId>vripper-electron</artifactId>
|
||||
<name>vripper-electron</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>electron</id>
|
||||
<properties>
|
||||
<buildClassifier>electron</buildClassifier>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
<artifactId>vripper-server</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>runtime</scope>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.3</version>
|
||||
<configuration>
|
||||
<nodeVersion>v12.2.0</nodeVersion>
|
||||
<npmVersion>6.9.0</npmVersion>
|
||||
<workingDirectory>build-dir</workingDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run dist</arguments>
|
||||
</configuration>
|
||||
<phase>generate-resources</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
@@ -0,0 +1,18 @@
|
||||
const cheerio = require('cheerio');
|
||||
const fs = require('fs');
|
||||
const rimraf = require("rimraf");
|
||||
const copydir = require("copy-dir");
|
||||
|
||||
|
||||
rimraf.sync('./build/vripper-ui');
|
||||
copydir.sync('../vripper-ui/dist/vripper-ui', './build/vripper-ui', {});
|
||||
|
||||
|
||||
const index = fs.readFileSync('./build/vripper-ui/index.html');
|
||||
|
||||
|
||||
const $ = cheerio.load(index);
|
||||
$('body').prepend(`<script>require('../renderer.js')</script>`);
|
||||
|
||||
fs.writeFileSync('./build/vripper-ui/index.html', $.html());
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
console.log("renderer intialized");
|
||||
const customTitlebar = require("custom-electron-titlebar");
|
||||
const electron = require("electron").remote;
|
||||
const contextMenu = require("electron-context-menu");
|
||||
|
||||
contextMenu({});
|
||||
const menu = new electron.Menu();
|
||||
const titlebar = new customTitlebar.Titlebar({
|
||||
backgroundColor: customTitlebar.Color.fromHex("#3f51b5"),
|
||||
menu: menu
|
||||
});
|
||||
titlebar.updateTitle("Viper Ripper");
|
||||
File diff suppressed because one or more lines are too long
+58
-16
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
<artifactId>vripper</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.4.7</version>
|
||||
</parent>
|
||||
<artifactId>vripper-server</artifactId>
|
||||
<name>vripper-server</name>
|
||||
@@ -28,7 +28,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -69,33 +72,29 @@
|
||||
<artifactId>vripper-ui</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>runtime</scope>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-resources</id>
|
||||
<phase>validate</phase>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/classes/static</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${project.parent.basedir}/vripper-ui/dist/vripper-ui/
|
||||
</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
@@ -149,4 +148,47 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>web</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<buildClassifier>web</buildClassifier>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-resources</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/classes/static</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${project.parent.basedir}/vripper-ui/dist/vripper-ui/
|
||||
</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>electron</id>
|
||||
<properties>
|
||||
<buildClassifier>electron</buildClassifier>
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package tn.mnlr.vripper;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -10,11 +13,14 @@ import java.util.Map;
|
||||
@Component
|
||||
public class SpringContext implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext context;
|
||||
private static final Logger logger = LoggerFactory.getLogger(SpringContext.class);
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
/**
|
||||
* Returns the Spring managed bean instance of the given class type (if it exists).
|
||||
* Returns null otherwise.
|
||||
*
|
||||
* @param beanClass
|
||||
* @return
|
||||
*/
|
||||
@@ -26,10 +32,16 @@ public class SpringContext implements ApplicationContextAware {
|
||||
return context.getBeansOfType(beanClass);
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
logger.info("Application terminating...");
|
||||
context.close();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
|
||||
// store ApplicationContext reference to access required beans later on
|
||||
SpringContext.context = context;
|
||||
SpringContext.context = ((ConfigurableApplicationContext) context);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import tn.mnlr.vripper.services.PersistenceService;
|
||||
import tn.mnlr.vripper.services.VipergirlsAuthService;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
@SpringBootApplication
|
||||
public class VripperApplication {
|
||||
@@ -22,9 +21,14 @@ public class VripperApplication {
|
||||
|
||||
public static final String dataPath = System.getProperty("vripper.datapath", ".") + File.separator + "data.json";
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(VripperApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
SpringApplication.run(VripperApplication.class, args);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to run the application", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Component
|
||||
public class AppCommandRunner implements CommandLineRunner {
|
||||
@@ -40,8 +44,10 @@ public class VripperApplication {
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
|
||||
persistenceService.restore();
|
||||
appSettingsService.restore();
|
||||
registerShutdownHook();
|
||||
|
||||
try {
|
||||
authService.authenticate();
|
||||
@@ -49,7 +55,11 @@ public class VripperApplication {
|
||||
logger.error("Cannot authenticate user with ViperGirls", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerShutdownHook() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(SpringContext::close));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ public class Image {
|
||||
}
|
||||
imageStateProcessor = BehaviorProcessor.create();
|
||||
subscription = imageStateProcessor
|
||||
.onBackpressureBuffer()
|
||||
.onBackpressureLatest()
|
||||
.doOnNext(appStateService::onImageUpdate)
|
||||
.subscribe();
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ public class Post {
|
||||
|
||||
private String title;
|
||||
|
||||
private String postCounter;
|
||||
|
||||
private String url;
|
||||
|
||||
private List<Image> images;
|
||||
@@ -37,12 +39,15 @@ public class Post {
|
||||
|
||||
private int total;
|
||||
|
||||
public Post(String title, String url, List<Image> images, Map<String, String> metadata, String postId, AppStateService appStateService) {
|
||||
private boolean removed = false;
|
||||
|
||||
public Post(String title, String url, List<Image> images, Map<String, String> metadata, String postId, String postCounter, AppStateService appStateService) {
|
||||
this.title = title;
|
||||
this.url = url;
|
||||
this.images = images;
|
||||
this.metadata = metadata;
|
||||
this.postId = postId;
|
||||
this.postCounter = postCounter;
|
||||
this.appStateService = appStateService;
|
||||
total = images.size();
|
||||
status = Status.PENDING;
|
||||
@@ -50,6 +55,11 @@ public class Post {
|
||||
appStateService.getLivePostsState().onNext(this);
|
||||
}
|
||||
|
||||
public void setRemoved(boolean removed) {
|
||||
this.removed = removed;
|
||||
appStateService.getLivePostsState().onNext(this);
|
||||
}
|
||||
|
||||
public void increase() {
|
||||
done.incrementAndGet();
|
||||
appStateService.getLivePostsState().onNext(this);
|
||||
|
||||
+4
@@ -10,4 +10,8 @@ public abstract class PostPersistanceMixin {
|
||||
|
||||
@JsonIgnore
|
||||
private AppStateService appStateService;
|
||||
|
||||
|
||||
@JsonIgnore
|
||||
private boolean removed;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
@@ -42,7 +41,7 @@ public class AcidimgHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
Node contDiv;
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package tn.mnlr.vripper.host;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
@@ -9,20 +11,21 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.w3c.dom.Document;
|
||||
import tn.mnlr.vripper.services.AppSettingsService;
|
||||
import tn.mnlr.vripper.entities.Image;
|
||||
import tn.mnlr.vripper.exception.DownloadException;
|
||||
import tn.mnlr.vripper.exception.HostException;
|
||||
import tn.mnlr.vripper.exception.HtmlProcessorException;
|
||||
import tn.mnlr.vripper.q.ImageFileData;
|
||||
import tn.mnlr.vripper.services.ConnectionManager;
|
||||
import tn.mnlr.vripper.services.HtmlProcessorService;
|
||||
import tn.mnlr.vripper.services.XpathService;
|
||||
import tn.mnlr.vripper.services.*;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
@@ -44,6 +47,9 @@ abstract public class Host {
|
||||
@Autowired
|
||||
private ConnectionManager cm;
|
||||
|
||||
@Autowired
|
||||
private DownloadSpeedService downloadSpeedService;
|
||||
|
||||
abstract protected String getHost();
|
||||
|
||||
public boolean isSupported(String url) {
|
||||
@@ -70,17 +76,12 @@ abstract public class Host {
|
||||
* END HOST SPECIFIC
|
||||
*/
|
||||
|
||||
if(!imageFileData.getImageName().toLowerCase().endsWith(".jpg")) {
|
||||
imageFileData.setImageName(imageFileData.getImageName() + ".jpg");
|
||||
}
|
||||
|
||||
File destinationFolder = new File(appSettingsService.getDownloadPath(), sanitize(image.getPostName()));
|
||||
imageFileData.setImageName(formatImageFileName(imageFileData.getImageName()));
|
||||
File destinationFolder = new File(appSettingsService.getDownloadPath(), sanitize(image.getPostName() + "_" + image.getPostId()));
|
||||
logger.info(String.format("Saving to %s", destinationFolder.getPath()));
|
||||
if (!destinationFolder.exists()) {
|
||||
logger.info(String.format("Creating %s", destinationFolder.getPath()));
|
||||
destinationFolder.mkdirs();
|
||||
} else {
|
||||
logger.warn(String.format("%s already exists, the file will be overridden", destinationFolder.getPath()));
|
||||
}
|
||||
|
||||
HttpClient client = cm.getClient().build();
|
||||
@@ -93,9 +94,10 @@ abstract public class Host {
|
||||
throw new DownloadException(String.format("Server returned code %d", response.getStatusLine().getStatusCode()));
|
||||
}
|
||||
|
||||
File outputFile = new File(destinationFolder.getPath() + File.separator + imageFileData.getImageName() + ".tmp");
|
||||
try (
|
||||
InputStream downloadStream = response.getEntity().getContent();
|
||||
FileOutputStream fos = new FileOutputStream(destinationFolder.getPath() + File.separator + imageFileData.getImageName())
|
||||
FileOutputStream fos = new FileOutputStream(outputFile)
|
||||
) {
|
||||
|
||||
image.setTotal(response.getEntity().getContentLength());
|
||||
@@ -107,8 +109,10 @@ abstract public class Host {
|
||||
while ((read = downloadStream.read(buffer, 0, READ_BUFFER_SIZE)) != -1) {
|
||||
fos.write(buffer, 0, read);
|
||||
image.increase(read);
|
||||
downloadSpeedService.increase(read);
|
||||
}
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
checkImageTypeAndRename(outputFile, imageFileData.getImageName());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -119,6 +123,42 @@ abstract public class Host {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkImageTypeAndRename(File outputFile, String imageName) throws HostException {
|
||||
try {
|
||||
ImageInputStream iis = ImageIO.createImageInputStream(outputFile);
|
||||
Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
|
||||
if (!it.hasNext()) {
|
||||
throw new HostException("Image file is not recognized!");
|
||||
}
|
||||
ImageReader reader = it.next();
|
||||
String formatName = reader.getFormatName();
|
||||
if (formatName.toUpperCase().equals("JPEG")) {
|
||||
formatName = "JPG";
|
||||
}
|
||||
String outImageName = imageName + "." + formatName.toUpperCase();
|
||||
outputFile.renameTo(new File(outputFile.getParent(), outImageName));
|
||||
} catch (Exception e) {
|
||||
throw new HostException("Failed to rename output file", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will sanitize the image name and remove extension
|
||||
*
|
||||
* @param imageName
|
||||
* @return
|
||||
*/
|
||||
protected String formatImageFileName(String imageName) {
|
||||
int extensionIndex = imageName.lastIndexOf('.');
|
||||
String fileName;
|
||||
if (extensionIndex != -1) {
|
||||
fileName = imageName.substring(0, extensionIndex);
|
||||
} else {
|
||||
fileName = imageName;
|
||||
}
|
||||
return sanitize(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just for testing, you may ignore
|
||||
* @throws Exception
|
||||
@@ -145,14 +185,18 @@ abstract public class Host {
|
||||
return imgUrl;
|
||||
}
|
||||
|
||||
protected final Document getDocument(final String url) throws HostException {
|
||||
protected final Response getResponse(final String url) throws HostException {
|
||||
String basePage;
|
||||
|
||||
HttpClient client = cm.getClient().build();
|
||||
HttpGet httpGet = cm.buildHttpGet(url);
|
||||
|
||||
Header[] headers;
|
||||
logger.info(String.format("Requesting %s", url));
|
||||
try (CloseableHttpResponse response = (CloseableHttpResponse) client.execute(httpGet)) {
|
||||
if (response.getStatusLine().getStatusCode() / 100 != 2) {
|
||||
throw new HostException(String.format("Unexpected response code: %d", response.getStatusLine().getStatusCode()));
|
||||
}
|
||||
headers = response.getAllHeaders();
|
||||
basePage = EntityUtils.toString(response.getEntity());
|
||||
logger.debug(String.format("%s response: %n%s", url, basePage));
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
@@ -162,7 +206,7 @@ abstract public class Host {
|
||||
|
||||
try {
|
||||
logger.info(String.format("Cleaning %s response", url));
|
||||
return htmlProcessorService.clean(basePage);
|
||||
return new Response(htmlProcessorService.clean(basePage), headers);
|
||||
} catch (HtmlProcessorException e) {
|
||||
throw new HostException(e);
|
||||
}
|
||||
@@ -175,4 +219,14 @@ abstract public class Host {
|
||||
}
|
||||
|
||||
protected abstract void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException;
|
||||
|
||||
@Getter
|
||||
public static class Response {
|
||||
protected Response(Document document, Header[] headers) {
|
||||
this.document = document;
|
||||
this.headers = headers;
|
||||
}
|
||||
private Document document;
|
||||
private Header[] headers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package tn.mnlr.vripper.host;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import tn.mnlr.vripper.exception.HostException;
|
||||
import tn.mnlr.vripper.exception.HtmlProcessorException;
|
||||
import tn.mnlr.vripper.exception.XpathException;
|
||||
import tn.mnlr.vripper.q.ImageFileData;
|
||||
import tn.mnlr.vripper.services.ConnectionManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class ImageBamHost extends Host {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImageBamHost.class);
|
||||
|
||||
private static final String host = "imagebam.com";
|
||||
public static final String CONTINUE_BUTTON_XPATH = "//a[@title='Continue to your image']";
|
||||
public static final String IMG_XPATH = "//img[@class='image']";
|
||||
|
||||
@Autowired
|
||||
private ConnectionManager cm;
|
||||
|
||||
@Override
|
||||
protected String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Response response = getResponse(url);
|
||||
Document doc = response.getDocument();
|
||||
Header[] headers = response.getHeaders();
|
||||
String cookies = Arrays.asList(headers)
|
||||
.stream()
|
||||
.filter(e -> e.getName().toLowerCase().contains("Set-Cookie".toLowerCase()))
|
||||
.map(e -> e.getValue().split(";")[0].trim())
|
||||
.collect(Collectors.joining("; "));
|
||||
|
||||
Node contDiv;
|
||||
try {
|
||||
logger.info(String.format("Looking for xpath expression %s in %s", CONTINUE_BUTTON_XPATH, url));
|
||||
contDiv = xpathService.getAsNode(doc, CONTINUE_BUTTON_XPATH);
|
||||
} catch (XpathException e) {
|
||||
throw new HostException(e);
|
||||
}
|
||||
|
||||
if (contDiv != null) {
|
||||
logger.info(String.format("Getting cookies to use for %s", url));
|
||||
logger.info(String.format("Click button found for %s", url));
|
||||
HttpClient client = cm.getClient().build();
|
||||
HttpGet httpGet = cm.buildHttpGet(url);
|
||||
httpGet.addHeader("Referer", url);
|
||||
httpGet.addHeader("Cookie", cookies);
|
||||
logger.info(String.format("Requesting %s", httpGet));
|
||||
try (CloseableHttpResponse res = (CloseableHttpResponse) client.execute(httpGet)) {
|
||||
String s = EntityUtils.toString(res.getEntity());
|
||||
logger.debug(String.format("%s response is:%n%s", httpGet, s));
|
||||
logger.debug(String.format("Cleaning response for %s", httpGet));
|
||||
doc = htmlProcessorService.clean(s);
|
||||
EntityUtils.consumeQuietly(res.getEntity());
|
||||
} catch (IOException | HtmlProcessorException e) {
|
||||
throw new HostException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Node imgNode;
|
||||
try {
|
||||
logger.info(String.format("Looking for xpath expression %s in %s", IMG_XPATH, url));
|
||||
imgNode = xpathService.getAsNode(doc, IMG_XPATH);
|
||||
} catch (XpathException e) {
|
||||
throw new HostException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info(String.format("Resolving name and image url for %s", url));
|
||||
String imgTitle = imgNode.getAttributes().getNamedItem("id").getTextContent().trim();
|
||||
String imgUrl = imgNode.getAttributes().getNamedItem("src").getTextContent().trim();
|
||||
|
||||
imageFileData.setImageUrl(imgUrl);
|
||||
imageFileData.setImageName(imgTitle.isEmpty() ? imgUrl.substring(imgUrl.lastIndexOf('/') + 1) : imgTitle);
|
||||
} catch(Exception e) {
|
||||
throw new HostException("Unexpected error occurred", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package tn.mnlr.vripper.host;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import tn.mnlr.vripper.exception.HostException;
|
||||
import tn.mnlr.vripper.exception.XpathException;
|
||||
import tn.mnlr.vripper.q.ImageFileData;
|
||||
import tn.mnlr.vripper.services.ConnectionManager;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ImageTwistHost extends Host {
|
||||
|
||||
public static final String IMG_XPATH = "//img[contains(@class, 'img')]";
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImageTwistHost.class);
|
||||
private static final String host = "imagetwist.com";
|
||||
@Autowired
|
||||
private ConnectionManager cm;
|
||||
|
||||
@Override
|
||||
protected String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
Node imgNode;
|
||||
try {
|
||||
logger.info(String.format("Looking for xpath expression %s in %s", IMG_XPATH, url));
|
||||
imgNode = xpathService.getAsNode(doc, IMG_XPATH);
|
||||
} catch (XpathException e) {
|
||||
throw new HostException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
logger.info(String.format("Resolving name and image url for %s", url));
|
||||
String imgTitle = Optional.ofNullable(imgNode.getAttributes().getNamedItem("alt")).map(Node::getTextContent).map(String::trim).orElse(null);
|
||||
String imgUrl = imgNode.getAttributes().getNamedItem("src").getTextContent().trim();
|
||||
|
||||
imageFileData.setImageUrl(imgUrl);
|
||||
imageFileData.setImageName(imgTitle);
|
||||
} catch (Exception e) {
|
||||
throw new HostException("Unexpected error occurred", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package tn.mnlr.vripper.host;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -12,8 +11,6 @@ import tn.mnlr.vripper.exception.XpathException;
|
||||
import tn.mnlr.vripper.q.ImageFileData;
|
||||
import tn.mnlr.vripper.services.ConnectionManager;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Service
|
||||
public class ImageZillaHost extends Host {
|
||||
|
||||
@@ -33,7 +30,7 @@ public class ImageZillaHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
String title;
|
||||
try {
|
||||
@@ -55,7 +52,7 @@ public class ImageZillaHost extends Host {
|
||||
|
||||
try {
|
||||
imageFileData.setImageUrl(url.replace("show", "images"));
|
||||
imageFileData.setImageName(title.substring(8));
|
||||
imageFileData.setImageName(title);
|
||||
} catch (Exception e) {
|
||||
throw new HostException("Unexpected error occurred", e);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class ImgboxHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
Node imgNode;
|
||||
try {
|
||||
@@ -46,7 +46,7 @@ public class ImgboxHost extends Host {
|
||||
String imgUrl = imgNode.getAttributes().getNamedItem("src").getTextContent().trim();
|
||||
|
||||
imageFileData.setImageUrl(imgUrl);
|
||||
imageFileData.setImageName(imgTitle.substring(8));
|
||||
imageFileData.setImageName(imgTitle);
|
||||
} catch (Exception e) {
|
||||
throw new HostException("Unexpected error occurred", e);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ImxHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
Node contDiv;
|
||||
try {
|
||||
|
||||
@@ -30,7 +30,7 @@ public class PixhostHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
Node imgNode;
|
||||
try {
|
||||
|
||||
@@ -31,7 +31,7 @@ public class TurboImageHost extends Host {
|
||||
@Override
|
||||
protected void setNameAndUrl(final String url, final ImageFileData imageFileData) throws HostException {
|
||||
|
||||
Document doc = getDocument(url);
|
||||
Document doc = getResponse(url).getDocument();
|
||||
|
||||
String title;
|
||||
try {
|
||||
|
||||
@@ -9,6 +9,7 @@ import tn.mnlr.vripper.entities.Image;
|
||||
import tn.mnlr.vripper.entities.Post;
|
||||
import tn.mnlr.vripper.services.AppStateService;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -18,6 +19,8 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class DownloadQ {
|
||||
|
||||
private static final List<Post.Status> FINISHED = Arrays.asList(Post.Status.ERROR, Post.Status.COMPLETE, Post.Status.STOPPED);
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DownloadQ.class);
|
||||
|
||||
@Autowired
|
||||
@@ -31,15 +34,11 @@ public class DownloadQ {
|
||||
@Getter
|
||||
private boolean notPauseQ = true;
|
||||
|
||||
public synchronized void put(Image image) {
|
||||
try {
|
||||
logger.info(String.format("Enqueuing a job for %s", image.getUrl()));
|
||||
DownloadJob downloadJob = new DownloadJob(image);
|
||||
downloadQ.put(downloadJob);
|
||||
appStateService.newDownloadJob(downloadJob);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
public synchronized void put(Image image) throws InterruptedException {
|
||||
logger.info(String.format("Enqueuing a job for %s", image.getUrl()));
|
||||
DownloadJob downloadJob = new DownloadJob(image);
|
||||
downloadQ.put(downloadJob);
|
||||
appStateService.newDownloadJob(downloadJob);
|
||||
}
|
||||
|
||||
public DownloadJob take() throws InterruptedException {
|
||||
@@ -48,13 +47,14 @@ public class DownloadQ {
|
||||
return downloadJob;
|
||||
}
|
||||
|
||||
public void enqueue(Post post) {
|
||||
public void enqueue(Post post) throws InterruptedException {
|
||||
|
||||
post.getImages()
|
||||
.forEach(this::put);
|
||||
for (Image image : post.getImages()) {
|
||||
put(image);
|
||||
}
|
||||
}
|
||||
|
||||
public void restart(String postId) {
|
||||
public void restart(String postId) throws InterruptedException {
|
||||
if (appStateService.getRunningPosts().get(postId) != null && appStateService.getRunningPosts().get(postId).get() > 0) {
|
||||
logger.warn(String.format("Cannot restart, jobs are currently running for post id %s", postId));
|
||||
return;
|
||||
@@ -69,10 +69,10 @@ public class DownloadQ {
|
||||
}
|
||||
appStateService.getPost(postId).setStatus(Post.Status.PENDING);
|
||||
logger.info(String.format("Restarting %d jobs for post id %s", images.size(), postId));
|
||||
images.forEach(image -> {
|
||||
for (Image image : images) {
|
||||
image.init();
|
||||
put(image);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void removeScheduled(Image image) {
|
||||
@@ -105,6 +105,9 @@ public class DownloadQ {
|
||||
|
||||
public synchronized void stop(String postId) {
|
||||
try {
|
||||
if (FINISHED.contains(appStateService.getPost(postId).getStatus())) {
|
||||
return;
|
||||
}
|
||||
notPauseQ = false;
|
||||
appStateService.getPost(postId).setStatus(Post.Status.STOPPED);
|
||||
List<Image> images = appStateService.getPost(postId)
|
||||
@@ -122,4 +125,19 @@ public class DownloadQ {
|
||||
notPauseQ = true;
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return downloadQ.size();
|
||||
}
|
||||
|
||||
public synchronized void stopAll() {
|
||||
appStateService.getCurrentPosts().values().stream().map(Post::getPostId).forEach(this::stop);
|
||||
}
|
||||
|
||||
public synchronized void restartAll() throws InterruptedException {
|
||||
for (Post post : appStateService.getCurrentPosts().values()) {
|
||||
String postId = post.getPostId();
|
||||
restart(postId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,18 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.services.AppSettingsService;
|
||||
import tn.mnlr.vripper.entities.Image;
|
||||
import tn.mnlr.vripper.services.AppSettingsService;
|
||||
import tn.mnlr.vripper.services.AppStateService;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -42,6 +40,8 @@ public class ExecutionService {
|
||||
|
||||
private ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
private Thread executionThread;
|
||||
|
||||
private RetryPolicy<Object> retryPolicy;
|
||||
|
||||
private List<DownloadJob> running = Collections.synchronizedList(new ArrayList<>());
|
||||
@@ -57,14 +57,21 @@ public class ExecutionService {
|
||||
.withMaxRetries(2)
|
||||
.abortOn(InterruptedException.class)
|
||||
.onFailedAttempt(e -> logger.warn(String.format("#%d tries failed", e.getAttemptCount()), e.getLastFailure()));
|
||||
|
||||
executionThread = new Thread(this::start, "Executor thread");
|
||||
executionThread.start();
|
||||
}
|
||||
|
||||
private ExecutionService() {
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void runExecutorThread() {
|
||||
new Thread(this::start, "Executor thread").start();
|
||||
@PreDestroy
|
||||
private void destroy() throws InterruptedException {
|
||||
logger.info("Shutting down ExecutionService");
|
||||
executionThread.interrupt();
|
||||
executor.shutdown();
|
||||
appStateService.getCurrentPosts().keySet().forEach(p -> {
|
||||
logger.info(String.format("Stopping download jobs for %s", p));
|
||||
this.stop(p);
|
||||
});
|
||||
executor.awaitTermination(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void stop(String postId) {
|
||||
@@ -77,7 +84,9 @@ public class ExecutionService {
|
||||
|
||||
data.forEach(e -> {
|
||||
futures.get(e.getImage().getUrl()).cancel(true);
|
||||
e.getImageFileData().getImageRequest().abort();
|
||||
if(e.getImageFileData().getImageRequest() != null) {
|
||||
e.getImageFileData().getImageRequest().abort();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -91,13 +100,17 @@ public class ExecutionService {
|
||||
}
|
||||
|
||||
public void start() {
|
||||
while (true) {
|
||||
while (!Thread.interrupted()) {
|
||||
if (canRun()) {
|
||||
DownloadJob take = null;
|
||||
try {
|
||||
take = downloadQ.take();
|
||||
if (take == null) {
|
||||
continue;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
|
||||
DownloadJob finalTake = take;
|
||||
@@ -117,7 +130,7 @@ public class ExecutionService {
|
||||
appStateService.doneDownloadJob(finalTake.getImage());
|
||||
logger.info(String.format("Finished downloading %s", finalTake.getImage().getUrl()));
|
||||
synchronized (threadCount) {
|
||||
int i = threadCount.decrementAndGet();
|
||||
threadCount.decrementAndGet();
|
||||
running.remove(finalTake);
|
||||
futures.remove(finalTake.getImage().getUrl());
|
||||
threadCount.notify();
|
||||
@@ -133,9 +146,14 @@ public class ExecutionService {
|
||||
threadCount.wait(2_000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int runningCount() {
|
||||
return running.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.exception.ValidationException;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
@@ -32,6 +33,7 @@ public class AppSettingsService {
|
||||
private final String V_USERNAME = "VUSERNAME";
|
||||
private final String V_PASSWORD = "VPASSWORD";
|
||||
private final String V_THANKS = "VTHANKS";
|
||||
private final String DESKTOP_CLIPBOARD = "DESKTOP_CLIPBOARD";
|
||||
|
||||
private String downloadPath;
|
||||
private int maxThreads;
|
||||
@@ -40,6 +42,7 @@ public class AppSettingsService {
|
||||
private String vUsername;
|
||||
private String vPassword;
|
||||
private boolean vThanks;
|
||||
private boolean desktopClipboard;
|
||||
|
||||
public void setVPassword(String vPassword) {
|
||||
if(vPassword.isEmpty()) {
|
||||
@@ -52,14 +55,16 @@ public class AppSettingsService {
|
||||
public void restore() {
|
||||
|
||||
downloadPath = prefs.get(DOWNLOAD_PATH, System.getProperty("user.dir"));
|
||||
maxThreads = prefs.getInt(MAX_THREADS, 1);
|
||||
autoStart = prefs.getBoolean(AUTO_START, false);
|
||||
maxThreads = prefs.getInt(MAX_THREADS, 4);
|
||||
autoStart = prefs.getBoolean(AUTO_START, true);
|
||||
vLogin = prefs.getBoolean(V_LOGIN, false);
|
||||
vUsername = prefs.get(V_USERNAME, "");
|
||||
vPassword = prefs.get(V_PASSWORD, "");
|
||||
vThanks = prefs.getBoolean(V_THANKS, false);
|
||||
desktopClipboard = prefs.getBoolean(DESKTOP_CLIPBOARD, false);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void save() {
|
||||
|
||||
prefs.put(DOWNLOAD_PATH, downloadPath);
|
||||
@@ -69,6 +74,7 @@ public class AppSettingsService {
|
||||
prefs.put(V_USERNAME, vUsername);
|
||||
prefs.put(V_PASSWORD, vPassword);
|
||||
prefs.putBoolean(V_THANKS, vThanks);
|
||||
prefs.putBoolean(DESKTOP_CLIPBOARD, desktopClipboard);
|
||||
|
||||
try {
|
||||
prefs.sync();
|
||||
@@ -113,8 +119,10 @@ public class AppSettingsService {
|
||||
private String vPassword;
|
||||
@JsonProperty("vThanks")
|
||||
private boolean vThanks;
|
||||
@JsonProperty("desktopClipboard")
|
||||
private boolean desktopClipboard;
|
||||
|
||||
public Settings(String downloadPath, int maxThreads, boolean autoStart, boolean vLogin, String vUsername, String vPassword, boolean vThanks) {
|
||||
public Settings(String downloadPath, int maxThreads, boolean autoStart, boolean vLogin, String vUsername, String vPassword, boolean vThanks, boolean desktopClipboard) {
|
||||
this.downloadPath = downloadPath;
|
||||
this.maxThreads = maxThreads;
|
||||
this.autoStart = autoStart;
|
||||
@@ -122,6 +130,7 @@ public class AppSettingsService {
|
||||
this.vUsername = vUsername;
|
||||
this.vPassword = vPassword;
|
||||
this.vThanks = vThanks;
|
||||
this.desktopClipboard = desktopClipboard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import io.reactivex.processors.ReplayProcessor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -10,10 +9,12 @@ import tn.mnlr.vripper.entities.Post;
|
||||
import tn.mnlr.vripper.host.Host;
|
||||
import tn.mnlr.vripper.q.DownloadJob;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Getter
|
||||
@@ -27,10 +28,6 @@ public class AppStateService {
|
||||
|
||||
private PublishProcessor<Image> liveImageUpdates = PublishProcessor.create();
|
||||
|
||||
// private ReplayProcessor<Image> allImageState = ReplayProcessor.create();
|
||||
|
||||
// private ReplayProcessor<Post> snapshotPostsState = ReplayProcessor.create();
|
||||
|
||||
private PublishProcessor<Post> livePostsState = PublishProcessor.create();
|
||||
|
||||
@Autowired
|
||||
@@ -52,8 +49,8 @@ public class AppStateService {
|
||||
return new Image(pageUrl, postId, postName, host, this);
|
||||
}
|
||||
|
||||
public Post createPost(String title, String url, List<Image> images, Map<String, String> metadata, String postId) {
|
||||
return new Post(title, url, images, metadata, postId, this);
|
||||
public Post createPost(String title, String url, List<Image> images, Map<String, String> metadata, String postId, String postCounter) {
|
||||
return new Post(title, url, images, metadata, postId, postCounter, this);
|
||||
}
|
||||
|
||||
public Post getPost(String postId) {
|
||||
@@ -82,7 +79,9 @@ public class AppStateService {
|
||||
if (post.getImages().stream().map(Image::getStatus).filter(e -> e.equals(Image.Status.ERROR)).count() > 0) {
|
||||
post.setStatus(Post.Status.ERROR);
|
||||
} else {
|
||||
post.setStatus(Post.Status.COMPLETE);
|
||||
if (!Post.Status.STOPPED.equals(post.getStatus())) {
|
||||
post.setStatus(Post.Status.COMPLETE);
|
||||
}
|
||||
}
|
||||
livePostsState.onNext(post);
|
||||
}
|
||||
@@ -93,4 +92,39 @@ public class AppStateService {
|
||||
runningPosts.put(key, new AtomicInteger(0));
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void remove(String postId) {
|
||||
currentPosts.get(postId).setRemoved(true);
|
||||
runningPosts.remove(postId);
|
||||
currentPosts.remove(postId);
|
||||
Iterator<Map.Entry<String, Image>> iterator = currentImages.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Image> entry = iterator.next();
|
||||
if(entry.getValue().getPostId().equals(postId)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
persistenceService.getProcessor().onNext(currentPosts);
|
||||
}
|
||||
|
||||
public synchronized List<String> clearAll() {
|
||||
List<String> toRemove = this.currentPosts
|
||||
.values()
|
||||
.stream()
|
||||
.filter(e -> e.getStatus().equals(Post.Status.COMPLETE) && e.getDone().get() >= e.getTotal())
|
||||
.map(Post::getPostId)
|
||||
.collect(Collectors.toList());
|
||||
toRemove.forEach(this::remove);
|
||||
return toRemove;
|
||||
}
|
||||
|
||||
public synchronized List<String> removeAll() {
|
||||
List<String> toRemove = this.currentPosts
|
||||
.values()
|
||||
.stream()
|
||||
.map(Post::getPostId)
|
||||
.collect(Collectors.toList());
|
||||
toRemove.forEach(this::remove);
|
||||
return toRemove;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class DownloadSpeed {
|
||||
|
||||
private final String type = "downSpeed";
|
||||
private String speed;
|
||||
|
||||
public DownloadSpeed(long bytes) {
|
||||
speed = formatSI(bytes);
|
||||
}
|
||||
|
||||
private String formatSI(long bytes) {
|
||||
return humanReadableByteCount(bytes, true);
|
||||
}
|
||||
|
||||
private String humanReadableByteCount(long bytes, boolean si) {
|
||||
int unit = si ? 1000 : 1024;
|
||||
if (bytes < unit) return bytes + " B";
|
||||
int exp = (int) (Math.log(bytes) / Math.log(unit));
|
||||
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
|
||||
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Service
|
||||
@EnableScheduling
|
||||
public class DownloadSpeedService {
|
||||
|
||||
private AtomicLong read = new AtomicLong(0);
|
||||
|
||||
@Getter
|
||||
private PublishProcessor<Long> readBytesPerSecond = PublishProcessor.create();
|
||||
|
||||
public void increase(long read) {
|
||||
this.read.addAndGet(read);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
private void calc() {
|
||||
readBytesPerSecond.onNext(read.getAndSet(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
public class GlobalState {
|
||||
|
||||
private final String type = "globalState";
|
||||
private long running;
|
||||
private long queued;
|
||||
private long remaining;
|
||||
private long error;
|
||||
|
||||
public GlobalState(long running, long queued, long remaining, long error) {
|
||||
this.running = running;
|
||||
this.queued = queued;
|
||||
this.remaining = remaining;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
GlobalState that = (GlobalState) o;
|
||||
return running == that.running &&
|
||||
queued == that.queued &&
|
||||
remaining == that.remaining &&
|
||||
error == that.error &&
|
||||
Objects.equals(type, that.type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(type, running, queued, remaining, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.entities.Image;
|
||||
import tn.mnlr.vripper.q.DownloadQ;
|
||||
import tn.mnlr.vripper.q.ExecutionService;
|
||||
|
||||
@Service
|
||||
@EnableScheduling
|
||||
public class GlobalStateService {
|
||||
|
||||
@Autowired
|
||||
private DownloadQ downloadQ;
|
||||
|
||||
@Autowired
|
||||
private ExecutionService executionService;
|
||||
|
||||
@Autowired
|
||||
private AppStateService appStateService;
|
||||
|
||||
@Getter
|
||||
private GlobalState currentState;
|
||||
|
||||
@Getter
|
||||
private PublishProcessor<GlobalState> liveGlobalState = PublishProcessor.create();
|
||||
|
||||
@Scheduled(fixedDelay = 3000)
|
||||
private void interval() {
|
||||
GlobalState newGlobalState = new GlobalState(
|
||||
executionService.runningCount(),
|
||||
downloadQ.size(),
|
||||
appStateService.getCurrentImages()
|
||||
.values()
|
||||
.stream()
|
||||
.filter(e -> e.getTotal() == 0 || e.getTotal() != e.getCurrent().get())
|
||||
.count(),
|
||||
appStateService.getCurrentImages()
|
||||
.values()
|
||||
.stream()
|
||||
.filter(e -> e.getStatus().equals(Image.Status.ERROR))
|
||||
.count());
|
||||
if (!newGlobalState.equals(currentState)) {
|
||||
currentState = newGlobalState;
|
||||
liveGlobalState.onNext(currentState);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package tn.mnlr.vripper.services;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import lombok.Getter;
|
||||
import org.slf4j.Logger;
|
||||
@@ -13,13 +14,19 @@ import tn.mnlr.vripper.entities.Post;
|
||||
import tn.mnlr.vripper.entities.mixin.persistance.ImagePersistanceMixin;
|
||||
import tn.mnlr.vripper.entities.mixin.persistance.PostPersistanceMixin;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class PersistenceService {
|
||||
@@ -31,15 +38,33 @@ public class PersistenceService {
|
||||
|
||||
private ObjectMapper om;
|
||||
|
||||
private Disposable subscription;
|
||||
|
||||
@Getter
|
||||
private PublishProcessor<Map<String, Post>> processor = PublishProcessor.create();
|
||||
|
||||
private PersistenceService() {
|
||||
File dataFile = new File(VripperApplication.dataPath);
|
||||
if(!dataFile.exists()) {
|
||||
try {
|
||||
if(dataFile.createNewFile()) {
|
||||
logger.info("Data file successfully created");
|
||||
try (FileWriter fw = new FileWriter(dataFile)) {
|
||||
fw.write("{}");
|
||||
}
|
||||
} else {
|
||||
logger.info("Data file already exists");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("Unable to create data file", e);
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
om = new ObjectMapper();
|
||||
om.addMixIn(Image.class, ImagePersistanceMixin.class);
|
||||
om.addMixIn(Post.class, PostPersistanceMixin.class);
|
||||
processor
|
||||
.onBackpressureBuffer()
|
||||
subscription = processor
|
||||
.onBackpressureLatest()
|
||||
.buffer(10, TimeUnit.SECONDS)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.get(0))
|
||||
@@ -47,8 +72,17 @@ public class PersistenceService {
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
this.subscription.dispose();
|
||||
logger.info(String.format("Destroying %s", PersistenceService.class.getSimpleName()));
|
||||
logger.info("Persisting data before destroying");
|
||||
this.persist(stateService.getCurrentPosts());
|
||||
}
|
||||
|
||||
public void persist(Map<String, Post> currentPosts) {
|
||||
try(PrintWriter out = new PrintWriter(VripperApplication.dataPath)) {
|
||||
|
||||
try (PrintWriter out = new PrintWriter(VripperApplication.dataPath, "UTF-8")) {
|
||||
out.print(om.writeValueAsString(currentPosts));
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to persist app state", e);
|
||||
@@ -70,7 +104,7 @@ public class PersistenceService {
|
||||
|
||||
String jsonContent;
|
||||
try {
|
||||
jsonContent = new String(Files.readAllBytes(Paths.get(VripperApplication.dataPath)));
|
||||
jsonContent = Files.readAllLines(Paths.get(VripperApplication.dataPath), StandardCharsets.UTF_8).stream().collect(Collectors.joining());
|
||||
} catch (Exception e) {
|
||||
logger.warn("data file not found, previous state cannot be restored", e);
|
||||
return;
|
||||
@@ -80,17 +114,20 @@ public class PersistenceService {
|
||||
|
||||
stateService.getCurrentPosts().clear();
|
||||
stateService.getCurrentPosts().putAll(read);
|
||||
stateService.getCurrentPosts().values().forEach(p -> {
|
||||
if(Arrays.asList(Post.Status.DOWNLOADING, Post.Status.PARTIAL).contains(p.getStatus())) {
|
||||
p.setStatus(Post.Status.STOPPED);
|
||||
}
|
||||
});
|
||||
|
||||
stateService.getCurrentImages().clear();
|
||||
read.values().stream().flatMap(e -> e.getImages().stream()).forEach(e -> {
|
||||
e.setAppStateService(stateService);
|
||||
stateService.getCurrentImages().put(e.getUrl(), e);
|
||||
// stateService.getAllImageState().onNext(e);
|
||||
});
|
||||
|
||||
read.values().forEach(e -> {
|
||||
e.setAppStateService(stateService);
|
||||
// stateService.getSnapshotPostsState().onNext(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ public class PostParser {
|
||||
private final static String POSTS_XPATH = "//li[contains(@id,'post_')][not(contains(@id,'post_thank'))]";
|
||||
private final static String REAL_THREAD_XPATH = ".//a[@class='postcounter']";
|
||||
private final static String THREAD_TITLE_XPATH = "//li[contains(@class, 'lastnavbit')]/span";
|
||||
private final static String POST_TITLE_XPATH = ".//h2";
|
||||
private final static String POST_TITLE_XPATH = ".//h2[contains(@class, 'title')]";
|
||||
private final static String POST_COUNTER_XPATH = ".//a[contains(@class, 'postcounter')]";
|
||||
private final static String POST_LINKS_XPATH = ".//a";
|
||||
|
||||
@Autowired
|
||||
@@ -147,6 +148,20 @@ public class PostParser {
|
||||
throw new PostParseException(e);
|
||||
}
|
||||
|
||||
String postCounter;
|
||||
try {
|
||||
logger.info(String.format("Finding post's counter"));
|
||||
Node counterNode = xpathService.getAsNode(postsNodeList.item(i), POST_COUNTER_XPATH);
|
||||
if (counterNode != null) {
|
||||
postCounter = counterNode.getTextContent().trim();
|
||||
logger.info(String.format("Found post's counter: %s", postCounter));
|
||||
} else {
|
||||
postCounter = "";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new PostParseException(e);
|
||||
}
|
||||
|
||||
ArrayList<Image> imagesList = new ArrayList<>();
|
||||
try {
|
||||
logger.info(String.format("Finding all links for post with id %s using xpath %s", postId, POST_LINKS_XPATH));
|
||||
@@ -177,7 +192,7 @@ public class PostParser {
|
||||
|
||||
if (!imagesList.isEmpty()) {
|
||||
logger.info(String.format("Found %d images for post with id %s", imagesList.size(), postId));
|
||||
posts.add(appStateService.createPost(postTitle, realUrl, imagesList, null, postId));
|
||||
posts.add(appStateService.createPost(postTitle, realUrl, imagesList, null, postId, postCounter));
|
||||
} else {
|
||||
logger.warn(String.format("No images found for post with id %s, skipping", postId));
|
||||
}
|
||||
|
||||
+72
-4
@@ -8,9 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import tn.mnlr.vripper.services.AppSettingsService;
|
||||
import tn.mnlr.vripper.entities.Post;
|
||||
import tn.mnlr.vripper.q.DownloadQ;
|
||||
import tn.mnlr.vripper.services.AppSettingsService;
|
||||
import tn.mnlr.vripper.services.AppStateService;
|
||||
import tn.mnlr.vripper.services.PostParser;
|
||||
import tn.mnlr.vripper.services.VipergirlsAuthService;
|
||||
@@ -67,13 +67,21 @@ public class PostRestEndpoint {
|
||||
if(appSettingsService.isAutoStart()) {
|
||||
logger.info("Auto start downloads option is enabled");
|
||||
logger.info(String.format("Starting to enqueue %d jobs for %s", parsed.stream().flatMap(e -> e.getImages().stream()).count(), url.url));
|
||||
parsed.forEach(post -> downloadQ.enqueue(post));
|
||||
for (Post post : parsed) {
|
||||
downloadQ.enqueue(post);
|
||||
}
|
||||
logger.info(String.format("Done enqueuing jobs for %s", url.url));
|
||||
} else {
|
||||
logger.info("Auto start downloads option is disabled");
|
||||
}
|
||||
logger.info(String.format("Done processing thread: %s", url.url));
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
return ResponseEntity.ok(new ParseResult(parsed.size()));
|
||||
}
|
||||
|
||||
@PostMapping("/clipboard/post")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public ResponseEntity processPostFromClipboard(@RequestBody ThreadUrl url) throws Exception {
|
||||
return this.processPost(url);
|
||||
}
|
||||
|
||||
@PostMapping("/post/restart")
|
||||
@@ -82,12 +90,44 @@ public class PostRestEndpoint {
|
||||
downloadQ.restart(postId.getPostId());
|
||||
}
|
||||
|
||||
@PostMapping("/post/restart/all")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public void restartPost() throws Exception {
|
||||
downloadQ.restartAll();
|
||||
}
|
||||
|
||||
@PostMapping("/post/stop")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public void stop(@RequestBody PostId postId) throws Exception {
|
||||
public void stop(@RequestBody PostId postId) {
|
||||
downloadQ.stop(postId.getPostId());
|
||||
}
|
||||
|
||||
@PostMapping("/post/stop/all")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public void stopAll() {
|
||||
downloadQ.stopAll();
|
||||
}
|
||||
|
||||
@PostMapping("/post/remove")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public ResponseEntity remove(@RequestBody PostId postId) {
|
||||
downloadQ.stop(postId.getPostId());
|
||||
appStateService.remove(postId.getPostId());
|
||||
return ResponseEntity.ok(new RemoveResult(postId.getPostId()));
|
||||
}
|
||||
|
||||
@PostMapping("/post/clear/all")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public ResponseEntity clearAll() {
|
||||
return ResponseEntity.ok(new RemoveAllResult(appStateService.clearAll()));
|
||||
}
|
||||
|
||||
@PostMapping("/post/remove/all")
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public ResponseEntity removeAll() {
|
||||
return ResponseEntity.ok(new RemoveAllResult(appStateService.removeAll()));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
private static class ThreadUrl {
|
||||
@@ -98,4 +138,32 @@ public class PostRestEndpoint {
|
||||
private static class PostId {
|
||||
private String postId;
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static class ParseResult {
|
||||
ParseResult(int parsed) {
|
||||
this.parsed = parsed;
|
||||
}
|
||||
private int parsed;
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static class RemoveAllResult {
|
||||
private List<String> postIds;
|
||||
private int removed;
|
||||
|
||||
RemoveAllResult(List<String> postIds) {
|
||||
this.removed = postIds.size();
|
||||
this.postIds = postIds;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static class RemoveResult {
|
||||
private String postId;
|
||||
|
||||
RemoveResult(String postId) {
|
||||
this.postId = postId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -56,11 +56,12 @@ public class SettingsRestEndpoint {
|
||||
this.settings.setVPassword("");
|
||||
this.settings.setVThanks(false);
|
||||
}
|
||||
this.settings.setDesktopClipboard(settings.isDesktopClipboard());
|
||||
|
||||
this.settings.save();
|
||||
|
||||
vipergirlsAuthService.authenticate();
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
return ResponseEntity.ok(getSettings());
|
||||
}
|
||||
|
||||
@GetMapping("/settings")
|
||||
@@ -74,7 +75,8 @@ public class SettingsRestEndpoint {
|
||||
settings.isVLogin(),
|
||||
settings.getVUsername(),
|
||||
settings.getVPassword(),
|
||||
settings.isVThanks()
|
||||
settings.isVThanks(),
|
||||
settings.isDesktopClipboard()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+115
-27
@@ -17,8 +17,12 @@ import tn.mnlr.vripper.entities.Post;
|
||||
import tn.mnlr.vripper.entities.mixin.ui.ImageUIMixin;
|
||||
import tn.mnlr.vripper.entities.mixin.ui.PostUIMixin;
|
||||
import tn.mnlr.vripper.services.AppStateService;
|
||||
import tn.mnlr.vripper.services.DownloadSpeed;
|
||||
import tn.mnlr.vripper.services.DownloadSpeedService;
|
||||
import tn.mnlr.vripper.services.GlobalStateService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -30,37 +34,38 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);
|
||||
|
||||
@Getter
|
||||
private static class WSMessage {
|
||||
|
||||
private String cmd;
|
||||
private String payload;
|
||||
|
||||
enum CMD {
|
||||
POSTS_SUB,
|
||||
POST_DETAILS_SUB,
|
||||
POSTS_UNSUB,
|
||||
POST_DETAILS_UNSUB
|
||||
}
|
||||
}
|
||||
|
||||
public WebSocketHandler() {
|
||||
om.addMixIn(Image.class, ImageUIMixin.class).addMixIn(Post.class, PostUIMixin.class);
|
||||
}
|
||||
|
||||
private Map<String, Disposable> postsSubscriptions = new ConcurrentHashMap<>();
|
||||
private Map<String, Disposable> postDetailsSubscriptions = new ConcurrentHashMap<>();
|
||||
private ObjectMapper om = new ObjectMapper();
|
||||
@Autowired
|
||||
private GlobalStateService globalStateService;
|
||||
|
||||
@Autowired
|
||||
private AppStateService appStateService;
|
||||
|
||||
@Autowired
|
||||
private DownloadSpeedService downloadSpeedService;
|
||||
|
||||
private Map<String, Disposable> postsSubscriptions = new ConcurrentHashMap<>();
|
||||
private Map<String, Disposable> postDetailsSubscriptions = new ConcurrentHashMap<>();
|
||||
private Map<String, Disposable> stateSubscriptions = new ConcurrentHashMap<>();
|
||||
private Map<String, Disposable> downloadSpeedSubscriptions = new ConcurrentHashMap<>();
|
||||
|
||||
private ObjectMapper om = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
|
||||
WSMessage wsMessage = om.readValue(message.getPayload(), WSMessage.class);
|
||||
WSMessage.CMD cmd = WSMessage.CMD.valueOf(wsMessage.getCmd());
|
||||
switch (cmd) {
|
||||
case GLOBAL_STATE_SUB:
|
||||
subscribeForGlobalState(session);
|
||||
break;
|
||||
case SPEED_SUB:
|
||||
subscribeForSpeed(session);
|
||||
break;
|
||||
case POSTS_SUB:
|
||||
subscribeForPosts(session);
|
||||
break;
|
||||
@@ -75,9 +80,70 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
||||
logger.info(String.format("Client %s unsubscribed from posts", session.getId()));
|
||||
Optional.ofNullable(postsSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
break;
|
||||
case GLOBAL_STATE_UNSUB:
|
||||
logger.info(String.format("Client %s unsubscribed from global state", session.getId()));
|
||||
Optional.ofNullable(stateSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
break;
|
||||
case SPEED_UNSUB:
|
||||
logger.info(String.format("Client %s unsubscribed from download speed info", session.getId()));
|
||||
Optional.ofNullable(downloadSpeedSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void subscribeForGlobalState(WebSocketSession session) {
|
||||
|
||||
logger.info(String.format("Client %s subscribed for global state", session.getId()));
|
||||
if (stateSubscriptions.containsKey(session.getId())) {
|
||||
stateSubscriptions.get(session.getId()).dispose();
|
||||
}
|
||||
|
||||
try {
|
||||
send(session, new TextMessage(om.writeValueAsString(Arrays.asList(globalStateService.getCurrentState()))));
|
||||
} catch (Exception e) {
|
||||
logger.error("Unexpected error occurred", e);
|
||||
}
|
||||
|
||||
stateSubscriptions.put(session.getId(),
|
||||
globalStateService.getLiveGlobalState()
|
||||
.onBackpressureBuffer()
|
||||
.observeOn(Schedulers.io())
|
||||
.map(Arrays::asList)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.subList(0, 1))
|
||||
.map(om::writeValueAsString)
|
||||
.map(TextMessage::new)
|
||||
.subscribe(msg -> send(session, msg), e -> logger.error("Failed to send data to client", e))
|
||||
);
|
||||
}
|
||||
|
||||
private void subscribeForSpeed(WebSocketSession session) {
|
||||
|
||||
logger.info(String.format("Client %s subscribed for download speed info", session.getId()));
|
||||
if (downloadSpeedSubscriptions.containsKey(session.getId())) {
|
||||
downloadSpeedSubscriptions.get(session.getId()).dispose();
|
||||
}
|
||||
|
||||
try {
|
||||
send(session, new TextMessage(om.writeValueAsString(Arrays.asList(new DownloadSpeed(0)))));
|
||||
} catch (Exception e) {
|
||||
logger.error("Unexpected error occurred", e);
|
||||
}
|
||||
|
||||
downloadSpeedSubscriptions.put(session.getId(),
|
||||
downloadSpeedService.getReadBytesPerSecond()
|
||||
.onBackpressureBuffer()
|
||||
.observeOn(Schedulers.io())
|
||||
.map(Arrays::asList)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.subList(0, 1))
|
||||
.map(e -> e.stream().map(DownloadSpeed::new).collect(Collectors.toList()))
|
||||
.map(om::writeValueAsString)
|
||||
.map(TextMessage::new)
|
||||
.subscribe(msg -> send(session, msg), e -> logger.error("Failed to send data to client", e))
|
||||
);
|
||||
}
|
||||
|
||||
private void subscribeForPosts(WebSocketSession session) {
|
||||
|
||||
logger.info(String.format("Client %s subscribed for posts", session.getId()));
|
||||
@@ -91,15 +157,17 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
||||
logger.error("Unexpected error occurred", e);
|
||||
}
|
||||
|
||||
postsSubscriptions.put(session.getId(), appStateService.getLivePostsState()
|
||||
.onBackpressureBuffer()
|
||||
.observeOn(Schedulers.io())
|
||||
.buffer(2, TimeUnit.SECONDS)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.stream().distinct().collect(Collectors.toList()))
|
||||
.map(om::writeValueAsString)
|
||||
.map(TextMessage::new)
|
||||
.subscribe(msg -> send(session, msg), e -> logger.error("Failed to send data to client", e))
|
||||
postsSubscriptions.put(session.getId(),
|
||||
appStateService.getLivePostsState()
|
||||
.onBackpressureBuffer()
|
||||
.observeOn(Schedulers.io())
|
||||
.filter(e -> !e.isRemoved())
|
||||
.buffer(500, TimeUnit.MILLISECONDS, 50)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.stream().distinct().collect(Collectors.toList()))
|
||||
.map(om::writeValueAsString)
|
||||
.map(TextMessage::new)
|
||||
.subscribe(msg -> send(session, msg), e -> logger.error("Failed to send data to client", e))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,7 +194,7 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
||||
.onBackpressureBuffer()
|
||||
.observeOn(Schedulers.io())
|
||||
.filter(e -> e.getPostId().equals(postId))
|
||||
.buffer(2, TimeUnit.SECONDS)
|
||||
.buffer(500, TimeUnit.MILLISECONDS, 50)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.map(e -> e.stream().distinct().collect(Collectors.toList()))
|
||||
.map(om::writeValueAsString)
|
||||
@@ -149,5 +217,25 @@ public class WebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
Optional.ofNullable(postsSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
Optional.ofNullable(postDetailsSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
Optional.ofNullable(stateSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
Optional.ofNullable(downloadSpeedSubscriptions.remove(session.getId())).ifPresent(d -> d.dispose());
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static class WSMessage {
|
||||
|
||||
private String cmd;
|
||||
private String payload;
|
||||
|
||||
enum CMD {
|
||||
POSTS_SUB,
|
||||
POST_DETAILS_SUB,
|
||||
POSTS_UNSUB,
|
||||
POST_DETAILS_UNSUB,
|
||||
GLOBAL_STATE_SUB,
|
||||
GLOBAL_STATE_UNSUB,
|
||||
SPEED_SUB,
|
||||
SPEED_UNSUB
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
logging.level.org.springframework.web=INFO
|
||||
logging.level.root=INFO
|
||||
logging.level.org.apache.http=INFO
|
||||
logging.file=${user.dir}/vripper.log
|
||||
logging.file=${user.dir}/vripper.log
|
||||
server.port=${vripper.server.port:8080}
|
||||
management.endpoints.web.exposure.include=shutdown
|
||||
management.endpoint.shutdown.enabled=true
|
||||
endpoints.shutdown.enabled=true
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource=
|
||||
"org/springframework/boot/logging/logback/defaults.xml" />
|
||||
<include resource=
|
||||
"org/springframework/boot/logging/logback/file-appender.xml" />
|
||||
|
||||
<include resource=
|
||||
"org/springframework/boot/logging/logback/console-appender.xml" />
|
||||
<root level="INFO">
|
||||
<appender-ref ref="FILE" />
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
</configuration>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
Generated
+573
-246
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,12 @@
|
||||
{
|
||||
"name": "vripper-ui",
|
||||
"version": "0.0.0",
|
||||
"version": "1.4.7",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"start": "ng serve",
|
||||
"build": "ng build",
|
||||
"prod": "ng build --prod",
|
||||
"build-electron": "ng build --prod --base-href ./",
|
||||
"build-web": "ng build --prod",
|
||||
"test": "ng test",
|
||||
"lint": "ng lint",
|
||||
"e2e": "ng e2e"
|
||||
@@ -27,6 +28,7 @@
|
||||
"ag-grid-community": "^21.0.1",
|
||||
"core-js": "^2.5.4",
|
||||
"hammerjs": "^2.0.8",
|
||||
"ngx-electron": "^2.1.1",
|
||||
"rxjs": "~6.3.3",
|
||||
"tslib": "^1.9.0",
|
||||
"zone.js": "~0.8.26"
|
||||
@@ -40,9 +42,10 @@
|
||||
"@types/jasminewd2": "~2.0.3",
|
||||
"@types/node": "~8.9.4",
|
||||
"codelyzer": "~4.5.0",
|
||||
"electron": "^5.0.5",
|
||||
"jasmine-core": "~2.99.1",
|
||||
"jasmine-spec-reporter": "~4.2.1",
|
||||
"karma": "~3.1.1",
|
||||
"karma": "^4.2.0",
|
||||
"karma-chrome-launcher": "~2.2.0",
|
||||
"karma-coverage-istanbul-reporter": "~2.0.1",
|
||||
"karma-jasmine": "~1.1.2",
|
||||
|
||||
+96
-31
@@ -5,46 +5,111 @@
|
||||
<parent>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
<artifactId>vripper</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.4.7</version>
|
||||
</parent>
|
||||
<artifactId>vripper-ui</artifactId>
|
||||
<name>vripper-ui</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.3</version>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<nodeVersion>v12.2.0</nodeVersion>
|
||||
<npmVersion>6.9.0</npmVersion>
|
||||
<workingDirectory>build-dir</workingDirectory>
|
||||
<classifier>${buildClassifier}</classifier>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run prod</arguments>
|
||||
</configuration>
|
||||
<phase>generate-resources</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>web</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<buildClassifier>web</buildClassifier>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.3</version>
|
||||
<configuration>
|
||||
<nodeVersion>v12.2.0</nodeVersion>
|
||||
<npmVersion>6.9.0</npmVersion>
|
||||
<workingDirectory>build-dir</workingDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run build-web</arguments>
|
||||
</configuration>
|
||||
<phase>generate-resources</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>electron</id>
|
||||
<properties>
|
||||
<buildClassifier>electron</buildClassifier>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.3</version>
|
||||
<configuration>
|
||||
<nodeVersion>v12.2.0</nodeVersion>
|
||||
<npmVersion>6.9.0</npmVersion>
|
||||
<workingDirectory>build-dir</workingDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run build-electron</arguments>
|
||||
</configuration>
|
||||
<phase>generate-resources</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
<div *ngIf="state | async as _state">
|
||||
<div fxLayout="column" *ngIf="connecting(_state)" class="overlay loading" fxLayoutAlign="center center">
|
||||
<mat-spinner></mat-spinner>
|
||||
<h2>Connecting</h2>
|
||||
</div>
|
||||
<div fxLayout="column" *ngIf="noConnectionState(_state)" class="overlay no-connection" fxLayoutAlign="center center">
|
||||
<mat-icon class="overlay-icon">error</mat-icon>
|
||||
<h2>Connection with the server is closed</h2>
|
||||
</div>
|
||||
<div fxLayout="column" *ngIf="connecting()" class="overlay loading" fxLayoutAlign="center center">
|
||||
<mat-spinner></mat-spinner>
|
||||
<h2>Connecting</h2>
|
||||
</div>
|
||||
<div id="app-container" fxLayout="column" fxLayoutGap="20px">
|
||||
<div fxLayout="column" *ngIf="noConnectionState()" class="overlay no-connection" fxLayoutAlign="center center">
|
||||
<mat-icon class="overlay-icon">error</mat-icon>
|
||||
<h2>Connection with the server is closed</h2>
|
||||
</div>
|
||||
<div [ngClass]="{'electron': electronService.isElectronApp}" id="app-container" fxLayout="column" fxLayoutGap="20px">
|
||||
<div id="app-header" fxFlex="nogrow">
|
||||
<mat-toolbar class="mat-elevation-z2" color="primary">
|
||||
<mat-toolbar-row fxLayout="row">
|
||||
<span>Viper Ripper</span>
|
||||
<mat-toolbar-row fxLayout="row" fxLayoutAlign="center center">
|
||||
<span id="logo" fxFlex="0 0 48px"></span>
|
||||
<span id="title">Viper Ripper</span>
|
||||
<span fxFlex="grow"></span>
|
||||
<div>
|
||||
<button mat-icon-button [matMenuTriggerFor]="menu" aria-label="settings">
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
#logo {
|
||||
height: 48px;
|
||||
margin-right: 10px;
|
||||
background-image: url('../assets/logo.png');
|
||||
background-repeat: no-repeat;
|
||||
background-size: 48px 48px;
|
||||
}
|
||||
|
||||
#title {
|
||||
user-select: none
|
||||
}
|
||||
|
||||
.overlay-icon {
|
||||
font-size: 128px;
|
||||
width: 128px;
|
||||
@@ -7,3 +19,7 @@
|
||||
.no-connection {
|
||||
color: #eb6060;
|
||||
}
|
||||
|
||||
.electron {
|
||||
margin-top: 30px;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ClipboardService } from './clipboard.service';
|
||||
import { ElectronService } from 'ngx-electron';
|
||||
import { SettingsComponent } from './settings/settings.component';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { MatDialog } from '@angular/material';
|
||||
@@ -11,16 +13,25 @@ import { WsConnectionService, WSState } from './ws-connection.service';
|
||||
styleUrls: ['./app.component.scss']
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
constructor(public dialog: MatDialog, private breakpointObserver: BreakpointObserver, private ws: WsConnectionService) {
|
||||
this.state = this.ws.state;
|
||||
this.state.subscribe(e => {
|
||||
if (e === WSState.CLOSE) {
|
||||
this.dialog.closeAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
constructor(
|
||||
public dialog: MatDialog,
|
||||
private breakpointObserver: BreakpointObserver,
|
||||
private ws: WsConnectionService,
|
||||
public electronService: ElectronService,
|
||||
private clipboardService: ClipboardService
|
||||
) {
|
||||
this.currentState = WSState.INIT;
|
||||
this.ws.state.subscribe(e => {
|
||||
this.currentState = e;
|
||||
if (this.currentState === WSState.CLOSE || this.currentState === WSState.ERROR) {
|
||||
this.dialog.closeAll();
|
||||
} else if(this.currentState === WSState.OPEN) {
|
||||
this.clipboardService.init();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
state: Observable<WSState>;
|
||||
currentState: WSState;
|
||||
|
||||
isExtraSmall: Observable<BreakpointState> = this.breakpointObserver.observe(Breakpoints.XSmall);
|
||||
|
||||
@@ -45,13 +56,14 @@ export class AppComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
noConnectionState(state: WSState): boolean {
|
||||
return state === WSState.CLOSE;
|
||||
noConnectionState(): boolean {
|
||||
return this.currentState === WSState.CLOSE || this.currentState === WSState.ERROR;
|
||||
}
|
||||
|
||||
connecting(state: WSState): boolean {
|
||||
return state === WSState.CONNECTING;
|
||||
connecting(): boolean {
|
||||
return this.currentState === WSState.INIT || this.currentState === WSState.CONNECTING;
|
||||
}
|
||||
|
||||
ngOnInit() {}
|
||||
ngOnInit() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { PostDetailComponent } from './post-detail/post-detail.component';
|
||||
import { FlexLayoutModule } from '@angular/flex-layout';
|
||||
import { AppRoutingModule } from './app-routing.module';
|
||||
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||
import { AgGridModule } from 'ag-grid-angular';
|
||||
import { PostProgressRendererComponent } from './posts/post-progress.renderer.component';
|
||||
import { MenuRendererComponent } from './posts/menu.renderer.component';
|
||||
@@ -20,6 +20,10 @@ import { XhrInterceptorService } from './xhr-interceptor.service';
|
||||
import { HomeComponent } from './home/home.component';
|
||||
import { SettingsComponent } from './settings/settings.component';
|
||||
|
||||
import { NgxElectronModule } from 'ngx-electron';
|
||||
import { ServerService } from './server-service';
|
||||
import { ConfirmDialogComponent } from './common/confirmation-component/confirmation-dialog';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
@@ -30,11 +34,13 @@ import { SettingsComponent } from './settings/settings.component';
|
||||
PostDetailsProgressRendererComponent,
|
||||
LoginComponent,
|
||||
HomeComponent,
|
||||
SettingsComponent
|
||||
SettingsComponent,
|
||||
ConfirmDialogComponent
|
||||
],
|
||||
entryComponents: [
|
||||
PostDetailComponent,
|
||||
SettingsComponent
|
||||
SettingsComponent,
|
||||
ConfirmDialogComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserAnimationsModule,
|
||||
@@ -43,9 +49,11 @@ import { SettingsComponent } from './settings/settings.component';
|
||||
MaterialModule,
|
||||
FlexLayoutModule,
|
||||
AppRoutingModule,
|
||||
NgxElectronModule,
|
||||
ReactiveFormsModule,
|
||||
AgGridModule.withComponents([PostProgressRendererComponent, MenuRendererComponent, PostDetailsProgressRendererComponent])
|
||||
],
|
||||
providers: [AppService, WsConnectionService, { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptorService, multi: true }],
|
||||
providers: [AppService, WsConnectionService, { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptorService, multi: true }, ServerService],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ClipboardService } from './clipboard.service';
|
||||
|
||||
describe('ClipboardService', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({}));
|
||||
|
||||
it('should be created', () => {
|
||||
const service: ClipboardService = TestBed.get(ClipboardService);
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import { Subject, Observable } from 'rxjs';
|
||||
import { ElectronService } from 'ngx-electron';
|
||||
import { Injectable, OnInit } from '@angular/core';
|
||||
import { Clipboard } from 'electron';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Settings } from './common/settings.model';
|
||||
import { ServerService } from './server-service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ClipboardService {
|
||||
private links$: Subject<string> = new Subject();
|
||||
private interval: NodeJS.Timer;
|
||||
private lastText = '';
|
||||
constructor(
|
||||
private electronService: ElectronService,
|
||||
private serverService: ServerService,
|
||||
private httpClient: HttpClient
|
||||
) {}
|
||||
|
||||
init(settings?: Settings) {
|
||||
if (!settings) {
|
||||
this.httpClient.get<Settings>(this.serverService.baseUrl + '/settings').subscribe(
|
||||
data => {
|
||||
this._init(data);
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
this._init(settings);
|
||||
}
|
||||
}
|
||||
|
||||
_init(settings: Settings) {
|
||||
if (!this.electronService.isElectronApp) {
|
||||
console.log('Clipboard deactive, not an electron app');
|
||||
return;
|
||||
}
|
||||
if (this.interval != null) {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
if (!settings.desktopClipboard) {
|
||||
return;
|
||||
}
|
||||
|
||||
const clipboard: Clipboard = this.electronService.clipboard;
|
||||
this.interval = setInterval(() => {
|
||||
const text = clipboard.readText();
|
||||
|
||||
if (this.textHasDiff(text, this.lastText)) {
|
||||
this.lastText = text;
|
||||
if (text.indexOf('https://vipergirls.to/threads') !== -1) {
|
||||
this.links$.next(text);
|
||||
}
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
|
||||
private textHasDiff(a, b) {
|
||||
return a && b !== a;
|
||||
}
|
||||
|
||||
get links(): Observable<string> {
|
||||
return this.links$.asObservable();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
export enum CMD {
|
||||
POSTS_SUB = 'POSTS_SUB',
|
||||
POST_DETAILS_SUB = 'POST_DETAILS_SUB',
|
||||
GLOBAL_STATE_SUB = 'GLOBAL_STATE_SUB',
|
||||
SPEED_SUB = 'SPEED_SUB',
|
||||
POSTS_UNSUB = 'POSTS_UNSUB',
|
||||
POST_DETAILS_UNSUB = 'POST_DETAILS_UNSUB'
|
||||
POST_DETAILS_UNSUB = 'POST_DETAILS_UNSUB',
|
||||
GLOBAL_STATE_UNSUB = 'GLOBAL_STATE_UNSUB',
|
||||
SPEED_UNSUB = 'SPEED_UNSUB'
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<h1 mat-dialog-title>{{ data.header }}</h1>
|
||||
<mat-dialog-content>
|
||||
<p>{{ data.content }}</p>
|
||||
</mat-dialog-content>
|
||||
<mat-dialog-actions align="end">
|
||||
<button cdkFocusInitial mat-dialog-close="no" mat-raised-button>No</button>
|
||||
<button color="primary" mat-dialog-close="yes" mat-raised-button>Yes</button>
|
||||
</mat-dialog-actions>
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Component, Inject } from '@angular/core';
|
||||
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
|
||||
|
||||
export interface DialogConfirmation {
|
||||
header: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-confirmation-dialog',
|
||||
templateUrl: 'confirmation-dialog.html'
|
||||
})
|
||||
export class ConfirmDialogComponent {
|
||||
constructor(public dialogRef: MatDialogRef<ConfirmDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: DialogConfirmation) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export class DownloadSpeed {
|
||||
constructor(public speed: string) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export class GlobalState {
|
||||
constructor(public running: number, public queued: number, public remaining: number, public error: number) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface ParseResponse {
|
||||
parsed: number;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface RemoveAllResponse {
|
||||
removed: number;
|
||||
postIds: string[];
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface RemoveResponse {
|
||||
postId: string;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export interface Settings {
|
||||
downloadPath: string;
|
||||
maxThreads: number;
|
||||
autoStart: boolean;
|
||||
vLogin: boolean;
|
||||
vUsername: string;
|
||||
vPassword: string;
|
||||
vThanks: boolean;
|
||||
desktopClipboard: boolean;
|
||||
}
|
||||
@@ -1,18 +1,46 @@
|
||||
<div *ngIf="loading" class="overlay loading" fxLayout="row" fxLayoutAlign="center center">
|
||||
<mat-spinner></mat-spinner>
|
||||
</div>
|
||||
<div fxLayout="column" style="height: 100%">
|
||||
<div fxLayout="column" fxLayoutGap="10px" style="height: 100%">
|
||||
<div fxFlex="nogrow">
|
||||
<form #f="ngForm" fxLayout="row" fxLayoutAlign="center center" fxLayoutGap="20px" autocomplete="off">
|
||||
<form
|
||||
#f="ngForm"
|
||||
(ngSubmit)="submit(f)"
|
||||
autocomplete="off"
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="center center"
|
||||
fxLayoutGap="20px"
|
||||
>
|
||||
<mat-form-field fxFlex="grow">
|
||||
<input name="url" [(ngModel)]="input" matInput placeholder="Put a vipergirls.to link" required>
|
||||
<input [(ngModel)]="input" matInput name="url" placeholder="Put a vipergirls.to link" required/>
|
||||
</mat-form-field>
|
||||
<div>
|
||||
<button (click)="submit(f)" type="button" mat-raised-button color="primary" [disabled]="f.invalid">Grab</button>
|
||||
<button type="submit" mat-raised-button color="primary" [disabled]="f.invalid">Grab</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div fxLayout="column" fxFlex="grow">
|
||||
<app-posts style="height: 100%; width: 100%;" fxFlex="nogrow"></app-posts>
|
||||
<div fxLayout="row" fxLayoutAlign="end" fxLayoutGap="10px" id="controls">
|
||||
<button (click)="restartAll()" aria-label="Start" color="primary" mat-mini-fab title="Start">
|
||||
<mat-icon>play_arrow</mat-icon>
|
||||
</button>
|
||||
<button (click)="stopAll()" aria-label="Stop" color="primary" mat-mini-fab title="Stop">
|
||||
<mat-icon>stop</mat-icon>
|
||||
</button>
|
||||
<div fxFlex="grow"></div>
|
||||
<button (click)="clear()" aria-label="Clear completed" color="primary" mat-mini-fab title="Clear completed">
|
||||
<mat-icon>clear_all</mat-icon>
|
||||
</button>
|
||||
<button (click)="remove()" aria-label="Remove All" color="primary" mat-mini-fab title="Remove All">
|
||||
<mat-icon>delete_forever</mat-icon>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<mat-divider></mat-divider>
|
||||
<div [fxFlex]="electronService.isElectronApp ? '0 0 calc(100% - 190px)' : '0 0 calc(100% - 163px)'">
|
||||
<app-posts [ngStyle]="{'height': electronService.isElectronApp ? 'calc(100% - 37px)' : '100%'}"
|
||||
style="width: 100%;"></app-posts>
|
||||
</div>
|
||||
<div style="text-align: end">{{ downloadSpeed!.speed + '/s' }} | Downloading: {{ globalState!.running }} | Queued: {{
|
||||
globalState!.queued }} | Remaining:
|
||||
{{ globalState!.remaining }} | Error: {{ globalState!.error }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
div#controls {
|
||||
margin-left: 10px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
@@ -1,43 +1,203 @@
|
||||
import { Component, OnInit, HostBinding } from '@angular/core';
|
||||
import { GlobalState } from './../common/global-state.model';
|
||||
import { ElectronService } from 'ngx-electron';
|
||||
import { ClipboardService } from './../clipboard.service';
|
||||
import { ParseResponse } from './../common/parse-response.model';
|
||||
import { Component, OnInit, ViewChild, Inject, NgZone, OnDestroy } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { finalize } from 'rxjs/operators';
|
||||
import { MatSnackBar } from '@angular/material';
|
||||
import { finalize, filter, flatMap } from 'rxjs/operators';
|
||||
import { MatSnackBar, MatDialog } from '@angular/material';
|
||||
import { NgForm } from '@angular/forms';
|
||||
import { ServerService } from '../server-service';
|
||||
import { RemoveAllResponse } from '../common/remove-all-response.model';
|
||||
import { PostsComponent } from '../posts/posts.component';
|
||||
import { ConfirmDialogComponent } from '../common/confirmation-component/confirmation-dialog';
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { WsHandler } from '../ws-handler';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { WSMessage } from '../common/ws-message.model';
|
||||
import { CMD } from '../common/cmd.enum';
|
||||
import { DownloadSpeed } from '../common/download-speed.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
templateUrl: './home.component.html',
|
||||
styleUrls: ['./home.component.scss']
|
||||
})
|
||||
export class HomeComponent implements OnInit {
|
||||
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
private _snackBar: MatSnackBar
|
||||
) { }
|
||||
|
||||
export class HomeComponent implements OnInit, OnDestroy {
|
||||
@ViewChild(PostsComponent)
|
||||
private postsComponent: PostsComponent;
|
||||
loading = false;
|
||||
input: string;
|
||||
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
private _snackBar: MatSnackBar,
|
||||
private serverService: ServerService,
|
||||
private clipboardService: ClipboardService,
|
||||
public dialog: MatDialog,
|
||||
public electronService: ElectronService,
|
||||
private ngZone: NgZone,
|
||||
private wsConnectionService: WsConnectionService,
|
||||
) {
|
||||
this.websocketHandlerPromise = this.wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
subscriptions: Subscription[] = [];
|
||||
globalState: GlobalState = new GlobalState(0, 0, 0, 0);
|
||||
downloadSpeed: DownloadSpeed = new DownloadSpeed('0 B');
|
||||
|
||||
ngOnInit() {
|
||||
this.clipboardService.links.subscribe(e => {
|
||||
this.ngZone.run(() => {
|
||||
this.loading = true;
|
||||
this.httpClient
|
||||
.post<ParseResponse>(this.serverService.baseUrl + '/clipboard/post', { url: e })
|
||||
.pipe(
|
||||
finalize(() => {
|
||||
this.loading = false;
|
||||
})
|
||||
)
|
||||
.subscribe(
|
||||
data => {
|
||||
this._snackBar.open(`${data.parsed} posts parsed`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
console.log('Connecting to global state and download speed');
|
||||
this.subscriptions.push(
|
||||
handler.subscribeForGlobalState((e: GlobalState[]) => {
|
||||
this.ngZone.run(() => {
|
||||
this.globalState = e[0];
|
||||
});
|
||||
})
|
||||
);
|
||||
this.subscriptions.push(
|
||||
handler.subscribeForSpeed((e: DownloadSpeed[]) => {
|
||||
this.ngZone.run(() => {
|
||||
this.downloadSpeed = e[0];
|
||||
});
|
||||
})
|
||||
);
|
||||
handler.send(new WSMessage(CMD.GLOBAL_STATE_SUB.toString()));
|
||||
handler.send(new WSMessage(CMD.SPEED_SUB.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
submit(form: NgForm) {
|
||||
this.loading = true;
|
||||
this.httpClient
|
||||
.post(environment.localhost + '/post', { url: this.input })
|
||||
.pipe(finalize(() => {
|
||||
this.loading = false;
|
||||
this.input = null;
|
||||
form.resetForm();
|
||||
}))
|
||||
.subscribe(data => {
|
||||
}, error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000,
|
||||
});
|
||||
});
|
||||
this.ngZone.run(() => {
|
||||
this.loading = true;
|
||||
this.httpClient
|
||||
.post<ParseResponse>(this.serverService.baseUrl + '/post', { url: this.input })
|
||||
.pipe(
|
||||
finalize(() => {
|
||||
this.loading = false;
|
||||
this.input = null;
|
||||
form.resetForm();
|
||||
})
|
||||
)
|
||||
.subscribe(
|
||||
data => {
|
||||
this._snackBar.open(`${data.parsed} posts parsed`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.ngZone.run(() => {
|
||||
this.httpClient.post<RemoveAllResponse>(this.serverService.baseUrl + '/post/clear/all', {}).subscribe(
|
||||
data => {
|
||||
this.postsComponent.removeRows(data.postIds);
|
||||
this._snackBar.open(`${data.removed} items cleared`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
remove() {
|
||||
this.ngZone.run(() => {
|
||||
this.dialog
|
||||
.open(ConfirmDialogComponent, {
|
||||
maxHeight: '100vh',
|
||||
maxWidth: '100vw',
|
||||
height: '200px',
|
||||
width: '60%',
|
||||
data: { header: 'Confirmation', content: 'Are you sure you want to remove all items ?' }
|
||||
})
|
||||
.afterClosed()
|
||||
.pipe(
|
||||
filter(e => e === 'yes'),
|
||||
flatMap(e => this.httpClient.post<RemoveAllResponse>(this.serverService.baseUrl + '/post/remove/all', {}))
|
||||
)
|
||||
.subscribe(
|
||||
data => {
|
||||
this.postsComponent.removeRows(data.postIds);
|
||||
this._snackBar.open(`${data.removed} items removed`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
stopAll() {
|
||||
this.ngZone.run(() => {
|
||||
this.httpClient.post(this.serverService.baseUrl + '/post/stop/all', {})
|
||||
.subscribe(
|
||||
() => {
|
||||
this._snackBar.open(`Download stopped`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
restartAll() {
|
||||
this.ngZone.run(() => {
|
||||
this.httpClient.post(this.serverService.baseUrl + '/post/restart/all', {})
|
||||
.subscribe(
|
||||
() => {
|
||||
this._snackBar.open(`Download started`, null, { duration: 5000 });
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this.subscriptions.forEach(e => e.unsubscribe());
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
handler.send(new WSMessage(CMD.GLOBAL_STATE_UNSUB.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ import {
|
||||
MatCardModule,
|
||||
MatSlideToggleModule,
|
||||
MatSnackBarModule,
|
||||
MatSnackBar
|
||||
MatSnackBar,
|
||||
MatTabsModule,
|
||||
MatDividerModule
|
||||
} from '@angular/material';
|
||||
|
||||
@NgModule({
|
||||
@@ -30,7 +32,9 @@ import {
|
||||
MatDialogModule,
|
||||
MatCardModule,
|
||||
MatSlideToggleModule,
|
||||
MatSnackBarModule
|
||||
MatSnackBarModule,
|
||||
MatTabsModule,
|
||||
MatDividerModule
|
||||
],
|
||||
providers: [MatSnackBar]
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PostDetailsProgressRendererComponent } from './post-details-progress.component';
|
||||
import { WsConnectionService } from './../ws-connection.service';
|
||||
import { Component, OnInit, ViewChild, Inject, OnDestroy } from '@angular/core';
|
||||
import { Component, OnInit, ViewChild, Inject, OnDestroy, NgZone } from '@angular/core';
|
||||
import { MatSort, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
|
||||
import { PostDetailsDataSource } from './post-details.datasource';
|
||||
import { PostState } from '../posts/post-state.model';
|
||||
@@ -16,7 +16,8 @@ export class PostDetailComponent implements OnInit, OnDestroy {
|
||||
constructor(
|
||||
public dialogRef: MatDialogRef<PostDetailComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public dialogData: PostState,
|
||||
private wsConnection: WsConnectionService) {
|
||||
private wsConnection: WsConnectionService,
|
||||
private zone: NgZone) {
|
||||
|
||||
this.gridOptions = <GridOptions> {
|
||||
columnDefs: [
|
||||
@@ -29,6 +30,7 @@ export class PostDetailComponent implements OnInit, OnDestroy {
|
||||
cellClass: 'no-padding'
|
||||
}
|
||||
],
|
||||
enableBrowserTooltips: true,
|
||||
rowHeight: 48,
|
||||
rowData: [],
|
||||
frameworkComponents: {
|
||||
@@ -39,7 +41,7 @@ export class PostDetailComponent implements OnInit, OnDestroy {
|
||||
getRowNodeId: (data) => data['url'],
|
||||
onGridReady: () => {
|
||||
this.gridOptions.api.sizeColumnsToFit();
|
||||
this.dataSource = new PostDetailsDataSource(this.wsConnection, this.gridOptions, this.dialogData.postId);
|
||||
this.dataSource = new PostDetailsDataSource(this.wsConnection, this.gridOptions, this.dialogData.postId, this.zone);
|
||||
this.dataSource.connect();
|
||||
},
|
||||
onGridSizeChanged: () => this.gridOptions.api.sizeColumnsToFit(),
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
|
||||
import { AgRendererComponent } from 'ag-grid-angular';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { PostDetails } from './post-details.model';
|
||||
import { WsHandler } from '../ws-handler';
|
||||
import { ICellRendererParams } from 'ag-grid-community';
|
||||
|
||||
@Component({
|
||||
selector: 'app-progress-cell',
|
||||
@@ -20,7 +22,11 @@ import { PostDetails } from './post-details.model';
|
||||
class="progress-bar-back"
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-between"
|
||||
[ngClass]="{ error: postDetails.status === 'ERROR', stopped: postDetails.status === 'STOPPED' }"
|
||||
[ngClass]="{
|
||||
error: postDetails.status === 'ERROR',
|
||||
stopped: postDetails.status === 'STOPPED',
|
||||
pending: postDetails.status === 'PENDING'
|
||||
}"
|
||||
>
|
||||
<div>{{ postDetails.url }}</div>
|
||||
<div>{{ trunc(postDetails.progress) + '%' }}</div>
|
||||
@@ -45,7 +51,7 @@ import { PostDetails } from './post-details.model';
|
||||
background-color: #3865a3;
|
||||
}
|
||||
.pending {
|
||||
background-color: #a8a8a8;
|
||||
background-color: white;
|
||||
}
|
||||
.error {
|
||||
background-color: #eb6060;
|
||||
@@ -60,10 +66,16 @@ import { PostDetails } from './post-details.model';
|
||||
]
|
||||
})
|
||||
export class PostDetailsProgressRendererComponent implements AgRendererComponent, OnInit, OnDestroy {
|
||||
constructor(private wsConnectionService: WsConnectionService) {}
|
||||
constructor(
|
||||
private wsConnectionService: WsConnectionService,
|
||||
private zone: NgZone
|
||||
) {
|
||||
this.websocketHandlerPromise = this.wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
subscription: Subscription;
|
||||
params: any;
|
||||
params: ICellRendererParams;
|
||||
postDetails: PostDetails;
|
||||
|
||||
trunc(value: number): number {
|
||||
@@ -71,11 +83,15 @@ export class PostDetailsProgressRendererComponent implements AgRendererComponent
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscription = this.wsConnectionService.subscribeForPostDetails(e => {
|
||||
e.forEach(v => {
|
||||
if (this.postDetails.url === v.url) {
|
||||
this.postDetails = v;
|
||||
}
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
this.subscription = handler.subscribeForPostDetails(e => {
|
||||
this.zone.run(() => {
|
||||
e.forEach(v => {
|
||||
if (this.postDetails.url === v.url) {
|
||||
this.postDetails = v;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -84,12 +100,12 @@ export class PostDetailsProgressRendererComponent implements AgRendererComponent
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
|
||||
agInit(params: any): void {
|
||||
agInit(params: ICellRendererParams): void {
|
||||
this.params = params;
|
||||
this.postDetails = params.data;
|
||||
}
|
||||
|
||||
refresh(params: any): boolean {
|
||||
refresh(params: ICellRendererParams): boolean {
|
||||
this.postDetails = params.data;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,46 +1,54 @@
|
||||
import { WsHandler } from './../ws-handler';
|
||||
import { GridOptions } from 'ag-grid-community';
|
||||
import { Subject, Subscription } from 'rxjs';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { WsConnectionService, WSState } from '../ws-connection.service';
|
||||
import { WSMessage } from '../common/ws-message.model';
|
||||
import { CMD } from '../common/cmd.enum';
|
||||
declare var SockJS;
|
||||
import { NgZone } from '@angular/core';
|
||||
|
||||
export class PostDetailsDataSource {
|
||||
constructor(
|
||||
private wsConnectionService: WsConnectionService,
|
||||
private gridOptions: GridOptions,
|
||||
private postId: string,
|
||||
private zone: NgZone
|
||||
) {
|
||||
this.websocketHandlerPromise = wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
constructor(private websocketConnection: WsConnectionService, private gridOptions: GridOptions, private postId: string) {
|
||||
this.websocket = websocketConnection.getConnection();
|
||||
}
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
|
||||
websocket: Subject<any>;
|
||||
subscriptions: Subscription[] = [];
|
||||
|
||||
subscriptions: Subscription[] = [];
|
||||
|
||||
connect() {
|
||||
|
||||
console.log('Connecting to post details datasource');
|
||||
this.subscriptions.push(this.websocketConnection.subscribeForPostDetails(e => {
|
||||
connect() {
|
||||
console.log('Connecting to post details datasource');
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
this.subscriptions.push(
|
||||
handler.subscribeForPostDetails(e => {
|
||||
this.zone.run(() => {
|
||||
const toAdd = [];
|
||||
const toUpdate = [];
|
||||
e.forEach(v => {
|
||||
if (this.gridOptions.api.getRowNode(v.url) == null) {
|
||||
toAdd.push(v);
|
||||
} else {
|
||||
toUpdate.push(v);
|
||||
}
|
||||
if (this.gridOptions.api.getRowNode(v.url) == null) {
|
||||
toAdd.push(v);
|
||||
} else {
|
||||
toUpdate.push(v);
|
||||
}
|
||||
});
|
||||
this.gridOptions.api.updateRowData({ update: toUpdate, add: toAdd });
|
||||
}));
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
this.subscriptions.push(this.websocketConnection.state.subscribe((e) => {
|
||||
if (e === WSState.OPEN) {
|
||||
this.websocket.next(new WSMessage(CMD.POST_DETAILS_SUB.toString(), this.postId));
|
||||
}
|
||||
}));
|
||||
}
|
||||
handler.send(new WSMessage(CMD.POST_DETAILS_SUB.toString(), this.postId));
|
||||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
console.log('Disconnecting from post details datasource');
|
||||
this.subscriptions.forEach(e => e.unsubscribe());
|
||||
this.websocket.next(new WSMessage(CMD.POST_DETAILS_UNSUB.toString()));
|
||||
}
|
||||
disconnect() {
|
||||
console.log('Disconnecting from post details datasource');
|
||||
this.subscriptions.forEach(e => e.unsubscribe());
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
handler.send(new WSMessage(CMD.POST_DETAILS_UNSUB.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
|
||||
import { AgRendererComponent } from 'ag-grid-angular';
|
||||
import { PostState } from './post-state.model';
|
||||
import { PostDetailComponent } from '../post-detail/post-detail.component';
|
||||
import { MatDialog } from '@angular/material';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { MatDialog, MatSnackBar } from '@angular/material';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { BreakpointObserver, BreakpointState, Breakpoints } from '@angular/cdk/layout';
|
||||
import { Observable, Subscription } from 'rxjs';
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { WsHandler } from '../ws-handler';
|
||||
import { ServerService } from '../server-service';
|
||||
import { ICellRendererParams } from 'ag-grid-community';
|
||||
import { RemoveResponse } from '../common/remove-response.model';
|
||||
import { ConfirmDialogComponent } from '../common/confirmation-component/confirmation-dialog';
|
||||
import { filter, flatMap } from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-menu-cell',
|
||||
@@ -22,7 +27,7 @@ import { WsConnectionService } from '../ws-connection.service';
|
||||
<button
|
||||
*ngIf="
|
||||
postData.status === 'PENDING' ||
|
||||
postData.status === 'COMPLETE' ||
|
||||
(postData.status === 'COMPLETE' && postData.progress !== 100) ||
|
||||
postData.status === 'ERROR' ||
|
||||
postData.status === 'STOPPED'
|
||||
"
|
||||
@@ -39,34 +44,49 @@ import { WsConnectionService } from '../ws-connection.service';
|
||||
</ng-container>
|
||||
</button>
|
||||
<button (click)="seeDetails()" mat-menu-item>
|
||||
<mat-icon>details</mat-icon>
|
||||
<mat-icon>list</mat-icon>
|
||||
<span>Details</span>
|
||||
</button>
|
||||
<button (click)="remove()" mat-menu-item>
|
||||
<mat-icon>delete</mat-icon>
|
||||
<span>Remove</span>
|
||||
</button>
|
||||
</mat-menu>
|
||||
`
|
||||
})
|
||||
export class MenuRendererComponent implements OnInit, OnDestroy, AgRendererComponent {
|
||||
constructor(
|
||||
public dialog: MatDialog,
|
||||
private httpClient: HttpClient,
|
||||
private breakpointObserver: BreakpointObserver,
|
||||
private wsConnectionService: WsConnectionService,
|
||||
private serverService: ServerService,
|
||||
private zone: NgZone,
|
||||
private _snackBar: MatSnackBar
|
||||
) {
|
||||
this.websocketHandlerPromise = this.wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
isExtraSmall: Observable<BreakpointState> = this.breakpointObserver.observe(Breakpoints.XSmall);
|
||||
|
||||
params: any;
|
||||
params: ICellRendererParams;
|
||||
|
||||
postData: PostState;
|
||||
|
||||
subscription: Subscription;
|
||||
|
||||
constructor(
|
||||
public dialog: MatDialog,
|
||||
private httpClient: HttpClient,
|
||||
private breakpointObserver: BreakpointObserver,
|
||||
private wsConnectionService: WsConnectionService
|
||||
) {}
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscription = this.wsConnectionService.subscribeForPosts(e => {
|
||||
e.forEach(v => {
|
||||
if (this.postData.postId === v.postId) {
|
||||
this.postData = v;
|
||||
}
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
this.subscription = handler.subscribeForPosts(e => {
|
||||
this.zone.run(() => {
|
||||
e.forEach(v => {
|
||||
if (this.postData.postId === v.postId) {
|
||||
this.postData = v;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -98,29 +118,70 @@ export class MenuRendererComponent implements OnInit, OnDestroy, AgRendererCompo
|
||||
}
|
||||
|
||||
restart() {
|
||||
this.httpClient.post(environment.localhost + '/post/restart', { postId: this.postData.postId }).subscribe(
|
||||
data => {},
|
||||
this.httpClient.post(this.serverService.baseUrl + '/post/restart', { postId: this.postData.postId }).subscribe(
|
||||
() => {
|
||||
this._snackBar.open('Download started', null, {
|
||||
duration: 5000
|
||||
});
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
remove() {
|
||||
this.dialog
|
||||
.open(ConfirmDialogComponent, {
|
||||
maxHeight: '100vh',
|
||||
maxWidth: '100vw',
|
||||
height: '200px',
|
||||
width: '60%',
|
||||
data: { header: 'Confirmation', content: 'Are you sure you want to remove this item ?' }
|
||||
})
|
||||
.afterClosed()
|
||||
.pipe(
|
||||
filter(e => e === 'yes'),
|
||||
flatMap(e =>
|
||||
this.httpClient.post<RemoveResponse>(this.serverService.baseUrl + '/post/remove', {
|
||||
postId: this.postData.postId
|
||||
})
|
||||
)
|
||||
)
|
||||
.subscribe(
|
||||
data => {
|
||||
const toRemove = [];
|
||||
const nodeToDelete = this.params.api.getRowNode(data.postId);
|
||||
if (nodeToDelete != null) {
|
||||
toRemove.push(nodeToDelete.data);
|
||||
}
|
||||
this.params.api.updateRowData({ remove: toRemove });
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.httpClient.post(environment.localhost + '/post/stop', { postId: this.postData.postId }).subscribe(
|
||||
data => {},
|
||||
this.httpClient.post(this.serverService.baseUrl + '/post/stop', { postId: this.postData.postId }).subscribe(
|
||||
() => {
|
||||
this._snackBar.open('Download stopped', null, {
|
||||
duration: 5000
|
||||
});
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
agInit(params: any): void {
|
||||
agInit(params: ICellRendererParams): void {
|
||||
this.params = params;
|
||||
this.postData = params.data;
|
||||
}
|
||||
|
||||
refresh(params: any): boolean {
|
||||
refresh(params: ICellRendererParams): boolean {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +1,127 @@
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { PostState } from './post-state.model';
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
|
||||
import { AgRendererComponent } from 'ag-grid-angular';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { WsHandler } from '../ws-handler';
|
||||
import { ICellRendererParams } from 'ag-grid-community';
|
||||
|
||||
@Component({
|
||||
selector: 'app-progress-cell',
|
||||
template: `
|
||||
<div style="height: 100%;">
|
||||
<div class="progress-text" fxLayout="row" fxLayoutAlign="space-between">
|
||||
<div>{{ postState.title + postState.postCounter }}</div>
|
||||
<div>{{ trunc(postState.progress) + '%' }}</div>
|
||||
</div>
|
||||
<div
|
||||
class="progress-bar"
|
||||
[ngClass]="{
|
||||
complete: postState.status === 'COMPLETE',
|
||||
downloading: postState.status === 'DOWNLOADING',
|
||||
stopped: postState.status === 'STOPPED',
|
||||
partial: postState.status === 'PARTIAL'
|
||||
'complete': postState.status === 'COMPLETE',
|
||||
'downloading': postState.status === 'DOWNLOADING',
|
||||
'stopped': postState.status === 'STOPPED',
|
||||
'partial': postState.status === 'PARTIAL',
|
||||
'error': postState.status === 'ERROR',
|
||||
'pending': postState.status === 'PENDING'
|
||||
}"
|
||||
[style.width]="trunc(postState.progress) + '%'"
|
||||
></div>
|
||||
<div
|
||||
class="progress-bar-back"
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-between"
|
||||
[ngClass]="{ error: postState.status === 'ERROR' }"
|
||||
>
|
||||
<div>{{ postState.title }}</div>
|
||||
<div>{{ trunc(postState.progress) + '%' }}</div>
|
||||
</div>
|
||||
[ngClass]="{
|
||||
'complete-back': postState.status === 'COMPLETE',
|
||||
'downloading-back': postState.status === 'DOWNLOADING',
|
||||
'stopped-back': postState.status === 'STOPPED',
|
||||
'partial-back': postState.status === 'PARTIAL',
|
||||
'error-back': postState.status === 'ERROR',
|
||||
'pending-back': postState.status === 'PENDING'
|
||||
}"
|
||||
></div>
|
||||
</div>
|
||||
`,
|
||||
styles: [
|
||||
`
|
||||
.progress-bar-back {
|
||||
.progress-text {
|
||||
position: relative;
|
||||
height: 45px;
|
||||
bottom: 45px;
|
||||
padding: 0 8px;
|
||||
transition: background-color 0.5s;
|
||||
z-index: 2;
|
||||
}
|
||||
.progress-bar {
|
||||
background-color: #87a2c7;
|
||||
position: relative;
|
||||
height: 100%;
|
||||
bottom: 45px;
|
||||
transition: width 0.5s, background-color 0.5s;
|
||||
z-index: 1;
|
||||
}
|
||||
.progress-bar-back {
|
||||
position: relative;
|
||||
height: 100%;
|
||||
bottom: 90px;
|
||||
transition: background-color 0.5s;
|
||||
}
|
||||
.complete {
|
||||
background-color: #3865a3;
|
||||
}
|
||||
.complete-back {
|
||||
background-color: lightgrey;
|
||||
}
|
||||
.pending {
|
||||
background-color: #a8a8a8;
|
||||
background-color: white;
|
||||
}
|
||||
.pending-back {
|
||||
background-color: lightgrey;
|
||||
}
|
||||
.error {
|
||||
background-color: #eb6060;
|
||||
}
|
||||
.error-back {
|
||||
background-color: #eb6060;
|
||||
}
|
||||
.downloading {
|
||||
background-color: #87a2c7;
|
||||
}
|
||||
.downloading-back {
|
||||
background-color: white;
|
||||
}
|
||||
.stopped {
|
||||
background-color: grey;
|
||||
}
|
||||
.stopped-back {
|
||||
background-color: lightgrey;
|
||||
}
|
||||
.partial {
|
||||
background-color: #ffcc00;
|
||||
background-color: #ffcc00;
|
||||
}
|
||||
.partial-back {
|
||||
background-color: white;
|
||||
}
|
||||
`
|
||||
]
|
||||
})
|
||||
export class PostProgressRendererComponent implements AgRendererComponent, OnInit, OnDestroy {
|
||||
params: any;
|
||||
constructor(private wsConnectionService: WsConnectionService, private zone: NgZone) {
|
||||
this.websocketHandlerPromise = this.wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
params: ICellRendererParams;
|
||||
postState: PostState;
|
||||
subscription: Subscription;
|
||||
|
||||
constructor(private wsConnectionService: WsConnectionService) {}
|
||||
|
||||
trunc(value: number): number {
|
||||
return Math.trunc(value);
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscription = this.wsConnectionService.subscribeForPosts(e => {
|
||||
e.forEach(v => {
|
||||
if (this.postState.postId === v.postId) {
|
||||
this.postState = v;
|
||||
}
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
this.subscription = handler.subscribeForPosts(e => {
|
||||
this.zone.run(() => {
|
||||
e.forEach(v => {
|
||||
if (this.postState.postId === v.postId) {
|
||||
this.postState = v;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -89,12 +130,12 @@ export class PostProgressRendererComponent implements AgRendererComponent, OnIni
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
|
||||
agInit(params: any): void {
|
||||
agInit(params: ICellRendererParams): void {
|
||||
this.params = params;
|
||||
this.postState = params.data;
|
||||
}
|
||||
|
||||
refresh(params: any): boolean {
|
||||
refresh(params: ICellRendererParams): boolean {
|
||||
this.postState = params.data;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
export class PostState {
|
||||
|
||||
constructor(public postId: string, public title: string, public progress: number, public status: string) { }
|
||||
|
||||
constructor(
|
||||
public type: string,
|
||||
public postId: string,
|
||||
public postCounter: string,
|
||||
public title: string,
|
||||
public progress: number,
|
||||
public status: string,
|
||||
public removed: boolean
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,60 @@
|
||||
import { PostState } from './post-state.model';
|
||||
import { CMD } from './../common/cmd.enum';
|
||||
import { WSMessage } from './../common/ws-message.model';
|
||||
import { Subject, Subscription } from 'rxjs';
|
||||
import { WsConnectionService, WSState } from '../ws-connection.service';
|
||||
import { Subscription } from 'rxjs';
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { GridOptions } from 'ag-grid-community';
|
||||
import { WsHandler } from '../ws-handler';
|
||||
import { NgZone } from '@angular/core';
|
||||
|
||||
export class PostsDataSource {
|
||||
|
||||
constructor(private websocketConnection: WsConnectionService,
|
||||
private gridOptions: GridOptions) {
|
||||
this.websocket = websocketConnection.getConnection();
|
||||
constructor(
|
||||
private wsConnectionService: WsConnectionService,
|
||||
private gridOptions: GridOptions,
|
||||
private zone: NgZone
|
||||
) {
|
||||
this.websocketHandlerPromise = this.wsConnectionService.getConnection();
|
||||
}
|
||||
|
||||
websocket: Subject<any>;
|
||||
|
||||
websocketHandlerPromise: Promise<WsHandler>;
|
||||
subscriptions: Subscription[] = [];
|
||||
|
||||
connect() {
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
console.log('Connecting to posts datasource');
|
||||
this.subscriptions.push(
|
||||
handler.subscribeForPosts((e: PostState[]) => {
|
||||
this.zone.run(() => {
|
||||
const toAdd = [];
|
||||
const toUpdate = [];
|
||||
const toRemove = [];
|
||||
e.forEach(v => {
|
||||
if (v.removed) {
|
||||
if (this.gridOptions.api.getRowNode(v.postId) != null) {
|
||||
toRemove.push(this.gridOptions.api.getRowNode(v.postId).data);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Connecting to posts datasource');
|
||||
this.subscriptions.push(this.websocketConnection.subscribeForPosts(e => {
|
||||
const toAdd = [];
|
||||
const toUpdate = [];
|
||||
e.forEach(v => {
|
||||
if (this.gridOptions.api.getRowNode(v.postId) == null) {
|
||||
toAdd.push(v);
|
||||
} else {
|
||||
toUpdate.push(v);
|
||||
}
|
||||
});
|
||||
this.gridOptions.api.updateRowData({ update: toUpdate, add: toAdd });
|
||||
}));
|
||||
|
||||
this.subscriptions.push(this.websocketConnection.state.subscribe((e) => {
|
||||
if (e === WSState.OPEN) {
|
||||
this.websocket.next(new WSMessage(CMD.POSTS_SUB.toString()));
|
||||
}
|
||||
}));
|
||||
if (this.gridOptions.api.getRowNode(v.postId) == null) {
|
||||
toAdd.push(v);
|
||||
} else {
|
||||
toUpdate.push(v);
|
||||
}
|
||||
});
|
||||
this.gridOptions.api.updateRowData({ update: toUpdate, add: toAdd, remove: toRemove });
|
||||
});
|
||||
})
|
||||
);
|
||||
handler.send(new WSMessage(CMD.POSTS_SUB.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
console.log('Disconnecting from posts datasource');
|
||||
this.subscriptions.forEach(e => e.unsubscribe());
|
||||
this.websocket.next(new WSMessage(CMD.POSTS_UNSUB.toString()));
|
||||
this.websocketHandlerPromise.then((handler: WsHandler) => {
|
||||
handler.send(new WSMessage(CMD.POSTS_UNSUB.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
|
||||
import { PostsDataSource } from './post.datasource';
|
||||
import { WsConnectionService } from '../ws-connection.service';
|
||||
import { GridOptions } from 'ag-grid-community';
|
||||
@@ -13,7 +13,10 @@ import { Subject } from 'rxjs';
|
||||
})
|
||||
export class PostsComponent implements OnInit, OnDestroy {
|
||||
|
||||
constructor(private wsConnection: WsConnectionService) {
|
||||
constructor(
|
||||
private wsConnection: WsConnectionService,
|
||||
private zone: NgZone
|
||||
) {
|
||||
this.gridOptions = <GridOptions> {
|
||||
columnDefs: [
|
||||
{
|
||||
@@ -34,6 +37,7 @@ export class PostsComponent implements OnInit, OnDestroy {
|
||||
suppressAutoSize: true
|
||||
}
|
||||
],
|
||||
enableBrowserTooltips: true,
|
||||
rowHeight: 48,
|
||||
rowData: [],
|
||||
frameworkComponents: {
|
||||
@@ -45,7 +49,7 @@ export class PostsComponent implements OnInit, OnDestroy {
|
||||
getRowNodeId: (data) => data['postId'],
|
||||
onGridReady: () => {
|
||||
this.gridOptions.api.sizeColumnsToFit();
|
||||
this.dataSource = new PostsDataSource(this.wsConnection, this.gridOptions);
|
||||
this.dataSource = new PostsDataSource(this.wsConnection, this.gridOptions, this.zone);
|
||||
this.dataSource.connect();
|
||||
},
|
||||
onGridSizeChanged: () => this.gridOptions.api.sizeColumnsToFit(),
|
||||
@@ -57,6 +61,22 @@ export class PostsComponent implements OnInit, OnDestroy {
|
||||
gridOptions: GridOptions;
|
||||
dataSource: PostsDataSource;
|
||||
|
||||
removeRows(postIds: string[]): void {
|
||||
if (postIds == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const toRemove = [];
|
||||
|
||||
postIds.forEach(p => {
|
||||
const nodeToDelete = this.gridOptions.api.getRowNode(p);
|
||||
if (nodeToDelete != null) {
|
||||
toRemove.push(nodeToDelete.data);
|
||||
}
|
||||
});
|
||||
this.gridOptions.api.updateRowData({ remove: toRemove });
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable()
|
||||
export class ServerService {
|
||||
|
||||
private _baseUrl: string;
|
||||
|
||||
constructor() {}
|
||||
|
||||
set baseUrl(baseUrl: string) {
|
||||
this._baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
get baseUrl(): string {
|
||||
return this._baseUrl;
|
||||
}
|
||||
}
|
||||
@@ -4,61 +4,85 @@
|
||||
</div>
|
||||
<mat-dialog-content fxFlex="grow">
|
||||
<div class="container">
|
||||
<form #f="ngForm" autocomplete="off">
|
||||
<mat-form-field>
|
||||
<input
|
||||
[(ngModel)]="settings.downloadPath"
|
||||
matInput
|
||||
placeholder="Download Path"
|
||||
name="downloadPath"
|
||||
required
|
||||
/>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field>
|
||||
<input
|
||||
[(ngModel)]="settings.maxThreads"
|
||||
type="number"
|
||||
matInput
|
||||
placeholder="Max concurrent downloads"
|
||||
name="maxThreads"
|
||||
min="1"
|
||||
max="8"
|
||||
required
|
||||
/>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-checkbox color="primary" [(ngModel)]="settings.autoStart" name="autoStart">Auto start downloads</mat-checkbox>
|
||||
|
||||
<mat-slide-toggle color="primary" [(ngModel)]="settings.vLogin" name="vLogin">
|
||||
ViperGirls Authentication
|
||||
</mat-slide-toggle>
|
||||
|
||||
<section *ngIf="settings.vLogin">
|
||||
<div>
|
||||
<mat-form-field>
|
||||
<input [(ngModel)]="settings.vUsername" matInput placeholder="ViperGirls Username" name="vUsername" />
|
||||
</mat-form-field>
|
||||
<mat-tab-group dynamicHeight="true">
|
||||
<mat-tab label="General Settings">
|
||||
<form [formGroup]="generalSettingsForm" autocomplete="off">
|
||||
<div fxLayout="row" fxLayoutGap="20px" fxLayoutAlign="center center">
|
||||
<mat-form-field fxFlex="grow">
|
||||
<input
|
||||
formControlName="downloadPath"
|
||||
matInput
|
||||
placeholder="Download Path"
|
||||
name="downloadPath"
|
||||
required
|
||||
/>
|
||||
</mat-form-field>
|
||||
<button fxFlex="100px" (click)="browse()" mat-raised-button *ngIf="electronService.isElectronApp">
|
||||
Browse
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<mat-form-field>
|
||||
<input
|
||||
[(ngModel)]="settings.vPassword"
|
||||
type="password"
|
||||
formControlName="maxThreads"
|
||||
type="number"
|
||||
matInput
|
||||
placeholder="ViperGirls Password"
|
||||
name="vPassword"
|
||||
placeholder="Max concurrent downloads"
|
||||
name="maxThreads"
|
||||
min="1"
|
||||
max="8"
|
||||
required
|
||||
/>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-checkbox color="primary" [(ngModel)]="settings.vThanks" name="vThanks">Leave thanks</mat-checkbox>
|
||||
</div>
|
||||
</section>
|
||||
</form>
|
||||
<mat-checkbox color="primary" formControlName="autoStart" name="autoStart"
|
||||
>Auto start downloads</mat-checkbox
|
||||
>
|
||||
|
||||
<mat-slide-toggle color="primary" formControlName="vLogin" name="vLogin">
|
||||
ViperGirls Authentication
|
||||
</mat-slide-toggle>
|
||||
|
||||
<section *ngIf="generalSettingsForm.get('vLogin').value">
|
||||
<div>
|
||||
<mat-form-field>
|
||||
<input formControlName="vUsername" matInput placeholder="ViperGirls Username" name="vUsername" />
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field>
|
||||
<input
|
||||
formControlName="vPassword"
|
||||
type="password"
|
||||
matInput
|
||||
placeholder="ViperGirls Password"
|
||||
name="vPassword"
|
||||
/>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-checkbox color="primary" formControlName="vThanks" name="vThanks">Leave thanks</mat-checkbox>
|
||||
</div>
|
||||
</section>
|
||||
</form>
|
||||
</mat-tab>
|
||||
<mat-tab label="Desktop Integration" *ngIf="electronService.isElectronApp">
|
||||
<form [formGroup]="desktopSettingsForm" autocomplete="off">
|
||||
<mat-checkbox color="primary" formControlName="desktopClipboard" name="desktopClipboard"
|
||||
>Monitor Clipboard</mat-checkbox
|
||||
>
|
||||
</form>
|
||||
</mat-tab>
|
||||
</mat-tab-group>
|
||||
</div>
|
||||
</mat-dialog-content>
|
||||
<mat-dialog-actions fxFlex="nogrow" align="end">
|
||||
<button mat-raised-button cdkFocusInitial mat-dialog-close>Close</button>
|
||||
<button mat-raised-button (click)="onSubmit(f)" color="primary" type="submit" [disabled]="f.pristine || f.invalid">
|
||||
<button mat-raised-button mat-dialog-close>Close</button>
|
||||
<button
|
||||
mat-raised-button
|
||||
(click)="onSubmit()"
|
||||
color="primary"
|
||||
type="submit"
|
||||
[disabled]="(generalSettingsForm.pristine && desktopSettingsForm.pristine) || generalSettingsForm.invalid || desktopSettingsForm.invalid"
|
||||
>
|
||||
Apply
|
||||
</button>
|
||||
</mat-dialog-actions>
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.container form section {
|
||||
padding: 10px;
|
||||
.container form {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.container form section > * {
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
import { ClipboardService } from './../clipboard.service';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { NgForm } from '@angular/forms';
|
||||
import { FormGroup, FormControl } from '@angular/forms';
|
||||
import { MatSnackBar } from '@angular/material';
|
||||
|
||||
export interface Settings {
|
||||
downloadPath: string;
|
||||
maxThreads: number;
|
||||
autoStart: boolean;
|
||||
vLogin: boolean;
|
||||
vUsername: string;
|
||||
vPassword: string;
|
||||
vThanks: boolean;
|
||||
}
|
||||
import { ServerService } from '../server-service';
|
||||
import { ElectronService } from 'ngx-electron';
|
||||
import { Settings } from '../common/settings.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-settings',
|
||||
@@ -20,40 +13,75 @@ export interface Settings {
|
||||
styleUrls: ['./settings.component.scss']
|
||||
})
|
||||
export class SettingsComponent implements OnInit {
|
||||
constructor(private httpClient: HttpClient, private _snackBar: MatSnackBar) {}
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
private _snackBar: MatSnackBar,
|
||||
private serverService: ServerService,
|
||||
public electronService: ElectronService,
|
||||
private clipboardService: ClipboardService
|
||||
) {}
|
||||
|
||||
settings: Settings = {
|
||||
downloadPath: null,
|
||||
maxThreads: null,
|
||||
autoStart: false,
|
||||
vLogin: false,
|
||||
vUsername: null,
|
||||
vPassword: null,
|
||||
vThanks: false,
|
||||
};
|
||||
generalSettingsForm = new FormGroup({
|
||||
downloadPath: new FormControl(''),
|
||||
maxThreads: new FormControl(''),
|
||||
autoStart: new FormControl(false),
|
||||
vLogin: new FormControl(false),
|
||||
vUsername: new FormControl(''),
|
||||
vPassword: new FormControl(''),
|
||||
vThanks: new FormControl(false)
|
||||
});
|
||||
|
||||
desktopSettingsForm = new FormGroup({
|
||||
desktopClipboard: new FormControl(false)
|
||||
});
|
||||
|
||||
ngOnInit() {
|
||||
this.httpClient
|
||||
.get<Settings>(environment.localhost + '/settings')
|
||||
.subscribe(data => {
|
||||
this.settings = data;
|
||||
}, error => {
|
||||
this.httpClient.get<Settings>(this.serverService.baseUrl + '/settings').subscribe(
|
||||
data => {
|
||||
this.generalSettingsForm.reset(data);
|
||||
this.desktopSettingsForm.reset(data);
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
onSubmit(f: NgForm): void {
|
||||
browse() {
|
||||
const result: string[] | undefined = this.electronService.remote.dialog.showOpenDialog(
|
||||
this.electronService.remote.getCurrentWindow(),
|
||||
{
|
||||
properties: ['openDirectory']
|
||||
}
|
||||
);
|
||||
|
||||
if (result !== undefined) {
|
||||
this.generalSettingsForm.get('downloadPath').setValue(result[0]);
|
||||
this.generalSettingsForm.get('downloadPath').markAsDirty();
|
||||
this.generalSettingsForm.get('downloadPath').markAsTouched();
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
this.httpClient
|
||||
.post(environment.localhost + '/settings', this.settings)
|
||||
.subscribe(() => {
|
||||
this._snackBar.open('Settings updated', null, {
|
||||
duration: 5000,
|
||||
});
|
||||
f.resetForm(this.settings);
|
||||
}, error => {
|
||||
this._snackBar.open(error.error.message, null, {
|
||||
duration: 5000,
|
||||
});
|
||||
});
|
||||
.post<Settings>(this.serverService.baseUrl + '/settings', {
|
||||
...this.generalSettingsForm.value,
|
||||
...this.desktopSettingsForm.value
|
||||
})
|
||||
.subscribe(
|
||||
data => {
|
||||
this._snackBar.open('Settings updated', null, {
|
||||
duration: 5000
|
||||
});
|
||||
this.generalSettingsForm.reset(data);
|
||||
this.desktopSettingsForm.reset(data);
|
||||
this.clipboardService.init(data);
|
||||
},
|
||||
error => {
|
||||
this._snackBar.open(error.error.message, null, {
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,97 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observer, Subject, BehaviorSubject, ConnectableObservable, Observable, Subscription } from 'rxjs';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { multicast, map, filter } from 'rxjs/operators';
|
||||
import { PostState } from './posts/post-state.model';
|
||||
import { PostDetails } from './post-detail/post-details.model';
|
||||
import { multicast } from 'rxjs/operators';
|
||||
import { ElectronService } from 'ngx-electron';
|
||||
import { WsHandler } from './ws-handler';
|
||||
import { ServerService } from './server-service';
|
||||
declare var SockJS;
|
||||
|
||||
export enum WSState {
|
||||
INIT, CONNECTING, ERROR, CLOSE, OPEN
|
||||
INIT,
|
||||
CONNECTING,
|
||||
ERROR,
|
||||
CLOSE,
|
||||
OPEN
|
||||
}
|
||||
|
||||
const maxAttemps = 5;
|
||||
|
||||
@Injectable()
|
||||
export class WsConnectionService {
|
||||
|
||||
private websocket: Subject<any>;
|
||||
|
||||
private state$: BehaviorSubject<WSState> = new BehaviorSubject(WSState.INIT);
|
||||
private sock;
|
||||
private _state: WSState = WSState.INIT;
|
||||
private wsHandlerPromise: Promise<WsHandler>;
|
||||
private wsHandler: WsHandler;
|
||||
|
||||
constructor() {
|
||||
this.tryConnect();
|
||||
constructor(private electronService: ElectronService, private serverService: ServerService) {
|
||||
this.wsHandlerPromise = new Promise((resolve, reject) => {
|
||||
if (this.wsHandler != null) {
|
||||
return this.wsHandler;
|
||||
}
|
||||
if (this.electronService.isElectronApp) {
|
||||
this.electronService.ipcRenderer.send('get-port');
|
||||
|
||||
// wait for MainIPC
|
||||
this.electronService.ipcRenderer.once('port', (event, port) => {
|
||||
console.log('server running on port', port);
|
||||
this.serverService.baseUrl = 'http://localhost:' + port;
|
||||
this.tryConnect(resolve, reject);
|
||||
});
|
||||
} else {
|
||||
this.serverService.baseUrl = environment.localhost;
|
||||
this.tryConnect(resolve, reject);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public get state(): Observable<WSState> {
|
||||
return this.state$.asObservable();
|
||||
}
|
||||
|
||||
tryConnect() {
|
||||
tryConnect(resolve, reject) {
|
||||
let attempts = 0;
|
||||
this.connect();
|
||||
const subscription: Subscription = this.state$.subscribe(e => {
|
||||
if (e === WSState.OPEN) {
|
||||
this.wsHandler = new WsHandler(this.websocket);
|
||||
resolve(this.wsHandler);
|
||||
}
|
||||
});
|
||||
const interval = setInterval(() => {
|
||||
if (this._state !== WSState.OPEN && attempts >= maxAttemps) {
|
||||
clearInterval(interval);
|
||||
this._state = WSState.ERROR;
|
||||
this.state$.next(this._state);
|
||||
reject('Failed to connect to server');
|
||||
subscription.unsubscribe();
|
||||
return;
|
||||
} else if (this._state === WSState.OPEN) {
|
||||
clearInterval(interval);
|
||||
subscription.unsubscribe();
|
||||
return;
|
||||
}
|
||||
this.connect();
|
||||
attempts++;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
connect() {
|
||||
this._state = WSState.CONNECTING;
|
||||
this.state$.next(this._state);
|
||||
this.sock = new SockJS(environment.localhost + '/endpoint');
|
||||
this.sock = new SockJS(this.serverService.baseUrl + '/endpoint');
|
||||
const observable = Observable.create((obs: Observer<string>) => {
|
||||
this.sock.onmessage = (message) => obs.next(message.data);
|
||||
this.sock.onerror = (error) => {
|
||||
this.sock.onmessage = message => obs.next(message.data);
|
||||
this.sock.onerror = error => {
|
||||
console.log('Sockjs error', error);
|
||||
obs.error(error);
|
||||
this._state = WSState.ERROR;
|
||||
this.state$.next(this._state);
|
||||
};
|
||||
this.sock.onclose = () => {
|
||||
console.log('Sockjs disconnected');
|
||||
obs.complete();
|
||||
this._state = WSState.CLOSE;
|
||||
this.state$.next(this._state);
|
||||
@@ -47,71 +100,22 @@ export class WsConnectionService {
|
||||
|
||||
this.sock.onopen = () => {
|
||||
console.log('Sockjs connection established');
|
||||
const observer = {
|
||||
next: (data: Object) => {
|
||||
if (this.sock.readyState === WebSocket.OPEN) {
|
||||
this.sock.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
};
|
||||
this.websocket = Subject.create(observer, observable).pipe(multicast(() => new Subject()));
|
||||
(<ConnectableObservable<any>>(<unknown>this.websocket)).connect();
|
||||
this._state = WSState.OPEN;
|
||||
this.state$.next(this._state);
|
||||
};
|
||||
|
||||
const observer = {
|
||||
next: (data: Object) => {
|
||||
if (this.sock.readyState === WebSocket.OPEN) {
|
||||
this.sock.send(JSON.stringify(data));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
this.websocket = Subject.create(observer, observable).pipe(multicast(() => new Subject()));
|
||||
(<ConnectableObservable<any>><unknown>this.websocket).connect();
|
||||
}
|
||||
|
||||
subscribeForPosts(callback: (stream: Array<PostState>) => void): Subscription {
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e[0].type === 'post'),
|
||||
map(e => {
|
||||
const values = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
values.push(new PostState(
|
||||
element.postId,
|
||||
element.title,
|
||||
element.done === 0 && element.total === 0 ? 0 : (element.done / element.total) * 100,
|
||||
element.status)
|
||||
);
|
||||
});
|
||||
return values;
|
||||
})
|
||||
)
|
||||
.subscribe(e => {
|
||||
callback(e);
|
||||
});
|
||||
}
|
||||
|
||||
subscribeForPostDetails(callback: (stream: Array<PostDetails>) => void): Subscription {
|
||||
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e[0].type === 'img'),
|
||||
map(e => {
|
||||
const values = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
values.push(new PostDetails(element.postId,
|
||||
element.postName,
|
||||
element.url,
|
||||
element.current === 0 && element.total === 0 ? 0 : (element.current / element.total) * 100,
|
||||
element.status));
|
||||
});
|
||||
return values;
|
||||
})
|
||||
)
|
||||
.subscribe(e => callback(e));
|
||||
}
|
||||
|
||||
getConnection() {
|
||||
if (this.sock == null || this.sock.readyState === 3 || this.sock.readyState === 2) {
|
||||
this.tryConnect();
|
||||
}
|
||||
return this.websocket;
|
||||
getConnection(): Promise<WsHandler> {
|
||||
return this.wsHandlerPromise;
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import { Subject, Subscription } from 'rxjs';
|
||||
import { WSMessage } from './common/ws-message.model';
|
||||
import { PostState } from './posts/post-state.model';
|
||||
import { map, filter } from 'rxjs/operators';
|
||||
import { PostDetails } from './post-detail/post-details.model';
|
||||
import { GlobalState } from './common/global-state.model';
|
||||
import { DownloadSpeed } from './common/download-speed.model';
|
||||
|
||||
export class WsHandler {
|
||||
constructor(private websocket: Subject<any>) {}
|
||||
|
||||
subscribeForGlobalState(callback: (stateStream: Array<GlobalState>) => void): Subscription {
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e.length > 0 && e.filter(v => v.type === 'globalState').length > 0),
|
||||
map(e => {
|
||||
const state: Array<GlobalState> = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
state.push(
|
||||
new GlobalState(
|
||||
element.running,
|
||||
element.queued,
|
||||
element.remaining,
|
||||
element.error
|
||||
)
|
||||
);
|
||||
});
|
||||
return state;
|
||||
})
|
||||
)
|
||||
.subscribe(e => {
|
||||
callback(e);
|
||||
});
|
||||
}
|
||||
|
||||
subscribeForSpeed(callback: (speedStream: Array<DownloadSpeed>) => void): Subscription {
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e.length > 0 && e.filter(v => v.type === 'downSpeed').length > 0),
|
||||
map(e => {
|
||||
const speed: Array<DownloadSpeed> = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
speed.push(
|
||||
new DownloadSpeed(
|
||||
element.speed
|
||||
)
|
||||
);
|
||||
});
|
||||
return speed;
|
||||
})
|
||||
)
|
||||
.subscribe(e => {
|
||||
callback(e);
|
||||
});
|
||||
}
|
||||
|
||||
subscribeForPosts(callback: (postStream: Array<PostState>) => void): Subscription {
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e.length > 0 && e.filter(v => v.type === 'post').length > 0),
|
||||
map(e => {
|
||||
const posts: Array<PostState> = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
posts.push(
|
||||
new PostState(
|
||||
element.type,
|
||||
element.postId,
|
||||
element.postCounter,
|
||||
element.title,
|
||||
element.done === 0 && element.total === 0 ? 0 : (element.done / element.total) * 100,
|
||||
element.status,
|
||||
element.removed
|
||||
)
|
||||
);
|
||||
});
|
||||
return posts;
|
||||
})
|
||||
)
|
||||
.subscribe(e => {
|
||||
callback(e);
|
||||
});
|
||||
}
|
||||
|
||||
subscribeForPostDetails(callback: (stream: Array<PostDetails>) => void): Subscription {
|
||||
return this.websocket
|
||||
.pipe(
|
||||
map(e => JSON.parse(e)),
|
||||
filter(e => e.length > 0 && e[0].type === 'img'),
|
||||
map(e => {
|
||||
const values = [];
|
||||
(<Array<any>>e).forEach(element => {
|
||||
values.push(
|
||||
new PostDetails(
|
||||
element.postId,
|
||||
element.postName,
|
||||
element.url,
|
||||
element.current === 0 && element.total === 0 ? 0 : (element.current / element.total) * 100,
|
||||
element.status
|
||||
)
|
||||
);
|
||||
});
|
||||
return values;
|
||||
})
|
||||
)
|
||||
.subscribe(e => callback(e));
|
||||
}
|
||||
|
||||
send(wsMessage: WSMessage) {
|
||||
this.websocket.next(wsMessage);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 15 KiB |
@@ -2,12 +2,20 @@
|
||||
@import "~@angular/material/prebuilt-themes/indigo-pink.css";
|
||||
@import "~ag-grid-community/dist/styles/ag-grid.css";
|
||||
@import "~ag-grid-community/dist/styles/ag-theme-material.css";
|
||||
body {
|
||||
margin: 0;
|
||||
|
||||
html {
|
||||
border: 3px solid rgb(63, 81, 181);
|
||||
box-sizing: border-box;
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
body {
|
||||
& .window-title {
|
||||
font-family: "Arial";
|
||||
}
|
||||
}
|
||||
|
||||
app-root {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
@@ -56,4 +64,12 @@ div.ag-cell.no-padding {
|
||||
overflow: hidden;
|
||||
text-overflow:ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.ag-body-horizontal-scroll {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.ag-center-cols-viewport {
|
||||
overflow: hidden !important;
|
||||
}
|
||||
Reference in New Issue
Block a user