Skip to content

Commit

Permalink
fixed novel cover and ads script - v2.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-nyt committed Feb 27, 2021
1 parent 4db0d0a commit 0701a25
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 229 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NovelScraper",
"version": "2.0.3",
"version": "2.0.4",
"description": "App for downloading novels from pirate sites.",
"homepage": "https://github.com/HanaDigital/NovelScraper",
"author": {
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"devDependencies": {
"@angular-builders/custom-webpack": "9.2.0",
"@angular-devkit/build-angular": "0.1000.2",
"@angular-devkit/build-angular": "0.1102.2",
"@angular-eslint/builder": "0.0.1-alpha.32",
"@angular-eslint/eslint-plugin": "0.0.1-alpha.32",
"@angular-eslint/eslint-plugin-template": "0.0.1-alpha.32",
Expand Down Expand Up @@ -67,7 +67,7 @@
"conventional-changelog-cli": "2.0.34",
"core-js": "3.6.5",
"cross-env": "7.0.2",
"electron": "^10.1.3",
"electron": "10.4.0",
"electron-builder": "22.8.1",
"electron-reload": "1.5.0",
"eslint": "7.4.0",
Expand All @@ -86,7 +86,7 @@
"ts-node": "8.10.2",
"tslib": "2.0.0",
"typescript": "3.9.6",
"wait-on": "5.0.1",
"wait-on": "5.2.1",
"webdriver-manager": "12.1.7",
"zone.js": "0.10.3"
},
Expand All @@ -99,7 +99,7 @@
"electron-updater": "4.3.5",
"mime": "2.4.6",
"nconf": "0.10.0",
"nodepub": "2.2.0",
"nodepub": "3.0.1",
"request": "2.88.2",
"rimraf": "3.0.2",
"shelljs": "0.8.4"
Expand Down
77 changes: 18 additions & 59 deletions src/app/pages/novel/novel.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
<div class="pageHeader">
<h1 class="title">
<img class="back" src="assets/img/drop-arrow.svg" />
<img
class="backButton"
src="assets/img/back-arrow.svg"
(click)="goBack()"
/>
<img class="backButton" src="assets/img/back-arrow.svg" (click)="goBack()" />
{{ source.name.toUpperCase() }}
<hr />
</h1>
Expand All @@ -16,59 +12,30 @@ <h1 class="title">
<div class="mainWrapper">
<div class="pageWrapper">
<div class="novelPageController">
<div
class="progressBar"
[ngStyle]="{
<div class="progressBar" [ngStyle]="{
width: novel.downloading ? '275px' : '0px'
}"
>
}">
<div class="progress">
{{ database.getPercentage(downloadID) }}
</div>
<div
class="bar"
[ngStyle]="{
<div class="bar" [ngStyle]="{
width: database.getPercentage(downloadID)
}"
></div>
}"></div>
</div>

<div class="add" *ngIf="!novel.inLibrary">
<img
src="assets/img/add.svg"
(click)="addToLibrary()"
/>
<img src="assets/img/add.svg" (click)="addToLibrary()" />
</div>
<div class="in" *ngIf="novel.inLibrary">
<img
src="assets/img/browser.svg"
(click)="openInBrowser()"
/>
<img
src="assets/img/delete.svg"
(click)="toggleDeleteModal()"
/>
<img src="assets/img/browser.svg" (click)="openInBrowser()" />
<img src="assets/img/delete.svg" (click)="toggleDeleteModal()" />
<img src="assets/img/refresh.svg" (click)="refresh()" />
<img
src="assets/img/folder.svg"
(click)="openFolder()"
*ngIf="novel.downloaded"
/>
<img
src="assets/img/update.svg"
*ngIf="novel.downloaded && !novel.downloading"
(click)="download()"
/>
<img
src="assets/img/cancel.svg"
*ngIf="novel.downloading"
(click)="cancelDownload()"
/>
<img
src="assets/img/download.svg"
*ngIf="!novel.downloaded && !novel.downloading"
(click)="download()"
/>
<img src="assets/img/folder.svg" (click)="openFolder()" *ngIf="novel.downloaded" />
<img src="assets/img/update.svg" *ngIf="novel.downloaded && !novel.downloading"
(click)="download()" />
<img src="assets/img/cancel.svg" *ngIf="novel.downloading" (click)="cancelDownload()" />
<img src="assets/img/download.svg" *ngIf="!novel.downloaded && !novel.downloading"
(click)="download()" />
</div>
</div>

Expand All @@ -83,22 +50,14 @@ <h1 class="infoBox">
<h4>LATEST CHAPTER</h4>
<span>{{ novel.latestChapter }}</span>
</div>
<div
class="infoBox totalChapters"
*ngIf="!novel.downloaded"
>
<div class="infoBox totalChapters" *ngIf="!novel.downloaded">
<h4>TOTAL CHAPTERS</h4>
<span>{{ novel.totalChapters }}</span>
</div>
<div
class="infoBox totalChapters"
*ngIf="novel.downloaded"
>
<div class="infoBox totalChapters" *ngIf="novel.downloaded">
<h4>DOWNLOADED</h4>
<span
>{{ novel.downloadedChapters }} /
{{ novel.totalChapters }}</span
>
<span>{{ novel.downloadedChapters }} /
{{ novel.totalChapters }}</span>
</div>
<div class="infoBox source">
<h4>SOURCE</h4>
Expand Down
101 changes: 60 additions & 41 deletions src/app/services/novel-factory.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Injectable } from '@angular/core';
import { DatabaseService } from './database.service';
import { novelObj, chapterObj } from '../resources/types';
import shellJS from 'shelljs'; // For folder creation, idk why I didn't just do it with fs
import { remote } from 'electron';
import * as path from 'path';
import rimraf from 'rimraf'; // For recursive folder deletion
import epubGen from 'nodepub';
import mime from 'mime';
import { writeFileSync, createWriteStream, writeFile } from 'fs';
import request from 'request';
import { Injectable } from "@angular/core";
import { DatabaseService } from "./database.service";
import { novelObj, chapterObj } from "../resources/types";
import shellJS from "shelljs"; // For folder creation, idk why I didn't just do it with fs
import { remote } from "electron";
import * as path from "path";
import rimraf from "rimraf"; // For recursive folder deletion
const epubGen = require("nodepub");
import mime from "mime";
import { writeFileSync, createWriteStream, writeFile } from "fs";
import request from "request";

@Injectable({
providedIn: 'root'
providedIn: "root",
})
export class NovelFactoryService {

// Path to the downloads folder
downloadFolder = path.join(remote.app.getPath("downloads"), "NovelScraper-Library");
downloadFolder = path.join(
remote.app.getPath("downloads"),
"NovelScraper-Library"
);

constructor(public database: DatabaseService,) { }
constructor(public database: DatabaseService) {}

async generateEpub(novel: novelObj, chapters: chapterObj[], downloadID: number): Promise<void> {
async generateEpub(
novel: novelObj,
chapters: chapterObj[],
downloadID: number
): Promise<void> {
// Create the folder for storing the novel files
const novelFile = novel.name.replace(/[/\\?%*:|"<>]/g, '');
const novelFile = novel.name.replace(/[/\\?%*:|"<>]/g, "");
const novelFolder = novel.folderPath;
await shellJS.mkdir('-p', novelFolder);
await shellJS.mkdir("-p", novelFolder);

// Save chapters in a json file and set the novel as download
await this.saveChapters(novel, chapters, novelFolder);
Expand All @@ -34,53 +40,66 @@ export class NovelFactoryService {

// Set some meta data for the epub file
const metadata = {
id: '0000-0000-0001',
id: "0000-0000-0001",
cover: coverPath,
title: novel.name,
author: novel.author,
genre: novel.genre,
language: 'en',
description: 'This novel was downloaded from a pirate site using NovelScraper.'
+ ' If possible, please support the author: ' + novel.author,
contents: 'Table of Contents',
images: []
}
language: "en",
description:
"This novel was downloaded from a pirate site using NovelScraper." +
" If possible, please support the author: " +
novel.author,
contents: "Table of Contents",
images: [],
};

// Generate the epub file and add the chapters
const epub = epubGen.document(metadata, coverPath);
for (const chapter of chapters) epub.addSection(chapter.title, chapter.data);
epub.writeEPUB(
(error) => console.log(error),
novelFolder, novelFile,
() => console.log("[NOVEL FACTORY]: Novel saved!")
);
const epub = epubGen.document(metadata);
for (const chapter of chapters)
epub.addSection(chapter.title, chapter.data);

setTimeout(async () => {
await epub.writeEPUB(novelFolder, novelFile);

this.database.updateDownloading(novel.link, false);
this.database.updateDownloaded(novel.link, true);
this.database.updateIsUpdated(novel.link, true);
this.database.setDownloaded(downloadID);
this.database.updateDownloading(novel.link, false);
this.database.updateDownloaded(novel.link, true);
this.database.updateIsUpdated(novel.link, true);
this.database.setDownloaded(downloadID);
}, 2000);
}

// Save chapters to a json file so we don't re-download when updating
async saveChapters(novel: novelObj, chapters: chapterObj[], novelFolder: string): Promise<void> {
async saveChapters(
novel: novelObj,
chapters: chapterObj[],
novelFolder: string
): Promise<void> {
const chaptersFile = path.join(novelFolder, "chapters.json");
const json = JSON.stringify(chapters, null, 4);
writeFile(chaptersFile, json, (err) => {
if (err) console.error(err);
else {
// Update total chapters of the novel
novel.downloadedChapters = chapters.length;
this.database.updateDownloadedChapters(novel.link, chapters.length);
this.database.updateDownloadedChapters(
novel.link,
chapters.length
);
}
})
});
}

// Download novel cover and return the path
async downloadCover(novelCover: string, novelFolder: string): Promise<string> {
async downloadCover(
novelCover: string,
novelFolder: string
): Promise<string> {
const coverExtension = mime.getExtension(mime.getType(novelCover));
const coverPath = path.join(novelFolder, "cover." + coverExtension);
const out = createWriteStream(coverPath);

const req = await request({ method: 'GET', uri: novelCover });
const req = await request({ method: "GET", uri: novelCover });
await req.pipe(out);

return coverPath;
Expand Down
Loading

0 comments on commit 0701a25

Please sign in to comment.